Search in sources :

Example 1 with BlockStateData

use of net.glowstone.block.state.BlockStateData 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 2 with BlockStateData

use of net.glowstone.block.state.BlockStateData 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)

Aggregations

ArrayList (java.util.ArrayList)2 GlowBlock (net.glowstone.block.GlowBlock)2 BlockStateData (net.glowstone.block.state.BlockStateData)2 InvalidBlockStateException (net.glowstone.block.state.InvalidBlockStateException)2 LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)2 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 Location (org.bukkit.Location)2 Material (org.bukkit.Material)2 ResourceBundle (java.util.ResourceBundle)1