Skip to content

Commit

Permalink
Added command usage to renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Sep 24, 2023
1 parent bf86186 commit 39f4226
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class SetBlockCommand {
public static void register(CommandDispatcher<CommanderCommandSource> 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);
Expand All @@ -28,7 +28,7 @@ public static void register(CommandDispatcher<CommanderCommandSource> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public static void register(CommandDispatcher<CommanderCommandSource> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class EntitySelector {
private final @Nullable String playerName;
private final MinMaxBounds.Doubles distance;
private final Function<Vec3d, Vec3d> position;
private final AABB aABB;
private final @Nullable AABB aABB;

public EntitySelector(int maxResults, boolean includesEntities, BiConsumer<Entity, List<? extends Entity>> order, @Nullable Class<? extends Entity> limitToType, boolean typeInverse, boolean currentEntity, Predicate<Entity> predicate, @Nullable String entityId, @Nullable String playerName, MinMaxBounds.Doubles distance, Function<Vec3d, Vec3d> position, AABB aABB) {
public EntitySelector(int maxResults, boolean includesEntities, BiConsumer<Entity, List<? extends Entity>> order, @Nullable Class<? extends Entity> limitToType, boolean typeInverse, boolean currentEntity, Predicate<Entity> predicate, @Nullable String entityId, @Nullable String playerName, MinMaxBounds.Doubles distance, Function<Vec3d, Vec3d> position, @Nullable AABB aABB) {
this.maxResults = maxResults;
this.includesEntities = includesEntities;
this.order = order;
Expand Down Expand Up @@ -76,25 +76,24 @@ public List<? extends Entity> 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<? extends Entity> 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);
}
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<String> commandUsage = getCommandUsage(this.tablessCursor);
for (int i = 0; i < commandUsage.size(); i++) {
this.renderSingleSuggestionLine(this.mc.fontRenderer, "§8" + commandUsage.get(i), i, true);
}
}
}
}
Expand Down Expand Up @@ -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<String> getCommandUsage(int cursor) {
List<String> commandUsage = new ArrayList<>();
if (this.parseResults == null) return commandUsage;
for (Map.Entry<CommandNode<CommanderCommandSource>, 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 39f4226

Please sign in to comment.