Skip to content

Commit

Permalink
Merge pull request #29 from ItsTheBdoge/master
Browse files Browse the repository at this point in the history
Added command slot exception
  • Loading branch information
TheIllusiveC4 authored Oct 31, 2019
2 parents 76c0615 + 07cae45 commit cf465de
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/main/java/top/theillusivec4/curios/Curios.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import top.theillusivec4.curios.client.gui.CuriosScreen;
import top.theillusivec4.curios.client.gui.GuiEventHandler;
import top.theillusivec4.curios.client.render.CuriosLayer;
import top.theillusivec4.curios.common.CommandCurios;
import top.theillusivec4.curios.common.command.CommandCurios;
import top.theillusivec4.curios.common.CuriosConfig;
import top.theillusivec4.curios.common.CuriosIMC;
import top.theillusivec4.curios.common.CuriosRegistry;
Expand Down Expand Up @@ -116,7 +116,7 @@ private void enqueue(InterModEnqueueEvent evt) {
send(CuriosAPI.IMC.REGISTER_ICON, new Tuple<>(icon,
new ResourceLocation(MODID, "textures/item/empty_" + icon + "_slot.png")));
}
}
}

private void process(InterModProcessEvent evt) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
* License along with Curios. If not, see <https://www.gnu.org/licenses/>.
*/

package top.theillusivec4.curios.common;
package top.theillusivec4.curios.common.command;

import com.google.common.collect.Maps;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import java.util.SortedMap;
import net.minecraft.command.CommandSource;
Expand Down Expand Up @@ -51,75 +50,75 @@ public static void register(CommandDispatcher<CommandSource> dispatcher) {
.requires(player -> player.hasPermissionLevel(opPermissionLevel));

curiosCommand.then(Commands.literal("set")
.then(Commands.argument("slot", StringArgumentType.string())
.then(Commands.argument("slot", CuriosStringArgType.string())
.suggests((ctx, builder) -> ISuggestionProvider
.suggest(CuriosAPI.getTypeIdentifiers(), builder))
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> setSlotsOfPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"), 1))
CuriosStringArgType.getString(context, "slot"), 1))
.then(Commands.argument("amount", IntegerArgumentType.integer())
.executes(context -> setSlotsOfPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"),
CuriosStringArgType.getString(context, "slot"),
IntegerArgumentType.getInteger(context, "amount")))))));

curiosCommand.then(Commands.literal("add")
.then(Commands.argument("slot", StringArgumentType.string())
.then(Commands.argument("slot", CuriosStringArgType.string())
.suggests((ctx, builder) -> ISuggestionProvider
.suggest(CuriosAPI.getTypeIdentifiers(), builder))
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> addSlotToPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"), 1))
CuriosStringArgType.getString(context, "slot"), 1))
.then(Commands.argument("amount", IntegerArgumentType.integer())
.executes(context -> addSlotToPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"),
CuriosStringArgType.getString(context, "slot"),
IntegerArgumentType.getInteger(context, "amount")))))));

curiosCommand.then(Commands.literal("remove")
.then(Commands.argument("slot", StringArgumentType.string())
.then(Commands.argument("slot", CuriosStringArgType.string())
.suggests((ctx, builder) -> ISuggestionProvider
.suggest(CuriosAPI.getTypeIdentifiers(), builder))
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> removeSlotFromPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"), 1))
CuriosStringArgType.getString(context, "slot"), 1))
.then(Commands.argument("amount", IntegerArgumentType.integer())
.executes(context -> removeSlotFromPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"),
CuriosStringArgType.getString(context, "slot"),
IntegerArgumentType.getInteger(context, "amount")))))));

curiosCommand.then(Commands.literal("enable")
.then(Commands.argument("slot", StringArgumentType.string())
.then(Commands.argument("slot", CuriosStringArgType.string())
.suggests((ctx, builder) -> ISuggestionProvider
.suggest(CuriosAPI.getTypeIdentifiers(), builder))
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> enableSlotForPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"))))));
CuriosStringArgType.getString(context, "slot"))))));

curiosCommand.then(Commands.literal("disable")
.then(Commands.argument("slot", StringArgumentType.string())
.then(Commands.argument("slot", CuriosStringArgType.string())
.suggests((ctx, builder) -> ISuggestionProvider
.suggest(CuriosAPI.getTypeIdentifiers(), builder))
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> disableSlotForPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"))))));
CuriosStringArgType.getString(context, "slot"))))));

curiosCommand.then(Commands.literal("clear")
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> clearSlotsForPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"), ""))
.then(Commands.argument("slot", StringArgumentType.string())
.then(Commands.argument("slot", CuriosStringArgType.string())
.suggests((ctx, builder) -> ISuggestionProvider
.suggest(CuriosAPI.getTypeIdentifiers(), builder))
.executes(context -> clearSlotsForPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
StringArgumentType.getString(context, "slot"))))));
CuriosStringArgType.getString(context, "slot"))))));

curiosCommand.then(Commands.literal("reset")
.then(Commands.argument("player", EntityArgument.player())
Expand All @@ -131,10 +130,10 @@ public static void register(CommandDispatcher<CommandSource> dispatcher) {

private static int setSlotsOfPlayer(CommandSource source, ServerPlayerEntity playerMP, String slot, int amount)
{
CuriosAPI.setSlotsForType(slot, playerMP, amount);
source.sendFeedback(new TranslationTextComponent("commands.curios.set.success", slot, CuriosAPI.getSlotsForType(playerMP, slot),
playerMP.getDisplayName()), true);
return Command.SINGLE_SUCCESS;
CuriosAPI.setSlotsForType(slot, playerMP, amount);
source.sendFeedback(new TranslationTextComponent("commands.curios.set.success", slot, CuriosAPI.getSlotsForType(playerMP, slot),
playerMP.getDisplayName()), true);
return Command.SINGLE_SUCCESS;
}

private static int addSlotToPlayer(CommandSource source, ServerPlayerEntity playerMP, String slot,
Expand Down Expand Up @@ -218,6 +217,8 @@ private static int resetSlotsForPlayer(CommandSource source, ServerPlayerEntity
return Command.SINGLE_SUCCESS;
}

private static boolean doesSlotExist(String identifier) { return CuriosAPI.getTypeIdentifiers().contains(identifier); }

private static void clear(CurioStackHandler stacks) {

for (int i = 0; i < stacks.getSlots(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package top.theillusivec4.curios.common.command;

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 net.minecraft.util.text.TranslationTextComponent;
import top.theillusivec4.curios.api.CuriosAPI;

import java.util.Arrays;
import java.util.Collection;

public class CuriosStringArgType implements ArgumentType<String>
{
public static final SimpleCommandExceptionType SLOT_NOT_EXIST = new SimpleCommandExceptionType(new TranslationTextComponent("arguments.curios.slot_not_exist"));
private final StringType type;

private CuriosStringArgType(final StringType type) {
this.type = type;
}

public static CuriosStringArgType string() {
return new CuriosStringArgType(StringType.QUOTABLE_PHRASE);
}

public static String getString(final CommandContext<?> context, final String name) throws CommandSyntaxException
{
String val = context.getArgument(name, String.class);
if(!CuriosAPI.getTypeIdentifiers().contains(val))
throw SLOT_NOT_EXIST.create();
return val;
}

public StringType getType() {
return type;
}

@Override
public String parse(final StringReader reader) throws CommandSyntaxException {
if (type == StringType.GREEDY_PHRASE) {
final String text = reader.getRemaining();
reader.setCursor(reader.getTotalLength());
return text;
} else if (type == StringType.SINGLE_WORD) {
return reader.readUnquotedString();
} else {
return reader.readString();
}
}

@Override
public String toString() {
return "string()";
}

@Override
public Collection<String> getExamples() {
return type.getExamples();
}

public static String escapeIfRequired(final String input) {
for (final char c : input.toCharArray()) {
if (!StringReader.isAllowedInUnquotedString(c)) {
return escape(input);
}
}
return input;
}

private static String escape(final String input) {
final StringBuilder result = new StringBuilder("\"");

for (int i = 0; i < input.length(); i++) {
final char c = input.charAt(i);
if (c == '\\' || c == '"') {
result.append('\\');
}
result.append(c);
}

result.append("\"");
return result.toString();
}

public enum StringType {
SINGLE_WORD("word", "words_with_underscores"),
QUOTABLE_PHRASE("\"quoted phrase\"", "word", "\"\""),
GREEDY_PHRASE("word", "words with spaces", "\"and symbols\""),;

private final Collection<String> examples;

StringType(final String... examples) {
this.examples = Arrays.asList(examples);
}

public Collection<String> getExamples() {
return examples;
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/curios/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"commands.curios.clear.success": "Slot %s has been cleared for %s",
"commands.curios.clearAll.success": "All slots have been cleared for %s",
"commands.curios.reset.success": "Reset slots for %s",
"arguments.curios.slot_not_exist": "Slot does not exist!",
"item.curios.amulet": "Curious Amulet",
"item.curios.ring": "Curious Ring",
"item.curios.crown": "Curious Crown",
Expand Down

0 comments on commit cf465de

Please sign in to comment.