use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.
the class MojangsonParseTest method canParseType.
@Test
public void canParseType() {
try {
CompoundTag compound = Mojangson.parseCompound(testCase.getValue());
Tag value = compound.getValue().get("value");
// Checks if the TagType of the case and the parsed type are equal.
if (value.getType() != testCase.getKey()) {
fail("Incorrect type parsing for case " + testCase.getKey().getName() + " (Got " + value.getType().getName() + ") for Mojansgon: " + testCase.getValue());
}
} catch (MojangsonParseException e) {
// Catches a parse failure.
fail("Could not parse case for " + testCase.getKey().getName() + "( " + testCase.getValue() + "): " + e.getMessage());
}
}
use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.
the class MojangsonParseTest method canParseType.
@MethodSource("getCases")
@ParameterizedTest
public void canParseType(TagType key, String json) {
try {
CompoundTag compound = Mojangson.parseCompound(json);
Tag value = compound.getValue().get("value");
// Checks if the TagType of the case and the parsed type are equal.
if (value.getType() != key) {
fail("Incorrect type parsing for case " + key.getName() + " (Got " + value.getType().getName() + ") for Mojansgon: " + json);
}
} catch (MojangsonParseException e) {
// Catches a parse failure.
fail("Could not parse case for " + key.getName() + "( " + json + "): " + e.getMessage());
}
}
use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.
the class SetBlockCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length < 4) {
sendUsageMessage(sender, commandMessages);
return false;
}
String itemName = CommandUtils.toNamespaced(args[3].toLowerCase());
Material type = ItemIds.getBlock(itemName);
if (type == null) {
new LocalizedStringImpl("setblock.invalid.type", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, itemName);
return false;
}
Location location = CommandUtils.getLocation(CommandUtils.getLocation(sender), args[0], args[1], args[2]);
GlowBlock block = (GlowBlock) location.getBlock();
byte dataValue = 0;
if (args.length > 4) {
String state = args[4];
BlockStateData data = CommandUtils.readState(sender, type, state);
if (data == null) {
return false;
}
if (data.isNumeric()) {
dataValue = data.getNumericValue();
} else {
try {
dataValue = StateSerialization.parseData(type, data).getData();
} catch (InvalidBlockStateException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
return false;
}
}
}
block.setType(type, dataValue, true);
if (args.length > 5 && block.getBlockEntity() != null) {
String dataTag = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(5, args.length));
try {
CompoundTag prev = new CompoundTag();
block.getBlockEntity().saveNbt(prev);
CompoundTag tag = Mojangson.parseCompound(dataTag);
tag.mergeInto(prev, true);
block.getBlockEntity().loadNbt(prev);
} catch (MojangsonParseException e) {
commandMessages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
return false;
}
}
new LocalizedStringImpl("setblock.done", commandMessages.getResourceBundle()).send(sender);
return true;
}
use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.
the class TestForCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length < 1) {
sendUsageMessage(sender, commandMessages);
return false;
}
String name = args[0];
Entity[] entities;
if (name.startsWith("@")) {
CommandTarget target = new CommandTarget(sender, name);
entities = target.getMatched(CommandUtils.getLocation(sender));
if (entities.length == 0) {
commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, name);
return false;
}
} else {
// TODO: Select custom-named non-player entities?
GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
if (player == null) {
commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
return false;
} else {
entities = new Entity[] { player };
}
}
LocalizedStringImpl foundMessage = new LocalizedStringImpl("testfor.found", commandMessages.getResourceBundle());
if (args.length >= 2) {
String data = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
CompoundTag tag;
try {
tag = Mojangson.parseCompound(data);
} catch (MojangsonParseException e) {
commandMessages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
return false;
}
LocalizedStringImpl wrongDataMessage = new LocalizedStringImpl("testfor.wrong-data", commandMessages.getResourceBundle());
for (Entity entity : entities) {
if (entity instanceof GlowEntity) {
CompoundTag entityTag = new CompoundTag();
EntityStorage.save((GlowEntity) entity, entityTag);
if (tag.matches(entityTag)) {
foundMessage.send(sender, CommandUtils.getName(entity));
} else {
wrongDataMessage.sendInColor(ChatColor.RED, sender, CommandUtils.getName(entity));
}
}
}
} else {
for (Entity entity : entities) {
foundMessage.send(sender, CommandUtils.getName(entity));
}
}
// matching entities.
return true;
}
use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.
the class Mojangson method parseArray.
/**
* Parses an Array value from a Mojangson string.
*
* @param mojangson The Mojangson string
* @return a ByteArrayTag value if the array contains byte values, an IntArrayTag value if the
* array contains int values or a ListTag with the array's elements.
* @throws MojangsonParseException if the Mojangson string could not be parsed as an
* Array value.
*/
public static Tag parseArray(String mojangson) throws MojangsonParseException {
// Parsing context magic value
final int parseArrayStart = 0;
// Parsing context magic value
final int parseArrayElement = 1;
// The current context of the parser
int context = parseArrayStart;
// Temporary value being parsed, in its raw form
String tmpval = "";
// The scope level of the array, this allows coherent nested arrays and compounds.
int scope = 0;
// The current character is part of a string inclusion
boolean inString = false;
// The element content type.
TagType tagType = null;
List<Tag> values = new ArrayList<>();
for (int index = 0; index < mojangson.length(); index++) {
char character = mojangson.charAt(index);
if (character == STRING_QUOTES.getSymbol()) {
inString = !inString;
}
if (character == WHITE_SPACE.getSymbol()) {
if (!inString) {
continue;
}
}
if ((character == COMPOUND_START.getSymbol() || character == ARRAY_START.getSymbol()) && !inString) {
scope++;
}
if ((character == COMPOUND_END.getSymbol() || character == ARRAY_END.getSymbol()) && !inString) {
scope--;
}
if (context == parseArrayStart) {
if (character != ARRAY_START.getSymbol()) {
throw new MojangsonParseException("Index: " + index + ", symbol: \'" + character + "\'", MojangsonParseException.ParseExceptionReason.UNEXPECTED_SYMBOL);
}
context++;
continue;
}
if (context == parseArrayElement) {
if ((character == ELEMENT_SEPERATOR.getSymbol() || character == ARRAY_END.getSymbol()) && scope <= 1 && !inString) {
if (tmpval.length() == 0) {
continue;
}
Tag val = parseTag(tmpval);
if (tagType == null) {
tagType = val.getType();
} else if (tagType != val.getType()) {
throw new MojangsonParseException("Index: " + index + ", value: \'" + tmpval + "\'", MojangsonParseException.ParseExceptionReason.INCOMPATIBLE_TYPE);
}
values.add(val);
tmpval = "";
continue;
}
tmpval += character;
}
}
if (tagType == TagType.BYTE) {
byte[] bytes = new byte[values.size()];
for (int i = 0; i < values.size(); i++) {
bytes[i] = (byte) values.get(i).getValue();
}
return new ByteArrayTag(bytes);
} else if (tagType == TagType.INT) {
int[] ints = new int[values.size()];
for (int i = 0; i < values.size(); i++) {
ints[i] = (int) values.get(i).getValue();
}
return new IntArrayTag(ints);
} else {
return new ListTag<>(tagType, values);
}
}
Aggregations