From 0941d9403fbe871c7069005f78cf115f00cd48eb Mon Sep 17 00:00:00 2001 From: Snownee Date: Sat, 12 Dec 2020 19:09:10 +0800 Subject: [PATCH] Zappers Safe-NBT support --- .../zapper/ZapperInteractionHandler.java | 27 ++++++++++++++++--- .../curiosities/zapper/ZapperItem.java | 24 ++++++++++++++--- .../zapper/blockzapper/BlockzapperItem.java | 4 +-- .../zapper/terrainzapper/TerrainTools.java | 8 +++++- .../zapper/terrainzapper/WorldshaperItem.java | 4 +-- .../schematics/block/LaunchedItem.java | 7 +++++ .../behaviour/linked/LinkBehaviour.java | 15 +++++------ 7 files changed, 70 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java b/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java index ccc010a83..edd0e003f 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java +++ b/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperInteractionHandler.java @@ -1,6 +1,9 @@ package com.simibubi.create.content.curiosities.zapper; +import java.util.Objects; + import com.simibubi.create.AllSoundEvents; +import com.simibubi.create.AllTags.AllBlockTags; import com.simibubi.create.foundation.networking.AllPackets; import com.simibubi.create.foundation.networking.NbtPacket; import com.simibubi.create.foundation.utility.BlockHelper; @@ -12,6 +15,7 @@ import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.NBTUtil; import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.state.properties.StairsShape; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResultType; import net.minecraft.util.Hand; import net.minecraft.util.SoundCategory; @@ -68,7 +72,7 @@ public class ZapperInteractionHandler { if (BlockHelper.getRequiredItem(newState) .isEmpty()) return false; - if (player.world.getTileEntity(pos) != null) + if (newState.hasTileEntity() && !AllBlockTags.SAFE_NBT.matches(newState)) return false; if (newState.has(BlockStateProperties.DOUBLE_BLOCK_HALF)) return false; @@ -85,14 +89,31 @@ public class ZapperInteractionHandler { if (newState.has(BlockStateProperties.WATERLOGGED)) newState = newState.with(BlockStateProperties.WATERLOGGED, false); + CompoundNBT data = null; + TileEntity tile = player.world.getTileEntity(pos); + if (tile != null) { + data = tile.write(new CompoundNBT()); + data.remove("x"); + data.remove("y"); + data.remove("z"); + data.remove("id"); + } CompoundNBT tag = stack.getOrCreateTag(); - if (tag.contains("BlockUsed") && NBTUtil.readBlockState(stack.getTag() - .getCompound("BlockUsed")) == newState) + if (tag.contains("BlockUsed") + && NBTUtil.readBlockState( + stack.getTag().getCompound("BlockUsed")) == newState + && Objects.equals(data, tag.get("BlockData"))) { return false; + } tag.put("BlockUsed", NBTUtil.writeBlockState(newState)); + if (data == null) + tag.remove("BlockData"); + else + tag.put("BlockData", data); player.world.playSound(player, player.getPosition(), AllSoundEvents.BLOCKZAPPER_CONFIRM.get(), SoundCategory.BLOCKS, 0.5f, 0.8f); + return true; } diff --git a/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperItem.java b/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperItem.java index 37253ceea..7dfb7dc06 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperItem.java +++ b/src/main/java/com/simibubi/create/content/curiosities/zapper/ZapperItem.java @@ -5,6 +5,7 @@ import java.util.List; import javax.annotation.Nonnull; import com.simibubi.create.AllSoundEvents; +import com.simibubi.create.AllTags.AllBlockTags; import com.simibubi.create.foundation.item.ItemDescription; import com.simibubi.create.foundation.networking.AllPackets; import com.simibubi.create.foundation.utility.BlockHelper; @@ -23,6 +24,7 @@ import net.minecraft.item.Rarity; import net.minecraft.item.UseAction; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.NBTUtil; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResultType; import net.minecraft.util.Hand; @@ -35,12 +37,12 @@ import net.minecraft.util.math.RayTraceContext.BlockMode; import net.minecraft.util.math.RayTraceContext.FluidMode; import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.ITextComponent; -import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.World; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.network.PacketDistributor; @@ -149,6 +151,10 @@ public abstract class ZapperItem extends Item { if (nbt.contains("BlockUsed")) stateToUse = NBTUtil.readBlockState(nbt.getCompound("BlockUsed")); stateToUse = BlockHelper.setZeroAge(stateToUse); + CompoundNBT data = null; + if (AllBlockTags.SAFE_NBT.matches(stateToUse) && nbt.contains("BlockData", NBT.TAG_COMPOUND)) { + data = nbt.getCompound("BlockData"); + } // Raytrace - Find the target Vec3d start = player.getPositionVec() @@ -181,7 +187,7 @@ public abstract class ZapperItem extends Item { } // Server side - if (activate(world, player, item, stateToUse, raytrace)) { + if (activate(world, player, item, stateToUse, raytrace, data)) { applyCooldown(player, item, gunInOtherHand); AllPackets.channel.send(PacketDistributor.TRACKING_ENTITY.with(() -> player), new ZapperBeamPacket(barrelPos, raytrace.getHitVec(), hand, false)); @@ -200,7 +206,7 @@ public abstract class ZapperItem extends Item { } protected abstract boolean activate(World world, PlayerEntity player, ItemStack item, BlockState stateToUse, - BlockRayTraceResult raytrace); + BlockRayTraceResult raytrace, CompoundNBT data); @OnlyIn(Dist.CLIENT) protected abstract void openHandgunGUI(ItemStack item, boolean b); @@ -234,4 +240,16 @@ public abstract class ZapperItem extends Item { return UseAction.NONE; } + public static void setTileData(World world, BlockPos pos, CompoundNBT data) { + if (data != null) { + TileEntity tile = world.getTileEntity(pos); + if (tile != null && !tile.onlyOpsCanSetNbt()) { + data.putInt("x", pos.getX()); + data.putInt("y", pos.getY()); + data.putInt("z", pos.getZ()); + tile.read(data); + } + } + } + } diff --git a/src/main/java/com/simibubi/create/content/curiosities/zapper/blockzapper/BlockzapperItem.java b/src/main/java/com/simibubi/create/content/curiosities/zapper/blockzapper/BlockzapperItem.java index e6051d6de..5e27ad20c 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/zapper/blockzapper/BlockzapperItem.java +++ b/src/main/java/com/simibubi/create/content/curiosities/zapper/blockzapper/BlockzapperItem.java @@ -22,7 +22,6 @@ import com.simibubi.create.foundation.utility.NBTHelper; import net.minecraft.advancements.CriteriaTriggers; import net.minecraft.block.Block; import net.minecraft.block.BlockState; -import net.minecraft.block.RedstoneLampBlock; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.Entity; @@ -97,7 +96,7 @@ public class BlockzapperItem extends ZapperItem { @Override protected boolean activate(World world, PlayerEntity player, ItemStack stack, BlockState selectedState, - BlockRayTraceResult raytrace) { + BlockRayTraceResult raytrace, CompoundNBT data) { CompoundNBT nbt = stack.getOrCreateTag(); boolean replace = nbt.contains("Replace") && nbt.getBoolean("Replace"); @@ -136,6 +135,7 @@ public class BlockzapperItem extends ZapperItem { blocksnapshot.restore(true, false); return false; } + setTileData(world, placed, data); if (player instanceof ServerPlayerEntity && world instanceof ServerWorld) { ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player; diff --git a/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/TerrainTools.java b/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/TerrainTools.java index ad9602f1e..51401b113 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/TerrainTools.java +++ b/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/TerrainTools.java @@ -4,11 +4,13 @@ import java.util.List; import javax.annotation.Nullable; +import com.simibubi.create.content.curiosities.zapper.ZapperItem; import com.simibubi.create.foundation.gui.AllIcons; import com.simibubi.create.foundation.utility.Lang; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -34,7 +36,7 @@ public enum TerrainTools { return this != Clear && this != Flatten; } - public void run(World world, List targetPositions, Direction facing, @Nullable BlockState paintedState) { + public void run(World world, List targetPositions, Direction facing, @Nullable BlockState paintedState, @Nullable CompoundNBT data) { switch (this) { case Clear: targetPositions.forEach(p -> world.setBlockState(p, Blocks.AIR.getDefaultState())); @@ -45,6 +47,7 @@ public enum TerrainTools { if (!isReplaceable(toReplace)) return; world.setBlockState(p, paintedState); + ZapperItem.setTileData(world, p, data); }); break; case Flatten: @@ -64,11 +67,13 @@ public enum TerrainTools { if (!isReplaceable(toReplace)) return; world.setBlockState(p, paintedState); + ZapperItem.setTileData(world, p, data); }); break; case Place: targetPositions.forEach(p -> { world.setBlockState(p, paintedState); + ZapperItem.setTileData(world, p, data); }); break; case Replace: @@ -77,6 +82,7 @@ public enum TerrainTools { if (isReplaceable(toReplace)) return; world.setBlockState(p, paintedState); + ZapperItem.setTileData(world, p, data); }); break; } diff --git a/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItem.java b/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItem.java index 7c7b91e72..9f2e4d019 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItem.java +++ b/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItem.java @@ -60,7 +60,7 @@ public class WorldshaperItem extends ZapperItem { @Override protected boolean activate(World world, PlayerEntity player, ItemStack stack, BlockState stateToUse, - BlockRayTraceResult raytrace) { + BlockRayTraceResult raytrace, CompoundNBT data) { BlockPos targetPos = raytrace.getPos(); List affectedPositions = new ArrayList<>(); @@ -77,7 +77,7 @@ public class WorldshaperItem extends ZapperItem { for (BlockPos blockPos : brush.getIncludedPositions()) affectedPositions.add(targetPos.add(blockPos)); PlacementPatterns.applyPattern(affectedPositions, stack); - tool.run(world, affectedPositions, raytrace.getFace(), stateToUse); + tool.run(world, affectedPositions, raytrace.getFace(), stateToUse, data); return true; } 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 075a01513..5502356af 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 @@ -107,6 +107,10 @@ public abstract class LaunchedItem { CompoundNBT serializeNBT = super.serializeNBT(); serializeNBT.put("BlockState", NBTUtil.writeBlockState(state)); if (data != null) { + data.remove("x"); + data.remove("y"); + data.remove("z"); + data.remove("id"); serializeNBT.put("Data", data); } return serializeNBT; @@ -154,6 +158,9 @@ public abstract class LaunchedItem { if (data != null) { TileEntity tile = world.getTileEntity(target); if (tile != null) { + data.putInt("x", target.getX()); + data.putInt("y", target.getY()); + data.putInt("z", target.getZ()); tile.read(data); } } diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java index 651fd7828..871c126ce 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/linked/LinkBehaviour.java @@ -1,9 +1,8 @@ package com.simibubi.create.foundation.tileEntity.behaviour.linked; -import java.util.function.Consumer; import java.util.function.Function; -import java.util.function.Supplier; - +import java.util.function.IntConsumer; +import java.util.function.IntSupplier; import org.apache.commons.lang3.tuple.Pair; import com.simibubi.create.Create; @@ -35,8 +34,8 @@ public class LinkBehaviour extends TileEntityBehaviour { public boolean newPosition; private Mode mode; - private Supplier transmission; - private Consumer signalCallback; + private IntSupplier transmission; + private IntConsumer signalCallback; protected LinkBehaviour(SmartTileEntity te, Pair slots) { super(te); @@ -49,7 +48,7 @@ public class LinkBehaviour extends TileEntityBehaviour { } public static LinkBehaviour receiver(SmartTileEntity te, Pair slots, - Consumer signalCallback) { + IntConsumer signalCallback) { LinkBehaviour behaviour = new LinkBehaviour(te, slots); behaviour.signalCallback = signalCallback; behaviour.mode = Mode.RECEIVE; @@ -57,7 +56,7 @@ public class LinkBehaviour extends TileEntityBehaviour { } public static LinkBehaviour transmitter(SmartTileEntity te, Pair slots, - Supplier transmission) { + IntSupplier transmission) { LinkBehaviour behaviour = new LinkBehaviour(te, slots); behaviour.transmission = transmission; behaviour.mode = Mode.TRANSMIT; @@ -81,7 +80,7 @@ public class LinkBehaviour extends TileEntityBehaviour { } public int getTransmittedStrength() { - return mode == Mode.TRANSMIT ? transmission.get() : 0; + return mode == Mode.TRANSMIT ? transmission.getAsInt() : 0; } public void updateReceiver(int networkPower) {