From 4abb37280518f725006298fe3a81c71c5f20e896 Mon Sep 17 00:00:00 2001 From: moonleay Date: Sun, 28 Apr 2024 03:29:49 +0200 Subject: [PATCH] feat: added gamemode checks, Editor now only works in Creative mode Signed-off-by: moonleay --- .../gimble/client/editor/ClientEditor.kt | 12 ++++++-- .../moonleay/gimble/mixin/BulldozerMixin.java | 12 ++++++-- .../gimble/mixin/BulldozerMixin2.java | 10 ++++++- .../gimble/mixin/ForcePlaceMixin.java | 6 ++-- .../gimble/mixin/NoBlockUpdatesMixin.java | 5 ++-- .../gimble/mixin/NoClipCameraFixMixin.java | 3 +- .../moonleay/gimble/mixin/NoClipMixin.java | 5 ++++ .../gimble/mixin/NormalModeMixin.java | 29 +++++++++++++------ .../gimble/mixin/ReplaceModeMixin.java | 3 ++ 9 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt b/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt index f5155e6..894ba28 100644 --- a/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt +++ b/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt @@ -79,6 +79,8 @@ object ClientEditor { ChatUtil.showToastToSelf("Gimble is disabled", "You cannot change modes", MinecraftClient.getInstance()) return } + if (!MinecraftClient.getInstance().player!!.isCreative) + return CURRENT_MODE = mode this.onUpdated() @@ -92,6 +94,9 @@ object ClientEditor { ChatUtil.showToastToSelf("Gimble is disabled", "You cannot change modifiers", MinecraftClient.getInstance()) return } + if (!MinecraftClient.getInstance().player!!.isCreative) + return + if (CURRENT_MODE.incompatibleModifiers.contains(mod)){ if (TEMP_DISABLED_MODIFIERS.contains(mod)) TEMP_DISABLED_MODIFIERS.remove(mod) @@ -166,7 +171,7 @@ object ClientEditor { * */ fun getModeDisplayText(): Text { val displayText = StringBuilder(CURRENT_MODE.displayName) - if (CURRENT_MODE_MODIFIER.isNotEmpty() && this.isAllowed()) { + if (CURRENT_MODE_MODIFIER.isNotEmpty() && this.isAllowed() && MinecraftClient.getInstance().player?.isCreative == true) { displayText.append(" [") for (i in CURRENT_MODE_MODIFIER.indices) { displayText.append(CURRENT_MODE_MODIFIER[i].displayName) @@ -175,8 +180,9 @@ object ClientEditor { } } displayText.append("]") - } else if (!this.isAllowed()) { - displayText.append(" [DISABLED]") + } else if (!this.isAllowed() || !MinecraftClient.getInstance().player?.isCreative!!) { + displayText.clear() + displayText.append("[GIMBLE DISABLED]") } return Text.of(displayText.toString()) } diff --git a/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin.java b/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin.java index 70995ad..1134cd5 100644 --- a/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin.java @@ -1,8 +1,10 @@ package net.moonleay.gimble.mixin; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; import net.moonleay.gimble.client.editor.ClientEditor; import net.moonleay.gimble.editor.state.mode.Capability; +import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -10,14 +12,20 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.util.Objects; + @Mixin(MinecraftClient.class) public abstract class BulldozerMixin { @Shadow protected int attackCooldown; + @Shadow + @Nullable + public ClientPlayerEntity player; + @Inject(method = "doAttack", at = @At(value = "HEAD")) private void func(CallbackInfoReturnable cir) { - if (!ClientEditor.INSTANCE.shouldClient(Capability.BULLDOZER)) { + if (!ClientEditor.INSTANCE.shouldClient(Capability.BULLDOZER) || !Objects.requireNonNull(this.player).isCreative()) { return; } this.attackCooldown = 0; @@ -25,7 +33,7 @@ public abstract class BulldozerMixin { @Inject(method = "handleBlockBreaking", at = @At(value = "HEAD")) private void func2(boolean breaking, CallbackInfo ci) { - if (!ClientEditor.INSTANCE.shouldClient(Capability.BULLDOZER) || !breaking) { + if (!ClientEditor.INSTANCE.shouldClient(Capability.BULLDOZER) || !breaking || !Objects.requireNonNull(this.player).isCreative()) { return; } this.attackCooldown = 0; diff --git a/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin2.java b/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin2.java index f0bac3f..ae8d081 100644 --- a/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin2.java +++ b/src/main/java/net/moonleay/gimble/mixin/BulldozerMixin2.java @@ -1,24 +1,32 @@ package net.moonleay.gimble.mixin; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayerInteractionManager; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.moonleay.gimble.client.editor.ClientEditor; import net.moonleay.gimble.editor.state.mode.Capability; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.util.Objects; + @Mixin(ClientPlayerInteractionManager.class) public class BulldozerMixin2 { @Shadow private int blockBreakingCooldown; + @Shadow + @Final + private MinecraftClient client; + @Inject(method = "updateBlockBreakingProgress", at = @At("HEAD")) private void func(BlockPos pos, Direction direction, CallbackInfoReturnable cir) { - if (!ClientEditor.INSTANCE.shouldClient(Capability.BULLDOZER)) { + if (!ClientEditor.INSTANCE.shouldClient(Capability.BULLDOZER) || !Objects.requireNonNull(this.client.player).isCreative()) { return; } this.blockBreakingCooldown = 0; diff --git a/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java b/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java index 700403c..4a861fd 100644 --- a/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java @@ -17,9 +17,11 @@ public class ForcePlaceMixin { @Inject(method = "canPlace", at = @At("HEAD"), cancellable = true) private void func(ItemPlacementContext context, BlockState state, CallbackInfoReturnable cir) { - if (context.getPlayer() == null) { + if (context.getPlayer() == null) return; - } + if (!context.getPlayer().isCreative()) + return; + UUID id = context.getPlayer().getGameProfile().getId(); if (!ServerEditorManager.INSTANCE.shouldPlayer(id, Capability.FORCE_PLACE)) { diff --git a/src/main/java/net/moonleay/gimble/mixin/NoBlockUpdatesMixin.java b/src/main/java/net/moonleay/gimble/mixin/NoBlockUpdatesMixin.java index f5aa1d8..4055ad1 100644 --- a/src/main/java/net/moonleay/gimble/mixin/NoBlockUpdatesMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/NoBlockUpdatesMixin.java @@ -18,9 +18,10 @@ public class NoBlockUpdatesMixin { @Inject(method = "place(Lnet/minecraft/item/ItemPlacementContext;Lnet/minecraft/block/BlockState;)Z", at = @At("HEAD"), cancellable = true) private void func(ItemPlacementContext context, BlockState state, CallbackInfoReturnable cir) { - if (context.getPlayer() == null) { + if (context.getPlayer() == null) + return; + if (!context.getPlayer().isCreative()) return; - } UUID id = context.getPlayer().getGameProfile().getId(); if (!ServerEditorManager.INSTANCE.shouldPlayer(id, Capability.NO_UPDATES)) { diff --git a/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFixMixin.java b/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFixMixin.java index a58778d..0874f9a 100644 --- a/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFixMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFixMixin.java @@ -1,5 +1,6 @@ package net.moonleay.gimble.mixin; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.Camera; import net.moonleay.gimble.client.editor.ClientEditor; import net.moonleay.gimble.editor.state.mode.Capability; @@ -13,7 +14,7 @@ public class NoClipCameraFixMixin { @Inject(method = "clipToSpace", at = @At("HEAD"), cancellable = true) private void fixCameraInNoClip(double desiredCameraDistance, CallbackInfoReturnable cir) { - if (!ClientEditor.INSTANCE.shouldClient(Capability.NO_CLIP)) { + if (!ClientEditor.INSTANCE.shouldClient(Capability.NO_CLIP) || (!(MinecraftClient.getInstance().player != null && MinecraftClient.getInstance().player.isCreative()))) { return; } cir.setReturnValue(desiredCameraDistance); diff --git a/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java b/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java index e84762c..f66885d 100644 --- a/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java @@ -23,6 +23,9 @@ public abstract class NoClipMixin extends LivingEntity { @Shadow public abstract PlayerAbilities getAbilities(); + @Shadow + public abstract boolean isCreative(); + protected NoClipMixin(EntityType entityType, World world) { super(entityType, world); } // Server side @@ -31,6 +34,8 @@ public abstract class NoClipMixin extends LivingEntity { @Inject(method = "tick", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/player/PlayerEntity;noClip:Z", shift = At.Shift.AFTER) ) private void enoClip(CallbackInfo ci) { + if (!this.isCreative()) + return; UUID uuid = this.getGameProfile().getId(); if (!ServerEditorManager.INSTANCE.shouldPlayer(uuid, Capability.NO_CLIP)) { diff --git a/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java b/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java index c0e5bef..372a400 100644 --- a/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java @@ -1,6 +1,7 @@ package net.moonleay.gimble.mixin; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.option.GameOptions; import net.moonleay.gimble.client.editor.ClientEditor; import net.moonleay.gimble.editor.state.mode.Capability; @@ -19,12 +20,19 @@ public class NormalModeMixin { @Nullable public GameOptions options; + @Shadow + @Nullable + public ClientPlayerEntity player; + @Inject(method = "openPauseMenu", at = @At("HEAD"), cancellable = true) private void setNormalMode(boolean pause, CallbackInfo ci) { if (ClientEditor.INSTANCE.isInNonDefaultMode() && ClientEditor.INSTANCE.isAllowed()) { - // Set the editor mode to normal - ClientEditor.INSTANCE.setMode(Mode.NORMAL); - ci.cancel(); + assert this.player != null; + if (this.player.isCreative()) { + // Set the editor mode to normal + ClientEditor.INSTANCE.setMode(Mode.NORMAL); + ci.cancel(); + } } } @@ -32,15 +40,18 @@ public class NormalModeMixin { private void blockWorldManipulation(CallbackInfo ci) { // This could be replaced by net.minecraft.world.GameMode#isBlockBreakingRestricted if (!ClientEditor.INSTANCE.shouldClient(Capability.CAN_INTERACT)) { - while(this.options.attackKey.wasPressed()) { - } + assert this.player != null; + if (this.player.isCreative()) { + while (this.options.attackKey.wasPressed()) { + } - while(this.options.useKey.wasPressed()) { - } + while (this.options.useKey.wasPressed()) { + } - while(this.options.pickItemKey.wasPressed()) { + while (this.options.pickItemKey.wasPressed()) { + } + ci.cancel(); } - ci.cancel(); } } } diff --git a/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java b/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java index 947e6e7..cfaed01 100644 --- a/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java @@ -37,6 +37,9 @@ public abstract class ReplaceModeMixin { @Inject(method = "doItemUse", at = @At("HEAD")) private void replaceBlock(CallbackInfo ci) { + assert this.player != null; + if (!this.player.isCreative()) + return; // Check if should run if (!ClientEditor.INSTANCE.shouldClient(Capability.REPLACE)) return; // Mode is not REPLACE, ignore