Skip to content
Open
Changes from 2 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 @@ -27,6 +27,9 @@
import org.skriptlang.skript.lang.properties.handlers.WXYZHandler;
import org.skriptlang.skript.lang.properties.handlers.base.PropertyHandler;

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

import java.io.StreamCorruptedException;

@ApiStatus.Internal
Expand Down Expand Up @@ -147,10 +150,49 @@ public boolean requiresSourceExprChange() {
}

private static class LocationParser extends Parser<Location> {
private static final String NUMBER = "[-+]?\\d+(?:\\.\\d+)?";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't use a regex matcher, split based on keywords and use the same parsers that are used for toString in the first place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Don't use a regex matcher, split based on keywords and use the same parsers that are used for toString in the first place.

which approach would you recommend?

substring at each
x:
y:
z:
...
manually handle world as a part of pitch

or, do the same thing but split at commas?
the issue is there's still some requirement to run different code to get the world since the format is
x: 0, y: 0, z: 0, yaw: 0, pitch: 0 in world 'world'
since in world is basically apart of pitch if I split at by commas
this is why I thought using regex instead was a no brainer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Split at x: , , y: , , z: , , yaw: , , pitch: and in ' (the exact strings from toString); if the last of which is missing, then the world was null. Not using regex helps avoid bugs here, for example your current NUMBER regex is incorrect because it does not match NaN or Infinity (which are valid outputs from toString!)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Pushed my most recent changes. is this what you meant?


private static final Pattern LOCATION_PATTERN = Pattern.compile(
"^x:\\s*(" + NUMBER + ")" +
",\\s*y:\\s*(" + NUMBER + ")" +
",\\s*z:\\s*(" + NUMBER + ")" +
",\\s*yaw:\\s*(" + NUMBER + ")" +
",\\s*pitch:\\s*(" + NUMBER + ")" +
"(?:\\s+in\\s+'([^']+)')?$"
);

//<editor-fold desc="location parser" defaultstate="collapsed">
@Override
public boolean canParse(ParseContext context) {
return false;
return true;
Comment thread
MrScopes marked this conversation as resolved.
Outdated
}

@Override
public @Nullable Location parse(String input, ParseContext context) {
Matcher matcher = LOCATION_PATTERN.matcher(input.trim());
if (!matcher.matches())
return null;

try {
double x = Double.parseDouble(matcher.group(1));
double y = Double.parseDouble(matcher.group(2));
double z = Double.parseDouble(matcher.group(3));
float yaw = Float.parseFloat(matcher.group(4));
float pitch = Float.parseFloat(matcher.group(5));

String worldName = matcher.group(6);
World world = null;

if (worldName != null) {
world = Bukkit.getWorld(worldName);
if (world == null)
return null;
Comment thread
MrScopes marked this conversation as resolved.
Outdated
}

return new Location(world, x, y, z, yaw, pitch);
} catch (NumberFormatException exception) {
return null;
Comment thread
MrScopes marked this conversation as resolved.
}
}

@Override
Expand Down
Loading