use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.
the class Blueprint method deserializeNBT.
@Override
public void deserializeNBT(NBTTagCompound nbt) throws InvalidInputDataException {
super.deserializeNBT(nbt);
palette.clear();
for (NBTTagCompound schematicBlockTag : NBTUtilBC.readCompoundList(nbt.getTag("palette")).collect(Collectors.toList())) {
// TODO: Allow reading blueprints partially - invalid elements should be replaced with air
// (Although this needs to add a "pass-through" ISchematicBlock that will store the
// invalid NBTTagCompound and show up in the tooltip as an error, so that we can migrate
// schematics through mod additions/deletions)
palette.add(SchematicBlockManager.readFromNBT(schematicBlockTag));
}
data = new int[Snapshot.getDataSize(size)];
NBTTagList serializedDataList = nbt.hasKey("data", Constants.NBT.TAG_LIST) ? nbt.getTagList("data", Constants.NBT.TAG_INT) : null;
int[] serializedDataIntArray = nbt.hasKey("data", Constants.NBT.TAG_INT_ARRAY) ? nbt.getIntArray("data") : null;
if (serializedDataIntArray == null && serializedDataList == null) {
throw new InvalidInputDataException("Can't read a blueprint with no data!");
}
int serializedDataLength = serializedDataList == null ? serializedDataIntArray.length : serializedDataList.tagCount();
if (serializedDataLength != getDataSize()) {
throw new InvalidInputDataException("Serialized data has length of " + serializedDataLength + ", but we expected " + getDataSize() + " (" + size.toString() + ")");
}
for (int z = 0; z < size.getZ(); z++) {
for (int y = 0; y < size.getY(); y++) {
for (int x = 0; x < size.getX(); x++) {
data[posToIndex(x, y, z)] = serializedDataList == null ? serializedDataIntArray[posToIndex(x, y, z)] : serializedDataList.getIntAt(posToIndex(x, y, z));
}
}
}
for (NBTTagCompound schematicEntityTag : NBTUtilBC.readCompoundList(nbt.getTag("entities")).collect(Collectors.toList())) {
entities.add(SchematicEntityManager.readFromNBT(schematicEntityTag));
}
}
use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.
the class NbtSquisher method expand.
public static NBTTagCompound expand(InputStream stream) throws IOException {
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
stream.mark(5);
int byte1 = stream.read();
int byte2 = stream.read();
if (byte1 == NbtSquishConstants.BUILDCRAFT_MAGIC_1 && byte2 == NbtSquishConstants.BUILDCRAFT_MAGIC_2) {
// Defiantly a BC stream
int type = stream.read();
if (type == TYPE_MC) {
return CompressedStreamTools.read(new DataInputStream(stream));
} else if (type == TYPE_MC_GZIP) {
return CompressedStreamTools.readCompressed(stream);
} else if (type == TYPE_BC_1) {
return readBuildCraftV1Direct(new DataInputStream(stream));
} else if (type == TYPE_BC_1_GZIP) {
return readBuildCraftV1Direct(new DataInputStream(new GZIPInputStream(stream)));
} else {
throw new InvalidInputDataException("Cannot handle BuildCraft saved NBT type " + type);
}
} else if (byte1 == NbtSquishConstants.GZIP_MAGIC_1 && byte2 == NbtSquishConstants.GZIP_MAGIC_2) {
// Defiantly a GZIP stream
// Assume its a vanilla file
stream.reset();
return CompressedStreamTools.readCompressed(stream);
}
// Its not a new BC style nbt, try to red it as if it was an older style nbt
// Reset + mark the same point, this time we only want to reset back 1 or 2 bytes
stream.reset();
stream.mark(5);
int type = stream.read();
if (type == TYPE_MC) {
return CompressedStreamTools.read(new DataInputStream(stream));
} else if (type == TYPE_MC_GZIP) {
return CompressedStreamTools.readCompressed(stream);
} else if (type == TYPE_BC_1) {
return readBuildCraftV1Direct(new DataInputStream(stream));
} else if (type == TYPE_BC_1_GZIP) {
return readBuildCraftV1Direct(new DataInputStream(new GZIPInputStream(stream)));
} else if (type == Constants.NBT.TAG_COMPOUND) {
// Assume vanilla, but reset back to the first byte as vanilla needs
stream.reset();
return CompressedStreamTools.read(new DataInputStream(stream));
} else {
throw new InvalidInputDataException("Cannot handle unknown saved NBT type " + type);
}
}
use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.
the class ItemSchematicSingle method onItemUseFirst.
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
if (world.isRemote) {
return EnumActionResult.PASS;
}
ItemStack stack = player.getHeldItem(hand);
if (player.isSneaking()) {
NBTTagCompound itemData = NBTUtilBC.getItemData(StackUtil.asNonNull(stack));
itemData.removeTag(NBT_KEY);
if (itemData.hasNoTags()) {
stack.setTagCompound(null);
}
stack.setItemDamage(DAMAGE_CLEAN);
return EnumActionResult.SUCCESS;
}
int damage = stack.getItemDamage();
if (damage != DAMAGE_USED) {
IBlockState state = world.getBlockState(pos);
ISchematicBlock schematicBlock = SchematicBlockManager.getSchematicBlock(new SchematicBlockContext(world, pos, pos, state, state.getBlock()));
if (schematicBlock.isAir()) {
return EnumActionResult.FAIL;
}
NBTUtilBC.getItemData(stack).setTag(NBT_KEY, SchematicBlockManager.writeToNBT(schematicBlock));
stack.setItemDamage(DAMAGE_USED);
return EnumActionResult.SUCCESS;
} else {
BlockPos placePos = pos;
boolean replaceable = world.getBlockState(pos).getBlock().isReplaceable(world, pos);
if (!replaceable) {
placePos = placePos.offset(side);
}
if (!world.mayPlace(world.getBlockState(pos).getBlock(), placePos, false, side, null)) {
return EnumActionResult.FAIL;
}
if (replaceable && !world.isAirBlock(placePos)) {
world.setBlockToAir(placePos);
}
try {
ISchematicBlock schematicBlock = getSchematic(stack);
if (schematicBlock != null) {
if (!schematicBlock.isBuilt(world, placePos) && schematicBlock.canBuild(world, placePos)) {
List<FluidStack> requiredFluids = schematicBlock.computeRequiredFluids();
List<ItemStack> requiredItems = schematicBlock.computeRequiredItems();
if (requiredFluids.isEmpty()) {
InventoryWrapper itemTransactor = new InventoryWrapper(player.inventory);
if (StackUtil.mergeSameItems(requiredItems).stream().noneMatch(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), true).isEmpty())) {
if (schematicBlock.build(world, placePos)) {
StackUtil.mergeSameItems(requiredItems).forEach(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), false));
SoundUtil.playBlockPlace(world, placePos);
player.swingArm(hand);
return EnumActionResult.SUCCESS;
}
} else {
player.sendStatusMessage(new TextComponentString("Not enough items. Total needed: " + StackUtil.mergeSameItems(requiredItems).stream().map(s -> s.getTextComponent().getFormattedText() + " x " + s.getCount()).collect(Collectors.joining(", "))), true);
}
} else {
player.sendStatusMessage(new TextComponentString("Schematic requires fluids"), true);
}
}
}
} catch (InvalidInputDataException e) {
player.sendStatusMessage(new TextComponentString("Invalid schematic: " + e.getMessage()), true);
e.printStackTrace();
}
return EnumActionResult.FAIL;
}
}
use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.
the class SchematicEntityManager method readFromNBT.
@Nonnull
public static ISchematicEntity readFromNBT(NBTTagCompound schematicEntityTag) throws InvalidInputDataException {
ResourceLocation name = new ResourceLocation(schematicEntityTag.getString("name"));
SchematicEntityFactory<?> factory = SchematicEntityFactoryRegistry.getFactoryByName(name);
if (factory == null) {
throw new InvalidInputDataException("Unknown schematic type " + name);
}
ISchematicEntity schematicEntity = factory.supplier.get();
NBTTagCompound data = schematicEntityTag.getCompoundTag("data");
try {
schematicEntity.deserializeNBT(data);
return schematicEntity;
} catch (InvalidInputDataException e) {
throw new InvalidInputDataException("Failed to load the schematic from " + data, e);
}
}
use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.
the class Snapshot method readFromNBT.
public static Snapshot readFromNBT(NBTTagCompound nbt) throws InvalidInputDataException {
NBTBase tag = nbt.getTag("type");
EnumSnapshotType type = NBTUtilBC.readEnum(tag, EnumSnapshotType.class);
if (type == null) {
throw new InvalidInputDataException("Unknown snapshot type " + tag);
}
Snapshot snapshot = Snapshot.create(type);
snapshot.deserializeNBT(nbt);
return snapshot;
}
Aggregations