-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from ItsTheBdoge/master
Added command slot exception
- Loading branch information
Showing
4 changed files
with
126 additions
and
23 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
101 changes: 101 additions & 0 deletions
101
src/main/java/top/theillusivec4/curios/common/command/CuriosStringArgType.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,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; | ||
} | ||
} | ||
} |
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