semi-auto cleanup

This commit is contained in:
gnembon committed 2023-02-04 13:26:21 +01:00
1 parent cad80f82c6
commit 8f61271606
99 files changed
+11463 -8007

No files matched your search

@@ -5,11 +5,13 @@ import carpet.script.value.FunctionValue;
import com.google.common.collect.Lists;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import net.minecraft.commands.CommandSourceStack;
import static net.minecraft.commands.Commands.literal;
@@ -20,41 +22,46 @@ public class CommandToken implements Comparable<CommandToken>
public boolean isArgument;
public CommandArgument type;
private CommandToken(String surface, CommandArgument type )
private CommandToken(final String surface, final CommandArgument type)
{
this.surface = surface;
this.type = type;
isArgument = type != null;
}
public static CommandToken getToken(String source, CarpetScriptHost host)
public static CommandToken getToken(String source, final CarpetScriptHost host)
{
// todo add more type checking and return null
if (!source.startsWith("<"))
{
if (!source.matches("[_a-zA-Z]+")) return null;
return new CommandToken(source, null);
return source.matches("[_a-zA-Z]+") ? new CommandToken(source, null) : null;
}
source = source.substring(1, source.length()-1);
if (!source.matches("[_a-zA-Z]+")) return null;
CommandArgument arg = CommandArgument.getTypeForArgument(source, host);
return new CommandToken(source, arg);
source = source.substring(1, source.length() - 1);
return source.matches("[_a-zA-Z]+") ? new CommandToken(source, CommandArgument.getTypeForArgument(source, host)) : null;
}
public static List<CommandToken> parseSpec(String spec, CarpetScriptHost host) throws CommandSyntaxException
public static List<CommandToken> parseSpec(String spec, final CarpetScriptHost host) throws CommandSyntaxException
{
spec = spec.trim();
if (spec.isEmpty()) return Collections.emptyList();
List<CommandToken> elements = new ArrayList<>();
HashSet<String> seenArgs = new HashSet<>();
for (String el: spec.split("\\s+"))
if (spec.isEmpty())
{
CommandToken tok = CommandToken.getToken(el, host);
if (tok == null) throw CommandArgument.error("Unrecognized command token: "+ el);
return Collections.emptyList();
}
final List<CommandToken> elements = new ArrayList<>();
final HashSet<String> seenArgs = new HashSet<>();
for (final String el : spec.split("\\s+"))
{
final CommandToken tok = CommandToken.getToken(el, host);
if (tok == null)
{
throw CommandArgument.error("Unrecognized command token: " + el);
}
if (tok.isArgument)
{
if (seenArgs.contains(tok.surface))
throw CommandArgument.error("Repeated command argument: "+tok.surface+", for '"+spec+"'. Argument names have to be unique");
{
throw CommandArgument.error("Repeated command argument: " + tok.surface + ", for '" + spec + "'. Argument names have to be unique");
}
seenArgs.add(tok.surface);
}
elements.add(tok);
@@ -62,28 +69,33 @@ public class CommandToken implements Comparable<CommandToken>
return elements;
}
public static String specFromSignature(FunctionValue function)
public static String specFromSignature(final FunctionValue function)
{
List<String> tokens = Lists.newArrayList(function.getString());
for (String arg : function.getArguments()) tokens.add("<"+arg+">");
final List<String> tokens = Lists.newArrayList(function.getString());
for (final String arg : function.getArguments())
{
tokens.add("<" + arg + ">");
}
return String.join(" ", tokens);
}
public ArgumentBuilder<CommandSourceStack, ?> getCommandNode(CarpetScriptHost host) throws CommandSyntaxException
public ArgumentBuilder<CommandSourceStack, ?> getCommandNode(final CarpetScriptHost host) throws CommandSyntaxException
{
if (isArgument)
return CommandArgument.argumentNode(surface, host);
return literal(surface);
return isArgument ? CommandArgument.argumentNode(surface, host) : literal(surface);
}
@Override
public boolean equals(Object o)
public boolean equals(final Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommandToken that = (CommandToken) o;
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
final CommandToken that = (CommandToken) o;
return surface.equals(that.surface) &&
Objects.equals(type, that.type);
}
@@ -95,10 +107,16 @@ public class CommandToken implements Comparable<CommandToken>
}
@Override
public int compareTo(CommandToken o)
public int compareTo(final CommandToken o)
{
if (isArgument && !o.isArgument) return 1;
if (!isArgument && o.isArgument) return -1;
if (isArgument && !o.isArgument)
{
return 1;
}
if (!isArgument && o.isArgument)
{
return -1;
}
return surface.compareTo(o.surface);
}
}