From 6886f2d89416bf56d0255f6aac3036141ce41caf Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Fri, 17 May 2024 01:06:17 +0300 Subject: Add LOCATION_CHANGE event, fired upon parsing /locraw (#716) --- .../java/de/hysky/skyblocker/events/SkyblockEvents.java | 13 +++++++++++++ src/main/java/de/hysky/skyblocker/utils/Utils.java | 8 ++++++++ 2 files changed, 21 insertions(+) (limited to 'src/main/java/de/hysky/skyblocker') diff --git a/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java b/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java index 303e454f..c268103d 100644 --- a/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java +++ b/src/main/java/de/hysky/skyblocker/events/SkyblockEvents.java @@ -1,5 +1,6 @@ package de.hysky.skyblocker.events; +import de.hysky.skyblocker.utils.Location; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.event.Event; @@ -19,6 +20,12 @@ public final class SkyblockEvents { } }); + public static final Event LOCATION_CHANGE = EventFactory.createArrayBacked(SkyblockEvents.SkyblockLocationChange.class, callbacks -> location -> { + for (SkyblockEvents.SkyblockLocationChange callback : callbacks) { + callback.onSkyblockLocationChange(location); + } + }); + @Environment(EnvType.CLIENT) @FunctionalInterface public interface SkyblockJoin { @@ -30,4 +37,10 @@ public final class SkyblockEvents { public interface SkyblockLeave { void onSkyblockLeave(); } + + @Environment(EnvType.CLIENT) + @FunctionalInterface + public interface SkyblockLocationChange { + void onSkyblockLocationChange(Location location); + } } diff --git a/src/main/java/de/hysky/skyblocker/utils/Utils.java b/src/main/java/de/hysky/skyblocker/utils/Utils.java index 00028011..62a3b897 100644 --- a/src/main/java/de/hysky/skyblocker/utils/Utils.java +++ b/src/main/java/de/hysky/skyblocker/utils/Utils.java @@ -74,6 +74,8 @@ public class Utils { private static long clientWorldJoinTime = 0; private static boolean sentLocRaw = false; private static boolean canSendLocRaw = false; + //This is required to prevent the location change event from being fired twice. + private static boolean locationChanged = true; private static String mayor = ""; @@ -383,6 +385,7 @@ public class Utils { MessageScheduler.INSTANCE.sendMessageAfterCooldown("/locraw"); sentLocRaw = true; canSendLocRaw = false; + locationChanged = true; } } else { resetLocRawInfo(); @@ -413,6 +416,11 @@ public class Utils { if (locRaw.has("map")) { map = locRaw.get("map").getAsString(); } + + if (locationChanged) { + SkyblockEvents.LOCATION_CHANGE.invoker().onSkyblockLocationChange(location); + locationChanged = false; + } } /** -- cgit