Skip to content

Commit 1ce4ea7

Browse files
Merge pull request #1 from WesterosCraft/timeandweather
PTime Command, not tested on server build. Only on singleplayer Open to LAN
2 parents 1a86618 + 963a394 commit 1ce4ea7

10 files changed

Lines changed: 269 additions & 36 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package westeroscraft;
2+
3+
import net.fabricmc.api.ClientModInitializer;
4+
import westeroscraft.commands.NVCommand;
5+
import westeroscraft.commands.PTimeCommand;
6+
import westeroscraft.commands.PWeatherCommand;
7+
8+
public class WesterosCraftEssentialsClient implements ClientModInitializer {
9+
public static WesterosCraftEssentialsClient INSTANCE;
10+
public long time;
11+
public boolean enabledTime = false;
12+
13+
public float rainLevel;
14+
public float thunderLevel;
15+
public boolean enabledWeather = false;
16+
17+
@Override
18+
public void onInitializeClient() {
19+
PTimeCommand.register();
20+
PWeatherCommand.register();
21+
NVCommand.register();
22+
INSTANCE = this;
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package westeroscraft.commands;
2+
3+
public enum ETime {
4+
SUNRISE(0),
5+
DAY(1000),
6+
MORNING(2000),
7+
NOON(6000),
8+
AFTERNOON(9000),
9+
SUNSET(12000),
10+
NIGHT(13000),
11+
MIDNIGHT(18000);
12+
13+
public final int time;
14+
15+
ETime(int i) {
16+
this.time = i;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package westeroscraft.commands;
2+
3+
public enum EWeather {
4+
CLEAR("clear"),
5+
RAIN("rain"),
6+
RESET("reset"),
7+
THUNDER("thunder");
8+
9+
public final String string;
10+
11+
EWeather(String w) { this.string = w; }
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package westeroscraft.commands;
2+
3+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
4+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
5+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
6+
import net.minecraft.client.player.LocalPlayer;
7+
import net.minecraft.network.chat.Component;
8+
import net.minecraft.server.level.ServerPlayer;
9+
import net.minecraft.world.effect.MobEffectInstance;
10+
import net.minecraft.world.effect.MobEffects;
11+
12+
public class NVCommand {
13+
public static void register() {
14+
ClientCommandRegistrationCallback.EVENT.register((dispatcher, access) -> dispatcher.register(ClientCommandManager.literal("nv").executes((context) -> nightVision(context.getSource()))));
15+
}
16+
17+
public static int nightVision(FabricClientCommandSource source) {
18+
//We check for both ServerPlayer and LocalPlayer to allow for it to be used in singleplayer.
19+
if(source.getEntity() instanceof ServerPlayer player){
20+
MobEffectInstance nv = player.getEffect(MobEffects.NIGHT_VISION);
21+
if(nv == null) {
22+
player.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION, Integer.MAX_VALUE, 0, false, false, false));
23+
source.sendFeedback(Component.literal("Enabled Night Vision"));
24+
} else {
25+
player.removeEffect(MobEffects.NIGHT_VISION);
26+
source.sendFeedback(Component.literal("Disabled Night Vision"));
27+
}
28+
} else if(source.getEntity() instanceof LocalPlayer player) {
29+
MobEffectInstance nv = player.getEffect(MobEffects.NIGHT_VISION);
30+
if(nv == null) {
31+
player.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION, Integer.MAX_VALUE, 0, false, false, false));
32+
source.sendFeedback(Component.literal("Enabled Night Vision"));
33+
} else {
34+
player.removeEffect(MobEffects.NIGHT_VISION);
35+
source.sendFeedback(Component.literal("Disabled Night Vision"));
36+
}
37+
}
38+
return 1;
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package westeroscraft.commands;
2+
3+
import com.mojang.brigadier.arguments.IntegerArgumentType;
4+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
5+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
6+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
7+
import net.minecraft.commands.arguments.TimeArgument;
8+
import westeroscraft.WesterosCraftEssentialsClient;
9+
10+
public class PTimeCommand {
11+
public static void register() {
12+
ClientCommandRegistrationCallback.EVENT.register((dispatcher, access) -> dispatcher.register(ClientCommandManager.literal("ptime")
13+
.then(ClientCommandManager.literal("reset").executes((context) -> resetTime(context.getSource())))
14+
.then(ClientCommandManager.literal("set")
15+
.then(ClientCommandManager.literal("normal").executes((context) -> resetTime(context.getSource())))
16+
.then(ClientCommandManager.literal("default").executes((context) -> resetTime(context.getSource())))
17+
.then(ClientCommandManager.literal("sunrise").executes((context) -> setTime(context.getSource(), ETime.SUNRISE.time, false)))
18+
.then(ClientCommandManager.literal("day").executes((context) -> setTime(context.getSource(), ETime.DAY.time, false)))
19+
.then(ClientCommandManager.literal("morning").executes((context) -> setTime(context.getSource(), ETime.MORNING.time, false)))
20+
.then(ClientCommandManager.literal("noon").executes((context) -> setTime(context.getSource(), ETime.NOON.time, false)))
21+
.then(ClientCommandManager.literal("afternoon").executes((context) -> setTime(context.getSource(), ETime.AFTERNOON.time, false)))
22+
.then(ClientCommandManager.literal("sunset").executes((context) -> setTime(context.getSource(), ETime.SUNSET.time, false)))
23+
.then(ClientCommandManager.literal("midnight").executes((context) -> setTime(context.getSource(), ETime.MIDNIGHT.time, false)))
24+
.then(ClientCommandManager.argument("time", TimeArgument.time()).executes((context) -> setTime(context.getSource(), IntegerArgumentType.getInteger(context, "time"), false)))
25+
)
26+
.then(ClientCommandManager.literal("setrelative")
27+
.then(ClientCommandManager.literal("ahead")
28+
.then(ClientCommandManager.argument("time", TimeArgument.time()).executes((context) -> setTime(context.getSource(), IntegerArgumentType.getInteger(context, "time"), true)))
29+
)
30+
.then(ClientCommandManager.literal("behind")
31+
.then(ClientCommandManager.argument("time", TimeArgument.time()).executes((context) -> setTime(context.getSource(), -IntegerArgumentType.getInteger(context, "time"), true)))
32+
)
33+
)
34+
));
35+
}
36+
37+
public static int resetTime(FabricClientCommandSource source) {
38+
WesterosCraftEssentialsClient.INSTANCE.enabledTime = false;
39+
WesterosCraftEssentialsClient.INSTANCE.time = source.getWorld().getDayTime();
40+
return 1;
41+
}
42+
43+
public static int setTime(FabricClientCommandSource source, long time, boolean relative) {
44+
if(relative) {
45+
if(WesterosCraftEssentialsClient.INSTANCE.enabledTime) {
46+
WesterosCraftEssentialsClient.INSTANCE.time += time;
47+
} else {
48+
WesterosCraftEssentialsClient.INSTANCE.time = source.getWorld().getDayTime() + time;
49+
}
50+
} else {
51+
WesterosCraftEssentialsClient.INSTANCE.time = time;
52+
}
53+
WesterosCraftEssentialsClient.INSTANCE.enabledTime = true;
54+
return 1;
55+
}
56+
}
57+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package westeroscraft.commands;
2+
3+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
4+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
5+
import westeroscraft.WesterosCraftEssentialsClient;
6+
7+
public class PWeatherCommand {
8+
public static void register() {
9+
ClientCommandRegistrationCallback.EVENT.register((dispatcher, access) -> dispatcher.register(ClientCommandManager.literal("pweather")
10+
.then(ClientCommandManager.literal(EWeather.CLEAR.string).executes((context) -> setWeather( 0f, 0f)))
11+
.then(ClientCommandManager.literal(EWeather.RAIN.string).executes((context) -> setWeather(1f, 0f)))
12+
.then(ClientCommandManager.literal(EWeather.THUNDER.string).executes((context) -> setWeather(1f, 1f)))
13+
.then(ClientCommandManager.literal(EWeather.RESET.string).executes((context) -> resetWeather()))
14+
));
15+
}
16+
17+
public static int resetWeather() {
18+
WesterosCraftEssentialsClient.INSTANCE.enabledWeather = false;
19+
return 1;
20+
}
21+
22+
public static int setWeather(float rainLevel, float thunderLevel) {
23+
WesterosCraftEssentialsClient.INSTANCE.enabledWeather = true;
24+
WesterosCraftEssentialsClient.INSTANCE.rainLevel = rainLevel;
25+
WesterosCraftEssentialsClient.INSTANCE.thunderLevel = thunderLevel;
26+
return 1;
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package westeroscraft.mixin;
2+
3+
import net.fabricmc.api.EnvType;
4+
import net.fabricmc.api.Environment;
5+
import net.minecraft.client.multiplayer.ClientLevel;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
10+
import westeroscraft.WesterosCraftEssentialsClient;
11+
12+
@Mixin(ClientLevel.ClientLevelData.class)
13+
public abstract class ClientLevelDataMixin {
14+
15+
@Inject(at = @At("RETURN"), method = "getDayTime", cancellable = true)
16+
@Environment(EnvType.CLIENT)
17+
public void getDayTime(CallbackInfoReturnable<Long> cir) {
18+
if(WesterosCraftEssentialsClient.INSTANCE.enabledTime) {
19+
cir.setReturnValue(WesterosCraftEssentialsClient.INSTANCE.time);
20+
} else cir.cancel();
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package westeroscraft.mixin;
2+
3+
import net.minecraft.client.multiplayer.ClientLevel;
4+
import net.minecraft.core.Holder;
5+
import net.minecraft.core.RegistryAccess;
6+
import net.minecraft.resources.ResourceKey;
7+
import net.minecraft.util.profiling.ProfilerFiller;
8+
import net.minecraft.world.level.Level;
9+
import net.minecraft.world.level.dimension.DimensionType;
10+
import net.minecraft.world.level.storage.WritableLevelData;
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import westeroscraft.WesterosCraftEssentialsClient;
13+
14+
import java.util.function.Supplier;
15+
16+
@Mixin(ClientLevel.class)
17+
public abstract class ClientLevelMixin extends Level {
18+
19+
protected ClientLevelMixin(WritableLevelData writableLevelData, ResourceKey<Level> resourceKey, RegistryAccess registryAccess, Holder<DimensionType> holder, Supplier<ProfilerFiller> supplier, boolean bl, boolean bl2, long l, int i) {
20+
super(writableLevelData, resourceKey, registryAccess, holder, supplier, bl, bl2, l, i);
21+
}
22+
23+
@Override
24+
public float getRainLevel(float d) {
25+
if(WesterosCraftEssentialsClient.INSTANCE.enabledWeather) {
26+
return WesterosCraftEssentialsClient.INSTANCE.rainLevel;
27+
}
28+
return super.getRainLevel(d);
29+
}
30+
31+
@Override
32+
public float getThunderLevel(float d) {
33+
if(WesterosCraftEssentialsClient.INSTANCE.enabledWeather) {
34+
return WesterosCraftEssentialsClient.INSTANCE.thunderLevel;
35+
}
36+
return super.getThunderLevel(d);
37+
}
38+
}

src/main/resources/fabric.mod.json

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
1-
{
2-
"schemaVersion": 1,
3-
"id": "westeroscraft-essentials",
4-
"version": "${version}",
5-
"name": "WesterosCraft Essentials",
6-
"description": "WesterosCraftCore is a custom Fabric mod that handles various requirements for the WesterosCraft server",
7-
"authors": [
8-
"geeberry",
9-
"mikeprimm"
10-
],
11-
"contact": {
12-
"homepage": "https://westeroscraft.com/",
13-
"sources": "https://github.com/FabricMC/fabric-example-mod"
14-
},
15-
"license": "CC0-1.0",
16-
"icon": "assets/westeroscraft-essentials/icon.png",
17-
"environment": "*",
18-
"entrypoints": {
19-
"main": [
20-
"westeroscraft.WesterosCraftEssentials"
21-
],
22-
"fabric-datagen": [
23-
"westeroscraft.WesterosCraftEssentialsDataGenerator"
24-
]
25-
},
26-
"mixins": [
27-
"westeroscraft-essentials.mixins.json"
28-
],
29-
"depends": {
30-
"fabricloader": ">=0.18.4",
31-
"minecraft": "~1.21.1",
32-
"java": ">=21",
33-
"fabric-api": "*"
34-
}
35-
}
1+
{
2+
"schemaVersion": 1,
3+
"id": "westeroscraft-essentials",
4+
"version": "${version}",
5+
"name": "WesterosCraft Essentials",
6+
"description": "WesterosCraftCore is a custom Fabric mod that handles various requirements for the WesterosCraft server",
7+
"authors": ["geeberry", "mikeprimm", "earlofberkeley"],
8+
"contact": {
9+
"homepage": "https://westeroscraft.com/",
10+
"sources": "https://github.com/FabricMC/fabric-example-mod"
11+
},
12+
"license": "CC0-1.0",
13+
"icon": "assets/westeroscraft-essentials/icon.png",
14+
"environment": "*",
15+
"entrypoints": {
16+
"main": ["westeroscraft.WesterosCraftEssentials"],
17+
"client": ["westeroscraft.WesterosCraftEssentialsClient"],
18+
"fabric-datagen": ["westeroscraft.WesterosCraftEssentialsDataGenerator"]
19+
},
20+
"mixins": ["westeroscraft-essentials.mixins.json"],
21+
"depends": {
22+
"fabricloader": ">=0.18.4",
23+
"minecraft": "~1.21.1",
24+
"java": ">=21",
25+
"fabric-api": "*"
26+
}
27+
}

src/main/resources/westeroscraft-essentials.mixins.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"DoorBlockMixin",
1616
"TrapDoorBlockMixin",
1717
"FenceGateBlockMixin",
18-
"ServerPlayerMixin"
18+
"ServerPlayerMixin",
19+
"ClientLevelMixin",
20+
"ClientLevelDataMixin"
1921
],
2022
"injectors": {
2123
"defaultRequire": 1

0 commit comments

Comments
 (0)