-
Notifications
You must be signed in to change notification settings - Fork 6
JM style waypoint sharing #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Cathal-poon
wants to merge
5
commits into
LordFokas:mc-1.18.2-v0.8.0-overhaul
Choose a base branch
from
Cathal-poon:JM-Waypoint-Sharing
base: mc-1.18.2-v0.8.0-overhaul
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3a3f66a
BME-105 WIP
Cathal-poon af75280
Merge remote-tracking branch 'upstream/mc-1.18.2-v0.8.0-overhaul' int…
Cathal-poon 82c1a31
BME-105: Share Waypoint from world map context menu
Cathal-poon 7fb9c38
BME-105 Chat parsing and highlighting for waypoint sharing
Cathal-poon 44ea047
Merge remote-tracking branch 'upstream/mc-1.18.2-v0.8.0-overhaul' int…
Cathal-poon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/main/java/com/eerussianguy/blazemap/feature/waypoints/WaypointSharing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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