Search in sources :

Example 6 with MojangsonParseException

use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.

the class SummonCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!testPermission(sender))
        return true;
    if (sender instanceof ConsoleCommandSender) {
        sender.sendMessage("This command can only be executed by a player or via command blocks.");
        return true;
    }
    Location location = null;
    if (sender instanceof Player) {
        location = ((Player) sender).getLocation().clone();
    } else if (sender instanceof BlockCommandSender) {
        location = ((BlockCommandSender) sender).getBlock().getLocation().clone();
    }
    if (args.length == 0) {
        sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
        return false;
    }
    if (args.length >= 4) {
        location = CommandUtils.getLocation(location, args[1], args[2], args[3]);
    }
    if (location == null) {
        return false;
    }
    location.setYaw(0.0f);
    location.setPitch(0.0f);
    CompoundTag tag = null;
    if (args.length >= 5) {
        String data = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(4, args.length));
        try {
            tag = Mojangson.parseCompound(data);
        } catch (MojangsonParseException e) {
            sender.sendMessage(ChatColor.RED + "Invalid Data Tag: " + e.getMessage());
        }
    }
    String entityName = args[0];
    if (!checkSummon(sender, entityName)) {
        return true;
    }
    GlowEntity entity;
    if (EntityType.fromName(entityName) != null) {
        entity = (GlowEntity) location.getWorld().spawnEntity(location, EntityType.fromName(entityName));
    } else {
        Class<? extends GlowEntity> clazz = EntityRegistry.getCustomEntityDescriptor(entityName).getEntityClass();
        entity = ((GlowWorld) location.getWorld()).spawn(location, clazz, CreatureSpawnEvent.SpawnReason.CUSTOM);
    }
    if (tag != null) {
        EntityStorage.load(entity, tag);
    }
    sender.sendMessage("Object successfully summoned.");
    return true;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) GlowEntity(net.glowstone.entity.GlowEntity) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 7 with MojangsonParseException

use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.

the class SummonCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (sender instanceof ConsoleCommandSender) {
        commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL).send(sender);
        return true;
    }
    Location location = CommandUtils.getLocation(sender);
    if (args.length == 0) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    if (args.length >= 4) {
        location = CommandUtils.getLocation(location, args[1], args[2], args[3]);
    }
    location.setYaw(0.0f);
    location.setPitch(0.0f);
    CompoundTag tag = null;
    if (args.length >= 5) {
        String data = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(4, args.length));
        try {
            tag = Mojangson.parseCompound(data);
        } catch (MojangsonParseException e) {
            commandMessages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
        }
    }
    String entityName = args[0];
    if (!checkSummon(sender, entityName, commandMessages)) {
        return true;
    }
    GlowEntity entity;
    if (EntityType.fromName(entityName) != null) {
        entity = (GlowEntity) location.getWorld().spawnEntity(location, EntityType.fromName(entityName));
    } else {
        Class<? extends GlowEntity> clazz = EntityRegistry.getCustomEntityDescriptor(entityName).getEntityClass();
        entity = ((GlowWorld) location.getWorld()).spawn(location, clazz, CreatureSpawnEvent.SpawnReason.CUSTOM);
    }
    if (tag != null) {
        EntityStorage.load(entity, tag);
    }
    new LocalizedStringImpl("summon.done", commandMessages.getResourceBundle()).send(sender);
    return true;
}
Also used : LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) GlowEntity(net.glowstone.entity.GlowEntity) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 8 with MojangsonParseException

use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.

the class TestForBlockCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages messages) {
    if (!testPermission(sender, messages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 4) {
        sendUsageMessage(sender, messages);
        return false;
    }
    String itemName = CommandUtils.toNamespaced(args[3].toLowerCase());
    Material type = ItemIds.getBlock(itemName);
    ResourceBundle bundle = messages.getResourceBundle();
    if (type == null) {
        new LocalizedStringImpl("testforblock.invalid-block", bundle).sendInColor(ChatColor.RED, sender, itemName);
    }
    Location location = CommandUtils.getLocation(CommandUtils.getLocation(sender), args[0], args[1], args[2]);
    GlowBlock block = (GlowBlock) location.getBlock();
    if (block.getType() != type) {
        new LocalizedStringImpl("testforblock.wrong-block", bundle).sendInColor(ChatColor.RED, sender, location.getBlockX(), location.getBlockY(), location.getBlockZ(), ItemIds.getName(block.getType()), ItemIds.getName(type));
        return false;
    }
    if (args.length > 4) {
        String state = args[4];
        BlockStateData data = CommandUtils.readState(sender, block.getType(), state);
        if (data == null) {
            return false;
        }
        if (data.isNumeric() && block.getData() != data.getNumericValue()) {
            new LocalizedStringImpl("testforblock.wrong-data", bundle).sendInColor(ChatColor.RED, sender, location.getBlockX(), location.getBlockY(), location.getBlockZ(), block.getData(), data);
            return false;
        } else if (!data.isNumeric()) {
            try {
                boolean matches = StateSerialization.matches(block.getType(), block.getState().getData(), data);
                if (!matches) {
                    // TODO: Print the actual state of the block
                    new LocalizedStringImpl("testforblock.wrong-state", bundle).sendInColor(ChatColor.RED, sender, location.getBlockX(), location.getBlockY(), location.getBlockZ(), state);
                    return false;
                }
            } catch (InvalidBlockStateException e) {
                sender.sendMessage(ChatColor.RED + e.getMessage());
                return false;
            }
        }
    }
    if (args.length > 5 && block.getBlockEntity() != null) {
        String dataTag = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(5, args.length));
        try {
            CompoundTag tag = Mojangson.parseCompound(dataTag);
            CompoundTag blockTag = new CompoundTag();
            block.getBlockEntity().saveNbt(blockTag);
            if (!tag.matches(blockTag)) {
                new LocalizedStringImpl("testforblock.wrong-data", bundle).sendInColor(ChatColor.RED, sender, location.getBlockX(), location.getBlockY(), location.getBlockZ(), blockTag, tag);
                return false;
            }
        } catch (MojangsonParseException e) {
            messages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
            return false;
        }
    }
    // All is well
    new LocalizedStringImpl("testforblock.done", bundle).send(sender, location.getBlockX(), location.getBlockY(), location.getBlockZ());
    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) ResourceBundle(java.util.ResourceBundle) BlockStateData(net.glowstone.block.state.BlockStateData) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 9 with MojangsonParseException

use of net.glowstone.util.mojangson.ex.MojangsonParseException in project Glowstone by GlowstoneMC.

the class Mojangson method parseCompound.

/**
 * Parses a Compound from a Mojangson string as an NBT CompoundTag.
 *
 * @param mojangson The Mojangson string
 * @return the parsed CompoundTag NBT value
 * @throws MojangsonParseException if the Mojangson string could not be parsed as a
 *                                 Compound value.
 */
public static CompoundTag parseCompound(String mojangson) throws MojangsonParseException {
    // Parsing context magic value
    final int parseCompoundStart = 0;
    // Parsing context magic value
    final int parseCompoundPairKey = 1;
    // Parsing context magic value
    final int parseCompoundPairValue = 2;
    // The current context of the parser
    int context = parseCompoundStart;
    // Temporary key being parsed, in its raw form
    String tmpkey = "";
    // Temporary value
    String tmpval = "";
    int scope = // The scope level of the compound, this allows coherent nested arrays and
    0;
    // compounds.
    // The current character is part of a string inclusion
    boolean inString = false;
    CompoundTag tag = new CompoundTag();
    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 == parseCompoundStart) {
            if (character != COMPOUND_START.getSymbol()) {
                throw new MojangsonParseException("Index: " + index + ", symbol: \'" + character + "\'", MojangsonParseException.ParseExceptionReason.UNEXPECTED_SYMBOL);
            }
            context++;
            continue;
        }
        if (context == parseCompoundPairKey) {
            if (character == ELEMENT_PAIR_SEPERATOR.getSymbol() && scope <= 1) {
                context++;
                continue;
            }
            tmpkey += character;
            continue;
        }
        if (context == parseCompoundPairValue) {
            if ((character == ELEMENT_SEPERATOR.getSymbol() || character == COMPOUND_END.getSymbol()) && scope <= 1 && !inString) {
                context = parseCompoundPairKey;
                tag.getValue().put(tmpkey, parseTag(tmpval));
                tmpkey = tmpval = "";
                continue;
            }
            tmpval += character;
        }
    }
    return tag;
}
Also used : MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) CompoundTag(net.glowstone.util.nbt.CompoundTag)

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