Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ private static void mapMenu(MapMenuSetupEvent evt) {

public static void initWaypoints() {
IEventBus bus = MinecraftForge.EVENT_BUS;
bus.addListener(WaypointSharing::onChatReceive);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be a bit nitpicky, but this should be called onChatReceived ... one letter missing :p

bus.register(WaypointServiceClient.class);
bus.register(WaypointServiceServer.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.eerussianguy.blazemap.engine.cache.ChunkMDCache;
import com.eerussianguy.blazemap.engine.client.ClientEngine;
import com.eerussianguy.blazemap.feature.waypoints.WaypointEditorFragment;
import com.eerussianguy.blazemap.feature.waypoints.WaypointSharing;
import com.eerussianguy.blazemap.feature.waypoints.service.WaypointServiceClient;
import com.eerussianguy.blazemap.lib.Colors;
import com.eerussianguy.blazemap.lib.Helpers;
Expand Down Expand Up @@ -44,6 +45,7 @@ public static MenuFolder waypoints(int blockX, int blockZ) {
folder.add(makeFolder("waypoint.options", waypoint.getIcon(), waypoint.getColor(), waypoint.getName(),
makeAction("waypoint.edit", null, () -> new WaypointEditorFragment(waypoint).open()),
makeAction("waypoint.hide", null, null),
makeAction("waypoint.share", null, () -> WaypointSharing.shareWaypoint(waypoint)),
makeAction("waypoint.delete", null, null)
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.eerussianguy.blazemap.feature.waypoints;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.eerussianguy.blazemap.BlazeMap;
import com.eerussianguy.blazemap.api.markers.Waypoint;
import com.eerussianguy.blazemap.lib.Helpers;

import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.ChatScreen;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.BaseComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import net.minecraftforge.client.event.ClientChatReceivedEvent;

public class WaypointSharing {

public static void onChatReceive(ClientChatReceivedEvent event){
Component incomingMessage = event.getMessage();

Component msg = parseChatMessage(incomingMessage); // could be one line.
event.setMessage(msg);
}

public static void shareWaypoint(Waypoint waypoint) {
BlockPos pos = waypoint.getPosition();
ResourceKey<Level> dimension = waypoint.getDimension();
String name = waypoint.getName();

String format = "[name:%1$s, x:%2$d, y:%3$d, z:%4$d, dim:%5$s]";

String msg = String.format(format, name, pos.getX(), pos.getY(), pos.getZ(), dimension.getRegistryName());

// Minecraft.getInstance().setScreen(new ChatScreen(msg));
Helpers.getPlayer().chat(msg);
}


/**
* Returns the same message from incomingMessage with waypoints highlighted.
* If there's no waypoint then the same message is sent back
*
* @param incomingMessage A Minecraft Component
* @return Component
*/
private static Component parseChatMessage(Component incomingMessage) {
String chatMessage = incomingMessage.getString();

// Check if a waypoint might be in chat
Pattern pattern = Pattern.compile("\\[[^\\[\\]]*,[^\\[\\]]*\\]"); // this doesn't guarentee that a waypoint exists int the message. Just that it's likely to exits
Matcher matcher = pattern.matcher(chatMessage);
boolean matchFound = matcher.find();

// Return original message if no waypoint is found
if (! matchFound) {
return incomingMessage;
}

// Seperatae the waypoint text from
BaseComponent parsedChat = new TextComponent("");
int previousEnd = 0;
do {
parsedChat.append(chatMessage.substring(previousEnd, matcher.start())); // Add the message before the waypoint
previousEnd = matcher.end();
parsedChat.append(parseWaypoint(matcher.group()));
matchFound = matcher.find();
} while (matchFound);
parsedChat.append(chatMessage.substring(previousEnd, chatMessage.length()));

return parsedChat;
}


/**
* Checks if the given string is in a waypoint format. Applies formatting to the text if it is and empty text if it's not.
* @param input The string to be checked. Should be wrapped in square brackets
* @return TextComponent The given string with appropriate formatting
*/
private static TextComponent parseWaypoint(String input) { // This should probably return a waypoint instead but I don't know if the API will allow partially formed Waypoints.
String workingString = input.substring(1, input.length()-1); // remove the [] from the beginning and end.
Pattern pattern = Pattern.compile("(?<parameter>\\w*):(?<value>[^,\\n\\r]*)");
Matcher matcher = pattern.matcher(workingString);
boolean matchFound = matcher.find();

TextComponent output = new TextComponent(input);

// return original message unmodified if no match is found
if (!matchFound) {
return output;
}

// parse the message and get all the valid fields.
Integer x, y, z; // Maybe use Options here
x = y = z = null;
String name, dimension; // TODO change dimension to Resource Key level
String parameter, value;

do {
parameter = matcher.group("parameter").toLowerCase();
value = matcher.group("value").trim().toLowerCase();
try {
switch (parameter) {
case "name":
name = value;
break;
case "x":
x = Integer.parseInt(value);
break;
case "y":
y = Integer.parseInt(value);
break;
case "z":
z = Integer.parseInt(value);
break;
case "dim":
dimension = value;
break;
default:
BlazeMap.LOGGER.info("Unsupported parameter :'%s'", parameter);
break;
}
} catch (NumberFormatException exception) {
BlazeMap.LOGGER.error(String.format("Can't parse '%s' to integer", value), exception);
}

matchFound = matcher.find();
} while (matchFound);


if (x == null || z == null) {
return output;
}

output.withStyle(ChatFormatting.UNDERLINE).withStyle(ChatFormatting.AQUA);

return output;
}


}
1 change: 1 addition & 0 deletions src/main/resources/assets/blazemap/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"blazemap.gui.worldmap.menu.waypoint.new": "Create New Waypoint",
"blazemap.gui.worldmap.menu.waypoint.edit": "Edit",
"blazemap.gui.worldmap.menu.waypoint.hide": "Hide",
"blazemap.gui.worldmap.menu.waypoint.share": "Share",
"blazemap.gui.worldmap.menu.waypoint.delete": "Delete",
"blazemap.gui.worldmap.menu.debug": "Debug",
"blazemap.gui.worldmap.menu.debug.redraw_chunk_md": "Redraw Chunk from MD Cache",
Expand Down