Search in sources :

Example 1 with MojangsonParseException

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());
    }
}
Also used : MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) CompoundTag(net.glowstone.util.nbt.CompoundTag) Tag(net.glowstone.util.nbt.Tag) CompoundTag(net.glowstone.util.nbt.CompoundTag) Test(org.junit.Test)

Example 2 with MojangsonParseException

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());
    }
}
Also used : MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) CompoundTag(net.glowstone.util.nbt.CompoundTag) Tag(net.glowstone.util.nbt.Tag) CompoundTag(net.glowstone.util.nbt.CompoundTag) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with MojangsonParseException

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;
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) InvalidBlockStateException(net.glowstone.block.state.InvalidBlockStateException) Material(org.bukkit.Material) BlockStateData(net.glowstone.block.state.BlockStateData) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 4 with MojangsonParseException

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;
}
Also used : Entity(org.bukkit.entity.Entity) GlowEntity(net.glowstone.entity.GlowEntity) CommandTarget(net.glowstone.command.CommandTarget) GlowPlayer(net.glowstone.entity.GlowPlayer) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) GlowEntity(net.glowstone.entity.GlowEntity) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 5 with MojangsonParseException

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);
    }
}
Also used : IntArrayTag(net.glowstone.util.nbt.IntArrayTag) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) TagType(net.glowstone.util.nbt.TagType) CompoundTag(net.glowstone.util.nbt.CompoundTag) ByteArrayTag(net.glowstone.util.nbt.ByteArrayTag) ListTag(net.glowstone.util.nbt.ListTag) FloatTag(net.glowstone.util.nbt.FloatTag) StringTag(net.glowstone.util.nbt.StringTag) DoubleTag(net.glowstone.util.nbt.DoubleTag) IntArrayTag(net.glowstone.util.nbt.IntArrayTag) LongArrayTag(net.glowstone.util.nbt.LongArrayTag) IntTag(net.glowstone.util.nbt.IntTag) LongTag(net.glowstone.util.nbt.LongTag) ShortTag(net.glowstone.util.nbt.ShortTag) ByteTag(net.glowstone.util.nbt.ByteTag) Tag(net.glowstone.util.nbt.Tag) ByteArrayTag(net.glowstone.util.nbt.ByteArrayTag)

Aggregations

MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)9 CompoundTag (net.glowstone.util.nbt.CompoundTag)9 ArrayList (java.util.ArrayList)5 LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)4 Location (org.bukkit.Location)4 GlowEntity (net.glowstone.entity.GlowEntity)3 Tag (net.glowstone.util.nbt.Tag)3 GlowBlock (net.glowstone.block.GlowBlock)2 BlockStateData (net.glowstone.block.state.BlockStateData)2 InvalidBlockStateException (net.glowstone.block.state.InvalidBlockStateException)2 Material (org.bukkit.Material)2 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)2 ResourceBundle (java.util.ResourceBundle)1 CommandTarget (net.glowstone.command.CommandTarget)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 ByteArrayTag (net.glowstone.util.nbt.ByteArrayTag)1 ByteTag (net.glowstone.util.nbt.ByteTag)1 DoubleTag (net.glowstone.util.nbt.DoubleTag)1 FloatTag (net.glowstone.util.nbt.FloatTag)1 IntArrayTag (net.glowstone.util.nbt.IntArrayTag)1