From 47b3679a1c490c585c4470341c736cc1eb4f6ae0 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Wed, 10 Jul 2024 23:38:08 +0300 Subject: [PATCH 1/9] Add client block pos and egg type argument types and update usages --- .../skyblock/chocolatefactory/EggFinder.java | 41 ++++--- .../dungeon/secrets/DungeonManager.java | 21 ++-- .../dwarven/CrystalsLocationsManager.java | 14 +-- .../skyblock/waypoint/OrderedWaypoints.java | 29 +++-- .../ClientBlockPosArgumentType.java | 87 ++++++++++++++ .../argumenttypes/ClientPosArgument.java | 27 +++++ .../DefaultClientPosArgument.java | 113 ++++++++++++++++++ .../argumenttypes/EggTypeArgumentType.java | 36 ++++++ .../LookingClientPosArgument.java | 106 ++++++++++++++++ 9 files changed, 418 insertions(+), 56 deletions(-) create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientPosArgument.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/DefaultClientPosArgument.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/LookingClientPosArgument.java diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java index 620da37c3e..38694ec45b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java @@ -1,16 +1,16 @@ package de.hysky.skyblocker.skyblock.chocolatefactory; import com.mojang.brigadier.Command; -import com.mojang.brigadier.arguments.IntegerArgumentType; -import com.mojang.brigadier.arguments.StringArgumentType; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.events.SkyblockEvents; import de.hysky.skyblocker.utils.*; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; +import de.hysky.skyblocker.utils.command.argumenttypes.EggTypeArgumentType; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import de.hysky.skyblocker.utils.waypoint.Waypoint; import it.unimi.dsi.fastutil.objects.ObjectImmutableList; -import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; @@ -24,6 +24,7 @@ import net.minecraft.text.HoverEvent; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import org.apache.commons.lang3.text.WordUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +32,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + public class EggFinder { private static final Pattern eggFoundPattern = Pattern.compile("^(?:HOPPITY'S HUNT You found a Chocolate|You have already collected this Chocolate) (Breakfast|Lunch|Dinner)"); private static final Pattern newEggPattern = Pattern.compile("^HOPPITY'S HUNT A Chocolate (Breakfast|Lunch|Dinner) Egg has appeared!$"); @@ -47,17 +51,15 @@ public static void init() { SkyblockEvents.LOCATION_CHANGE.register(EggFinder::handleLocationChange); ClientReceiveMessageEvents.GAME.register(EggFinder::onChatMessage); WorldRenderEvents.AFTER_TRANSLUCENT.register(EggFinder::renderWaypoints); - ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal(SkyblockerMod.NAMESPACE) - .then(ClientCommandManager.literal("eggFinder") - .then(ClientCommandManager.literal("shareLocation") - .then(ClientCommandManager.argument("x", IntegerArgumentType.integer()) - .then(ClientCommandManager.argument("y", IntegerArgumentType.integer()) - .then(ClientCommandManager.argument("z", IntegerArgumentType.integer()) - .then(ClientCommandManager.argument("eggType", StringArgumentType.word()) - .executes(context -> { - MessageScheduler.INSTANCE.sendMessageAfterCooldown("[Skyblocker] Chocolate " + context.getArgument("eggType", String.class) + " Egg found at " + context.getArgument("x", Integer.class) + " " + context.getArgument("y", Integer.class) + " " + context.getArgument("z", Integer.class) + "!"); - return Command.SINGLE_SUCCESS; - }))))))))); + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(literal(SkyblockerMod.NAMESPACE) + .then(literal("eggFinder") + .then(literal("shareLocation") + .then(argument("blockPos", ClientBlockPosArgumentType.blockPos()) + .then(argument("eggType", new EggTypeArgumentType()) + .executes(context -> { + MessageScheduler.INSTANCE.sendMessageAfterCooldown("[Skyblocker] Chocolate " + context.getArgument("eggType", EggType.class) + " Egg found at " + context.getArgument("blockPos", ClientPosArgument.class).toAbsoluteBlockPos(context.getSource()) .toShortString() + "!"); + return Command.SINGLE_SUCCESS; + }))))))); } private static void handleLocationChange(Location location) { @@ -161,7 +163,7 @@ private static void onChatMessage(Text text, boolean overlay) { record Egg(ArmorStandEntity entity, Waypoint waypoint) {} @SuppressWarnings("DataFlowIssue") //Removes that pesky "unboxing of Integer might cause NPE" warning when we already know it's not null - enum EggType { + public enum EggType { LUNCH(Formatting.BLUE.getColorValue(), "ewogICJ0aW1lc3RhbXAiIDogMTcxMTQ2MjU2ODExMiwKICAicHJvZmlsZUlkIiA6ICI3NzUwYzFhNTM5M2Q0ZWQ0Yjc2NmQ4ZGUwOWY4MjU0NiIsCiAgInByb2ZpbGVOYW1lIiA6ICJSZWVkcmVsIiwKICAic2lnbmF0dXJlUmVxdWlyZWQiIDogdHJ1ZSwKICAidGV4dHVyZXMiIDogewogICAgIlNLSU4iIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlLzdhZTZkMmQzMWQ4MTY3YmNhZjk1MjkzYjY4YTRhY2Q4NzJkNjZlNzUxZGI1YTM0ZjJjYmM2NzY2YTAzNTZkMGEiCiAgICB9CiAgfQp9"), DINNER(Formatting.GREEN.getColorValue(), "ewogICJ0aW1lc3RhbXAiIDogMTcxMTQ2MjY0OTcwMSwKICAicHJvZmlsZUlkIiA6ICI3NGEwMzQxNWY1OTI0ZTA4YjMyMGM2MmU1NGE3ZjJhYiIsCiAgInByb2ZpbGVOYW1lIiA6ICJNZXp6aXIiLAogICJzaWduYXR1cmVSZXF1aXJlZCIgOiB0cnVlLAogICJ0ZXh0dXJlcyIgOiB7CiAgICAiU0tJTiIgOiB7CiAgICAgICJ1cmwiIDogImh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZTVlMzYxNjU4MTlmZDI4NTBmOTg1NTJlZGNkNzYzZmY5ODYzMTMxMTkyODNjMTI2YWNlMGM0Y2M0OTVlNzZhOCIKICAgIH0KICB9Cn0"), BREAKFAST(Formatting.GOLD.getColorValue(), "ewogICJ0aW1lc3RhbXAiIDogMTcxMTQ2MjY3MzE0OSwKICAicHJvZmlsZUlkIiA6ICJiN2I4ZTlhZjEwZGE0NjFmOTY2YTQxM2RmOWJiM2U4OCIsCiAgInByb2ZpbGVOYW1lIiA6ICJBbmFiYW5hbmFZZzciLAogICJzaWduYXR1cmVSZXF1aXJlZCIgOiB0cnVlLAogICJ0ZXh0dXJlcyIgOiB7CiAgICAiU0tJTiIgOiB7CiAgICAgICJ1cmwiIDogImh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYTQ5MzMzZDg1YjhhMzE1ZDAzMzZlYjJkZjM3ZDhhNzE0Y2EyNGM1MWI4YzYwNzRmMWI1YjkyN2RlYjUxNmMyNCIKICAgIH0KICB9Cn0"); @@ -179,7 +181,7 @@ enum EggType { private long messageLastSent = 0; //This is to not create an array each time we iterate over the values - public static final ObjectImmutableList entries = ObjectImmutableList.of(BREAKFAST, LUNCH, DINNER); + public static final ObjectImmutableList entries = ObjectImmutableList.of(EggType.values()); EggType(int color, String texture) { this.color = color; @@ -187,12 +189,9 @@ enum EggType { } @Override + @SuppressWarnings("deprecation") // It's either a new dependency or a deprecated method, and I'd rather use the deprecated method public String toString() { - return switch (this) { - case LUNCH -> "Lunch"; - case DINNER -> "Dinner"; - case BREAKFAST -> "Breakfast"; - }; + return WordUtils.capitalizeFully(this.name()); } } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java index b998673133..e6faff7039 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java @@ -12,14 +12,16 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.serialization.JsonOps; import de.hysky.skyblocker.SkyblockerMod; -import de.hysky.skyblocker.config.configs.DungeonsConfig; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.config.configs.DungeonsConfig; import de.hysky.skyblocker.debug.Debug; import de.hysky.skyblocker.skyblock.dungeon.DungeonBoss; import de.hysky.skyblocker.skyblock.dungeon.DungeonMap; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Tickable; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; import de.hysky.skyblocker.utils.scheduler.Scheduler; import it.unimi.dsi.fastutil.objects.Object2ByteMap; import it.unimi.dsi.fastutil.objects.Object2ByteMaps; @@ -37,8 +39,6 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.CommandSource; -import net.minecraft.command.argument.BlockPosArgumentType; -import net.minecraft.command.argument.PosArgument; import net.minecraft.command.argument.TextArgumentType; import net.minecraft.component.DataComponentTypes; import net.minecraft.entity.Entity; @@ -53,7 +53,6 @@ import net.minecraft.item.map.MapState; import net.minecraft.registry.Registry; import net.minecraft.resource.Resource; -import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Identifier; @@ -387,13 +386,12 @@ private static int getRelativePos(FabricClientCommandSource source, BlockPos pos return Command.SINGLE_SUCCESS; } - private static RequiredArgumentBuilder addCustomWaypointCommand(boolean relative, CommandRegistryAccess registryAccess) { - return argument("pos", BlockPosArgumentType.blockPos()) + private static RequiredArgumentBuilder addCustomWaypointCommand(boolean relative, CommandRegistryAccess registryAccess) { + return argument("pos", ClientBlockPosArgumentType.blockPos()) .then(argument("secretIndex", IntegerArgumentType.integer()) .then(argument("category", SecretWaypoint.Category.CategoryArgumentType.category()) .then(argument("name", TextArgumentType.text(registryAccess)).executes(context -> { - // TODO Less hacky way with custom ClientBlockPosArgumentType - BlockPos pos = context.getArgument("pos", PosArgument.class).toAbsoluteBlockPos(new ServerCommandSource(null, context.getSource().getPosition(), context.getSource().getRotation(), null, 0, null, null, null, null)); + BlockPos pos = context.getArgument("pos", ClientPosArgument.class).toAbsoluteBlockPos(context.getSource()); return relative ? addCustomWaypointRelative(context, pos) : addCustomWaypoint(context, pos); })) ) @@ -419,11 +417,10 @@ private static int addCustomWaypointRelative(CommandContext removeCustomWaypointCommand(boolean relative) { - return argument("pos", BlockPosArgumentType.blockPos()) + private static RequiredArgumentBuilder removeCustomWaypointCommand(boolean relative) { + return argument("pos", ClientBlockPosArgumentType.blockPos()) .executes(context -> { - // TODO Less hacky way with custom ClientBlockPosArgumentType - BlockPos pos = context.getArgument("pos", PosArgument.class).toAbsoluteBlockPos(new ServerCommandSource(null, context.getSource().getPosition(), context.getSource().getRotation(), null, 0, null, null, null, null)); + BlockPos pos = context.getArgument("pos", ClientPosArgument.class).toAbsoluteBlockPos(context.getSource()); return relative ? removeCustomWaypointRelative(context, pos) : removeCustomWaypoint(context, pos); }); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java b/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java index d709181f91..9f5afcc3ee 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java @@ -8,6 +8,8 @@ import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import de.hysky.skyblocker.utils.scheduler.Scheduler; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; @@ -18,9 +20,6 @@ import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; import net.minecraft.client.MinecraftClient; import net.minecraft.command.CommandRegistryAccess; -import net.minecraft.command.argument.BlockPosArgumentType; -import net.minecraft.command.argument.PosArgument; -import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.ClickEvent; import net.minecraft.text.MutableText; import net.minecraft.text.Text; @@ -123,10 +122,10 @@ protected static Boolean checkInCrystals(BlockPos pos) { private static void registerWaypointLocationCommands(CommandDispatcher dispatcher, CommandRegistryAccess registryAccess) { dispatcher.register(literal(SkyblockerMod.NAMESPACE) .then(literal("crystalWaypoints") - .then(argument("pos", BlockPosArgumentType.blockPos()) + .then(argument("pos", ClientBlockPosArgumentType.blockPos()) .then(argument("place", StringArgumentType.greedyString()) .suggests((context, builder) -> suggestMatching(WAYPOINT_LOCATIONS.keySet(), builder)) - .executes(context -> addWaypointFromCommand(context.getSource(), getString(context, "place"), context.getArgument("pos", PosArgument.class))) + .executes(context -> addWaypointFromCommand(context.getSource(), getString(context, "place"), context.getArgument("pos", ClientPosArgument.class))) ) ) .then(literal("share") @@ -160,9 +159,8 @@ private static Text getLocationInputText(String location) { return text; } - public static int addWaypointFromCommand(FabricClientCommandSource source, String place, PosArgument location) { - // TODO Less hacky way with custom ClientBlockPosArgumentType - BlockPos blockPos = location.toAbsoluteBlockPos(new ServerCommandSource(null, source.getPosition(), source.getRotation(), null, 0, null, null, null, null)); + public static int addWaypointFromCommand(FabricClientCommandSource source, String place, ClientPosArgument location) { + BlockPos blockPos = location.toAbsoluteBlockPos(source); if (WAYPOINT_LOCATIONS.containsKey(place)) { addCustomWaypoint(place, blockPos); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java index 11ec1b8d07..10fe0b6bcc 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java @@ -15,6 +15,8 @@ import de.hysky.skyblocker.skyblock.item.CustomArmorDyeColors; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; import de.hysky.skyblocker.utils.render.RenderHelper; import de.hysky.skyblocker.utils.waypoint.Waypoint; import it.unimi.dsi.fastutil.floats.FloatArrayList; @@ -30,9 +32,6 @@ import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.CommandSource; -import net.minecraft.command.argument.BlockPosArgumentType; -import net.minecraft.command.argument.PosArgument; -import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; @@ -87,24 +86,24 @@ private static void registerCommands(CommandDispatcher CommandSource.suggestMatching(WAYPOINTS.keySet(), builder)) - .then(argument("pos", BlockPosArgumentType.blockPos()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", PosArgument.class), Integer.MIN_VALUE, null)) + .then(argument("pos", ClientBlockPosArgumentType.blockPos()) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), Integer.MIN_VALUE, null)) .then(argument("hex", word()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", PosArgument.class), Integer.MIN_VALUE, getString(context, "hex"))))))) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), Integer.MIN_VALUE, getString(context, "hex"))))))) .then(literal("addAt") .then(argument("groupName", word()) .suggests((source, builder) -> CommandSource.suggestMatching(WAYPOINTS.keySet(), builder)) .then(argument("index", IntegerArgumentType.integer(0)) - .then(argument("pos", BlockPosArgumentType.blockPos()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", PosArgument.class), IntegerArgumentType.getInteger(context, "index"), null)) + .then(argument("pos", ClientBlockPosArgumentType.blockPos()) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), IntegerArgumentType.getInteger(context, "index"), null)) .then(argument("hex", word()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", PosArgument.class), IntegerArgumentType.getInteger(context, "index"), getString(context, "hex")))))))) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), IntegerArgumentType.getInteger(context, "index"), getString(context, "hex")))))))) .then(literal("remove") .then(argument("groupName", word()) .suggests((source, builder) -> CommandSource.suggestMatching(WAYPOINTS.keySet(), builder)) .executes(context -> removeWaypointGroup(context.getSource(), getString(context, "groupName"))) - .then(argument("pos", BlockPosArgumentType.blockPos()) - .executes(context -> removeWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", PosArgument.class), Integer.MIN_VALUE))))) + .then(argument("pos", ClientBlockPosArgumentType.blockPos()) + .executes(context -> removeWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), Integer.MIN_VALUE))))) .then(literal("removeAt") .then(argument("groupName", word()) .suggests((source, builder) -> CommandSource.suggestMatching(WAYPOINTS.keySet(), builder)) @@ -126,8 +125,8 @@ private static void registerCommands(CommandDispatcher export(context.getSource())))))); } - private static int addWaypoint(FabricClientCommandSource source, String groupName, PosArgument posArgument, int index, String hex) { - BlockPos pos = posArgument.toAbsoluteBlockPos(new ServerCommandSource(null, source.getPosition(), source.getRotation(), null, 0, null, null, null, null)); + private static int addWaypoint(FabricClientCommandSource source, String groupName, ClientPosArgument posArgument, int index, String hex) { + BlockPos pos = posArgument.toAbsoluteBlockPos(source); SEMAPHORE.acquireUninterruptibly(); @@ -175,13 +174,13 @@ private static int removeWaypointGroup(FabricClientCommandSource source, String return Command.SINGLE_SUCCESS; } - private static int removeWaypoint(FabricClientCommandSource source, String groupName, PosArgument posArgument, int index) { + private static int removeWaypoint(FabricClientCommandSource source, String groupName, ClientPosArgument posArgument, int index) { if (WAYPOINTS.containsKey(groupName)) { SEMAPHORE.acquireUninterruptibly(); OrderedWaypointGroup group = WAYPOINTS.get(groupName); if (posArgument != null) { - BlockPos pos = posArgument.toAbsoluteBlockPos(new ServerCommandSource(null, source.getPosition(), source.getRotation(), null, 0, null, null, null, null)); + BlockPos pos = posArgument.toAbsoluteBlockPos(source); group.waypoints().removeIf(waypoint -> waypoint.getPos().equals(pos)); INDEX_STORE.removeInt(group.name()); diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java new file mode 100644 index 0000000000..484606ecbc --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java @@ -0,0 +1,87 @@ +package de.hysky.skyblocker.utils.command.argumenttypes; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.command.CommandSource; +import net.minecraft.server.command.CommandManager; +import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.concurrent.CompletableFuture; + +public class ClientBlockPosArgumentType implements ArgumentType { + private static final Collection EXAMPLES = Arrays.asList("0 0 0", "~ ~ ~", "^ ^ ^", "^1 ^ ^-5", "~0.5 ~1 ~-5"); + public static final SimpleCommandExceptionType UNLOADED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.pos.unloaded")); + public static final SimpleCommandExceptionType OUT_OF_WORLD_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.pos.outofworld")); + public static final SimpleCommandExceptionType OUT_OF_BOUNDS_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.pos.outofbounds")); + + public static ClientBlockPosArgumentType blockPos() { + return new ClientBlockPosArgumentType(); + } + + public static BlockPos getLoadedBlockPos(CommandContext context, String name) throws CommandSyntaxException { + ClientWorld clientWorld = context.getSource().getWorld(); + return getLoadedBlockPos(context, clientWorld, name); + } + + public static BlockPos getLoadedBlockPos(CommandContext context, ClientWorld world, String name) throws CommandSyntaxException { + BlockPos blockPos = getBlockPos(context, name); + if (!world.isChunkLoaded(blockPos)) { + throw UNLOADED_EXCEPTION.create(); + } else if (!world.isInBuildLimit(blockPos)) { + throw OUT_OF_WORLD_EXCEPTION.create(); + } else { + return blockPos; + } + } + + public static BlockPos getBlockPos(CommandContext context, String name) { + return context.getArgument(name, ClientPosArgument.class).toAbsoluteBlockPos(context.getSource()); + } + + public static BlockPos getValidBlockPos(CommandContext context, String name) throws CommandSyntaxException { + BlockPos blockPos = getBlockPos(context, name); + if (!World.isValid(blockPos)) { + throw OUT_OF_BOUNDS_EXCEPTION.create(); + } else { + return blockPos; + } + } + + public ClientPosArgument parse(StringReader stringReader) throws CommandSyntaxException { + return stringReader.canRead() && stringReader.peek() == '^' ? LookingClientPosArgument.parse(stringReader) : DefaultClientPosArgument.parse(stringReader); + } + + @Override + public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { + if (!(context.getSource() instanceof CommandSource)) { + return Suggestions.empty(); + } else { + String string = builder.getRemaining(); + Collection collection; + if (!string.isEmpty() && string.charAt(0) == '^') { + collection = Collections.singleton(CommandSource.RelativePosition.ZERO_LOCAL); + } else { + collection = ((CommandSource)context.getSource()).getBlockPositionSuggestions(); + } + + return CommandSource.suggestPositions(string, collection, builder, CommandManager.getCommandValidator(this::parse)); + } + } + + @Override + public Collection getExamples() { + return EXAMPLES; + } +} diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientPosArgument.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientPosArgument.java new file mode 100644 index 0000000000..ad0da38b3b --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientPosArgument.java @@ -0,0 +1,27 @@ +package de.hysky.skyblocker.utils.command.argumenttypes; + +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec2f; +import net.minecraft.util.math.Vec3d; + +/** + * This interface, its 2 implementations and ClientBlockPosArgumentType are all copied from minecraft + * and converted to use FabricClientCommandSource instead of ServerCommandSource. + * This removes the need for hacky workarounds such as creating new ServerCommandSources with null or 0 on every argument. + */ +public interface ClientPosArgument { + Vec3d toAbsolutePos(FabricClientCommandSource source); + + Vec2f toAbsoluteRotation(FabricClientCommandSource source); + + default BlockPos toAbsoluteBlockPos(FabricClientCommandSource source) { + return BlockPos.ofFloored(this.toAbsolutePos(source)); + } + + boolean isXRelative(); + + boolean isYRelative(); + + boolean isZRelative(); +} diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/DefaultClientPosArgument.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/DefaultClientPosArgument.java new file mode 100644 index 0000000000..99507f64b2 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/DefaultClientPosArgument.java @@ -0,0 +1,113 @@ +package de.hysky.skyblocker.utils.command.argumenttypes; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.command.argument.CoordinateArgument; +import net.minecraft.command.argument.Vec3ArgumentType; +import net.minecraft.util.math.Vec2f; +import net.minecraft.util.math.Vec3d; + +public class DefaultClientPosArgument implements ClientPosArgument { + private final CoordinateArgument x; + private final CoordinateArgument y; + private final CoordinateArgument z; + + public DefaultClientPosArgument(CoordinateArgument x, CoordinateArgument y, CoordinateArgument z) { + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public Vec3d toAbsolutePos(FabricClientCommandSource source) { + Vec3d vec3d = source.getPosition(); + return new Vec3d(this.x.toAbsoluteCoordinate(vec3d.x), this.y.toAbsoluteCoordinate(vec3d.y), this.z.toAbsoluteCoordinate(vec3d.z)); + } + + @Override + public Vec2f toAbsoluteRotation(FabricClientCommandSource source) { + Vec2f vec2f = source.getRotation(); + return new Vec2f((float)this.x.toAbsoluteCoordinate(vec2f.x), (float)this.y.toAbsoluteCoordinate(vec2f.y)); + } + + @Override + public boolean isXRelative() { + return this.x.isRelative(); + } + + @Override + public boolean isYRelative() { + return this.y.isRelative(); + } + + @Override + public boolean isZRelative() { + return this.z.isRelative(); + } + + public boolean equals(Object o) { + if (this == o) return true; + + return o instanceof DefaultClientPosArgument defaultPosArgument && + this.x.equals(defaultPosArgument.x) && this.y.equals(defaultPosArgument.y) && this.z.equals(defaultPosArgument.z); + } + + public static DefaultClientPosArgument parse(StringReader reader) throws CommandSyntaxException { + int i = reader.getCursor(); + CoordinateArgument coordinateArgument = CoordinateArgument.parse(reader); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + CoordinateArgument coordinateArgument2 = CoordinateArgument.parse(reader); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + CoordinateArgument coordinateArgument3 = CoordinateArgument.parse(reader); + return new DefaultClientPosArgument(coordinateArgument, coordinateArgument2, coordinateArgument3); + } else { + reader.setCursor(i); + throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } else { + reader.setCursor(i); + throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } + + public static DefaultClientPosArgument parse(StringReader reader, boolean centerIntegers) throws CommandSyntaxException { + int i = reader.getCursor(); + CoordinateArgument coordinateArgument = CoordinateArgument.parse(reader, centerIntegers); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + CoordinateArgument coordinateArgument2 = CoordinateArgument.parse(reader, false); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + CoordinateArgument coordinateArgument3 = CoordinateArgument.parse(reader, centerIntegers); + return new DefaultClientPosArgument(coordinateArgument, coordinateArgument2, coordinateArgument3); + } else { + reader.setCursor(i); + throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } else { + reader.setCursor(i); + throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } + + public static DefaultClientPosArgument absolute(double x, double y, double z) { + return new DefaultClientPosArgument(new CoordinateArgument(false, x), new CoordinateArgument(false, y), new CoordinateArgument(false, z)); + } + + public static DefaultClientPosArgument absolute(Vec2f vec) { + return new DefaultClientPosArgument(new CoordinateArgument(false, vec.x), new CoordinateArgument(false, vec.y), new CoordinateArgument(true, 0.0)); + } + + public static DefaultClientPosArgument zero() { + return new DefaultClientPosArgument(new CoordinateArgument(true, 0.0), new CoordinateArgument(true, 0.0), new CoordinateArgument(true, 0.0)); + } + + public int hashCode() { + int i = this.x.hashCode(); + i = 31 * i + this.y.hashCode(); + return 31 * i + this.z.hashCode(); + } +} diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java new file mode 100644 index 0000000000..3510e118c8 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java @@ -0,0 +1,36 @@ +package de.hysky.skyblocker.utils.command.argumenttypes; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import de.hysky.skyblocker.skyblock.chocolatefactory.EggFinder; +import net.minecraft.command.CommandSource; + +import java.util.Collection; +import java.util.concurrent.CompletableFuture; + +public class EggTypeArgumentType implements ArgumentType { + @Override + public EggFinder.EggType parse(StringReader reader) throws CommandSyntaxException { + String name = reader.readUnquotedString(); + for (EggFinder.EggType type : EggFinder.EggType.entries) { + if (type.name().equalsIgnoreCase(name)) return type; + } + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().create(); + } + + @Override + public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { + return context.getSource() instanceof CommandSource + ? CommandSource.suggestMatching(EggFinder.EggType.entries.stream().map(EggFinder.EggType::name).map(String::toLowerCase), builder) + : Suggestions.empty(); + } + + @Override + public Collection getExamples() { + return EggFinder.EggType.entries.stream().map(EggFinder.EggType::name).map(String::toLowerCase).toList(); + } +} diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/LookingClientPosArgument.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/LookingClientPosArgument.java new file mode 100644 index 0000000000..0e9204e1c6 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/LookingClientPosArgument.java @@ -0,0 +1,106 @@ +package de.hysky.skyblocker.utils.command.argumenttypes; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.command.argument.CoordinateArgument; +import net.minecraft.command.argument.Vec3ArgumentType; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.Vec2f; +import net.minecraft.util.math.Vec3d; + +import java.util.Objects; + +public class LookingClientPosArgument implements ClientPosArgument { + private final double x; + private final double y; + private final double z; + + public LookingClientPosArgument(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public Vec3d toAbsolutePos(FabricClientCommandSource source) { + Vec2f vec2f = source.getRotation(); + Vec3d vec3d = source.getPlayer().getPos(); + float f = MathHelper.cos((vec2f.y + 90.0F) * (float) (Math.PI / 180.0)); + float g = MathHelper.sin((vec2f.y + 90.0F) * (float) (Math.PI / 180.0)); + float h = MathHelper.cos(-vec2f.x * (float) (Math.PI / 180.0)); + float i = MathHelper.sin(-vec2f.x * (float) (Math.PI / 180.0)); + float j = MathHelper.cos((-vec2f.x + 90.0F) * (float) (Math.PI / 180.0)); + float k = MathHelper.sin((-vec2f.x + 90.0F) * (float) (Math.PI / 180.0)); + Vec3d vec3d2 = new Vec3d(f * h, i, g * h); + Vec3d vec3d3 = new Vec3d(f * j, k, g * j); + Vec3d vec3d4 = vec3d2.crossProduct(vec3d3).multiply(-1.0); + double d = vec3d2.x * this.z + vec3d3.x * this.y + vec3d4.x * this.x; + double e = vec3d2.y * this.z + vec3d3.y * this.y + vec3d4.y * this.x; + double l = vec3d2.z * this.z + vec3d3.z * this.y + vec3d4.z * this.x; + return new Vec3d(vec3d.x + d, vec3d.y + e, vec3d.z + l); + } + + @Override + public Vec2f toAbsoluteRotation(FabricClientCommandSource source) { + return Vec2f.ZERO; + } + + @Override + public boolean isXRelative() { + return true; + } + + @Override + public boolean isYRelative() { + return true; + } + + @Override + public boolean isZRelative() { + return true; + } + + public static LookingClientPosArgument parse(StringReader reader) throws CommandSyntaxException { + int i = reader.getCursor(); + double d = readCoordinate(reader, i); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + double e = readCoordinate(reader, i); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + double f = readCoordinate(reader, i); + return new LookingClientPosArgument(d, e, f); + } else { + reader.setCursor(i); + throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } else { + reader.setCursor(i); + throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } + + private static double readCoordinate(StringReader reader, int startingCursorPos) throws CommandSyntaxException { + if (!reader.canRead()) { + throw CoordinateArgument.MISSING_COORDINATE.createWithContext(reader); + } else if (reader.peek() != '^') { + reader.setCursor(startingCursorPos); + throw Vec3ArgumentType.MIXED_COORDINATE_EXCEPTION.createWithContext(reader); + } else { + reader.skip(); + return reader.canRead() && reader.peek() != ' ' ? reader.readDouble() : 0.0; + } + } + + public boolean equals(Object o) { + if (this == o) return true; + + return o instanceof LookingClientPosArgument lookingPosArgument + && this.x == lookingPosArgument.x && this.y == lookingPosArgument.y && this.z == lookingPosArgument.z; + } + + public int hashCode() { + return Objects.hash(x, y, z); + } +} From e650d8d364e2fc39ac8ebca29510b4c3541ad5b9 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 19:15:05 +0300 Subject: [PATCH 2/9] Fix /skyblocker diana clearGriffinBurrow pos command --- .../skyblocker/skyblock/waypoint/MythologicalRitual.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java index ffeba7ea21..5a5e18255d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java @@ -6,6 +6,8 @@ import de.hysky.skyblocker.utils.ColorUtils; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; import de.hysky.skyblocker.utils.render.RenderHelper; import de.hysky.skyblocker.utils.waypoint.Waypoint; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; @@ -19,7 +21,6 @@ import net.fabricmc.fabric.api.util.TriState; import net.minecraft.block.Blocks; import net.minecraft.client.MinecraftClient; -import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.network.packet.s2c.play.ParticleS2CPacket; @@ -67,8 +68,8 @@ public static void init() { return Command.SINGLE_SUCCESS; })) .then(literal("clearGriffinBurrow") - .then(argument("pos", BlockPosArgumentType.blockPos()).executes(context -> { - griffinBurrows.remove(context.getArgument("pos", BlockPos.class)); + .then(argument("position", ClientBlockPosArgumentType.blockPos()).executes(context -> { + griffinBurrows.remove(context.getArgument("position", ClientPosArgument.class).toAbsoluteBlockPos(context.getSource())); return Command.SINGLE_SUCCESS; })) ) From f5f068da1113fcb1b26fadade3a50b0a76c5b440 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 19:16:01 +0300 Subject: [PATCH 3/9] Reformat EggFinder --- .../hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java index 38694ec45b..63f5dfbd90 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java @@ -57,7 +57,7 @@ public static void init() { .then(argument("blockPos", ClientBlockPosArgumentType.blockPos()) .then(argument("eggType", new EggTypeArgumentType()) .executes(context -> { - MessageScheduler.INSTANCE.sendMessageAfterCooldown("[Skyblocker] Chocolate " + context.getArgument("eggType", EggType.class) + " Egg found at " + context.getArgument("blockPos", ClientPosArgument.class).toAbsoluteBlockPos(context.getSource()) .toShortString() + "!"); + MessageScheduler.INSTANCE.sendMessageAfterCooldown("[Skyblocker] Chocolate " + context.getArgument("eggType", EggType.class) + " Egg found at " + context.getArgument("blockPos", ClientPosArgument.class).toAbsoluteBlockPos(context.getSource()).toShortString() + "!"); return Command.SINGLE_SUCCESS; }))))))); } From a27e84c0a2fbadf2c44b472346cd9e3ea6b0a4d0 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 19:29:15 +0300 Subject: [PATCH 4/9] Change usage of Utils.getLocationRaw into Location comparison --- .../hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java index 5a5e18255d..e788468691 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java @@ -5,6 +5,7 @@ import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.ColorUtils; import de.hysky.skyblocker.utils.ItemUtils; +import de.hysky.skyblocker.utils.Location; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; @@ -191,7 +192,7 @@ public static void onChatMessage(Text message, boolean overlay) { } private static boolean isActive() { - return SkyblockerConfigManager.get().helpers.mythologicalRitual.enableMythologicalRitualHelper && Utils.getLocationRaw().equals("hub"); + return SkyblockerConfigManager.get().helpers.mythologicalRitual.enableMythologicalRitualHelper && Utils.getLocation() == Location.HUB; } private static void reset() { From ea7ac5d31233ceaba2fe618416d09cf040e1bfee Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 19:33:53 +0300 Subject: [PATCH 5/9] Reformat CustomArmorDyeColors --- .../de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java index 9731122001..013e0f8b18 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java @@ -39,7 +39,7 @@ private static int customizeDyeColor(FabricClientCommandSource source, String he return Command.SINGLE_SUCCESS; } - if (Utils.isOnSkyblock() && heldItem != null) { + if (Utils.isOnSkyblock() && heldItem != null) { if (heldItem.isIn(ItemTags.DYEABLE)) { String itemUuid = ItemUtils.getItemUuid(heldItem); From 66ac1ea70b7d749db96ee274f646f8a3e4e46c93 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 20:53:29 +0300 Subject: [PATCH 6/9] Clean up decompiled code --- .../ClientBlockPosArgumentType.java | 46 +++++++------------ src/main/resources/skyblocker.accesswidener | 1 + 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java index 484606ecbc..1257c1e474 100644 --- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java @@ -4,46 +4,39 @@ import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.client.world.ClientWorld; import net.minecraft.command.CommandSource; +import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.server.command.CommandManager; -import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.concurrent.CompletableFuture; -public class ClientBlockPosArgumentType implements ArgumentType { - private static final Collection EXAMPLES = Arrays.asList("0 0 0", "~ ~ ~", "^ ^ ^", "^1 ^ ^-5", "~0.5 ~1 ~-5"); - public static final SimpleCommandExceptionType UNLOADED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.pos.unloaded")); - public static final SimpleCommandExceptionType OUT_OF_WORLD_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.pos.outofworld")); - public static final SimpleCommandExceptionType OUT_OF_BOUNDS_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.pos.outofbounds")); +import static net.minecraft.command.argument.BlockPosArgumentType.*; +// Uses the static fields of BlockPosArgumentType to not create the same field twice +public class ClientBlockPosArgumentType implements ArgumentType { public static ClientBlockPosArgumentType blockPos() { return new ClientBlockPosArgumentType(); } public static BlockPos getLoadedBlockPos(CommandContext context, String name) throws CommandSyntaxException { - ClientWorld clientWorld = context.getSource().getWorld(); - return getLoadedBlockPos(context, clientWorld, name); + return getLoadedBlockPos(context, context.getSource().getWorld(), name); } public static BlockPos getLoadedBlockPos(CommandContext context, ClientWorld world, String name) throws CommandSyntaxException { BlockPos blockPos = getBlockPos(context, name); - if (!world.isChunkLoaded(blockPos)) { - throw UNLOADED_EXCEPTION.create(); - } else if (!world.isInBuildLimit(blockPos)) { - throw OUT_OF_WORLD_EXCEPTION.create(); - } else { - return blockPos; - } + if (!world.isChunkLoaded(blockPos)) throw UNLOADED_EXCEPTION.create(); + if (!world.isInBuildLimit(blockPos)) throw OUT_OF_WORLD_EXCEPTION.create(); + + return blockPos; + } public static BlockPos getBlockPos(CommandContext context, String name) { @@ -65,23 +58,16 @@ public ClientPosArgument parse(StringReader stringReader) throws CommandSyntaxEx @Override public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { - if (!(context.getSource() instanceof CommandSource)) { - return Suggestions.empty(); - } else { - String string = builder.getRemaining(); - Collection collection; - if (!string.isEmpty() && string.charAt(0) == '^') { - collection = Collections.singleton(CommandSource.RelativePosition.ZERO_LOCAL); - } else { - collection = ((CommandSource)context.getSource()).getBlockPositionSuggestions(); - } + if (!(context.getSource() instanceof CommandSource commandSource)) return Suggestions.empty(); - return CommandSource.suggestPositions(string, collection, builder, CommandManager.getCommandValidator(this::parse)); - } + String string = builder.getRemaining(); + Collection collection = !string.isEmpty() && string.charAt(0) == '^' ? Collections.singleton(CommandSource.RelativePosition.ZERO_LOCAL) : commandSource.getBlockPositionSuggestions(); + + return CommandSource.suggestPositions(string, collection, builder, CommandManager.getCommandValidator(this::parse)); } @Override public Collection getExamples() { - return EXAMPLES; + return BlockPosArgumentType.EXAMPLES; } } diff --git a/src/main/resources/skyblocker.accesswidener b/src/main/resources/skyblocker.accesswidener index 9fcdfa45a3..136124b728 100644 --- a/src/main/resources/skyblocker.accesswidener +++ b/src/main/resources/skyblocker.accesswidener @@ -18,3 +18,4 @@ accessible class net/minecraft/client/render/RenderPhase$LineWidth accessible class net/minecraft/client/render/RenderPhase$ColorLogic accessible class net/minecraft/client/render/RenderPhase$OffsetTexturing accessible class net/minecraft/client/render/RenderPhase$Textures +accessible field net/minecraft/command/argument/BlockPosArgumentType EXAMPLES Ljava/util/Collection; From 63c7a49cea4af5c1841475cda9bee377ee18d166 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 20:54:01 +0300 Subject: [PATCH 7/9] Add factory method for `EggTypeArgumentType` to match other argument type classes --- .../hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java | 3 +-- .../utils/command/argumenttypes/EggTypeArgumentType.java | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java index 63f5dfbd90..0dd594afcf 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java @@ -6,7 +6,6 @@ import de.hysky.skyblocker.events.SkyblockEvents; import de.hysky.skyblocker.utils.*; import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; import de.hysky.skyblocker.utils.command.argumenttypes.EggTypeArgumentType; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import de.hysky.skyblocker.utils.waypoint.Waypoint; @@ -55,7 +54,7 @@ public static void init() { .then(literal("eggFinder") .then(literal("shareLocation") .then(argument("blockPos", ClientBlockPosArgumentType.blockPos()) - .then(argument("eggType", new EggTypeArgumentType()) + .then(argument("eggType", EggTypeArgumentType.eggType()) .executes(context -> { MessageScheduler.INSTANCE.sendMessageAfterCooldown("[Skyblocker] Chocolate " + context.getArgument("eggType", EggType.class) + " Egg found at " + context.getArgument("blockPos", ClientPosArgument.class).toAbsoluteBlockPos(context.getSource()).toShortString() + "!"); return Command.SINGLE_SUCCESS; diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java index 3510e118c8..5448ad825b 100644 --- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java @@ -33,4 +33,8 @@ public CompletableFuture listSuggestions(CommandContext cont public Collection getExamples() { return EggFinder.EggType.entries.stream().map(EggFinder.EggType::name).map(String::toLowerCase).toList(); } + + public static EggTypeArgumentType eggType() { + return new EggTypeArgumentType(); + } } From bd9a837d955f4d28e19180cd842624b143d8eae6 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 21:43:02 +0300 Subject: [PATCH 8/9] Move block pos argument stuff into a different folder --- .../hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java | 2 +- .../skyblocker/skyblock/dungeon/secrets/DungeonManager.java | 4 ++-- .../skyblocker/skyblock/dwarven/CrystalsLocationsManager.java | 4 ++-- .../skyblocker/skyblock/waypoint/MythologicalRitual.java | 4 ++-- .../hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java | 4 ++-- .../{ => blockpos}/ClientBlockPosArgumentType.java | 3 +-- .../argumenttypes/{ => blockpos}/ClientPosArgument.java | 2 +- .../{ => blockpos}/DefaultClientPosArgument.java | 2 +- .../{ => blockpos}/LookingClientPosArgument.java | 2 +- 9 files changed, 13 insertions(+), 14 deletions(-) rename src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/{ => blockpos}/ClientBlockPosArgumentType.java (97%) rename src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/{ => blockpos}/ClientPosArgument.java (92%) rename src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/{ => blockpos}/DefaultClientPosArgument.java (98%) rename src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/{ => blockpos}/LookingClientPosArgument.java (98%) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java index 0dd594afcf..2c1ee2eba0 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java @@ -5,7 +5,7 @@ import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.events.SkyblockEvents; import de.hysky.skyblocker.utils.*; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; import de.hysky.skyblocker.utils.command.argumenttypes.EggTypeArgumentType; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import de.hysky.skyblocker.utils.waypoint.Waypoint; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java index e6faff7039..11f31f3489 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/DungeonManager.java @@ -20,8 +20,8 @@ import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Tickable; import de.hysky.skyblocker.utils.Utils; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientPosArgument; import de.hysky.skyblocker.utils.scheduler.Scheduler; import it.unimi.dsi.fastutil.objects.Object2ByteMap; import it.unimi.dsi.fastutil.objects.Object2ByteMaps; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java b/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java index 9f5afcc3ee..83167c18d7 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dwarven/CrystalsLocationsManager.java @@ -8,8 +8,8 @@ import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Utils; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientPosArgument; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import de.hysky.skyblocker.utils.scheduler.Scheduler; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java index e788468691..2b06477072 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/MythologicalRitual.java @@ -7,8 +7,8 @@ import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Location; import de.hysky.skyblocker.utils.Utils; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientPosArgument; import de.hysky.skyblocker.utils.render.RenderHelper; import de.hysky.skyblocker.utils.waypoint.Waypoint; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java index 10fe0b6bcc..b199ffa305 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java @@ -15,8 +15,8 @@ import de.hysky.skyblocker.skyblock.item.CustomArmorDyeColors; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Utils; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientBlockPosArgumentType; -import de.hysky.skyblocker.utils.command.argumenttypes.ClientPosArgument; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientPosArgument; import de.hysky.skyblocker.utils.render.RenderHelper; import de.hysky.skyblocker.utils.waypoint.Waypoint; import it.unimi.dsi.fastutil.floats.FloatArrayList; diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/ClientBlockPosArgumentType.java similarity index 97% rename from src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java rename to src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/ClientBlockPosArgumentType.java index 1257c1e474..3a2264141d 100644 --- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientBlockPosArgumentType.java +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/ClientBlockPosArgumentType.java @@ -1,4 +1,4 @@ -package de.hysky.skyblocker.utils.command.argumenttypes; +package de.hysky.skyblocker.utils.command.argumenttypes.blockpos; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; @@ -36,7 +36,6 @@ public static BlockPos getLoadedBlockPos(CommandContext context, String name) { diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientPosArgument.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/ClientPosArgument.java similarity index 92% rename from src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientPosArgument.java rename to src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/ClientPosArgument.java index ad0da38b3b..3dff86abc2 100644 --- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/ClientPosArgument.java +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/ClientPosArgument.java @@ -1,4 +1,4 @@ -package de.hysky.skyblocker.utils.command.argumenttypes; +package de.hysky.skyblocker.utils.command.argumenttypes.blockpos; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.util.math.BlockPos; diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/DefaultClientPosArgument.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/DefaultClientPosArgument.java similarity index 98% rename from src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/DefaultClientPosArgument.java rename to src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/DefaultClientPosArgument.java index 99507f64b2..47258b9f78 100644 --- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/DefaultClientPosArgument.java +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/DefaultClientPosArgument.java @@ -1,4 +1,4 @@ -package de.hysky.skyblocker.utils.command.argumenttypes; +package de.hysky.skyblocker.utils.command.argumenttypes.blockpos; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/LookingClientPosArgument.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/LookingClientPosArgument.java similarity index 98% rename from src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/LookingClientPosArgument.java rename to src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/LookingClientPosArgument.java index 0e9204e1c6..3f89f9c718 100644 --- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/LookingClientPosArgument.java +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/blockpos/LookingClientPosArgument.java @@ -1,4 +1,4 @@ -package de.hysky.skyblocker.utils.command.argumenttypes; +package de.hysky.skyblocker.utils.command.argumenttypes.blockpos; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; From 3a7dce4e3d60232805e6fb07ec5cbd5c26b7673e Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 11 Jul 2024 23:38:25 +0300 Subject: [PATCH 9/9] Add color argument types and update usages --- .../skyblock/chocolatefactory/EggFinder.java | 3 +- .../item/CustomArmorAnimatedDyes.java | 32 ++++++--------- .../skyblock/item/CustomArmorDyeColors.java | 23 ++++------- .../skyblock/waypoint/OrderedWaypoints.java | 27 +++++-------- .../argumenttypes/EggTypeArgumentType.java | 2 +- .../color/ColorArgumentType.java | 28 +++++++++++++ .../color/HexColorArgumentType.java | 38 ++++++++++++++++++ .../color/RgbColorArgumentType.java | 39 +++++++++++++++++++ .../assets/skyblocker/lang/en_us.json | 4 ++ 9 files changed, 141 insertions(+), 55 deletions(-) create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java index 2c1ee2eba0..6926fda8d1 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chocolatefactory/EggFinder.java @@ -5,8 +5,9 @@ import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.events.SkyblockEvents; import de.hysky.skyblocker.utils.*; -import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; import de.hysky.skyblocker.utils.command.argumenttypes.EggTypeArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; +import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientPosArgument; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import de.hysky.skyblocker.utils.waypoint.Waypoint; import it.unimi.dsi.fastutil.objects.ObjectImmutableList; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorAnimatedDyes.java b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorAnimatedDyes.java index 76e7f02ca2..56ce7bf866 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorAnimatedDyes.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorAnimatedDyes.java @@ -1,20 +1,15 @@ package de.hysky.skyblocker.skyblock.item; -import static com.mojang.brigadier.arguments.StringArgumentType.getString; -import static com.mojang.brigadier.arguments.StringArgumentType.word; -import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; -import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; - import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; - import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.command.argumenttypes.color.ColorArgumentType; import dev.isxander.yacl3.config.v2.api.SerialEntry; import it.unimi.dsi.fastutil.objects.Object2ObjectFunction; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; @@ -27,6 +22,9 @@ import net.minecraft.text.Text; import net.minecraft.util.math.MathHelper; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + public class CustomArmorAnimatedDyes { private static final Object2ObjectOpenHashMap STATE_TRACKER_MAP = new Object2ObjectOpenHashMap<>(); private static final Object2ObjectFunction NEW_STATE_TRACKER = _dye -> AnimatedDyeStateTracker.create(); @@ -42,23 +40,17 @@ private static void registerCommands(CommandDispatcher customizeAnimatedDye(context.getSource(), null, null, 0, false, 0)) - .then(argument("hex1", word()) - .then(argument("hex2", word()) + .executes(context -> customizeAnimatedDye(context.getSource(), Integer.MIN_VALUE, Integer.MIN_VALUE, 0, false, 0)) + .then(argument("hex1", ColorArgumentType.hex()) + .then(argument("hex2", ColorArgumentType.hex()) .then(argument("samples", IntegerArgumentType.integer(1)) .then(argument("cycleBack", BoolArgumentType.bool()) - .executes(context -> customizeAnimatedDye(context.getSource(), getString(context, "hex1"), getString(context, "hex2"), IntegerArgumentType.getInteger(context, "samples"), BoolArgumentType.getBool(context, "cycleBack"), DEFAULT_TICK_DELAY)) + .executes(context -> customizeAnimatedDye(context.getSource(), ColorArgumentType.getIntFromHex(context, "hex1"), ColorArgumentType.getIntFromHex(context, "hex2"), IntegerArgumentType.getInteger(context, "samples"), BoolArgumentType.getBool(context, "cycleBack"), DEFAULT_TICK_DELAY)) .then(argument("tickDelay", IntegerArgumentType.integer(0, 20)) - .executes(context ->customizeAnimatedDye(context.getSource(), getString(context, "hex1"), getString(context, "hex2"), IntegerArgumentType.getInteger(context, "samples"), BoolArgumentType.getBool(context, "cycleBack"), IntegerArgumentType.getInteger(context, "tickDelay"))))))))))); + .executes(context ->customizeAnimatedDye(context.getSource(), ColorArgumentType.getIntFromHex(context, "hex1"), ColorArgumentType.getIntFromHex(context, "hex2"), IntegerArgumentType.getInteger(context, "samples"), BoolArgumentType.getBool(context, "cycleBack"), IntegerArgumentType.getInteger(context, "tickDelay"))))))))))); } - private static int customizeAnimatedDye(FabricClientCommandSource source, String hex1, String hex2, int samples, boolean cycleBack, int tickDelay) { - if (hex1 != null && hex2 != null && (!CustomArmorDyeColors.isHexadecimalColor(hex1) || !CustomArmorDyeColors.isHexadecimalColor(hex2))) { - source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.customAnimatedDyes.invalidHex"))); - - return Command.SINGLE_SUCCESS; - } - + private static int customizeAnimatedDye(FabricClientCommandSource source, int color1, int color2, int samples, boolean cycleBack, int tickDelay) { ItemStack heldItem = source.getPlayer().getMainHandStack(); if (Utils.isOnSkyblock() && heldItem != null && !heldItem.isEmpty()) { @@ -68,7 +60,7 @@ private static int customizeAnimatedDye(FabricClientCommandSource source, String if (!itemUuid.isEmpty()) { Object2ObjectOpenHashMap customAnimatedDyes = SkyblockerConfigManager.get().general.customAnimatedDyes; - if (hex1 == null && hex2 == null) { + if (color1 == Integer.MIN_VALUE && color2 == Integer.MIN_VALUE) { if (customAnimatedDyes.containsKey(itemUuid)) { customAnimatedDyes.remove(itemUuid); SkyblockerConfigManager.save(); @@ -77,7 +69,7 @@ private static int customizeAnimatedDye(FabricClientCommandSource source, String source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.customAnimatedDyes.neverHad"))); } } else { - AnimatedDye animatedDye = new AnimatedDye(Integer.decode("0x" + hex1.replace("#", "")), Integer.decode("0x" + hex2.replace("#", "")), samples, cycleBack, tickDelay); + AnimatedDye animatedDye = new AnimatedDye(color1, color2, samples, cycleBack, tickDelay); customAnimatedDyes.put(itemUuid, animatedDye); SkyblockerConfigManager.save(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java index 013e0f8b18..62ffcf73a6 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java @@ -2,11 +2,11 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.StringArgumentType; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.command.argumenttypes.color.ColorArgumentType; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; @@ -25,20 +25,15 @@ private static void registerCommands(CommandDispatcher customizeDyeColor(context.getSource(), null)) - .then(ClientCommandManager.argument("hexCode", StringArgumentType.string()) - .executes(context -> customizeDyeColor(context.getSource(), StringArgumentType.getString(context, "hexCode"))))))); + .executes(context -> customizeDyeColor(context.getSource(), Integer.MIN_VALUE)) + .then(ClientCommandManager.argument("hexCode", ColorArgumentType.hex()) + .executes(context -> customizeDyeColor(context.getSource(), ColorArgumentType.getIntFromHex(context, "hexCode"))))))); } @SuppressWarnings("SameReturnValue") - private static int customizeDyeColor(FabricClientCommandSource source, String hex) { + private static int customizeDyeColor(FabricClientCommandSource source, int color) { ItemStack heldItem = source.getPlayer().getMainHandStack(); - if (hex != null && !isHexadecimalColor(hex)) { - source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.customDyeColors.invalidHex"))); - return Command.SINGLE_SUCCESS; - } - if (Utils.isOnSkyblock() && heldItem != null) { if (heldItem.isIn(ItemTags.DYEABLE)) { String itemUuid = ItemUtils.getItemUuid(heldItem); @@ -46,7 +41,7 @@ private static int customizeDyeColor(FabricClientCommandSource source, String he if (!itemUuid.isEmpty()) { Object2IntOpenHashMap customDyeColors = SkyblockerConfigManager.get().general.customDyeColors; - if (hex == null) { + if (color == Integer.MIN_VALUE) { if (customDyeColors.containsKey(itemUuid)) { customDyeColors.removeInt(itemUuid); SkyblockerConfigManager.save(); @@ -55,7 +50,7 @@ private static int customizeDyeColor(FabricClientCommandSource source, String he source.sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.customDyeColors.neverHad"))); } } else { - customDyeColors.put(itemUuid, Integer.decode("0x" + hex.replace("#", "")).intValue()); + customDyeColors.put(itemUuid, color); SkyblockerConfigManager.save(); source.sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.customDyeColors.added"))); } @@ -72,8 +67,4 @@ private static int customizeDyeColor(FabricClientCommandSource source, String he return Command.SINGLE_SUCCESS; } - - public static boolean isHexadecimalColor(String s) { - return s.replace("#", "").chars().allMatch(c -> "0123456789ABCDEFabcdef".indexOf(c) >= 0) && s.replace("#", "").length() == 6; - } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java index b199ffa305..b88eb38f19 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java @@ -12,11 +12,12 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; -import de.hysky.skyblocker.skyblock.item.CustomArmorDyeColors; +import de.hysky.skyblocker.utils.ColorUtils; import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientBlockPosArgumentType; import de.hysky.skyblocker.utils.command.argumenttypes.blockpos.ClientPosArgument; +import de.hysky.skyblocker.utils.command.argumenttypes.color.ColorArgumentType; import de.hysky.skyblocker.utils.render.RenderHelper; import de.hysky.skyblocker.utils.waypoint.Waypoint; import it.unimi.dsi.fastutil.floats.FloatArrayList; @@ -87,17 +88,17 @@ private static void registerCommands(CommandDispatcher CommandSource.suggestMatching(WAYPOINTS.keySet(), builder)) .then(argument("pos", ClientBlockPosArgumentType.blockPos()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), Integer.MIN_VALUE, null)) - .then(argument("hex", word()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), Integer.MIN_VALUE, getString(context, "hex"))))))) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), Integer.MIN_VALUE, Integer.MIN_VALUE)) + .then(argument("hex", ColorArgumentType.hex()) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), Integer.MIN_VALUE, ColorArgumentType.getIntFromHex(context, "hex"))))))) .then(literal("addAt") .then(argument("groupName", word()) .suggests((source, builder) -> CommandSource.suggestMatching(WAYPOINTS.keySet(), builder)) .then(argument("index", IntegerArgumentType.integer(0)) .then(argument("pos", ClientBlockPosArgumentType.blockPos()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), IntegerArgumentType.getInteger(context, "index"), null)) - .then(argument("hex", word()) - .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), IntegerArgumentType.getInteger(context, "index"), getString(context, "hex")))))))) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), IntegerArgumentType.getInteger(context, "index"), Integer.MIN_VALUE)) + .then(argument("hex", ColorArgumentType.hex()) + .executes(context -> addWaypoint(context.getSource(), getString(context, "groupName"), context.getArgument("pos", ClientPosArgument.class), IntegerArgumentType.getInteger(context, "index"), ColorArgumentType.getIntFromHex(context, "hex")))))))) .then(literal("remove") .then(argument("groupName", word()) .suggests((source, builder) -> CommandSource.suggestMatching(WAYPOINTS.keySet(), builder)) @@ -125,20 +126,12 @@ private static void registerCommands(CommandDispatcher export(context.getSource())))))); } - private static int addWaypoint(FabricClientCommandSource source, String groupName, ClientPosArgument posArgument, int index, String hex) { + private static int addWaypoint(FabricClientCommandSource source, String groupName, ClientPosArgument posArgument, int index, int color) { BlockPos pos = posArgument.toAbsoluteBlockPos(source); SEMAPHORE.acquireUninterruptibly(); - if (hex != null && !CustomArmorDyeColors.isHexadecimalColor(hex)) { - source.sendError(Constants.PREFIX.get().append(Text.translatable("skyblocker.waypoints.ordered.add.invalidHexColor"))); - SEMAPHORE.release(); - - return Command.SINGLE_SUCCESS; - } - - int rgb = hex != null ? Integer.decode("0x" + hex.replace("#", "")) : Integer.MIN_VALUE; - float[] colorComponents = rgb != Integer.MIN_VALUE ? new float[] { ((rgb >> 16) & 0xFF) / 255f, ((rgb >> 8) & 0xFF) / 255f, (rgb & 0xFF) / 255f } : new float[0]; + float[] colorComponents = color != Integer.MIN_VALUE ? ColorUtils.getFloatComponents(color) : new float[0]; OrderedWaypointGroup group = WAYPOINTS.computeIfAbsent(groupName, name -> new OrderedWaypointGroup(name, true, new ObjectArrayList<>())); OrderedWaypoint waypoint = new OrderedWaypoint(pos, colorComponents); diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java index 5448ad825b..a6532ff80f 100644 --- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java @@ -12,7 +12,7 @@ import java.util.Collection; import java.util.concurrent.CompletableFuture; -public class EggTypeArgumentType implements ArgumentType { +public final class EggTypeArgumentType implements ArgumentType { @Override public EggFinder.EggType parse(StringReader reader) throws CommandSyntaxException { String name = reader.readUnquotedString(); diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java new file mode 100644 index 0000000000..a6dc851056 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java @@ -0,0 +1,28 @@ +package de.hysky.skyblocker.utils.command.argumenttypes.color; + +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; + +/** + * Utility class that provides static methods for abstracting away the actual argument type classes. + */ +public final class ColorArgumentType { + private ColorArgumentType() {} // Prevent instantiation + + public static RgbColorArgumentType rgb() { + return new RgbColorArgumentType(); + } + + public static HexColorArgumentType hex() { + return new HexColorArgumentType(); + } + + public static int getIntFromHex(CommandContext context, String name) { + return HexColorArgumentType.getInt(context, name); + } + + public static int getIntFromRgb(CommandContext context, String name) { + return RgbColorArgumentType.getInt(context, name); + } +} + diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java new file mode 100644 index 0000000000..516eb7b927 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java @@ -0,0 +1,38 @@ +package de.hysky.skyblocker.utils.command.argumenttypes.color; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.text.Text; +import org.apache.commons.lang3.StringUtils; + +@SuppressWarnings("RedundantCast") +public final class HexColorArgumentType implements ArgumentType { + public static final DynamicCommandExceptionType WRONG_INPUT_WIDTH = new DynamicCommandExceptionType(found -> Text.translatable("argument.color.hex.invalidString", ((String) found).length())); + public static final DynamicCommandExceptionType INVALID_CHARACTER = new DynamicCommandExceptionType(character -> Text.translatable("argument.color.hex.invalidChar", (String) character)); + + @Override + public Integer parse(StringReader reader) throws CommandSyntaxException { + String input = reader.readString(); + if (StringUtils.startsWithIgnoreCase(input, "0x")) input = input.substring(2); +// else if (input.startsWith("#")) input = input.substring(1); // This doesn't work because minecraft has the # prefix reserved for tags, so inputs with that prefix never reach this reader + + if (input.length() != 6) throw WRONG_INPUT_WIDTH.create(input); + + for (int i = 0; i < input.length(); i++) { + char character = input.charAt(i); + if ((character < '0' || character > '9') && (character < 'a' || character > 'f') && (character < 'A' || character > 'F')) { + throw INVALID_CHARACTER.create(String.valueOf(character)); //Have to wrap character in a string, because mcdev doesn't appreciate chars and I cba to suppress the warnings + } + } + + return Integer.decode("#" + input); + } + + public static int getInt(CommandContext context, String name) { + return context.getArgument(name, Integer.class); + } +} diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java new file mode 100644 index 0000000000..d9a99faebe --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java @@ -0,0 +1,39 @@ +package de.hysky.skyblocker.utils.command.argumenttypes.color; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.text.Text; + +public final class RgbColorArgumentType implements ArgumentType { + public static final SimpleCommandExceptionType INCOMPLETE_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.color.rgb.incomplete")); + + @Override + public Integer parse(StringReader reader) throws CommandSyntaxException { + int i = reader.getCursor(); + int redArgument = IntegerArgumentType.integer(0x00, 0xFF).parse(reader); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + int greenArgument = IntegerArgumentType.integer(0x00, 0xFF).parse(reader); + if (reader.canRead() && reader.peek() == ' ') { + reader.skip(); + int blueArgument = IntegerArgumentType.integer(0x00, 0xFF).parse(reader); + return redArgument << 16 | greenArgument << 8 | blueArgument; + } else { + reader.setCursor(i); + throw INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } else { + reader.setCursor(i); + throw INCOMPLETE_EXCEPTION.createWithContext(reader); + } + } + + public static int getInt(CommandContext context, String name) { + return context.getArgument(name, Integer.class); + } +} diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index bf15404819..63fdcc6c0e 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -1,4 +1,8 @@ { + "argument.color.rgb.incomplete": "Incomplete (expected 3 color values).", + "argument.color.hex.invalidString": "Hex integer must be exactly 6 characters long, found %d characters.", + "argument.color.hex.invalidChar": "Invalid character for hex integer: %s. Valid characters: [0-9a-fA-F]", + "key.categories.skyblocker": "Skyblocker", "key.hotbarSlotLock": "Slot Lock (Hotbar)", "key.skyblocker.toggleB": "Toggle tab HUD to screen B",