From eb92c792c802980217df27c0629036cdf0f85b0c Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Sat, 29 Jun 2024 23:34:13 +0200 Subject: [PATCH 1/3] add allowLookups option to offlineplayer function --- .../skript/classes/data/DefaultFunctions.java | 66 ++++++++++++++----- .../tests/syntaxes/functions/offlinePlayer.sk | 7 ++ 2 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 src/test/skript/tests/syntaxes/functions/offlinePlayer.sk diff --git a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java index afc46cba346..afb2e9e146a 100644 --- a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java +++ b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java @@ -18,6 +18,7 @@ */ package ch.njol.skript.classes.data; +import ch.njol.skript.Skript; import ch.njol.skript.expressions.base.EventValueExpression; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.function.FunctionEvent; @@ -45,7 +46,9 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.ArrayList; import java.util.Calendar; +import java.util.List; import java.util.UUID; public class DefaultFunctions { @@ -556,23 +559,54 @@ public Player[] executeSimple(Object[][] params) { .examples("set {_p} to player(\"Notch\") # will return an online player whose name is or starts with 'Notch'", "set {_p} to player(\"Notch\", true) # will return the only online player whose name is 'Notch'", "set {_p} to player(\"069a79f4-44e9-4726-a5be-fca90e38aaf5\") # if player is offline") .since("2.8.0"); - Functions.registerFunction(new SimpleJavaFunction("offlineplayer", new Parameter[] { - new Parameter<>("nameOrUUID", DefaultClasses.STRING, true, null) - }, DefaultClasses.OFFLINE_PLAYER, true) { - @Override - public OfflinePlayer[] executeSimple(Object[][] params) { - String name = (String) params[0][0]; - UUID uuid = null; - if (name.length() > 16 || name.contains("-")) { // shortcut - try { - uuid = UUID.fromString(name); - } catch (IllegalArgumentException ignored) {} + { // offline player function + boolean hasIfCached = Skript.methodExists(Bukkit.class, "getOfflinePlayerIfCached", String.class); + + List> params = new ArrayList<>(); + params.add(new Parameter<>("nameOrUUID", DefaultClasses.STRING, true, null)); + if (hasIfCached) + params.add(new Parameter<>("allowLookups", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(true, true))); + + Functions.registerFunction(new SimpleJavaFunction("offlineplayer", params.toArray(new Parameter[0]), + DefaultClasses.OFFLINE_PLAYER, true) { + @Override + public OfflinePlayer[] executeSimple(Object[][] params) { + String name = (String) params[0][0]; + UUID uuid = null; + if (name.length() > 16 || name.contains("-")) { // shortcut + try { + uuid = UUID.fromString(name); + } catch (IllegalArgumentException ignored) { + } + } + OfflinePlayer result; + + if (uuid != null) { + result = Bukkit.getOfflinePlayer(uuid); // doesn't do lookups + } else if (hasIfCached && !((Boolean) params[1][0])) { + result = Bukkit.getOfflinePlayerIfCached(name); + if (result == null) + return new OfflinePlayer[0]; + } else { + result = Bukkit.getOfflinePlayer(name); + } + + return CollectionUtils.array(result); } - return CollectionUtils.array(uuid != null ? Bukkit.getOfflinePlayer(uuid) : Bukkit.getOfflinePlayer(name)); - } - }).description("Returns a offline player from their name or UUID. This function will still return the player if they're online.") - .examples("set {_p} to offlineplayer(\"Notch\")", "set {_p} to offlineplayer(\"069a79f4-44e9-4726-a5be-fca90e38aaf5\")") - .since("2.8.0"); + + }).description( + "Returns a offline player from their name or UUID. This function will still return the player if they're online. " + + "If Paper 1.16.5+ is used, the 'allowLookup' parameter can be set to false to prevent this function from doing a " + + "UUID lookup for players who have not joined before. Lookups can cause lag spikes up up to multiple seconds, so " + + "use offline players with caution." + ) + .examples( + "set {_p} to offlineplayer(\"Notch\")", + "set {_p} to offlineplayer(\"069a79f4-44e9-4726-a5be-fca90e38aaf5\")", + "set {_p} to offlineplayer(\"Notch\", false)" + ) + .since("2.8.0, INSERT VERSION (prevent lookups)"); + } // end offline player function Functions.registerFunction(new SimpleJavaFunction("isNaN", numberParam, DefaultClasses.BOOLEAN, true) { @Override diff --git a/src/test/skript/tests/syntaxes/functions/offlinePlayer.sk b/src/test/skript/tests/syntaxes/functions/offlinePlayer.sk new file mode 100644 index 00000000000..b96d67a7487 --- /dev/null +++ b/src/test/skript/tests/syntaxes/functions/offlinePlayer.sk @@ -0,0 +1,7 @@ +test "offline player function": + set {_lookup} to offlineplayer("Notch") + assert {_lookup} is set with "Failed to look up offline player" + +test "offline player function no lookup" when running minecraft "1.16": + set {_non-lookup} to offlineplayer("Dinnerbone", false) + assert {_non-lookup} is not set with "Looked up offline player when told not to" From 4d44c9f082ff2eca3d24b8503d15c8f55e0667f8 Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Sat, 29 Jun 2024 23:37:17 +0200 Subject: [PATCH 2/3] typo --- src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java index afb2e9e146a..93b5c34493c 100644 --- a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java +++ b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java @@ -597,7 +597,7 @@ public OfflinePlayer[] executeSimple(Object[][] params) { }).description( "Returns a offline player from their name or UUID. This function will still return the player if they're online. " + "If Paper 1.16.5+ is used, the 'allowLookup' parameter can be set to false to prevent this function from doing a " + - "UUID lookup for players who have not joined before. Lookups can cause lag spikes up up to multiple seconds, so " + + "UUID lookup for players who have not joined before. Lookups can cause lag spikes of up to multiple seconds, so " + "use offline players with caution." ) .examples( From d91fcd1acf8b8122011f54b1c910b65a416c410f Mon Sep 17 00:00:00 2001 From: sovde <10354869+sovdeeth@users.noreply.github.com> Date: Sat, 29 Jun 2024 23:50:13 +0200 Subject: [PATCH 3/3] less ambiguous wording in description --- src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java index 93b5c34493c..525e639fbc3 100644 --- a/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java +++ b/src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java @@ -597,7 +597,7 @@ public OfflinePlayer[] executeSimple(Object[][] params) { }).description( "Returns a offline player from their name or UUID. This function will still return the player if they're online. " + "If Paper 1.16.5+ is used, the 'allowLookup' parameter can be set to false to prevent this function from doing a " + - "UUID lookup for players who have not joined before. Lookups can cause lag spikes of up to multiple seconds, so " + + "web lookup for players who have not joined before. Lookups can cause lag spikes of up to multiple seconds, so " + "use offline players with caution." ) .examples(