use of net.glowstone.block.state.InvalidBlockStateException 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.block.state.InvalidBlockStateException in project Glowstone by GlowstoneMC.
the class WoolStateDataReader method read.
@Override
public Wool read(Material material, BlockStateData data) throws InvalidBlockStateException {
Wool wool = new Wool();
if (data.contains("color")) {
DyeColor color = StateSerialization.getColor(data.get("color"));
if (color == null) {
throw new InvalidBlockStateException(material, data);
}
wool.setColor(color);
} else {
wool.setColor(DyeColor.WHITE);
}
return wool;
}
use of net.glowstone.block.state.InvalidBlockStateException 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;
}
Aggregations