Skip to content
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

Add string to region converter #4699

Merged
merged 3 commits into from Apr 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 25 additions & 17 deletions src/main/java/ch/njol/skript/hooks/regions/classes/Region.java
Expand Up @@ -26,6 +26,7 @@
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.Converters;
import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -74,23 +75,7 @@ public Region parse(String s, final ParseContext context) {
if (!VariableString.isQuotedCorrectly(s, quoted))
return null;
s = VariableString.unquote(s, quoted);
Region r = null;
for (final World w : Bukkit.getWorlds()) {
@SuppressWarnings("null")
final Region r2 = RegionsPlugin.getRegion(w, s);
if (r2 == null)
continue;
if (r != null) {
Skript.error("Multiple regions with the name '" + s + "' exist");
return null;
}
r = r2;
}
if (r == null) {
Skript.error("Region '" + s + "' could not be found");
return null;
}
return r;
return Region.parse(s, true);
}

@Override
Expand All @@ -109,6 +94,29 @@ public boolean mustSyncDeserialization() {
return true;
}
}));
Converters.registerConverter(String.class, Region.class, s -> Region.parse(s, false));
}

@Nullable
private static Region parse(String s, boolean error) {
Region r = null;
for (World w : Bukkit.getWorlds()) {
Region r2 = RegionsPlugin.getRegion(w, s);
if (r2 == null)
continue;
if (r != null) {
if (error)
Skript.error("Multiple regions with the name '" + s + "' exist");
return null;
}
r = r2;
}
if (r == null) {
if (error)
Skript.error("Region '" + s + "' could not be found");
return null;
}
return r;
}

public abstract boolean contains(Location l);
Expand Down