diff --git a/src/main/java/xyz/devcomp/elytralock/ElytraLock.java b/src/main/java/xyz/devcomp/elytralock/ElytraLock.java index e8eab6e..3be9cd0 100644 --- a/src/main/java/xyz/devcomp/elytralock/ElytraLock.java +++ b/src/main/java/xyz/devcomp/elytralock/ElytraLock.java @@ -1,7 +1,8 @@ package xyz.devcomp.elytralock; import xyz.devcomp.elytralock.config.ConfigHandler; -import xyz.devcomp.elytralock.config.Util; +import xyz.devcomp.elytralock.config.ConfigUtil; +import xyz.devcomp.elytralock.events.ClientTickEndHandler; import xyz.devcomp.elytralock.events.HudRenderHandler; import org.lwjgl.glfw.GLFW; @@ -10,6 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.fabricmc.loader.api.FabricLoader; @@ -27,7 +29,7 @@ public class ElytraLock implements ClientModInitializer { @Override public void onInitializeClient() { - LOGGER.info("Elytra lock initializing!"); + LOGGER.info("ElytraLock initializing!"); lockKeybind = KeyBindingHelper.registerKeyBinding( new KeyBinding("key.elytralock.lock", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_J, "category.elytralock")); @@ -35,7 +37,7 @@ public class ElytraLock implements ClientModInitializer { client = MinecraftClient.getInstance(); - if (Util.isYaclLoaded()) { + if (ConfigUtil.isYaclLoaded()) { LOGGER.info("YACL_v3 is loaded, loading elytra toggle"); locked = new ConfigHandler().getInstance().toggle; } else { @@ -43,6 +45,9 @@ public class ElytraLock implements ClientModInitializer { } HudRenderCallback.EVENT.register(new HudRenderHandler()); + ClientTickEvents.END_CLIENT_TICK.register(new ClientTickEndHandler()); + + LOGGER.info("Registered HUD_RENDER & END_CLIENT_TICK events successfully!"); } public static boolean isLocked() { diff --git a/src/main/java/xyz/devcomp/elytralock/config/Util.java b/src/main/java/xyz/devcomp/elytralock/config/ConfigUtil.java similarity index 88% rename from src/main/java/xyz/devcomp/elytralock/config/Util.java rename to src/main/java/xyz/devcomp/elytralock/config/ConfigUtil.java index ad5635d..de42b2f 100644 --- a/src/main/java/xyz/devcomp/elytralock/config/Util.java +++ b/src/main/java/xyz/devcomp/elytralock/config/ConfigUtil.java @@ -2,7 +2,7 @@ package xyz.devcomp.elytralock.config; import xyz.devcomp.elytralock.ElytraLock; -public class Util { +public class ConfigUtil { public static boolean isYaclLoaded() { return ElytraLock.LOADER.isModLoaded("yet_another_config_lib_v3"); } diff --git a/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndEvent.java b/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndEvent.java deleted file mode 100644 index 1c23308..0000000 --- a/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndEvent.java +++ /dev/null @@ -1,10 +0,0 @@ -package xyz.devcomp.elytralock.events; - -import net.minecraft.client.MinecraftClient; - -public class ClientTickEndEvent { - public void callbackHandler(MinecraftClient client) { - // Check if guy wearing elytra, if so, then remove - - } -} diff --git a/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java b/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java new file mode 100644 index 0000000..4c270db --- /dev/null +++ b/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java @@ -0,0 +1,34 @@ +package xyz.devcomp.elytralock.events; + +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents.EndTick; +import net.minecraft.client.MinecraftClient; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import xyz.devcomp.elytralock.ElytraLock; + +public class ClientTickEndHandler implements EndTick { + public void onEndTick(MinecraftClient client) { + if (client.isWindowFocused() && ElytraLock.isLocked() && client.player != null) { + PlayerInventory inventory = client.player.getInventory(); + + // 0 -> boots + // 1 -> leggings + // 2 -> chestplate + // 3 -> helmet + ItemStack chestArmor = inventory.armor.get(2); + + if (chestArmor.isOf(Items.ELYTRA)) { + ElytraLock.LOGGER.info("Detected player wearing elytra even though it's locked"); + ItemStack elytra = chestArmor.copyAndEmpty(); + int inventorySlot = inventory.getSwappableHotbarSlot(); + + boolean ok = inventory.insertStack(inventorySlot, elytra); + + if (!ok) { + ElytraLock.LOGGER.warn("Failed to remove equipped elytra, which is locked"); + } + } + } + } +} diff --git a/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java b/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java index 2877b5d..12253a4 100644 --- a/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java +++ b/src/main/java/xyz/devcomp/elytralock/integrations/ModMenuIntegration.java @@ -1,7 +1,7 @@ package xyz.devcomp.elytralock.integrations; import xyz.devcomp.elytralock.config.ConfigHandler; -import xyz.devcomp.elytralock.config.Util; +import xyz.devcomp.elytralock.config.ConfigUtil; import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; @@ -10,7 +10,7 @@ public class ModMenuIntegration implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { return (parent) -> { - if (!Util.isYaclLoaded()) + if (!ConfigUtil.isYaclLoaded()) return parent; return new ConfigHandler().showGui(parent); }; diff --git a/src/main/resources/assets/elytra-lock/textures/gui/locked.png b/src/main/resources/assets/elytra-lock/textures/gui/locked.png new file mode 100644 index 0000000..a821a0c Binary files /dev/null and b/src/main/resources/assets/elytra-lock/textures/gui/locked.png differ diff --git a/src/main/resources/assets/elytra-lock/textures/gui/unlocked.png b/src/main/resources/assets/elytra-lock/textures/gui/unlocked.png new file mode 100644 index 0000000..a821a0c Binary files /dev/null and b/src/main/resources/assets/elytra-lock/textures/gui/unlocked.png differ