Improve performance of player() related methods (#1240)

This commit is contained in:
altrisi authored and GitHub committed 2022-01-08 23:11:10 +01:00
1 parent 9fb6ec91cb
commit 8f265f4e52
1 file changed
+32 -44
+32 -44
View File
@@ -3,6 +3,7 @@ package carpet.script.api;
import carpet.script.CarpetContext; import carpet.script.CarpetContext;
import carpet.script.CarpetEventServer; import carpet.script.CarpetEventServer;
import carpet.script.CarpetScriptHost; import carpet.script.CarpetScriptHost;
import carpet.script.Context;
import carpet.script.Expression; import carpet.script.Expression;
import carpet.script.argument.FunctionArgument; import carpet.script.argument.FunctionArgument;
import carpet.script.argument.Vector3Argument; import carpet.script.argument.Vector3Argument;
@@ -36,9 +37,20 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Entities { public class Entities {
private static ListValue getPlayersFromWorldMatching(Context c, Predicate<ServerPlayerEntity> condition) {
List<Value> ret = new ArrayList<>();
for (ServerPlayerEntity player: ((CarpetContext) c).s.getWorld().getPlayers()) {
if (condition.test(player)) {
ret.add(new EntityValue(player));
}
}
return ListValue.wrap(ret);
}
public static void apply(Expression expression) public static void apply(Expression expression)
{ {
expression.addContextFunction("player", -1, (c, t, lv) -> expression.addContextFunction("player", -1, (c, t, lv) ->
@@ -58,50 +70,26 @@ public class Entities {
return EntityValue.of(closestPlayer); return EntityValue.of(closestPlayer);
} }
String playerName = lv.get(0).getString(); String playerName = lv.get(0).getString();
Value retval = Value.NULL; return switch (playerName) {
if ("all".equalsIgnoreCase(playerName)) case "all" -> {
{ List<Value> ret = new ArrayList<>();
retval = ListValue.wrap( for (ServerPlayerEntity player: ((CarpetContext)c).s.getServer().getPlayerManager().getPlayerList())
((CarpetContext)c).s.getServer().getPlayerManager().getPlayerList(). ret.add(new EntityValue(player));
stream().map(EntityValue::new).collect(Collectors.toList())); yield ListValue.wrap(ret);
} }
else if ("*".equalsIgnoreCase(playerName)) case "*" -> getPlayersFromWorldMatching(c, p -> true);
{ case "survival" -> getPlayersFromWorldMatching(c, p -> p.interactionManager.isSurvivalLike());
retval = ListValue.wrap( case "creative" -> getPlayersFromWorldMatching(c, PlayerEntity::isCreative);
((CarpetContext)c).s.getWorld().getPlayers(). case "spectating" -> getPlayersFromWorldMatching(c, PlayerEntity::isSpectator);
stream().map(EntityValue::new).collect(Collectors.toList())); case "!spectating" -> getPlayersFromWorldMatching(c, p -> !p.isSpectator());
} default -> {
else if ("survival".equalsIgnoreCase(playerName)) ServerPlayerEntity player = ((CarpetContext) c).s.getServer().getPlayerManager().getPlayer(playerName);
{ if (player != null) {
retval = ListValue.wrap( yield new EntityValue(player);
((CarpetContext)c).s.getWorld().getPlayers((p) -> p.interactionManager.isSurvivalLike()). }
stream().map(EntityValue::new).collect(Collectors.toList())); yield Value.NULL;
} }
else if ("creative".equalsIgnoreCase(playerName)) };
{
retval = ListValue.wrap(
((CarpetContext)c).s.getWorld().getPlayers(PlayerEntity::isCreative).
stream().map(EntityValue::new).collect(Collectors.toList()));
}
else if ("spectating".equalsIgnoreCase(playerName))
{
retval = ListValue.wrap(
((CarpetContext)c).s.getWorld().getPlayers(PlayerEntity::isSpectator).
stream().map(EntityValue::new).collect(Collectors.toList()));
}
else if ("!spectating".equalsIgnoreCase(playerName))
{
retval = ListValue.wrap(
((CarpetContext)c).s.getWorld().getPlayers((p) -> !p.isSpectator()).
stream().map(EntityValue::new).collect(Collectors.toList()));
}
else
{
ServerPlayerEntity player = ((CarpetContext) c).s.getServer().getPlayerManager().getPlayer(playerName);
if (player != null)
retval = new EntityValue(player);
}
return retval;
}); });
expression.addContextFunction("spawn", -1, (c, t, lv) -> expression.addContextFunction("spawn", -1, (c, t, lv) ->