Skip to content

Commit

Permalink
Added more argument types and increased max suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Sep 18, 2023
1 parent 0285c42 commit 9ec131e
Show file tree
Hide file tree
Showing 26 changed files with 1,020 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/pedroricardo/commander/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Commander implements ModInitializer {
public static final String MOD_ID = "commander";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static int maxSuggestions = 4;
public static int maxSuggestions = 6;
public static boolean suggestionsFollowParameters = true;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static List<Suggestion> getLegacySuggestionList(String message, int curso
for (Command command : Commands.commands) {
List<String> path = new ArrayList<>();
path.add(command.getName());
if (CommanderCommandManager.getDispatcher().findNode(path) == null && command.getName().startsWith(textBeforeCursor.substring(1))) {
if (CommanderCommandManager.getDispatcher().findNode(path) == null && command.getName().startsWith(textBeforeCursor.substring(1)) && !command.getName().equals(textBeforeCursor.substring(1))) {
list.add(new Suggestion(new StringRange(1, 1 + command.getName().length()), command.getName()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import net.minecraft.client.Minecraft;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.net.command.ClientCommandHandler;
import net.minecraft.core.net.command.ClientPlayerCommandSender;
import net.minecraft.core.net.command.CommandHandler;
import net.minecraft.core.net.command.CommandSender;
import net.minecraft.core.util.phys.Vec3d;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -37,4 +43,9 @@ public EntityPlayer getSender() {
public boolean hasAdmin() {
return true;
}

@Override
public @Nullable Vec3d getCoordinates() {
return this.getSender().getPosition(1.0f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.pedroricardo.commander.content.commands.AchievementCommand;
import net.pedroricardo.commander.content.commands.TestCommand;
import net.minecraft.core.achievement.Achievement;
import net.minecraft.core.net.command.*;
import net.pedroricardo.commander.Commander;
import net.pedroricardo.commander.content.commands.*;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;

public class CommanderCommandManager {
public static int SINGLE_SUCCESS = 1;
private static final CommandDispatcher<CommanderCommandSource> DISPATCHER = new CommandDispatcher<>();

static {
AchievementCommand.register(DISPATCHER);
SummonCommand.register(DISPATCHER);
SetBlockCommand.register(DISPATCHER);
KillCommand.register(DISPATCHER);

TestCommand.register(DISPATCHER);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.pedroricardo.commander.content;

import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.net.command.CommandHandler;
import net.minecraft.core.net.command.CommandSender;
import net.minecraft.core.util.phys.Vec3d;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -21,4 +25,6 @@ default Collection<String> getEntitySuggestions() {
EntityPlayer getSender();

boolean hasAdmin();

@Nullable Vec3d getCoordinates();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package net.pedroricardo.commander.content;

import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.net.command.CommandHandler;
import net.minecraft.core.net.command.CommandSender;
import net.minecraft.core.net.command.ServerCommandHandler;
import net.minecraft.core.net.command.ServerPlayerCommandSender;
import net.minecraft.core.util.phys.Vec3d;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.entity.player.EntityPlayerMP;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -40,4 +46,9 @@ public EntityPlayer getSender() {
public boolean hasAdmin() {
return false;
}

@Override
public @Nullable Vec3d getCoordinates() {
return this.getSender().getPosition(1.0f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ public Achievement parse(StringReader reader) throws CommandSyntaxException {

for (Achievement achievement : AchievementList.achievementList) {
if (((StatNameAccessor)achievement).statName().equals(string)) {
reader.skip();
return achievement;
}
}
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKeyAndFormat("commands.commander.achievement.invalid_achievement", string));
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.achievement.invalid_achievement"));
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
for (Achievement achievement : AchievementList.achievementList) {
if (((StatNameAccessor)achievement).statName().startsWith(builder.getRemaining())) {
builder.suggest(((StatNameAccessor)achievement).statName());
builder.suggest(((StatNameAccessor)achievement).statName(), achievement::getStatName);
}
}
return builder.buildFuture();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.pedroricardo.commander.content.arguments;

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 net.minecraft.core.block.Block;
import net.minecraft.core.entity.Entity;
import net.minecraft.core.entity.EntityDispatcher;
import net.minecraft.core.lang.I18n;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class BlockArgumentType implements ArgumentType<Block> {
private static final List<String> EXAMPLES = Arrays.asList("tile.stone", "stone", "dirt");

public static ArgumentType<Block> block() {
return new BlockArgumentType();
}

@Override
public Block parse(StringReader reader) throws CommandSyntaxException {
final String string = reader.readString();

for (Block block : Block.blocksList) {
if (block == null) continue;
if (block.getKey().equals(string) || (block.getKey().startsWith("tile.") && block.getKey().substring("tile.".length()).equals(string))) {
return block;
}
}
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.block.invalid_block"));
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
String remaining = builder.getRemaining();
for (Block block : Block.blocksList) {
if (block == null) continue;
if ("tile.".startsWith(remaining) || remaining.startsWith("tile.")) {
if (block.getKey().startsWith(builder.getRemaining())) {
builder.suggest(block.getKey(), () -> I18n.getInstance().translateKey(block.getLanguageKey(0) + ".name"));
}
} else {
if (block.getKey().startsWith("tile.") && block.getKey().substring("tile.".length()).startsWith(builder.getRemaining())) {
builder.suggest(block.getKey().substring("tile.".length()), () -> I18n.getInstance().translateKey(block.getLanguageKey(0) + ".name"));
}
}
}
return builder.buildFuture();
}

@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package net.pedroricardo.commander.content.arguments;

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 net.minecraft.core.util.phys.Vec3d;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.exceptions.CommanderExceptions;
import net.pedroricardo.commander.content.helpers.BlockCoordinate;
import net.pedroricardo.commander.content.helpers.BlockCoordinates;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class BlockCoordinatesArgumentType implements ArgumentType<BlockCoordinates> {
private static final List<String> EXAMPLES = Arrays.asList("~ ~ ~", "0 0 0", "~ ~60 ~", "~-20 ~10 -25");

public static BlockCoordinatesArgumentType blockCoordinates() {
return new BlockCoordinatesArgumentType();
}

@Override
public BlockCoordinates parse(StringReader reader) throws CommandSyntaxException {
int i = reader.getCursor();
BlockCoordinate x = BlockCoordinate.parse(reader);
if (!reader.canRead() || reader.peek() != ' ') {
if (reader.peek() == 'f' || reader.peek() == 'd') {
reader.skip();
if (!reader.canRead() || reader.peek() != ' ') {
reader.setCursor(i);
throw CommanderExceptions.incomplete().createWithContext(reader);
}
} else {
reader.setCursor(i);
throw CommanderExceptions.incomplete().createWithContext(reader);
}
}
reader.skip();
BlockCoordinate y = BlockCoordinate.parse(reader);
if (!reader.canRead() || reader.peek() != ' ') {
if (reader.peek() == 'f' || reader.peek() == 'd') {
reader.skip();
if (!reader.canRead() || reader.peek() != ' ') {
reader.setCursor(i);
throw CommanderExceptions.incomplete().createWithContext(reader);
}
} else {
reader.setCursor(i);
throw CommanderExceptions.incomplete().createWithContext(reader);
}
}
reader.skip();
BlockCoordinate z = BlockCoordinate.parse(reader);
return new BlockCoordinates(x, y, z);
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
String string = builder.getRemaining();
Vec3d coordinates = ((CommanderCommandSource)context.getSource()).getCoordinates();

if (coordinates == null) return builder.buildFuture();

// Rounding the coordinates
int[] roundedCoordinates = new int[]{(int)Math.floor(coordinates.xCoord), (int)Math.floor(coordinates.yCoord - 1.6), (int)Math.floor(coordinates.zCoord)};

if (string.isEmpty()) {
String allCoordinates = roundedCoordinates[0] + " " + roundedCoordinates[1] + " " + roundedCoordinates[2];
try {
this.parse(new StringReader(allCoordinates));
builder.suggest(String.valueOf(roundedCoordinates[0]));
builder.suggest(roundedCoordinates[0] + " " + roundedCoordinates[1]);
builder.suggest(allCoordinates);
} catch (CommandSyntaxException ignored) {}
} else {
String[] strings = string.split(" ");
String allCoordinates;
switch (strings.length) {
case 1:
allCoordinates = strings[0] + " " + roundedCoordinates[1] + " " + roundedCoordinates[2];
try {
this.parse(new StringReader(allCoordinates));
builder.suggest(strings[0] + " " + roundedCoordinates[1]);
builder.suggest(allCoordinates);
} catch (CommandSyntaxException ignored) {}
break;
case 2:
allCoordinates = strings[0] + " " + strings[1] + " " + roundedCoordinates[2];
try {
this.parse(new StringReader(allCoordinates));
builder.suggest(allCoordinates);
} catch (CommandSyntaxException ignored) {}
break;
}
}
return builder.buildFuture();
}

@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package net.pedroricardo.commander.content.arguments;

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 net.minecraft.core.entity.Entity;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.lang.I18n;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.exceptions.CommanderExceptions;
import net.pedroricardo.commander.content.helpers.EntitySelector;
import net.pedroricardo.commander.content.helpers.EntitySelectorParser;

import java.util.concurrent.CompletableFuture;

public class EntityArgumentType implements ArgumentType<EntitySelector> {
private final boolean singleEntity, playerOnly;

private EntityArgumentType(boolean singleEntity, boolean playerOnly) {
this.singleEntity = singleEntity;
this.playerOnly = playerOnly;
}

public static EntityArgumentType entities() {
return new EntityArgumentType(false, false);
}

public static EntityArgumentType entity() {
return new EntityArgumentType(true, false);
}

public static EntityArgumentType players() {
return new EntityArgumentType(false, true);
}

public static EntityArgumentType player() {
return new EntityArgumentType(true, true);
}

@Override
public EntitySelector parse(StringReader reader) throws CommandSyntaxException {
int cursor = reader.getCursor();
EntitySelectorParser entitySelectorParser = new EntitySelectorParser(reader);
if (reader.canRead() && reader.peek() == '@') {
EntitySelector entitySelector = entitySelectorParser.parse();
if (this.singleEntity && entitySelector.getMaxResults() > 1) {
reader.setCursor(cursor);
if (this.playerOnly) {
throw CommanderExceptions.singlePlayerOnly().createWithContext(reader);
}
throw CommanderExceptions.singleEntityOnly().createWithContext(reader);
}
if (this.playerOnly && entitySelector.includesEntities() && !entitySelector.isCurrentEntity()) {
reader.setCursor(cursor);
throw CommanderExceptions.playerOnly().createWithContext(reader);
}
return entitySelector;
}

throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader);
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
if ("@p".startsWith(builder.getRemainingLowerCase()))
builder.suggest("@p", () -> I18n.getInstance().translateKey("argument_types.commander.entity.selector.nearest_player"));
if ("@a".startsWith(builder.getRemainingLowerCase()))
builder.suggest("@a", () -> I18n.getInstance().translateKey("argument_types.commander.entity.selector.all_players"));
if ("@r".startsWith(builder.getRemainingLowerCase()))
builder.suggest("@r", () -> I18n.getInstance().translateKey("argument_types.commander.entity.selector.random_player"));
if ("@s".startsWith(builder.getRemainingLowerCase()))
builder.suggest("@s", () -> I18n.getInstance().translateKey("argument_types.commander.entity.selector.self"));
if ("@e".startsWith(builder.getRemainingLowerCase()))
builder.suggest("@e", () -> I18n.getInstance().translateKey("argument_types.commander.entity.selector.all_entities"));
for (String name : ((CommanderCommandSource)context.getSource()).getEntitySuggestions()) {
if (name.startsWith(builder.getRemaining())) {
builder.suggest(name);
}
}
return builder.buildFuture();
}
}
Loading

0 comments on commit 9ec131e

Please sign in to comment.