From fb6d8814398a25801304d84f815b0ccfef0ebe6d Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:43:05 -0400 Subject: [PATCH] Refactor individual waypoint --- ...ntLocation.java => ChatPositionShare.java} | 51 +++++--- .../chat/chatcoords/ChatLocation.java | 26 ----- .../skyblock/waypoint/IndividualWaypoint.java | 110 +++++++++--------- .../assets/skyblocker/lang/en_us.json | 3 +- 4 files changed, 93 insertions(+), 97 deletions(-) rename src/main/java/de/hysky/skyblocker/skyblock/chat/{chatcoords/ChatWaypointLocation.java => ChatPositionShare.java} (51%) delete mode 100644 src/main/java/de/hysky/skyblocker/skyblock/chat/chatcoords/ChatLocation.java diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/chatcoords/ChatWaypointLocation.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatPositionShare.java similarity index 51% rename from src/main/java/de/hysky/skyblocker/skyblock/chat/chatcoords/ChatWaypointLocation.java rename to src/main/java/de/hysky/skyblocker/skyblock/chat/ChatPositionShare.java index b291a76355..92f17a5fc9 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/chatcoords/ChatWaypointLocation.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatPositionShare.java @@ -1,14 +1,23 @@ -package de.hysky.skyblocker.skyblock.chat.chatcoords; +package de.hysky.skyblocker.skyblock.chat; +import com.mojang.brigadier.Command; import de.hysky.skyblocker.annotations.Init; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.scheduler.MessageScheduler; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; import net.minecraft.client.MinecraftClient; import net.minecraft.text.ClickEvent; +import net.minecraft.text.HoverEvent; +import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import net.minecraft.util.math.Vec3d; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,9 +25,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public class ChatWaypointLocation { - - private static final Logger LOGGER = LoggerFactory.getLogger(ChatWaypointLocation.class); +public class ChatPositionShare { + private static final Logger LOGGER = LoggerFactory.getLogger(ChatPositionShare.class); private static final Pattern GENERIC_COORDS_PATTERN = Pattern.compile("x: (?-?[0-9]+), y: (?[0-9]+), z: (?-?[0-9]+)"); private static final Pattern SKYBLOCKER_COORDS_PATTERN = Pattern.compile("x: (?-?[0-9]+), y: (?[0-9]+), z: (?-?[0-9]+)(?: \\| (?[^|]+))"); @@ -27,12 +35,20 @@ public class ChatWaypointLocation { @Init public static void init() { - ClientReceiveMessageEvents.GAME.register(ChatWaypointLocation::onMessage); + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register( + ClientCommandManager.literal("skyblocker").then(ClientCommandManager.literal("sharePosition").executes(context -> sharePlayerPosition(context.getSource()))) + )); + ClientReceiveMessageEvents.GAME.register(ChatPositionShare::onMessage); } + private static int sharePlayerPosition(FabricClientCommandSource source) { + Vec3d pos = source.getPosition(); + MessageScheduler.INSTANCE.sendMessageAfterCooldown("x: " + (int) pos.getX() + ", y: " + (int) pos.getY() + ", z: " + (int) pos.getZ() + " | " + Utils.getIslandArea(), true); + return Command.SINGLE_SUCCESS; + } + private static void onMessage(Text text, boolean overlay) { if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().uiAndVisuals.waypoints.enableWaypoints) { - String message = text.getString(); for (Pattern pattern : PATTERNS) { @@ -42,10 +58,10 @@ private static void onMessage(Text text, boolean overlay) { String x = matcher.group("x"); String y = matcher.group("y"); String z = matcher.group("z"); - String area = matcher.group("area"); + String area = matcher.namedGroups().containsKey("area") ? matcher.group("area") : ""; requestWaypoint(x, y, z, area); } catch (Exception e) { - LOGGER.error("[SKYBLOCKER CHAT WAYPOINTS] Error creating chat waypoint: ", e); + LOGGER.error("[Skyblocker Chat Waypoints] Error creating chat waypoint: ", e); } break; } @@ -53,14 +69,17 @@ private static void onMessage(Text text, boolean overlay) { } } - private static void requestWaypoint(String x, String y, String z, String area) { + private static void requestWaypoint(String x, String y, String z, @NotNull String area) { String command = "/skyblocker waypoints individual " + x + " " + y + " " + z + " " + area; - - Text text = Constants.PREFIX.get() - .append(Text.translatable("skyblocker.config.chat.waypoints.display").formatted(Formatting.AQUA) - .styled(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)))) - .append(Text.of(area != null ? " at " + area : "")); - - MinecraftClient.getInstance().player.sendMessage(text, false); + MutableText requestMessage = Constants.PREFIX.get().append(Text.translatable("skyblocker.config.chat.waypoints.display").formatted(Formatting.AQUA) + .styled(style -> style + .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable("skyblocker.config.chat.waypoints.display"))) + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)) + ) + ); + if (!area.isEmpty()) { + requestMessage = requestMessage.append(" at ").append(Text.literal(area).formatted(Formatting.AQUA)); + } + MinecraftClient.getInstance().player.sendMessage(requestMessage, false); } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/chatcoords/ChatLocation.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/chatcoords/ChatLocation.java deleted file mode 100644 index d519a414cd..0000000000 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/chatcoords/ChatLocation.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.hysky.skyblocker.skyblock.chat.chatcoords; - -import com.mojang.brigadier.Command; -import de.hysky.skyblocker.annotations.Init; -import de.hysky.skyblocker.utils.Utils; -import de.hysky.skyblocker.utils.scheduler.MessageScheduler; -import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; -import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayerEntity; - -public class ChatLocation { - @Init - public static void init() { - ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register( - ClientCommandManager.literal("skyblocker").then(ClientCommandManager.literal("location").executes(context -> sharePlayerLocation())) - )); - } - - private static int sharePlayerLocation() { - ClientPlayerEntity thePlayer = MinecraftClient.getInstance().player; - MessageScheduler.INSTANCE.sendMessageAfterCooldown("x: " + (int) thePlayer.getX() + ", y: " + (int) thePlayer.getY() + ", z: " + (int) thePlayer.getZ() + " | " + Utils.getIslandArea(), true); - return Command.SINGLE_SUCCESS; - } - -} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/IndividualWaypoint.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/IndividualWaypoint.java index 7a47037336..02fc24c29a 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/IndividualWaypoint.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/IndividualWaypoint.java @@ -6,6 +6,7 @@ import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.annotations.Init; import de.hysky.skyblocker.utils.ColorUtils; +import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; @@ -16,69 +17,70 @@ import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; import java.awt.*; +import java.util.function.Consumer; +/** + * One single temporary waypoint that gets deleted when the player gets close or changes world. + * Used for sharing positions from chat or other temporary uses. + */ public class IndividualWaypoint extends NamedWaypoint { + private static IndividualWaypoint waypoint; - private static IndividualWaypoint waypoint; + @Init + public static void init() { + ClientTickEvents.END_CLIENT_TICK.register(IndividualWaypoint::onTick); + WorldRenderEvents.AFTER_TRANSLUCENT.register(context -> {if (waypoint != null) waypoint.render(context);}); + ClientPlayConnectionEvents.JOIN.register((ignore, ignore2, ignore3) -> waypoint = null); + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register( + ClientCommandManager.literal(SkyblockerMod.NAMESPACE).then(ClientCommandManager.literal("waypoints").then(ClientCommandManager.literal("individual") + .then(ClientCommandManager.argument("x", IntegerArgumentType.integer(Integer.MIN_VALUE)) + .then(ClientCommandManager.argument("y", IntegerArgumentType.integer(Integer.MIN_VALUE)) + .then(ClientCommandManager.argument("z", IntegerArgumentType.integer(Integer.MIN_VALUE)) + .then(ClientCommandManager.argument("area", StringArgumentType.greedyString()) + .executes(context -> setWaypoint( + context.getSource()::sendFeedback, + IntegerArgumentType.getInteger(context, "x"), + IntegerArgumentType.getInteger(context, "y"), + IntegerArgumentType.getInteger(context, "z"), + StringArgumentType.getString(context, "area") + )) + ) + ) + ) + ) + ))) + ); + } - @Init - public static void init() { + public IndividualWaypoint(BlockPos pos, Text name, float[] colorComponents) { + super(pos, name, colorComponents, DEFAULT_HIGHLIGHT_ALPHA, true); + } - ClientTickEvents.END_CLIENT_TICK.register(IndividualWaypoint::onTick); + private static int setWaypoint(Consumer feedback, int x, int y, int z, String area) { + setWaypoint(x, y, z, area); + feedback.accept(Constants.PREFIX.get().append(Text.translatable("skyblocker.config.chat.waypoints.displayed", x, y, z, area))); + return Command.SINGLE_SUCCESS; + } - WorldRenderEvents.AFTER_TRANSLUCENT.register(context -> { if (waypoint != null) waypoint.render(context); }); + private static void setWaypoint(int x, int y, int z, String area) { + String waypointName = area != null && !area.isEmpty() ? area : "Chat Waypoint"; - ClientPlayConnectionEvents.JOIN.register((ignore, ignore2, ignore3) -> waypoint = null); + Text waypointDisplay; + if (waypointName.charAt(0) == '⏣') { + waypointDisplay = Text.literal("⏣").formatted(Formatting.DARK_PURPLE) + .append(Text.literal(waypointName.substring(1)).formatted(Formatting.AQUA)); + } else { + waypointDisplay = Text.literal(waypointName).formatted(Formatting.AQUA); + } - ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register( - ClientCommandManager.literal(SkyblockerMod.NAMESPACE) - .then(ClientCommandManager.literal("waypoints") - .then(ClientCommandManager.literal("individual") - .then(ClientCommandManager.argument("x", IntegerArgumentType.integer(Integer.MIN_VALUE)) - .then(ClientCommandManager.argument("y", IntegerArgumentType.integer(Integer.MIN_VALUE)) - .then(ClientCommandManager.argument("z", IntegerArgumentType.integer(Integer.MIN_VALUE)) - .then(ClientCommandManager.argument("area", StringArgumentType.greedyString()) - .executes(context -> setWaypoint( - IntegerArgumentType.getInteger(context, "x"), - IntegerArgumentType.getInteger(context, "y"), - IntegerArgumentType.getInteger(context, "z"), - StringArgumentType.getString(context, "area") - )) - ) - ) - ) - ) - ) - ) - )); - } - - public IndividualWaypoint(BlockPos pos, Text name, float[] colorComponents) { - super(pos, name, colorComponents, 0.5f, true); - } - - private static int setWaypoint(int x, int y, int z, String area) { - String waypointName = area != null && !area.isEmpty() ? area : "Waypoint"; - - Text waypointDisplay; - if (waypointName.charAt(0) == '⏣') { - waypointDisplay = Text.literal("⏣").formatted(Formatting.DARK_PURPLE) - .append(Text.literal(waypointName.substring(1)).formatted(Formatting.AQUA)); - } else { - waypointDisplay = Text.literal(waypointName).formatted(Formatting.AQUA); - } - - waypoint = new IndividualWaypoint(new BlockPos(x, y, z), waypointDisplay, ColorUtils.getFloatComponents(Color.GREEN.getRGB())); - return Command.SINGLE_SUCCESS; - } - - private static void onTick(MinecraftClient c) { - if (waypoint != null && c.player.getPos().distanceTo(Vec3d.ofCenter(waypoint.pos)) <= 8) { - waypoint = null; - } - } + waypoint = new IndividualWaypoint(new BlockPos(x, y, z), waypointDisplay, ColorUtils.getFloatComponents(Color.GREEN.getRGB())); + } + private static void onTick(MinecraftClient client) { + if (waypoint != null && client.player != null && client.player.squaredDistanceTo(waypoint.centerPos) <= 8) { + waypoint = null; + } + } } diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index 43658d66b3..dfce9f3ea8 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -449,7 +449,8 @@ "skyblocker.config.chat.chatRules.screen.ruleScreen.sounds.pling": "Pling", "skyblocker.config.chat.chatRules.screen.ruleScreen.sounds.zombie": "Zombie", "skyblocker.config.chat.chatRules.screen.ruleScreen.true": "True", - "skyblocker.config.chat.waypoints.display": "[Display Waypoint]", + "skyblocker.config.chat.waypoints.display": "[Click to Display Waypoint]", + "skyblocker.config.chat.waypoints.displayed": "Displayed temporary waypoint at x: %d, y: %d, z: %d | %s", "skyblocker.config.chat.filter": "Filter",