-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more argument types and increased max suggestions
- Loading branch information
1 parent
0285c42
commit 9ec131e
Showing
26 changed files
with
1,020 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/net/pedroricardo/commander/content/arguments/BlockArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
src/main/java/net/pedroricardo/commander/content/arguments/BlockCoordinatesArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/net/pedroricardo/commander/content/arguments/EntityArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.