Skip to content

Commit

Permalink
Refactor individual waypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Oct 20, 2024
1 parent 3270096 commit fb6d881
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
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;

import java.util.List;
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: (?<x>-?[0-9]+), y: (?<y>[0-9]+), z: (?<z>-?[0-9]+)");
private static final Pattern SKYBLOCKER_COORDS_PATTERN = Pattern.compile("x: (?<x>-?[0-9]+), y: (?<y>[0-9]+), z: (?<z>-?[0-9]+)(?: \\| (?<area>[^|]+))");
Expand All @@ -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) {
Expand All @@ -42,25 +58,28 @@ 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;
}
}
}
}

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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Text> 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;
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/skyblocker/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit fb6d881

Please sign in to comment.