From 8930d64fd02705ff97e7b8fd7ca3cb0bb5104b37 Mon Sep 17 00:00:00 2001 From: moonleay Date: Sat, 27 Apr 2024 03:34:45 +0200 Subject: [PATCH] feat: added ForcePlace & No Updates, Modifier disabled messages moved to Toast --- README.md | 19 +++++------ .../gimble/client/editor/ClientEditor.kt | 7 ++-- .../moonleay/gimble/client/util/ChatUtil.kt | 6 ++++ .../gimble/editor/ServerEditorManager.kt | 2 +- .../moonleay/gimble/editor/state/mode/Mode.kt | 2 +- .../gimble/mixin/AlwaysAllowBlock.java | 19 ----------- .../gimble/mixin/ForcePlaceMixin.java | 28 ++++++++++------ .../moonleay/gimble/mixin/NoBlockUpdates.java | 32 +++++++++++++++++++ .../gimble/mixin/WorldSetBlockStateMixin.java | 18 ----------- .../gimble/networking/GimbleServer.kt | 2 +- src/main/resources/gimble.mixins.json | 5 ++- 11 files changed, 74 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/net/moonleay/gimble/mixin/AlwaysAllowBlock.java create mode 100644 src/main/java/net/moonleay/gimble/mixin/NoBlockUpdates.java delete mode 100644 src/main/java/net/moonleay/gimble/mixin/WorldSetBlockStateMixin.java diff --git a/README.md b/README.md index a0001dd..50d61e3 100644 --- a/README.md +++ b/README.md @@ -2,23 +2,24 @@ This project aims to improve the experience of creating structures in Minecraft. ## Features -- **Modes** Use different modes to edit structures. Think Vim for Minecraft. -- **Modifier** Use modifiers to change the behavior of the modes and your building experience. +- **Modes** Use different modes to edit structures. Think Vim for Minecraft. [x] +- **Modifier** Use modifiers to change the behavior of the modes and your building experience. [x] - **Undo/Redo** Easily undo and redo your changes. (planned) ## Modes -- **Normal** The default mode. Use it to change to other modes -- **Insert** Edit blocks in the world +- **Normal** The default mode. Use it to change to other modes [x] +- **Insert** Edit blocks in the world [x] - **Visual** Use selection to edit (contains worldedit support) (planned) -- **Replace** Replace blocks in the world +- **Replace** Replace blocks in the world [x] ## Modifiers - **Bulldozer** Basically an auto-clicker -- **Force Place** Ignore block placing restrictions -- **No Clip** Walk and fly through blocks -- **No Updates** Prevent block updates +- **Force Place** Ignore block placing restrictions [x] +- **No Clip** Walk and fly through blocks [x] +- **No Updates** Prevent block updates [x] - **No Gravity** Prevent blocks from falling (planned) -- **Better Fly** Disable floaty fly physics and set custom fly speeds(planned) +- **Better Fly** Disable floaty fly physics and set custom fly speeds (planned) +- **Angelic Placement** Place blocks in the air (planned) ## Installation This mod requires Fabric, the Fabric API, aswell as the HCConfigLib. 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 24121a6..fa35f58 100644 --- a/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt +++ b/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt @@ -72,8 +72,7 @@ object ClientEditor { this.updateServerState() if(TEMP_DISABLED_MODIFIERS.isNotEmpty()) { - ChatUtil.addToChatHistory("The following modifiers are not supported by this editor mode and are therefore currently disabled: " + - getListAsString(TEMP_DISABLED_MODIFIERS), MinecraftClient.getInstance()) + ChatUtil.showToastToSelf("${CURRENT_MODE.displayName} Mode disabled", getListAsString(TEMP_DISABLED_MODIFIERS), MinecraftClient.getInstance()) } } @@ -83,9 +82,9 @@ object ClientEditor { val sb = StringBuilder() for (mod in list) { sb.append(mod.displayName) - sb.append(" ") + sb.append(", ") } - return sb.toString().dropLast(1) + return sb.toString().dropLast(2) } fun getCurrentMode(): Mode { diff --git a/src/main/java/net/moonleay/gimble/client/util/ChatUtil.kt b/src/main/java/net/moonleay/gimble/client/util/ChatUtil.kt index 2d19bc0..cc30396 100644 --- a/src/main/java/net/moonleay/gimble/client/util/ChatUtil.kt +++ b/src/main/java/net/moonleay/gimble/client/util/ChatUtil.kt @@ -1,6 +1,7 @@ package net.moonleay.gimble.client.util import net.minecraft.client.MinecraftClient +import net.minecraft.client.toast.SystemToast import net.minecraft.text.Text object ChatUtil { @@ -17,4 +18,9 @@ object ChatUtil { fun addToChatHistory(message: String, client: MinecraftClient) { client.inGameHud.chatHud.addMessage(Text.of(message)) } + + fun showToastToSelf(title: String, description: String, client: MinecraftClient) { + val toast = SystemToast(SystemToast.Type.PERIODIC_NOTIFICATION, Text.of(title), Text.of(description)) + client.toastManager.add(toast) + } } diff --git a/src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt b/src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt index dc66198..0b5e387 100644 --- a/src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt +++ b/src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt @@ -9,7 +9,7 @@ import java.util.UUID object ServerEditorManager { private val LOGGER = LogManager.getLogger(BuildConstants.modName) - val STATEMAP = mutableMapOf() + private val STATEMAP = mutableMapOf() fun updateEditorState(playerUUID: UUID, editorState: EditorState) { STATEMAP[playerUUID] = editorState diff --git a/src/main/java/net/moonleay/gimble/editor/state/mode/Mode.kt b/src/main/java/net/moonleay/gimble/editor/state/mode/Mode.kt index 95d7961..b468a14 100644 --- a/src/main/java/net/moonleay/gimble/editor/state/mode/Mode.kt +++ b/src/main/java/net/moonleay/gimble/editor/state/mode/Mode.kt @@ -1,7 +1,7 @@ package net.moonleay.gimble.editor.state.mode enum class Mode(val displayName: String, val color: Int, val incompatibleModifiers: List){ - UNKNOWN("UNKNOWN", 0x000000, listOf()), // Unknown mode. This mode cannot be entered +// UNKNOWN("UNKNOWN", 0x000000, listOf()), // Unknown mode. This mode cannot be entered NORMAL("NORMAL", 0x90a959, listOf(ModeModifier.NO_UPDATES, ModeModifier.BULLDOZER, ModeModifier.FORCE_PLACE)), // Do nothing INSERT("INSERT", 0xf4bf75, listOf()), // Place and break blocks REPLACE("REPLACE", 0xac4242, listOf()), // Replace blocks diff --git a/src/main/java/net/moonleay/gimble/mixin/AlwaysAllowBlock.java b/src/main/java/net/moonleay/gimble/mixin/AlwaysAllowBlock.java deleted file mode 100644 index 2c64831..0000000 --- a/src/main/java/net/moonleay/gimble/mixin/AlwaysAllowBlock.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.moonleay.gimble.mixin; - -import net.minecraft.block.Block; -import net.minecraft.block.pattern.CachedBlockPosition; -import net.minecraft.item.ItemStack; -import net.minecraft.util.registry.Registry; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -@Mixin(ItemStack.class) -public class AlwaysAllowBlock { - - @Redirect(method = "useOnBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;canPlaceOn(Lnet/minecraft/util/registry/Registry;Lnet/minecraft/block/pattern/CachedBlockPosition;)Z")) - private boolean func(ItemStack instance, Registry blockRegistry, CachedBlockPosition pos) { - - return true; - } -} diff --git a/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java b/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java index 5a21038..115e2b1 100644 --- a/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/ForcePlaceMixin.java @@ -1,22 +1,30 @@ package net.moonleay.gimble.mixin; -import net.minecraft.item.ItemStack; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.server.network.ServerPlayerInteractionManager; -import net.minecraft.util.ActionResult; -import net.minecraft.util.Hand; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.world.World; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemPlacementContext; +import net.moonleay.gimble.editor.ServerEditorManager; +import net.moonleay.gimble.editor.state.mode.ModeModifier; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -@Mixin(ServerPlayerInteractionManager.class) +@Mixin(BlockItem.class) public class ForcePlaceMixin { - @Inject(method = "interactBlock", at = @At("HEAD")) - private void func(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable cir) { + @Inject(method = "canPlace", at = @At("HEAD"), cancellable = true) + private void func(ItemPlacementContext context, BlockState state, CallbackInfoReturnable cir) { + if (context.getPlayer() == null) { + return; + } + PlayerEntity player = context.getPlayer(); + if (!ServerEditorManager.INSTANCE.playerHasModifier(player.getGameProfile().getId(), ModeModifier.FORCE_PLACE)) { + return; + } + cir.setReturnValue(true); + cir.cancel(); } } diff --git a/src/main/java/net/moonleay/gimble/mixin/NoBlockUpdates.java b/src/main/java/net/moonleay/gimble/mixin/NoBlockUpdates.java new file mode 100644 index 0000000..6b42211 --- /dev/null +++ b/src/main/java/net/moonleay/gimble/mixin/NoBlockUpdates.java @@ -0,0 +1,32 @@ +package net.moonleay.gimble.mixin; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemPlacementContext; +import net.moonleay.gimble.editor.ServerEditorManager; +import net.moonleay.gimble.editor.state.mode.ModeModifier; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(BlockItem.class) +public class NoBlockUpdates { + + @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) { + return; + } + PlayerEntity player = context.getPlayer(); + + if (!ServerEditorManager.INSTANCE.playerHasModifier(player.getGameProfile().getId(), ModeModifier.NO_UPDATES)) { + return; + } + + cir.setReturnValue(context.getWorld().setBlockState(context.getBlockPos(), state, Block.REDRAW_ON_MAIN_THREAD, 0)); // This is scuffed, but works + cir.cancel(); + } +} diff --git a/src/main/java/net/moonleay/gimble/mixin/WorldSetBlockStateMixin.java b/src/main/java/net/moonleay/gimble/mixin/WorldSetBlockStateMixin.java deleted file mode 100644 index 3ad9a4d..0000000 --- a/src/main/java/net/moonleay/gimble/mixin/WorldSetBlockStateMixin.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.moonleay.gimble.mixin; - -import net.minecraft.block.BlockState; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(World.class) -public class WorldSetBlockStateMixin { - - @Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("HEAD")) - private void func(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable cir) { - System.out.println("setBlockState was run"); - } -} diff --git a/src/main/java/net/moonleay/gimble/networking/GimbleServer.kt b/src/main/java/net/moonleay/gimble/networking/GimbleServer.kt index f1decfe..c36848e 100644 --- a/src/main/java/net/moonleay/gimble/networking/GimbleServer.kt +++ b/src/main/java/net/moonleay/gimble/networking/GimbleServer.kt @@ -22,6 +22,6 @@ object GimbleServer { private fun handleStateUpdate(player: ServerPlayerEntity, buf: PacketByteBuf){ val state = Cbor.decodeFromByteArray(buf.readByteArray()) ServerEditorManager.updateEditorState(player.uuid, state) - player.sendMessage(Text.of("Mode: ${state.editorMode} with ${state.editorModifier}")) +// player.sendMessage(Text.of("Mode: ${state.editorMode} with ${state.editorModifier}")) } } diff --git a/src/main/resources/gimble.mixins.json b/src/main/resources/gimble.mixins.json index ee75399..70be15d 100644 --- a/src/main/resources/gimble.mixins.json +++ b/src/main/resources/gimble.mixins.json @@ -4,10 +4,9 @@ "package": "net.moonleay.gimble.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "AlwaysAllowBlock", "ForcePlaceMixin", - "NoClipMixin", - "WorldSetBlockStateMixin" + "NoBlockUpdates", + "NoClipMixin" ], "client": [ "HudMixin",