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

singleton command arguments #4334

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
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
import static net.minecraft.nbt.StringNbtReader.EXPECTED_VALUE;

public class CompoundNbtTagArgumentType implements ArgumentType<NbtCompound> {
private static final CompoundNbtTagArgumentType INSTANCE = new CompoundNbtTagArgumentType();
Copy link

@JAXPLE JAXPLE Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static class CompoundNbtTagArgumentTypeHolder {
    private static final CompoundNbtTagArgumentType INSTANCE = new CompoundNbtTagArgumentType();
}

private static final Collection<String> EXAMPLES = Arrays.asList("{foo:bar}", "{foo:[aa, bb],bar:15}");

public static CompoundNbtTagArgumentType create() {
return new CompoundNbtTagArgumentType();
return INSTANCE;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public static CompoundNbtTagArgumentType create() {
    return CompoundNbtTagArgumentTypeHolder.INSTANCE;
}

}

public static NbtCompound get(CommandContext<?> context) {
return context.getArgument("nbt", NbtCompound.class);
}

private CompoundNbtTagArgumentType() {}

@Override
public NbtCompound parse(StringReader reader) throws CommandSyntaxException {
reader.skipWhitespace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import net.minecraft.util.math.Direction;

public class DirectionArgumentType extends EnumArgumentType<Direction> {
private static final DirectionArgumentType INSTANCE = new DirectionArgumentType();

private DirectionArgumentType() {
super(Direction.CODEC, Direction::values);
}

public static DirectionArgumentType create() {
return new DirectionArgumentType();
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
import static net.minecraft.command.CommandSource.suggestMatching;

public class FakePlayerArgumentType implements ArgumentType<String> {
private static final FakePlayerArgumentType INSTANCE = new FakePlayerArgumentType();
private static final Collection<String> EXAMPLES = List.of("seasnail8169", "MineGame159");

public static FakePlayerArgumentType create() {
return new FakePlayerArgumentType();
return INSTANCE;
}

public static FakePlayerEntity get(CommandContext<?> context) {
return FakePlayerManager.get(context.getArgument("fp", String.class));
}

private FakePlayerArgumentType() {}

@Override
public String parse(StringReader reader) throws CommandSyntaxException {
return reader.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
import static net.minecraft.command.CommandSource.suggestMatching;

public class FriendArgumentType implements ArgumentType<String> {
private static final FriendArgumentType INSTANCE = new FriendArgumentType();
private static final Collection<String> EXAMPLES = List.of("seasnail8169", "MineGame159");

public static FriendArgumentType create() {
return new FriendArgumentType();
return INSTANCE;
}

public static Friend get(CommandContext<?> context) {
return Friends.get().get(context.getArgument("friend", String.class));
}

private FriendArgumentType() {}

@Override
public String parse(StringReader reader) throws CommandSyntaxException {
return reader.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
import java.util.stream.Collectors;

public class MacroArgumentType implements ArgumentType<Macro> {
private static final MacroArgumentType INSTANCE = new MacroArgumentType();
private static final DynamicCommandExceptionType NO_SUCH_MACRO = new DynamicCommandExceptionType(name -> Text.literal("Macro with name " + name + " doesn't exist."));

public static MacroArgumentType create() {
return new MacroArgumentType();
return INSTANCE;
}

public static Macro get(CommandContext<?> context) {
return context.getArgument("macro", Macro.class);
}

private MacroArgumentType() {}

@Override
public Macro parse(StringReader reader) throws CommandSyntaxException {
String argument = reader.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.stream.Collectors;

public class ModuleArgumentType implements ArgumentType<Module> {
private static final ModuleArgumentType INSTANCE = new ModuleArgumentType();
private static final DynamicCommandExceptionType NO_SUCH_MODULE = new DynamicCommandExceptionType(name -> Text.literal("Module with name " + name + " doesn't exist."));

private static final Collection<String> EXAMPLES = Modules.get().getAll()
Expand All @@ -31,13 +32,15 @@ public class ModuleArgumentType implements ArgumentType<Module> {
.collect(Collectors.toList());

public static ModuleArgumentType create() {
return new ModuleArgumentType();
return INSTANCE;
}

public static Module get(CommandContext<?> context) {
return context.getArgument("module", Module.class);
}

private ModuleArgumentType() {}

@Override
public Module parse(StringReader reader) throws CommandSyntaxException {
String argument = reader.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import java.util.concurrent.CompletableFuture;

public class NotebotSongArgumentType implements ArgumentType<Path> {
private static final NotebotSongArgumentType INSTANCE = new NotebotSongArgumentType();

public static NotebotSongArgumentType create() {
return new NotebotSongArgumentType();
return INSTANCE;
}

private NotebotSongArgumentType() {}

@Override
public Path parse(StringReader reader) throws CommandSyntaxException {
final String text = reader.getRemaining();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@
import static meteordevelopment.meteorclient.MeteorClient.mc;

public class PlayerArgumentType implements ArgumentType<PlayerEntity> {
private static final PlayerArgumentType INSTANCE = new PlayerArgumentType();
private static final DynamicCommandExceptionType NO_SUCH_PLAYER = new DynamicCommandExceptionType(name -> Text.literal("Player with name " + name + " doesn't exist."));

private static final Collection<String> EXAMPLES = List.of("seasnail8169", "MineGame159");

public static PlayerArgumentType create() {
return new PlayerArgumentType();
return INSTANCE;
}

public static PlayerEntity get(CommandContext<?> context) {
return context.getArgument("player", PlayerEntity.class);
}

private PlayerArgumentType() {}

@Override
public PlayerEntity parse(StringReader reader) throws CommandSyntaxException {
String argument = reader.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@
import static meteordevelopment.meteorclient.MeteorClient.mc;

public class PlayerListEntryArgumentType implements ArgumentType<PlayerListEntry> {
private static final PlayerListEntryArgumentType INSTANCE = new PlayerListEntryArgumentType();
private static final DynamicCommandExceptionType NO_SUCH_PLAYER = new DynamicCommandExceptionType(name -> Text.literal("Player list entry with name " + name + " doesn't exist."));

private static final Collection<String> EXAMPLES = List.of("seasnail8169", "MineGame159");

public static PlayerListEntryArgumentType create() {
return new PlayerListEntryArgumentType();
return INSTANCE;
}

public static PlayerListEntry get(CommandContext<?> context) {
return context.getArgument("player", PlayerListEntry.class);
}

private PlayerListEntryArgumentType() {}

@Override
public PlayerListEntry parse(StringReader reader) throws CommandSyntaxException {
String argument = reader.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@
import static net.minecraft.command.CommandSource.suggestMatching;

public class ProfileArgumentType implements ArgumentType<String> {
private static final ProfileArgumentType INSTANCE = new ProfileArgumentType();
private static final DynamicCommandExceptionType NO_SUCH_PROFILE = new DynamicCommandExceptionType(name -> Text.literal("Profile with name " + name + " doesn't exist."));

private static final Collection<String> EXAMPLES = List.of("pvp.meteorclient.com", "anarchy");

public static ProfileArgumentType create() {
return new ProfileArgumentType();
return INSTANCE;
}

public static Profile get(CommandContext<?> context) {
return Profiles.get().get(context.getArgument("profile", String.class));
}

private ProfileArgumentType() {}

@Override
public String parse(StringReader reader) throws CommandSyntaxException {
String argument = reader.getRemaining();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import java.util.stream.Stream;

public class SettingArgumentType implements ArgumentType<String> {
private static final SettingArgumentType INSTANCE = new SettingArgumentType();
private static final DynamicCommandExceptionType NO_SUCH_SETTING = new DynamicCommandExceptionType(name -> Text.literal("No such setting '" + name + "'."));

public static SettingArgumentType create() {
return new SettingArgumentType();
return INSTANCE;
}

public static Setting<?> get(CommandContext<?> context) throws CommandSyntaxException {
Expand All @@ -38,6 +39,8 @@ public static Setting<?> get(CommandContext<?> context) throws CommandSyntaxExce
return setting;
}

private SettingArgumentType() {}

@Override
public String parse(StringReader reader) throws CommandSyntaxException {
return reader.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
import java.util.concurrent.CompletableFuture;

public class SettingValueArgumentType implements ArgumentType<String> {
private static final SettingValueArgumentType INSTANCE = new SettingValueArgumentType();

public static SettingValueArgumentType create() {
return new SettingValueArgumentType();
return INSTANCE;
}

public static String get(CommandContext<?> context) {
return context.getArgument("value", String.class);
}

private SettingValueArgumentType() {}

@Override
public String parse(StringReader reader) throws CommandSyntaxException {
String text = reader.getRemaining();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.concurrent.CompletableFuture;

public class WaypointArgumentType implements ArgumentType<String> {
private static final WaypointArgumentType GREEDY = new WaypointArgumentType(true);
private static final WaypointArgumentType QUOTED = new WaypointArgumentType(false);
private static final DynamicCommandExceptionType NO_SUCH_WAYPOINT = new DynamicCommandExceptionType(name -> Text.literal("Waypoint with name '" + name + "' doesn't exist."));
private final boolean greedyString;

Expand All @@ -31,11 +33,11 @@ private WaypointArgumentType(boolean greedyString) {
}

public static WaypointArgumentType create() {
return new WaypointArgumentType(true);
return GREEDY;
}

public static WaypointArgumentType create(boolean greedy) {
return new WaypointArgumentType(greedy);
return greedy ? GREEDY : QUOTED;
}

public static Waypoint get(CommandContext<?> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
ItemStack stack = mc.player.getInventory().getMainHandStack();

if (validBasic(stack)) {
stack.setNbt(new CompoundNbtTagArgumentType().parse(new StringReader(mc.keyboard.getClipboard())));
stack.setNbt(CompoundNbtTagArgumentType.create().parse(new StringReader(mc.keyboard.getClipboard())));
setStack(stack);
}

Expand Down