Allow schematicannon to persist some safe block data

This commit is contained in:
Snownee 2020-12-11 20:03:16 +08:00
parent 550e058ec5
commit 517a4f0824
4 changed files with 67 additions and 10 deletions

View file

@ -109,7 +109,7 @@ public class AllTags {
} }
public static enum AllBlockTags { 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
; ;

View file

@ -19,6 +19,7 @@ import net.minecraft.nbt.NBTUtil;
import net.minecraft.particles.ParticleTypes; import net.minecraft.particles.ParticleTypes;
import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tags.FluidTags; import net.minecraft.tags.FluidTags;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction.Axis; import net.minecraft.util.Direction.Axis;
import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents; import net.minecraft.util.SoundEvents;
@ -26,6 +27,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.util.Constants;
public abstract class LaunchedItem { public abstract class LaunchedItem {
@ -90,18 +92,23 @@ public abstract class LaunchedItem {
public static class ForBlockState extends LaunchedItem { public static class ForBlockState extends LaunchedItem {
public BlockState state; public BlockState state;
public CompoundNBT data;
ForBlockState() {} 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); super(start, target, stack);
this.state = state; this.state = state;
this.data = data;
} }
@Override @Override
public CompoundNBT serializeNBT() { public CompoundNBT serializeNBT() {
CompoundNBT serializeNBT = super.serializeNBT(); CompoundNBT serializeNBT = super.serializeNBT();
serializeNBT.put("BlockState", NBTUtil.writeBlockState(state)); serializeNBT.put("BlockState", NBTUtil.writeBlockState(state));
if (data != null) {
serializeNBT.put("Data", data);
}
return serializeNBT; return serializeNBT;
} }
@ -109,6 +116,9 @@ public abstract class LaunchedItem {
void readNBT(CompoundNBT nbt) { void readNBT(CompoundNBT nbt) {
super.readNBT(nbt); super.readNBT(nbt);
state = NBTUtil.readBlockState(nbt.getCompound("BlockState")); state = NBTUtil.readBlockState(nbt.getCompound("BlockState"));
if (nbt.contains("Data", Constants.NBT.TAG_COMPOUND)) {
data = nbt.getCompound("Data");
}
} }
@Override @Override
@ -141,6 +151,12 @@ public abstract class LaunchedItem {
return; return;
} }
world.setBlockState(target, state, 18); 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); 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) { 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; this.length = length;
} }

View file

@ -3,9 +3,12 @@ package com.simibubi.create.content.schematics.block;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.annotation.Nullable;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllItems; import com.simibubi.create.AllItems;
import com.simibubi.create.AllSoundEvents; 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.BeltBlock;
import com.simibubi.create.content.contraptions.relays.belt.BeltPart; import com.simibubi.create.content.contraptions.relays.belt.BeltPart;
import com.simibubi.create.content.contraptions.relays.belt.BeltSlope; 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)) if (te instanceof BeltTileEntity && AllBlocks.BELT.has(blockState))
launchBelt(target, blockState, ((BeltTileEntity) te).beltLength); launchBelt(target, blockState, ((BeltTileEntity) te).beltLength);
else else
launchBlock(target, icon, blockState); launchBlock(target, icon, blockState, null);
} else } else {
launchBlock(target, icon, blockState); 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(); printerCooldown = config().schematicannonDelay.get();
fuelLevel -= getFuelUsageRate(); fuelLevel -= getFuelUsageRate();
@ -692,7 +703,7 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC
if (world == null) if (world == null)
return false; return false;
BlockState toReplace = world.getBlockState(pos); BlockState toReplace = world.getBlockState(pos);
boolean placingAir = state.getBlock() == Blocks.AIR; boolean placingAir = state.getBlock().isAir(state, world, pos);
BlockState toReplaceOther = null; BlockState toReplaceOther = null;
if (state.has(BlockStateProperties.BED_PART) && state.has(BlockStateProperties.HORIZONTAL_FACING) && state.get(BlockStateProperties.BED_PART) == BedPart.FOOT) 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(); playFiringSound();
} }
protected void launchBlock(BlockPos target, ItemStack stack, BlockState state) { protected void launchBlock(BlockPos target, ItemStack stack, BlockState state, @Nullable CompoundNBT data) {
if (state.getBlock() != Blocks.AIR) if (state.getBlock().isAir(state, world, target))
blocksPlaced++; blocksPlaced++;
flyingBlocks.add(new LaunchedItem.ForBlockState(this.getPos(), target, stack, state)); flyingBlocks.add(new LaunchedItem.ForBlockState(this.getPos(), target, stack, state, data));
playFiringSound(); playFiringSound();
} }

View file

@ -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": [
]
}