From a1923e3ad41bee045101f85223a0c7157edac097 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Wed, 22 May 2024 00:08:20 +0530 Subject: [PATCH] feat: create RunOnceOnToggle utility class and use that --- .vscode/settings.json | 3 +- build.gradle | 8 +--- .../events/ClientTickEndHandler.java | 39 ++++++++++--------- .../elytralock/events/HudRenderHandler.java | 4 +- .../elytralock/mixin/PlayerEntityMixin.java | 14 +++++-- .../elytralock/util/RunOnceOnToggle.java | 28 +++++++++++++ 6 files changed, 64 insertions(+), 32 deletions(-) create mode 100644 src/main/java/xyz/devcomp/elytralock/util/RunOnceOnToggle.java diff --git a/.vscode/settings.json b/.vscode/settings.json index a6b86d5..d6b6f31 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "java.configuration.updateBuildConfiguration": "automatic", - "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m -Xlog:disable" + "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m -Xlog:disable", + "editor.formatOnSave": true } \ No newline at end of file diff --git a/build.gradle b/build.gradle index d484732..618d959 100644 --- a/build.gradle +++ b/build.gradle @@ -104,11 +104,5 @@ publishing { } } - // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. - repositories { - // Add repositories to publish to here. - // Notice: This block does NOT have the same function as the block in the top level. - // The repositories here will be used for publishing your artifact, not for - // retrieving dependencies. - } + repositories {} } \ No newline at end of file diff --git a/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java b/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java index 62c1e6e..870b3ec 100644 --- a/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java +++ b/src/main/java/xyz/devcomp/elytralock/events/ClientTickEndHandler.java @@ -1,32 +1,33 @@ package xyz.devcomp.elytralock.events; +import java.util.function.Consumer; + 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 net.minecraft.item.Items; import xyz.devcomp.elytralock.ElytraLock; +import xyz.devcomp.elytralock.util.RunOnceOnToggle; public class ClientTickEndHandler implements EndTick { - private static boolean hasWarned = false; + private static RunOnceOnToggle impl = new RunOnceOnToggle( + new Consumer() { + public void accept(MinecraftClient client) { + 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"); + } + } + }); public void onEndTick(MinecraftClient client) { - if (ElytraLock.isLocked()) { - if (!hasWarned && client.isWindowFocused() && 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"); - hasWarned = true; - } - } - } else { - hasWarned = false; - } + impl.run(client); } } diff --git a/src/main/java/xyz/devcomp/elytralock/events/HudRenderHandler.java b/src/main/java/xyz/devcomp/elytralock/events/HudRenderHandler.java index 6d2270a..60b3f4e 100644 --- a/src/main/java/xyz/devcomp/elytralock/events/HudRenderHandler.java +++ b/src/main/java/xyz/devcomp/elytralock/events/HudRenderHandler.java @@ -18,7 +18,7 @@ public class HudRenderHandler implements HudRenderCallback { Window window = ElytraLock.client.getWindow(); int width = window.getScaledWidth(), height = window.getScaledHeight(); - + context.drawGuiTexture(icon, (width / 2) + 95, height - HEIGHT - 3, WIDTH, HEIGHT); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/java/xyz/devcomp/elytralock/mixin/PlayerEntityMixin.java b/src/main/java/xyz/devcomp/elytralock/mixin/PlayerEntityMixin.java index 53d2cc1..39f8975 100644 --- a/src/main/java/xyz/devcomp/elytralock/mixin/PlayerEntityMixin.java +++ b/src/main/java/xyz/devcomp/elytralock/mixin/PlayerEntityMixin.java @@ -1,6 +1,9 @@ package xyz.devcomp.elytralock.mixin; +import java.util.function.Consumer; + import xyz.devcomp.elytralock.ElytraLock; +import xyz.devcomp.elytralock.util.RunOnceOnToggle; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -15,11 +18,16 @@ import net.minecraft.entity.player.PlayerEntity; @Mixin(PlayerEntity.class) public class PlayerEntityMixin { + private static RunOnceOnToggle logOnce = new RunOnceOnToggle( + new Consumer() { + public void accept(String msg) { + ElytraLock.LOGGER.info(msg); + } + }); + @Inject(method = "checkFallFlying()Z", at = @At("HEAD"), cancellable = true) private void preventFallFlying(CallbackInfoReturnable info) { - if (ElytraLock.isLocked()) { - ElytraLock.LOGGER.info("Elytra is locked, so preventing fall flying"); + if (logOnce.run("Elytra is locked, so preventing fall flying")) info.setReturnValue(false); - } } } \ No newline at end of file diff --git a/src/main/java/xyz/devcomp/elytralock/util/RunOnceOnToggle.java b/src/main/java/xyz/devcomp/elytralock/util/RunOnceOnToggle.java new file mode 100644 index 0000000..8aa004b --- /dev/null +++ b/src/main/java/xyz/devcomp/elytralock/util/RunOnceOnToggle.java @@ -0,0 +1,28 @@ +package xyz.devcomp.elytralock.util; + +import java.util.function.Consumer; + +import xyz.devcomp.elytralock.ElytraLock; + +public class RunOnceOnToggle { + private static boolean hasRun = false; + private Consumer toRun; + + public RunOnceOnToggle(Consumer toRun) { + this.toRun = toRun; + } + + public boolean run(T param) { + boolean isLocked = ElytraLock.isLocked(); + if (isLocked) { + if (!hasRun) { + toRun.accept(param); + hasRun = true; + } + } else { + hasRun = false; + } + + return isLocked; + } +}