From 517a4f08243c641168b3fa5eb02cf37186e125fa Mon Sep 17 00:00:00 2001 From: Snownee Date: Fri, 11 Dec 2020 20:03:16 +0800 Subject: [PATCH] Allow schematicannon to persist some safe block data --- .../java/com/simibubi/create/AllTags.java | 2 +- .../schematics/block/LaunchedItem.java | 20 +++++++++++-- .../block/SchematicannonTileEntity.java | 25 +++++++++++----- .../data/create/tags/blocks/safe_nbt.json | 30 +++++++++++++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 src/main/resources/data/create/tags/blocks/safe_nbt.json diff --git a/src/main/java/com/simibubi/create/AllTags.java b/src/main/java/com/simibubi/create/AllTags.java index b486accd5..039c576ad 100644 --- a/src/main/java/com/simibubi/create/AllTags.java +++ b/src/main/java/com/simibubi/create/AllTags.java @@ -109,7 +109,7 @@ public class AllTags { } public static enum AllBlockTags { - WINDMILL_SAILS, FAN_HEATERS, WINDOWABLE, NON_MOVABLE, BRITTLE, SEATS, SAILS, VALVE_HANDLES, FAN_TRANSPARENT + WINDMILL_SAILS, FAN_HEATERS, WINDOWABLE, NON_MOVABLE, BRITTLE, SEATS, SAILS, VALVE_HANDLES, FAN_TRANSPARENT, SAFE_NBT ; diff --git a/src/main/java/com/simibubi/create/content/schematics/block/LaunchedItem.java b/src/main/java/com/simibubi/create/content/schematics/block/LaunchedItem.java index efebcdcd7..075a01513 100644 --- a/src/main/java/com/simibubi/create/content/schematics/block/LaunchedItem.java +++ b/src/main/java/com/simibubi/create/content/schematics/block/LaunchedItem.java @@ -19,6 +19,7 @@ import net.minecraft.nbt.NBTUtil; import net.minecraft.particles.ParticleTypes; import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.tags.FluidTags; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Direction.Axis; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvents; @@ -26,6 +27,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; import net.minecraftforge.common.IPlantable; +import net.minecraftforge.common.util.Constants; public abstract class LaunchedItem { @@ -90,18 +92,23 @@ public abstract class LaunchedItem { public static class ForBlockState extends LaunchedItem { public BlockState state; + public CompoundNBT data; ForBlockState() {} - public ForBlockState(BlockPos start, BlockPos target, ItemStack stack, BlockState state) { + public ForBlockState(BlockPos start, BlockPos target, ItemStack stack, BlockState state, CompoundNBT data) { super(start, target, stack); this.state = state; + this.data = data; } @Override public CompoundNBT serializeNBT() { CompoundNBT serializeNBT = super.serializeNBT(); serializeNBT.put("BlockState", NBTUtil.writeBlockState(state)); + if (data != null) { + serializeNBT.put("Data", data); + } return serializeNBT; } @@ -109,6 +116,9 @@ public abstract class LaunchedItem { void readNBT(CompoundNBT nbt) { super.readNBT(nbt); state = NBTUtil.readBlockState(nbt.getCompound("BlockState")); + if (nbt.contains("Data", Constants.NBT.TAG_COMPOUND)) { + data = nbt.getCompound("Data"); + } } @Override @@ -141,6 +151,12 @@ public abstract class LaunchedItem { return; } world.setBlockState(target, state, 18); + if (data != null) { + TileEntity tile = world.getTileEntity(target); + if (tile != null) { + tile.read(data); + } + } state.getBlock().onBlockPlacedBy(world, target, state, null, stack); } @@ -165,7 +181,7 @@ public abstract class LaunchedItem { } public ForBelt(BlockPos start, BlockPos target, ItemStack stack, BlockState state, int length) { - super(start, target, stack, state); + super(start, target, stack, state, null); this.length = length; } diff --git a/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java b/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java index e6a4eb1a9..872b36696 100644 --- a/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java +++ b/src/main/java/com/simibubi/create/content/schematics/block/SchematicannonTileEntity.java @@ -3,9 +3,12 @@ package com.simibubi.create.content.schematics.block; import java.util.LinkedList; import java.util.List; +import javax.annotation.Nullable; + import com.simibubi.create.AllBlocks; import com.simibubi.create.AllItems; import com.simibubi.create.AllSoundEvents; +import com.simibubi.create.AllTags.AllBlockTags; import com.simibubi.create.content.contraptions.relays.belt.BeltBlock; import com.simibubi.create.content.contraptions.relays.belt.BeltPart; import com.simibubi.create.content.contraptions.relays.belt.BeltSlope; @@ -458,9 +461,17 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC if (te instanceof BeltTileEntity && AllBlocks.BELT.has(blockState)) launchBelt(target, blockState, ((BeltTileEntity) te).beltLength); else - launchBlock(target, icon, blockState); - } else - launchBlock(target, icon, blockState); + launchBlock(target, icon, blockState, null); + } else { + CompoundNBT data = null; + if (AllBlockTags.SAFE_NBT.matches(blockState)) { + TileEntity tile = blockReader.getTileEntity(target); + if (tile != null && !tile.onlyOpsCanSetNbt()) { + data = tile.write(new CompoundNBT()); + } + } + launchBlock(target, icon, blockState, data); + } printerCooldown = config().schematicannonDelay.get(); fuelLevel -= getFuelUsageRate(); @@ -692,7 +703,7 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC if (world == null) return false; BlockState toReplace = world.getBlockState(pos); - boolean placingAir = state.getBlock() == Blocks.AIR; + boolean placingAir = state.getBlock().isAir(state, world, pos); BlockState toReplaceOther = null; if (state.has(BlockStateProperties.BED_PART) && state.has(BlockStateProperties.HORIZONTAL_FACING) && state.get(BlockStateProperties.BED_PART) == BedPart.FOOT) @@ -828,10 +839,10 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC playFiringSound(); } - protected void launchBlock(BlockPos target, ItemStack stack, BlockState state) { - if (state.getBlock() != Blocks.AIR) + protected void launchBlock(BlockPos target, ItemStack stack, BlockState state, @Nullable CompoundNBT data) { + if (state.getBlock().isAir(state, world, target)) blocksPlaced++; - flyingBlocks.add(new LaunchedItem.ForBlockState(this.getPos(), target, stack, state)); + flyingBlocks.add(new LaunchedItem.ForBlockState(this.getPos(), target, stack, state, data)); playFiringSound(); } diff --git a/src/main/resources/data/create/tags/blocks/safe_nbt.json b/src/main/resources/data/create/tags/blocks/safe_nbt.json new file mode 100644 index 000000000..d00a30821 --- /dev/null +++ b/src/main/resources/data/create/tags/blocks/safe_nbt.json @@ -0,0 +1,30 @@ +{ + "replace": false, + "values": [ + "#signs", + "create:linear_chassis", + "create:secondary_linear_chassis", + "create:radial_chassis", + "create:redstone_link", + "create:windmill_bearing", + "create:mechanical_bearing", + "create:clockwork_bearing", + "create:mechanical_piston", + "create:sticky_mechanical_piston", + "create:rope_pulley", + "create:cart_assembler", + "create:sequenced_gearshift", + "create:creative_motor", + "create:creative_fluid_tank", + "create:creative_crate", + "create:adjustable_repeater", + "create:adjustable_pulse_repeater", + "create:analog_lever", + "create:brass_funnel", + "create:brass_belt_funnel", + "create:andesite_funnel", + "create:andesite_belt_funnel" + ], + "optional": [ + ] +} \ No newline at end of file