From 597ffaf4b14ab9c1b1f0a3b6971fc07a135688a7 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 20 May 2024 20:05:06 +0530 Subject: [PATCH] feat: toggle system & improved locking mechanism --- build.gradle | 3 -- .../xyz/devcomp/elytralock/ElytraLock.java | 33 +++++++++++++++--- .../ClientPlayerInteractionManagerMixin.java | 34 +++++++++++++++++++ .../elytralock/mixin/EquipmentMixin.java | 30 ---------------- src/main/resources/assets/lang/en_us.json | 5 +++ src/main/resources/elytra-lock.mixins.json | 2 +- src/main/resources/fabric.mod.json | 6 ++-- 7 files changed, 72 insertions(+), 41 deletions(-) create mode 100644 src/main/java/xyz/devcomp/elytralock/mixin/ClientPlayerInteractionManagerMixin.java delete mode 100644 src/main/java/xyz/devcomp/elytralock/mixin/EquipmentMixin.java create mode 100644 src/main/resources/assets/lang/en_us.json diff --git a/build.gradle b/build.gradle index f734bd8..d18db53 100644 --- a/build.gradle +++ b/build.gradle @@ -19,12 +19,9 @@ repositories { } loom { - splitEnvironmentSourceSets() - mods { "elytra-lock" { sourceSet sourceSets.main - sourceSet sourceSets.client } } diff --git a/src/main/java/xyz/devcomp/elytralock/ElytraLock.java b/src/main/java/xyz/devcomp/elytralock/ElytraLock.java index e4231f5..6789c77 100644 --- a/src/main/java/xyz/devcomp/elytralock/ElytraLock.java +++ b/src/main/java/xyz/devcomp/elytralock/ElytraLock.java @@ -1,21 +1,46 @@ package xyz.devcomp.elytralock; +import org.lwjgl.glfw.GLFW; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.impl.networking.client.ClientPlayNetworkAddon; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import net.minecraft.network.message.MessageType; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import net.minecraft.network.packet.c2s.play.ClickSlotC2SPacket; +import net.minecraft.network.packet.c2s.play.SlotChangedStateC2SPacket; public class ElytraLock implements ClientModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger("Elytra Lock"); - + private static KeyBinding lockKeybind; + private static boolean locked = false; + private static MinecraftClient client; + @Override public void onInitializeClient() { - // This entrypoint is suitable for setting up client-specific logic, such as rendering. + lockKeybind = KeyBindingHelper.registerKeyBinding( + new KeyBinding("key.elytralock.lock", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_J, "category.elytralock")); + client = MinecraftClient.getInstance(); + LOGGER.info("Elytra lock initializing!"); } public static boolean isLocked() { - // TODO: Load from config - return true; + if (lockKeybind.wasPressed()) { + locked = !locked; + } + + client.inGameHud.getChatHud().addMessage(Text.translatable("elytralock.chat.lockedMessage")); + + return locked; } } \ No newline at end of file diff --git a/src/main/java/xyz/devcomp/elytralock/mixin/ClientPlayerInteractionManagerMixin.java b/src/main/java/xyz/devcomp/elytralock/mixin/ClientPlayerInteractionManagerMixin.java new file mode 100644 index 0000000..80ef6f3 --- /dev/null +++ b/src/main/java/xyz/devcomp/elytralock/mixin/ClientPlayerInteractionManagerMixin.java @@ -0,0 +1,34 @@ +package xyz.devcomp.elytralock.mixin; + +import xyz.devcomp.elytralock.ElytraLock; + +import org.apache.commons.lang3.mutable.MutableObject; + +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; + +import com.llamalad7.mixinextras.sugar.Local; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.network.ClientPlayerInteractionManager; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; + +@Environment(EnvType.CLIENT) +@Mixin(ClientPlayerInteractionManager.class) +public class ClientPlayerInteractionManagerMixin { + @Inject(method = "interactItem(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/ActionResult;", at = @At(value = "INVOKE", target = "net/minecraft/client/network/ClientPlayerInteractionManager.sendSequencedPacket (Lnet/minecraft/client/world/ClientWorld;Lnet/minecraft/client/network/SequencedPacketCreator;)V"), cancellable = true) + private void skipElytra(PlayerEntity player, Hand hand, CallbackInfoReturnable info, @Local MutableObject mutableObject) { + ItemStack itemStack = player.getStackInHand(hand); + if (itemStack.isOf(Items.ELYTRA) && ElytraLock.isLocked()) { + mutableObject.setValue(ActionResult.FAIL); + info.setReturnValue((ActionResult) mutableObject.getValue()); + } + } +} diff --git a/src/main/java/xyz/devcomp/elytralock/mixin/EquipmentMixin.java b/src/main/java/xyz/devcomp/elytralock/mixin/EquipmentMixin.java deleted file mode 100644 index 20bb4aa..0000000 --- a/src/main/java/xyz/devcomp/elytralock/mixin/EquipmentMixin.java +++ /dev/null @@ -1,30 +0,0 @@ -package xyz.devcomp.elytralock.mixin; - -import xyz.devcomp.elytralock.ElytraLock; - -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -import net.minecraft.item.Equipment; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.util.TypedActionResult; - -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; - -import com.llamalad7.mixinextras.sugar.Local; - -@Mixin(Equipment.class) -@Environment(EnvType.CLIENT) -public interface EquipmentMixin { - @Inject(method = "equipAndSwap(Lnet/minecraft/item/Item;Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/TypedActionResult;", at = @At(value = "INVOKE_ASSIGN", target = "net/minecraft/entity/player/PlayerEntity.getStackInHand (Lnet/minecraft/util/Hand;)Lnet/minecraft/item/ItemStack;"), cancellable = true) - private void injected(CallbackInfoReturnable> info, - @Local(ordinal = 0) ItemStack itemStack) { - if (itemStack.isOf(Items.ELYTRA) && ElytraLock.isLocked()) { - info.setReturnValue(TypedActionResult.fail(itemStack)); - } - } -} \ No newline at end of file diff --git a/src/main/resources/assets/lang/en_us.json b/src/main/resources/assets/lang/en_us.json new file mode 100644 index 0000000..2578f8e --- /dev/null +++ b/src/main/resources/assets/lang/en_us.json @@ -0,0 +1,5 @@ +{ + "category.elytralock": "Elytra Lock Keybinds", + "key.elytralock.lock": "Lock elytra", + "elytralock.chat.lockedMessage": "[elytra-lock] Elytra is locked!" +} \ No newline at end of file diff --git a/src/main/resources/elytra-lock.mixins.json b/src/main/resources/elytra-lock.mixins.json index baeef8c..61860e9 100644 --- a/src/main/resources/elytra-lock.mixins.json +++ b/src/main/resources/elytra-lock.mixins.json @@ -3,7 +3,7 @@ "package": "xyz.devcomp.elytralock.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "EquipmentMixin" + "ClientPlayerInteractionManagerMixin" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 2f86d4d..38a68a0 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -3,13 +3,13 @@ "id": "elytra-lock", "version": "${version}", "name": "elytra-lock", - "description": "This is an example description! Tell everyone what your mod is about!", + "description": "A simple mod which registers a keybind to lock your elytra from usage when not required.", "authors": [ - "Me!" + "CompeyDev " ], "contact": { "homepage": "https://fabricmc.net/", - "sources": "https://github.com/FabricMC/fabric-example-mod" + "sources": "https://github.com/CompeyDev/elytra-lock" }, "license": "CC0-1.0", "icon": "assets/elytra-lock/icon.png",