Migrate the Carpet codebase to Mojang Mappings

This commit is contained in:
altrisi committed 2022-01-24 20:33:16 +01:00
1 parent a1a2488bc3
commit 06af9ce8b1
346 files changed
+8353 -8492

No files matched your search

@@ -39,16 +39,16 @@ import net.fabricmc.loader.api.SemanticVersion;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.VersionParsingException;
import net.fabricmc.loader.api.metadata.version.VersionPredicate;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.nbt.NbtElement;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.BaseText;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.BaseComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import org.apache.commons.lang3.tuple.Pair;
import java.math.BigInteger;
@@ -68,26 +68,26 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.Math.max;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
public class CarpetScriptHost extends ScriptHost
{
private final CarpetScriptServer scriptServer;
public ServerCommandSource responsibleSource;
public CommandSourceStack responsibleSource;
private NbtElement globalState;
private Tag globalState;
private int saveTimeout;
public boolean persistenceRequired;
public Map<Value, Value> appConfig;
public Map<String, CommandArgument> appArgTypes;
Predicate<ServerCommandSource> commandValidator;
Predicate<CommandSourceStack> commandValidator;
boolean isRuleApp;
public AppStoreManager.StoreNode storeSource;
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config, Map<String, CommandArgument> argTypes, Predicate<ServerCommandSource> commandValidator, boolean isRuleApp)
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config, Map<String, CommandArgument> argTypes, Predicate<CommandSourceStack> commandValidator, boolean isRuleApp)
{
super(code, perUser, parent);
this.saveTimeout = 0;
@@ -109,7 +109,7 @@ public class CarpetScriptHost extends ScriptHost
storeSource = null;
}
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source, Predicate<ServerCommandSource> commandValidator, boolean isRuleApp, AppStoreManager.StoreNode storeSource)
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, CommandSourceStack source, Predicate<CommandSourceStack> commandValidator, boolean isRuleApp, AppStoreManager.StoreNode storeSource)
{
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap(), new HashMap<>(), commandValidator, isRuleApp);
// parse code and convert to expression
@@ -150,13 +150,13 @@ public class CarpetScriptHost extends ScriptHost
return host;
}
private static int execute(CommandContext<ServerCommandSource> ctx, String hostName, FunctionArgument funcSpec, List<String> paramNames) throws CommandSyntaxException
private static int execute(CommandContext<CommandSourceStack> ctx, String hostName, FunctionArgument funcSpec, List<String> paramNames) throws CommandSyntaxException
{
CarpetProfiler.ProfilerToken currentSection = CarpetProfiler.start_section(null, "Scarpet command", CarpetProfiler.TYPE.GENERAL);
CarpetScriptHost cHost = CarpetServer.scriptServer.modules.get(hostName).retrieveOwnForExecution(ctx.getSource());
List<String> argNames = funcSpec.function.getArguments();
if ((argNames.size()-funcSpec.args.size()) != paramNames.size())
throw new SimpleCommandExceptionType(new LiteralText("Target function "+funcSpec.function.getPrettyString()+" as wrong number of arguments, required "+paramNames.size()+", found "+argNames.size()+" with "+funcSpec.args.size()+" provided")).create();
throw new SimpleCommandExceptionType(new TextComponent("Target function "+funcSpec.function.getPrettyString()+" as wrong number of arguments, required "+paramNames.size()+", found "+argNames.size()+" with "+funcSpec.args.size()+" provided")).create();
List<Value> args = new ArrayList<>(argNames.size());
for (String s : paramNames)
{
@@ -171,8 +171,8 @@ public class CarpetScriptHost extends ScriptHost
return intres;
}
public LiteralArgumentBuilder<ServerCommandSource> addPathToCommand(
LiteralArgumentBuilder<ServerCommandSource> command,
public LiteralArgumentBuilder<CommandSourceStack> addPathToCommand(
LiteralArgumentBuilder<CommandSourceStack> command,
List<CommandToken> path,
FunctionArgument functionSpec
) throws CommandSyntaxException
@@ -187,7 +187,7 @@ public class CarpetScriptHost extends ScriptHost
}
List<CommandToken> reversedPath = new ArrayList<>(path);
Collections.reverse(reversedPath);
ArgumentBuilder<ServerCommandSource, ?> argChain = reversedPath.get(0).getCommandNode(this).executes(c -> execute(c, hostName, functionSpec, commandArgs));
ArgumentBuilder<CommandSourceStack, ?> argChain = reversedPath.get(0).getCommandNode(this).executes(c -> execute(c, hostName, functionSpec, commandArgs));
for (int i = 1; i < reversedPath.size(); i++)
{
argChain = reversedPath.get(i).getCommandNode(this).then(argChain);
@@ -195,13 +195,13 @@ public class CarpetScriptHost extends ScriptHost
return command.then(argChain);
}
public LiteralArgumentBuilder<ServerCommandSource> getNewCommandTree(
List<Pair<List<CommandToken>,FunctionArgument>> entries, Predicate<ServerCommandSource> useValidator
public LiteralArgumentBuilder<CommandSourceStack> getNewCommandTree(
List<Pair<List<CommandToken>,FunctionArgument>> entries, Predicate<CommandSourceStack> useValidator
) throws CommandSyntaxException
{
String hostName = main.getName();
Predicate<ServerCommandSource> configValidator = getCommandConfigPermissions();
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
Predicate<CommandSourceStack> configValidator = getCommandConfigPermissions();
LiteralArgumentBuilder<CommandSourceStack> command = literal(hostName).
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.test(player) && configValidator.test(player));
for (Pair<List<CommandToken>,FunctionArgument> commandData : entries)
{
@@ -210,7 +210,7 @@ public class CarpetScriptHost extends ScriptHost
return command;
}
public Predicate<ServerCommandSource> getCommandConfigPermissions() throws CommandSyntaxException
public Predicate<CommandSourceStack> getCommandConfigPermissions() throws CommandSyntaxException
{
Value confValue = appConfig.get(StringValue.of("command_permission"));
if (confValue == null) return s -> true;
@@ -218,16 +218,16 @@ public class CarpetScriptHost extends ScriptHost
{
int level = ((NumericValue) confValue).getInt();
if (level < 1 || level > 4) throw CommandArgument.error("Numeric permission level for custom commands should be between 1 and 4");
return s -> s.hasPermissionLevel(level);
return s -> s.hasPermission(level);
}
if (!(confValue instanceof FunctionValue))
{
String perm = confValue.getString().toLowerCase(Locale.ROOT);
switch (perm)
{
case "ops": return s -> s.hasPermissionLevel(2);
case "server": return s -> !(s.getEntity() instanceof ServerPlayerEntity);
case "players": return s -> s.getEntity() instanceof ServerPlayerEntity;
case "ops": return s -> s.hasPermission(2);
case "server": return s -> !(s.getEntity() instanceof ServerPlayer);
case "players": return s -> s.getEntity() instanceof ServerPlayer;
case "all": return s -> true;
default: throw CommandArgument.error("Unknown command permission: "+perm);
}
@@ -242,7 +242,7 @@ public class CarpetScriptHost extends ScriptHost
CarpetScriptHost cHost = null;
cHost = CarpetServer.scriptServer.modules.get(hostName).retrieveOwnForExecution(s);
Value response = cHost.handleCommand(s, fun, Collections.singletonList(
(s.getEntity() instanceof ServerPlayerEntity)?new EntityValue(s.getEntity()):Value.NULL)
(s.getEntity() instanceof ServerPlayer)?new EntityValue(s.getEntity()):Value.NULL)
);
boolean res = response.getBoolean();
CarpetProfiler.end_current_section(currentSection);
@@ -385,7 +385,7 @@ public class CarpetScriptHost extends ScriptHost
}
}
public Boolean addAppCommands(Consumer<Text> notifier)
public Boolean addAppCommands(Consumer<Component> notifier)
{
try
{
@@ -400,10 +400,10 @@ public class CarpetScriptHost extends ScriptHost
{
try
{
LiteralArgumentBuilder<ServerCommandSource> command = readCommands(commandValidator);
LiteralArgumentBuilder<CommandSourceStack> command = readCommands(commandValidator);
if (command != null)
{
scriptServer.server.getCommandManager().getDispatcher().register(command);
scriptServer.server.getCommands().getDispatcher().register(command);
return true;
}
else {
@@ -453,7 +453,7 @@ public class CarpetScriptHost extends ScriptHost
}
}
private Boolean addLegacyCommand(Consumer<Text> notifier)
private Boolean addLegacyCommand(Consumer<Component> notifier)
{
if (main == null) return false;
if (getFunction("__command") == null) return false;
@@ -464,7 +464,7 @@ public class CarpetScriptHost extends ScriptHost
return null;
}
Predicate<ServerCommandSource> configValidator;
Predicate<CommandSourceStack> configValidator;
try
{
configValidator = getCommandConfigPermissions();
@@ -475,7 +475,7 @@ public class CarpetScriptHost extends ScriptHost
return null;
}
String hostName = getName();
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
LiteralArgumentBuilder<CommandSourceStack> command = literal(hostName).
requires((player) -> scriptServer.modules.containsKey(hostName) && commandValidator.test(player) && configValidator.test(player)).
executes( (c) ->
{
@@ -525,11 +525,11 @@ public class CarpetScriptHost extends ScriptHost
})));
}
}
scriptServer.server.getCommandManager().getDispatcher().register(command);
scriptServer.server.getCommands().getDispatcher().register(command);
return true;
}
public LiteralArgumentBuilder<ServerCommandSource> readCommands(Predicate<ServerCommandSource> useValidator) throws CommandSyntaxException
public LiteralArgumentBuilder<CommandSourceStack> readCommands(Predicate<CommandSourceStack> useValidator) throws CommandSyntaxException
{
Value commands = appConfig.get(StringValue.of("commands"));
@@ -599,7 +599,7 @@ public class CarpetScriptHost extends ScriptHost
}
}
public CarpetScriptHost retrieveForExecution(ServerCommandSource source, ServerPlayerEntity player)
public CarpetScriptHost retrieveForExecution(CommandSourceStack source, ServerPlayer player)
{
CarpetScriptHost target = null;
if (!perUser)
@@ -608,13 +608,13 @@ public class CarpetScriptHost extends ScriptHost
}
else if (player != null)
{
target = (CarpetScriptHost) retrieveForExecution(player.getEntityName());
target = (CarpetScriptHost) retrieveForExecution(player.getScoreboardName());
}
if (target != null && target.errorSnooper == null) target.setChatErrorSnooper(source);
return target;
}
public CarpetScriptHost retrieveOwnForExecution(ServerCommandSource source) throws CommandSyntaxException
public CarpetScriptHost retrieveOwnForExecution(CommandSourceStack source) throws CommandSyntaxException
{
if (!perUser)
{
@@ -622,21 +622,21 @@ public class CarpetScriptHost extends ScriptHost
return this;
}
// user based
ServerPlayerEntity player;
ServerPlayer player;
try
{
player = source.getPlayer();
player = source.getPlayerOrException();
}
catch (CommandSyntaxException ignored)
{
throw new SimpleCommandExceptionType(new LiteralText("Cannot run player based apps without the player context")).create();
throw new SimpleCommandExceptionType(new TextComponent("Cannot run player based apps without the player context")).create();
}
CarpetScriptHost userHost = (CarpetScriptHost)retrieveForExecution(player.getEntityName());
CarpetScriptHost userHost = (CarpetScriptHost)retrieveForExecution(player.getScoreboardName());
if (userHost.errorSnooper == null) userHost.setChatErrorSnooper(source);
return userHost;
}
public Value handleCommandLegacy(ServerCommandSource source, String call, List<Integer> coords, String arg)
public Value handleCommandLegacy(CommandSourceStack source, String call, List<Integer> coords, String arg)
{
try
{
@@ -660,7 +660,7 @@ public class CarpetScriptHost extends ScriptHost
return Value.NULL;
}
public Value handleCommand(ServerCommandSource source, FunctionValue function, List<Value> args)
public Value handleCommand(CommandSourceStack source, FunctionValue function, List<Value> args)
{
try
{
@@ -681,7 +681,7 @@ public class CarpetScriptHost extends ScriptHost
return Value.NULL;
}
public Value callLegacy(ServerCommandSource source, String call, List<Integer> coords, String arg)
public Value callLegacy(CommandSourceStack source, String call, List<Integer> coords, String arg)
{
if (CarpetServer.scriptServer.stopAll)
throw new CarpetExpressionException("SCARPET PAUSED (unpause with /script resume)", null);
@@ -768,7 +768,7 @@ public class CarpetScriptHost extends ScriptHost
{
// TODO: this is just for now - invoke would be able to invoke other hosts scripts
assertAppIntegrity(function.getModule());
Context context = new CarpetContext(this, source, BlockPos.ORIGIN);
Context context = new CarpetContext(this, source, BlockPos.ZERO);
return scriptServer.events.handleEvents.getWhileDisabled(() -> function.getExpression().evalValue(
() -> function.lazyEval(context, Context.VOID, function.getExpression(), function.getToken(), argv),
context,
@@ -781,7 +781,7 @@ public class CarpetScriptHost extends ScriptHost
}
}
public Value call(ServerCommandSource source, FunctionValue function, List<Value> argv)
public Value call(CommandSourceStack source, FunctionValue function, List<Value> argv)
{
if (CarpetServer.scriptServer.stopAll)
throw new CarpetExpressionException("SCARPET PAUSED (unpause with /script resume)", null);
@@ -799,7 +799,7 @@ public class CarpetScriptHost extends ScriptHost
try
{
assertAppIntegrity(function.getModule());
Context context = new CarpetContext(this, source, BlockPos.ORIGIN);
Context context = new CarpetContext(this, source, BlockPos.ZERO);
return function.getExpression().evalValue(
() -> function.execute(context, Context.VOID, function.getExpression(), function.getToken(), argv),
context,
@@ -812,7 +812,7 @@ public class CarpetScriptHost extends ScriptHost
}
}
public Value callUDF(BlockPos pos, ServerCommandSource source, FunctionValue fun, List<Value> argv) throws InvalidCallbackException, IntegrityException
public Value callUDF(BlockPos pos, CommandSourceStack source, FunctionValue fun, List<Value> argv) throws InvalidCallbackException, IntegrityException
{
if (CarpetServer.scriptServer.stopAll)
return Value.NULL;
@@ -843,12 +843,12 @@ public class CarpetScriptHost extends ScriptHost
public Value callNow(FunctionValue fun, List<Value> arguments)
{
ServerPlayerEntity player = (user==null)?null:scriptServer.server.getPlayerManager().getPlayer(user);
ServerCommandSource source = (player != null)?player.getCommandSource():scriptServer.server.getCommandSource();
ServerPlayer player = (user==null)?null:scriptServer.server.getPlayerList().getPlayerByName(user);
CommandSourceStack source = (player != null)?player.createCommandSourceStack():scriptServer.server.createCommandSourceStack();
return scriptServer.events.handleEvents.getWhileDisabled(()->{
try
{
return callUDF(BlockPos.ORIGIN, source, fun, arguments);
return callUDF(BlockPos.ZERO, source, fun, arguments);
}
catch (InvalidCallbackException ignored)
{
@@ -873,9 +873,9 @@ public class CarpetScriptHost extends ScriptHost
{
String markerName = Auxiliary.MARKER_STRING + "_" + ((getName() == null) ? "" : getName());
for (ServerWorld world : scriptServer.server.getWorlds())
for (ServerLevel world : scriptServer.server.getAllLevels())
{
for (Entity e : world.getEntitiesByType(EntityType.ARMOR_STAND, (as) -> as.getScoreboardTags().contains(markerName)))
for (Entity e : world.getEntities(EntityType.ARMOR_STAND, (as) -> as.getTags().contains(markerName)))
{
e.discard();
}
@@ -890,12 +890,12 @@ public class CarpetScriptHost extends ScriptHost
Module.saveData(main, globalState);
}
private NbtElement loadState()
private Tag loadState()
{
return Module.getData(main);
}
public NbtElement readFileTag(FileArgument fdesc)
public Tag readFileTag(FileArgument fdesc)
{
if (getName() == null && !fdesc.isShared) return null;
if (fdesc.resource != null)
@@ -905,7 +905,7 @@ public class CarpetScriptHost extends ScriptHost
return ((CarpetScriptHost)parent).globalState;
}
public boolean writeTagFile(NbtElement tag, FileArgument fdesc)
public boolean writeTagFile(Tag tag, FileArgument fdesc)
{
if (getName() == null && !fdesc.isShared) return false; // if belongs to an app, cannot be default host.
@@ -973,14 +973,14 @@ public class CarpetScriptHost extends ScriptHost
}
}
public void setChatErrorSnooper(ServerCommandSource source)
public void setChatErrorSnooper(CommandSourceStack source)
{
responsibleSource = source;
errorSnooper = (expr, /*Nullable*/ token, ctx, message) ->
{
try
{
source.getPlayer();
source.getPlayerOrException();
}
catch (CommandSyntaxException e)
{
@@ -1021,7 +1021,7 @@ public class CarpetScriptHost extends ScriptHost
}
/**
* <p>Creates a {@link BaseText} using {@link Messenger} that has the locals in the {@code line} snippet with a hover over
* <p>Creates a {@link BaseComponent} using {@link Messenger} that has the locals in the {@code line} snippet with a hover over
* tooltip with the value of the local at that location</p>
* @param line The line to find references to locals on
* @param context The {@link Context} to extract the locals from
@@ -1031,7 +1031,7 @@ public class CarpetScriptHost extends ScriptHost
* @implNote The implementation of this method is far from perfect, and won't detect actual references to variables, but try to find the strings
* and add the hover effect to anything that equals to any variable name, so short variable names may appear on random positions
*/
private static BaseText withLocals(String format, String line, Context context)
private static BaseComponent withLocals(String format, String line, Context context)
{
format += " ";
List<String> stringsToFormat = new ArrayList<>();