feat: create RunOnceOnToggle utility class and use that

This commit is contained in:
Erica Marigold 2024-05-22 00:08:20 +05:30
parent 5712ba2050
commit a1923e3ad4
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
6 changed files with 64 additions and 32 deletions

View file

@ -1,4 +1,5 @@
{ {
"java.configuration.updateBuildConfiguration": "automatic", "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
} }

View file

@ -104,11 +104,5 @@ publishing {
} }
} }
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. repositories {}
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.
}
} }

View file

@ -1,32 +1,33 @@
package xyz.devcomp.elytralock.events; package xyz.devcomp.elytralock.events;
import java.util.function.Consumer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents.EndTick; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents.EndTick;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import xyz.devcomp.elytralock.ElytraLock; import xyz.devcomp.elytralock.ElytraLock;
import xyz.devcomp.elytralock.util.RunOnceOnToggle;
public class ClientTickEndHandler implements EndTick { public class ClientTickEndHandler implements EndTick {
private static boolean hasWarned = false; private static RunOnceOnToggle<MinecraftClient> impl = new RunOnceOnToggle<MinecraftClient>(
new Consumer<MinecraftClient>() {
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) { public void onEndTick(MinecraftClient client) {
if (ElytraLock.isLocked()) { impl.run(client);
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;
}
} }
} }

View file

@ -18,7 +18,7 @@ public class HudRenderHandler implements HudRenderCallback {
Window window = ElytraLock.client.getWindow(); Window window = ElytraLock.client.getWindow();
int width = window.getScaledWidth(), height = window.getScaledHeight(); int width = window.getScaledWidth(), height = window.getScaledHeight();
context.drawGuiTexture(icon, (width / 2) + 95, height - HEIGHT - 3, WIDTH, HEIGHT); context.drawGuiTexture(icon, (width / 2) + 95, height - HEIGHT - 3, WIDTH, HEIGHT);
} }
} }

View file

@ -1,6 +1,9 @@
package xyz.devcomp.elytralock.mixin; package xyz.devcomp.elytralock.mixin;
import java.util.function.Consumer;
import xyz.devcomp.elytralock.ElytraLock; import xyz.devcomp.elytralock.ElytraLock;
import xyz.devcomp.elytralock.util.RunOnceOnToggle;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@ -15,11 +18,16 @@ import net.minecraft.entity.player.PlayerEntity;
@Mixin(PlayerEntity.class) @Mixin(PlayerEntity.class)
public class PlayerEntityMixin { public class PlayerEntityMixin {
private static RunOnceOnToggle<String> logOnce = new RunOnceOnToggle<String>(
new Consumer<String>() {
public void accept(String msg) {
ElytraLock.LOGGER.info(msg);
}
});
@Inject(method = "checkFallFlying()Z", at = @At("HEAD"), cancellable = true) @Inject(method = "checkFallFlying()Z", at = @At("HEAD"), cancellable = true)
private void preventFallFlying(CallbackInfoReturnable<Boolean> info) { private void preventFallFlying(CallbackInfoReturnable<Boolean> info) {
if (ElytraLock.isLocked()) { if (logOnce.run("Elytra is locked, so preventing fall flying"))
ElytraLock.LOGGER.info("Elytra is locked, so preventing fall flying");
info.setReturnValue(false); info.setReturnValue(false);
}
} }
} }

View file

@ -0,0 +1,28 @@
package xyz.devcomp.elytralock.util;
import java.util.function.Consumer;
import xyz.devcomp.elytralock.ElytraLock;
public class RunOnceOnToggle<T> {
private static boolean hasRun = false;
private Consumer<T> toRun;
public RunOnceOnToggle(Consumer<T> 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;
}
}