From 39f42265b4afbe0aad77ea73acaedd07677aa285 Mon Sep 17 00:00:00 2001 From: Pedro270707 Date: Sun, 24 Sep 2023 15:26:03 -0300 Subject: [PATCH] Added command usage to renderer --- .../content/commands/SetBlockCommand.java | 6 ++-- .../content/commands/SummonCommand.java | 4 +-- .../content/helpers/EntitySelector.java | 28 +++++++++++-------- .../commander/gui/GuiChatSuggestions.java | 27 ++++++++++++++++-- .../mixin/ShowCommandSuggestionsMixin.java | 2 ++ 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java index 0e43ce3..28e732e 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/SetBlockCommand.java @@ -16,10 +16,10 @@ public class SetBlockCommand { public static void register(CommandDispatcher dispatcher) { dispatcher.register((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("setblock") .requires(sourceStack -> ((CommanderCommandSource)sourceStack).hasAdmin()) - .then(RequiredArgumentBuilder.argument("pos", BlockCoordinatesArgumentType.blockCoordinates()) + .then(RequiredArgumentBuilder.argument("position", BlockCoordinatesArgumentType.blockCoordinates()) .then(RequiredArgumentBuilder.argument("block", BlockArgumentType.block()) .executes(c -> { - BlockCoordinates coordinates = c.getArgument("pos", BlockCoordinates.class); + BlockCoordinates coordinates = c.getArgument("position", BlockCoordinates.class); Block block = c.getArgument("block", Block.class); ((CommanderCommandSource)c.getSource()).getWorld().setBlockWithNotify(coordinates.getX((CommanderCommandSource)c.getSource()), coordinates.getY((CommanderCommandSource)c.getSource(), true), coordinates.getZ((CommanderCommandSource)c.getSource()), block.id); @@ -28,7 +28,7 @@ public static void register(CommandDispatcher dispatcher }) .then(RequiredArgumentBuilder.argument("metadata", IntegerArgumentType.integer(0, 255)) .executes(c -> { - BlockCoordinates coordinates = c.getArgument("pos", BlockCoordinates.class); + BlockCoordinates coordinates = c.getArgument("position", BlockCoordinates.class); Block block = c.getArgument("block", Block.class); int metadata = c.getArgument("metadata", Integer.class); diff --git a/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java b/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java index 30a612c..15fef6f 100644 --- a/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java +++ b/src/main/java/net/pedroricardo/commander/content/commands/SummonCommand.java @@ -28,9 +28,9 @@ public static void register(CommandDispatcher dispatcher return CommanderCommandManager.SINGLE_SUCCESS; }) - .then(RequiredArgumentBuilder.argument("pos", Vec3dArgumentType.vec3d()) + .then(RequiredArgumentBuilder.argument("position", Vec3dArgumentType.vec3d()) .executes(c -> { - DoubleCoordinates coordinates = c.getArgument("pos", DoubleCoordinates.class); + DoubleCoordinates coordinates = c.getArgument("position", DoubleCoordinates.class); summonEntityAt(c, coordinates.getX(((CommanderCommandSource)c.getSource())), coordinates.getY(((CommanderCommandSource)c.getSource()), true), coordinates.getZ(((CommanderCommandSource)c.getSource())), 0.0f, 0.0f); diff --git a/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelector.java b/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelector.java index bc212a5..9cf1d43 100644 --- a/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelector.java +++ b/src/main/java/net/pedroricardo/commander/content/helpers/EntitySelector.java @@ -27,9 +27,9 @@ public class EntitySelector { private final @Nullable String playerName; private final MinMaxBounds.Doubles distance; private final Function position; - private final AABB aABB; + private final @Nullable AABB aABB; - public EntitySelector(int maxResults, boolean includesEntities, BiConsumer> order, @Nullable Class limitToType, boolean typeInverse, boolean currentEntity, Predicate predicate, @Nullable String entityId, @Nullable String playerName, MinMaxBounds.Doubles distance, Function position, AABB aABB) { + public EntitySelector(int maxResults, boolean includesEntities, BiConsumer> order, @Nullable Class limitToType, boolean typeInverse, boolean currentEntity, Predicate predicate, @Nullable String entityId, @Nullable String playerName, MinMaxBounds.Doubles distance, Function position, @Nullable AABB aABB) { this.maxResults = maxResults; this.includesEntities = includesEntities; this.order = order; @@ -76,25 +76,24 @@ public List get(CommanderCommandSource source) throws CommandS Vec3d position; if (sourceCoordinates != null) { position = this.position.apply(sourceCoordinates); - this.aABB.minX = this.aABB.minX + position.xCoord; - this.aABB.maxX = this.aABB.maxX + position.xCoord; - this.aABB.minY = this.aABB.minY + position.yCoord; - this.aABB.maxY = this.aABB.maxY + position.yCoord; - this.aABB.minZ = this.aABB.minZ + position.zCoord; - this.aABB.maxZ = this.aABB.maxZ + position.zCoord; + if (this.aABB != null) { + this.aABB.minX = this.aABB.minX + position.xCoord; + this.aABB.maxX = this.aABB.maxX + position.xCoord; + this.aABB.minY = this.aABB.minY + position.yCoord; + this.aABB.maxY = this.aABB.maxY + position.yCoord; + this.aABB.minZ = this.aABB.minZ + position.zCoord; + this.aABB.maxZ = this.aABB.maxZ + position.zCoord; + } } else { position = this.position.apply(Vec3d.createVector(0, 0, 0)); } - Commander.LOGGER.info(position.toString()); - Commander.LOGGER.info(this.aABB.toString()); - List temp = new ArrayList<>(entities); for (Entity entity : entities) { if ((limitToType != null && limitToType.isInstance(entity) == this.typeInverse) || !predicate.test(entity) || !this.distanceContains(entity, position.xCoord, position.yCoord, position.zCoord) - || !this.aABB.intersectsWith(entity.bb)) { + || !aABBIntersectsWithAABB(this.aABB, entity.bb)) { temp.remove(entity); } } @@ -121,6 +120,11 @@ private boolean distanceContains(Entity entity, double x, double y, double z) { return this.distance.contains(entity.distanceTo(x, y, z)); } + private static boolean aABBIntersectsWithAABB(AABB aABB1, AABB aABB2) { + if (aABB1 == null) return true; + return aABB2 != null && aABB1.intersectsWith(aABB2); + } + public int getMaxResults() { return this.maxResults; } diff --git a/src/main/java/net/pedroricardo/commander/gui/GuiChatSuggestions.java b/src/main/java/net/pedroricardo/commander/gui/GuiChatSuggestions.java index ee27648..3f86a11 100644 --- a/src/main/java/net/pedroricardo/commander/gui/GuiChatSuggestions.java +++ b/src/main/java/net/pedroricardo/commander/gui/GuiChatSuggestions.java @@ -6,6 +6,8 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.tree.CommandNode; +import com.mojang.brigadier.tree.LiteralCommandNode; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiChat; @@ -22,6 +24,7 @@ import java.awt.*; import java.util.List; import java.util.ArrayList; +import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -62,11 +65,16 @@ public void drawScreen() { if (!this.parseResults.getExceptions().isEmpty()) { int i = 0; for (Exception e : this.parseResults.getExceptions().values()) { - this.renderSingleSuggestionLine(this.mc.fontRenderer, "§e" + e.getMessage(), i); + this.renderSingleSuggestionLine(this.mc.fontRenderer, "§e" + e.getMessage(), i, false); i++; } } else if ((parseException = CommanderCommandManager.getParseException(this.parseResults)) != null) { - this.renderSingleSuggestionLine(this.mc.fontRenderer, "§e" + parseException.getMessage(), 0); + this.renderSingleSuggestionLine(this.mc.fontRenderer, "§e" + parseException.getMessage(), 0, false); + } else { + List commandUsage = getCommandUsage(this.tablessCursor); + for (int i = 0; i < commandUsage.size(); i++) { + this.renderSingleSuggestionLine(this.mc.fontRenderer, "§8" + commandUsage.get(i), i, true); + } } } } @@ -107,15 +115,28 @@ private void renderSuggestions(FontRenderer fontRenderer, String message, int cu } } - private void renderSingleSuggestionLine(FontRenderer fontRenderer, String text, int heightIndex) { + private void renderSingleSuggestionLine(FontRenderer fontRenderer, String text, int heightIndex, boolean followParameters) { int height = this.mc.resolution.scaledHeight - heightIndex * 12; int leftMargin = 2; int stringWidth = fontRenderer.getStringWidth(text); + if (Commander.suggestionsFollowParameters && this.parseResults != null && followParameters) + leftMargin += fontRenderer.getStringWidth(this.tablessMessage.substring(0, this.parseResults.getContext().findSuggestionContext(this.tablessCursor).startPos)) + 1; + this.drawRect(leftMargin, height - 27, stringWidth + leftMargin + 1, height - 15, Integer.MIN_VALUE); fontRenderer.drawStringWithShadow(text, leftMargin + 1, height - 25, 0xE0E0E0); } + private List getCommandUsage(int cursor) { + List commandUsage = new ArrayList<>(); + if (this.parseResults == null) return commandUsage; + for (Map.Entry, String> entry : CommanderCommandManager.getDispatcher().getSmartUsage(this.parseResults.getContext().findSuggestionContext(cursor).parent, this.commandSource).entrySet()) { + if (entry.getKey() instanceof LiteralCommandNode) continue; + commandUsage.add(entry.getValue()); + } + return commandUsage; + } + public void keyTyped(char c, int key) { if (key == 15) { if (Keyboard.isKeyDown(42)) { diff --git a/src/main/java/net/pedroricardo/commander/mixin/ShowCommandSuggestionsMixin.java b/src/main/java/net/pedroricardo/commander/mixin/ShowCommandSuggestionsMixin.java index e66f6ef..fe79d34 100644 --- a/src/main/java/net/pedroricardo/commander/mixin/ShowCommandSuggestionsMixin.java +++ b/src/main/java/net/pedroricardo/commander/mixin/ShowCommandSuggestionsMixin.java @@ -8,6 +8,7 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.tree.CommandNode; +import com.mojang.brigadier.tree.LiteralCommandNode; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiChat; import net.minecraft.client.gui.GuiScreen; @@ -28,6 +29,7 @@ import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map;