use of buildcraft.api.schematics.ISchematicBlock in project BuildCraft by BuildCraft.
the class TileReplacer method update.
@Override
public void update() {
if (world.isRemote) {
return;
}
if (!invSnapshot.getStackInSlot(0).isEmpty() && !invSchematicFrom.getStackInSlot(0).isEmpty() && !invSchematicTo.getStackInSlot(0).isEmpty()) {
Header header = BCBuildersItems.snapshot.getHeader(invSnapshot.getStackInSlot(0));
if (header != null) {
Snapshot snapshot = GlobalSavedDataSnapshots.get(world).getSnapshot(header.key);
if (snapshot instanceof Blueprint) {
Blueprint blueprint = (Blueprint) snapshot;
try {
ISchematicBlock from = SchematicBlockManager.readFromNBT(NBTUtilBC.getItemData(invSchematicFrom.getStackInSlot(0)).getCompoundTag(ItemSchematicSingle.NBT_KEY));
ISchematicBlock to = SchematicBlockManager.readFromNBT(NBTUtilBC.getItemData(invSchematicTo.getStackInSlot(0)).getCompoundTag(ItemSchematicSingle.NBT_KEY));
Blueprint newBlueprint = blueprint.copy();
newBlueprint.replace(from, to);
newBlueprint.computeKey();
GlobalSavedDataSnapshots.get(world).addSnapshot(newBlueprint);
invSnapshot.setStackInSlot(0, BCBuildersItems.snapshot.getUsed(EnumSnapshotType.BLUEPRINT, new Header(blueprint.key, getOwner().getId(), new Date(), header.name)));
invSchematicFrom.setStackInSlot(0, ItemStack.EMPTY);
invSchematicTo.setStackInSlot(0, ItemStack.EMPTY);
} catch (InvalidInputDataException e) {
e.printStackTrace();
}
}
}
}
}
use of buildcraft.api.schematics.ISchematicBlock in project BuildCraft by BuildCraft.
the class SchematicBlockManager method readFromNBT.
@Nonnull
public static ISchematicBlock readFromNBT(NBTTagCompound schematicBlockTag) throws InvalidInputDataException {
ResourceLocation name = new ResourceLocation(schematicBlockTag.getString("name"));
SchematicBlockFactory<?> factory = SchematicBlockFactoryRegistry.getFactoryByName(name);
if (factory == null) {
throw new InvalidInputDataException("Unknown schematic type " + name);
}
ISchematicBlock schematicBlock = factory.supplier.get();
NBTTagCompound data = schematicBlockTag.getCompoundTag("data");
try {
schematicBlock.deserializeNBT(data);
return schematicBlock;
} catch (InvalidInputDataException e) {
throw new InvalidInputDataException("Failed to load the schematic from " + data, e);
}
}
use of buildcraft.api.schematics.ISchematicBlock in project BuildCraft by BuildCraft.
the class SchematicBlockManager method getSchematicBlock.
@SuppressWarnings("WeakerAccess")
public static ISchematicBlock getSchematicBlock(SchematicBlockContext context) {
for (SchematicBlockFactory<?> schematicBlockFactory : Lists.reverse(SchematicBlockFactoryRegistry.getFactories())) {
if (schematicBlockFactory.predicate.test(context)) {
ISchematicBlock schematicBlock = schematicBlockFactory.supplier.get();
schematicBlock.init(context);
return schematicBlock;
}
}
throw new UnsupportedOperationException();
}
use of buildcraft.api.schematics.ISchematicBlock 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.schematics.ISchematicBlock in project BuildCraft by BuildCraft.
the class TileArchitectTable method scanSingleBlock.
private void scanSingleBlock() {
BlockPos size = box.size();
if (templateScannedBlocks == null || blueprintScannedData == null) {
boxIterator = new BoxIterator(box, EnumAxisOrder.XZY.getMinToMaxOrder(), true);
templateScannedBlocks = new BitSet(Snapshot.getDataSize(size));
blueprintScannedData = new int[Snapshot.getDataSize(size)];
}
// Read from world
BlockPos worldScanPos = boxIterator.getCurrent();
BlockPos schematicPos = worldScanPos.subtract(box.min());
if (snapshotType == EnumSnapshotType.TEMPLATE) {
templateScannedBlocks.set(Snapshot.posToIndex(box.size(), schematicPos), !world.isAirBlock(worldScanPos));
}
if (snapshotType == EnumSnapshotType.BLUEPRINT) {
ISchematicBlock schematicBlock = readSchematicBlock(worldScanPos);
int index = blueprintScannedPalette.indexOf(schematicBlock);
if (index == -1) {
index = blueprintScannedPalette.size();
blueprintScannedPalette.add(schematicBlock);
}
blueprintScannedData[Snapshot.posToIndex(box.size(), schematicPos)] = index;
}
createAndSendMessage(NET_SCAN, buffer -> MessageUtil.writeBlockPos(buffer, worldScanPos));
sendNetworkUpdate(NET_RENDER_DATA);
// Move scanPos along
boxIterator.advance();
if (boxIterator.hasFinished()) {
scanning = false;
boxIterator = null;
}
}
Aggregations