Use primitive specific Predicates instead of Function (#859)

This commit is contained in:
altrisi authored and GitHub committed 2021-05-15 18:54:16 -03:00
1 parent 69e9e9b0bb
commit be0635de82
3 files changed
+18 -17

No files matched your search

@@ -56,7 +56,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -76,10 +76,10 @@ public class CarpetScriptHost extends ScriptHost
public Map<Value, Value> appConfig; public Map<Value, Value> appConfig;
public Map<String, CommandArgument> appArgTypes; public Map<String, CommandArgument> appArgTypes;
Function<ServerCommandSource, Boolean> commandValidator; Predicate<ServerCommandSource> commandValidator;
boolean isRuleApp; boolean isRuleApp;
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config, Map<String, CommandArgument> argTypes, Function<ServerCommandSource, Boolean> commandValidator, boolean isRuleApp) private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config, Map<String, CommandArgument> argTypes, Predicate<ServerCommandSource> commandValidator, boolean isRuleApp)
{ {
super(code, perUser, parent); super(code, perUser, parent);
this.saveTimeout = 0; this.saveTimeout = 0;
@@ -100,7 +100,7 @@ public class CarpetScriptHost extends ScriptHost
this.isRuleApp = isRuleApp; this.isRuleApp = isRuleApp;
} }
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source, Function<ServerCommandSource, Boolean> commandValidator, boolean isRuleApp) public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source, Predicate<ServerCommandSource> commandValidator, boolean isRuleApp)
{ {
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap(), new HashMap<>(), commandValidator, isRuleApp); CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap(), new HashMap<>(), commandValidator, isRuleApp);
// parse code and convert to expression // parse code and convert to expression
@@ -180,13 +180,13 @@ public class CarpetScriptHost extends ScriptHost
} }
public LiteralArgumentBuilder<ServerCommandSource> getNewCommandTree( public LiteralArgumentBuilder<ServerCommandSource> getNewCommandTree(
List<Pair<List<CommandToken>,FunctionArgument>> entries, Function<ServerCommandSource, Boolean> useValidator List<Pair<List<CommandToken>,FunctionArgument>> entries, Predicate<ServerCommandSource> useValidator
) throws CommandSyntaxException ) throws CommandSyntaxException
{ {
String hostName = main.getName(); String hostName = main.getName();
Function<ServerCommandSource, Boolean> configValidator = getCommandConfigPermissions(); Predicate<ServerCommandSource> configValidator = getCommandConfigPermissions();
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName). LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.apply(player) && configValidator.apply(player)); requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.test(player) && configValidator.test(player));
for (Pair<List<CommandToken>,FunctionArgument> commandData : entries) for (Pair<List<CommandToken>,FunctionArgument> commandData : entries)
{ {
command = this.addPathToCommand(command, commandData.getKey(), commandData.getValue()); command = this.addPathToCommand(command, commandData.getKey(), commandData.getValue());
@@ -194,7 +194,7 @@ public class CarpetScriptHost extends ScriptHost
return command; return command;
} }
public Function<ServerCommandSource, Boolean> getCommandConfigPermissions() throws CommandSyntaxException public Predicate<ServerCommandSource> getCommandConfigPermissions() throws CommandSyntaxException
{ {
Value confValue = appConfig.get(StringValue.of("command_permission")); Value confValue = appConfig.get(StringValue.of("command_permission"));
if (confValue == null) return s -> true; if (confValue == null) return s -> true;
@@ -475,7 +475,7 @@ public class CarpetScriptHost extends ScriptHost
return null; return null;
} }
Function<ServerCommandSource, Boolean> configValidator; Predicate<ServerCommandSource> configValidator;
try try
{ {
configValidator = getCommandConfigPermissions(); configValidator = getCommandConfigPermissions();
@@ -487,7 +487,7 @@ public class CarpetScriptHost extends ScriptHost
} }
String hostName = getName(); String hostName = getName();
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName). LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> scriptServer.modules.containsKey(hostName) && commandValidator.apply(player) && configValidator.apply(player)). requires((player) -> scriptServer.modules.containsKey(hostName) && commandValidator.test(player) && configValidator.test(player)).
executes( (c) -> executes( (c) ->
{ {
CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource()); CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource());
@@ -540,7 +540,7 @@ public class CarpetScriptHost extends ScriptHost
return true; return true;
} }
public LiteralArgumentBuilder<ServerCommandSource> readCommands(Function<ServerCommandSource, Boolean> useValidator) throws CommandSyntaxException public LiteralArgumentBuilder<ServerCommandSource> readCommands(Predicate<ServerCommandSource> useValidator) throws CommandSyntaxException
{ {
Value commands = appConfig.get(StringValue.of("commands")); Value commands = appConfig.get(StringValue.of("commands"));
@@ -37,7 +37,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.argument;
@@ -226,7 +226,7 @@ public class CarpetScriptServer
return modules.get(name); return modules.get(name);
} }
public boolean addScriptHost(ServerCommandSource source, String name, Function<ServerCommandSource, Boolean> commandValidator, public boolean addScriptHost(ServerCommandSource source, String name, Predicate<ServerCommandSource> commandValidator,
boolean perPlayer, boolean autoload, boolean isRuleApp) boolean perPlayer, boolean autoload, boolean isRuleApp)
{ {
CarpetProfiler.ProfilerToken currentSection = CarpetProfiler.start_section(null, "Scarpet load", CarpetProfiler.TYPE.GENERAL); CarpetProfiler.ProfilerToken currentSection = CarpetProfiler.start_section(null, "Scarpet load", CarpetProfiler.TYPE.GENERAL);
@@ -457,7 +457,7 @@ public class CarpetScriptServer
static class TransferData static class TransferData
{ {
boolean perUser; boolean perUser;
Function<ServerCommandSource, Boolean> commandValidator; Predicate<ServerCommandSource> commandValidator;
boolean isRuleApp; boolean isRuleApp;
private TransferData(CarpetScriptHost host) private TransferData(CarpetScriptHost host)
{ {
@@ -113,6 +113,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -132,7 +133,7 @@ public class WorldAccess {
Context c, Context c,
String name, String name,
List<Value> params, List<Value> params,
BiFunction<BlockState, BlockPos, Boolean> test BiPredicate<BlockState, BlockPos> test
) )
{ {
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
@@ -140,9 +141,9 @@ public class WorldAccess {
throw new InternalExpressionException("'" + name + "' requires at least one parameter"); throw new InternalExpressionException("'" + name + "' requires at least one parameter");
Value v0 = params.get(0); Value v0 = params.get(0);
if (v0 instanceof BlockValue) if (v0 instanceof BlockValue)
return BooleanValue.of(test.apply(((BlockValue) v0).getBlockState(), ((BlockValue) v0).getPos())); return BooleanValue.of(test.test(((BlockValue) v0).getBlockState(), ((BlockValue) v0).getPos()));
BlockValue block = BlockArgument.findIn(cc, params, 0).block; BlockValue block = BlockArgument.findIn(cc, params, 0).block;
return BooleanValue.of(test.apply(block.getBlockState(), block.getPos())); return BooleanValue.of(test.test(block.getBlockState(), block.getPos()));
} }
private static Value stateStringQuery( private static Value stateStringQuery(