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 allowLookups option to offlineplayer function #6831

Merged
Merged
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
66 changes: 50 additions & 16 deletions src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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\") # <none> if player is offline")
.since("2.8.0");

Functions.registerFunction(new SimpleJavaFunction<OfflinePlayer>("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<Parameter<?>> 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>("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 " +
"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(
"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<Boolean>("isNaN", numberParam, DefaultClasses.BOOLEAN, true) {
@Override
Expand Down
7 changes: 7 additions & 0 deletions src/test/skript/tests/syntaxes/functions/offlinePlayer.sk
Original file line number Diff line number Diff line change
@@ -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"