From c2cc12423920e06761038a6f644695c562252850 Mon Sep 17 00:00:00 2001 From: reidbhuntley Date: Fri, 21 May 2021 20:33:28 -0400 Subject: [PATCH] Better support for partially safe NBT writing --- .../block/mechanicalArm/ArmTileEntity.java | 46 +++++++++++-------- .../block/SchematicannonTileEntity.java | 12 ++--- .../tileEntity/SmartTileEntity.java | 12 ++++- .../tileEntity/TileEntityBehaviour.java | 3 +- .../filtering/FilteringBehaviour.java | 3 ++ .../filtering/SidedFilteringBehaviour.java | 3 ++ .../behaviour/linked/LinkBehaviour.java | 3 ++ .../scrollvalue/ScrollValueBehaviour.java | 11 +++-- .../behaviour/simple/DeferralBehaviour.java | 5 +- .../foundation/utility/IPartialSafeNBT.java | 7 +++ 10 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/utility/IPartialSafeNBT.java diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java index ae0a584e3..0229882b3 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmTileEntity.java @@ -122,14 +122,14 @@ public class ArmTileEntity extends KineticTileEntity { } if (world.isRemote) return; - + if (phase == Phase.MOVE_TO_INPUT) collectItem(); else if (phase == Phase.MOVE_TO_OUTPUT) depositItem(); else if (phase == Phase.SEARCH_INPUTS || phase == Phase.DANCING) searchForItem(); - + if (targetReached) lazyTick(); } @@ -142,7 +142,7 @@ public class ArmTileEntity extends KineticTileEntity { return; if (chasedPointProgress < .5f) return; - if (phase == Phase.SEARCH_INPUTS || phase == Phase.DANCING) + if (phase == Phase.SEARCH_INPUTS || phase == Phase.DANCING) checkForMusic(); if (phase == Phase.SEARCH_OUTPUTS) searchForDestination(); @@ -175,7 +175,7 @@ public class ArmTileEntity extends KineticTileEntity { } private boolean tickMovementProgress() { - boolean targetReachedPreviously = chasedPointProgress >= 1; + boolean targetReachedPreviously = chasedPointProgress >= 1; chasedPointProgress += Math.min(256, Math.abs(getSpeed())) / 1024f; if (chasedPointProgress > 1) chasedPointProgress = 1; @@ -349,7 +349,7 @@ public class ArmTileEntity extends KineticTileEntity { chasedPointIndex = -1; sendData(); markDirty(); - + if (!prevHeld.isItemEqual(heldItem)) world.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, .125f, .5f + Create.random.nextFloat() * .25f); @@ -416,23 +416,26 @@ public class ArmTileEntity extends KineticTileEntity { markDirty(); } + public void writeInteractionPoints(CompoundNBT compound) { + if (updateInteractionPoints) { + compound.put("InteractionPoints", interactionPointTag); + } else { + ListNBT pointsNBT = new ListNBT(); + inputs.stream() + .map(aip -> aip.serialize(pos)) + .forEach(pointsNBT::add); + outputs.stream() + .map(aip -> aip.serialize(pos)) + .forEach(pointsNBT::add); + compound.put("InteractionPoints", pointsNBT); + } + } + @Override public void write(CompoundNBT compound, boolean clientPacket) { super.write(compound, clientPacket); - if (updateInteractionPoints) { - compound.put("InteractionPoints", interactionPointTag); - - } else { - ListNBT pointsNBT = new ListNBT(); - inputs.stream() - .map(aip -> aip.serialize(pos)) - .forEach(pointsNBT::add); - outputs.stream() - .map(aip -> aip.serialize(pos)) - .forEach(pointsNBT::add); - compound.put("InteractionPoints", pointsNBT); - } + writeInteractionPoints(compound); NBTHelper.writeEnum(compound, "Phase", phase); compound.putBoolean("Powered", redstoneLocked); @@ -441,6 +444,13 @@ public class ArmTileEntity extends KineticTileEntity { compound.putFloat("MovementProgress", chasedPointProgress); } + @Override + public void writeSafe(CompoundNBT compound, boolean clientPacket) { + super.writeSafe(compound, clientPacket); + + writeInteractionPoints(compound); + } + @Override protected void fromTag(BlockState state, CompoundNBT compound, boolean clientPacket) { int previousIndex = chasedPointIndex; 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 289408314..ef443d62d 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 @@ -31,6 +31,7 @@ import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; import com.simibubi.create.foundation.tileEntity.behaviour.BehaviourType; import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour; import com.simibubi.create.foundation.utility.BlockHelper; +import com.simibubi.create.foundation.utility.IPartialSafeNBT; import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.NBTProcessors; @@ -502,13 +503,10 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC if (AllBlockTags.SAFE_NBT.matches(blockState)) { data = tile.write(new CompoundNBT()); data = NBTProcessors.process(tile, data, true); - } else if (tile instanceof SmartTileEntity) { - FilteringBehaviour filtering = ((SmartTileEntity)tile).getBehaviour(FilteringBehaviour.TYPE); - if (filtering != null) { - data = new CompoundNBT(); - filtering.write(data, false); - data = NBTProcessors.process(tile, data, true); - } + } else if (tile instanceof IPartialSafeNBT) { + data = new CompoundNBT(); + ((IPartialSafeNBT) tile).writeSafe(data, false); + data = NBTProcessors.process(tile, data, true); } } diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/SmartTileEntity.java b/src/main/java/com/simibubi/create/foundation/tileEntity/SmartTileEntity.java index a589bc7ca..e036d7dab 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/SmartTileEntity.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/SmartTileEntity.java @@ -8,6 +8,8 @@ import java.util.function.Consumer; import com.simibubi.create.foundation.tileEntity.behaviour.BehaviourType; +import com.simibubi.create.foundation.utility.IPartialSafeNBT; + import net.minecraft.block.BlockState; import net.minecraft.nbt.CompoundNBT; import net.minecraft.tileentity.ITickableTileEntity; @@ -16,7 +18,7 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.items.CapabilityItemHandler; -public abstract class SmartTileEntity extends SyncedTileEntity implements ITickableTileEntity { +public abstract class SmartTileEntity extends SyncedTileEntity implements ITickableTileEntity, IPartialSafeNBT { private final Map, TileEntityBehaviour> behaviours; // Internally maintained to be identical to behaviorMap.values() in order to improve iteration performance. @@ -118,6 +120,14 @@ public abstract class SmartTileEntity extends SyncedTileEntity implements ITicka behaviourList.forEach(tb -> tb.write(compound, clientPacket)); } + public void writeSafe(CompoundNBT compound, boolean clientPacket) { + super.write(compound); + behaviourList.forEach(tb -> { + if (tb.isSafeNBT()) + tb.write(compound, clientPacket); + }); + } + @Override public void remove() { forEachBehaviour(TileEntityBehaviour::remove); diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java index 925677123..931b5f4bd 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/TileEntityBehaviour.java @@ -42,6 +42,8 @@ public abstract class TileEntityBehaviour { } + public boolean isSafeNBT() { return false; } + public void onBlockChanged(BlockState oldState) { } @@ -94,5 +96,4 @@ public abstract class TileEntityBehaviour { SmartTileEntity ste = (SmartTileEntity) te; return ste.getBehaviour(type); } - } diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/FilteringBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/FilteringBehaviour.java index 803476b13..c6b7acebe 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/FilteringBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/FilteringBehaviour.java @@ -57,6 +57,9 @@ public class FilteringBehaviour extends TileEntityBehaviour { fluidFilter = false; } + @Override + public boolean isSafeNBT() { return true; } + @Override public void write(CompoundNBT nbt, boolean clientPacket) { nbt.put("Filter", getFilter().serializeNBT()); diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/SidedFilteringBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/SidedFilteringBehaviour.java index 88097a681..9a7f0a490 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/SidedFilteringBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/filtering/SidedFilteringBehaviour.java @@ -58,6 +58,9 @@ public class SidedFilteringBehaviour extends FilteringBehaviour { removeFilter(d); } + @Override + public boolean isSafeNBT() { return true; } + @Override public void write(CompoundNBT nbt, boolean clientPacket) { nbt.put("Filters", NBTHelper.writeCompoundList(sidedFilters.entrySet(), entry -> { 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 3d708e8f1..27d529ca7 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 @@ -115,6 +115,9 @@ public class LinkBehaviour extends TileEntityBehaviour { getHandler().removeFromNetwork(this); } + @Override + public boolean isSafeNBT() { return true; } + @Override public void write(CompoundNBT nbt, boolean clientPacket) { super.write(nbt, clientPacket); diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/scrollvalue/ScrollValueBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/scrollvalue/ScrollValueBehaviour.java index c67fc80e0..d189a689d 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/scrollvalue/ScrollValueBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/scrollvalue/ScrollValueBehaviour.java @@ -54,6 +54,9 @@ public class ScrollValueBehaviour extends TileEntityBehaviour { ticksUntilScrollPacket = -1; } + @Override + public boolean isSafeNBT() { return true; } + @Override public void write(CompoundNBT nbt, boolean clientPacket) { nbt.putInt("ScrollValue", value); @@ -95,7 +98,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour { clientCallback = valueCallback; return this; } - + public ScrollValueBehaviour withCallback(Consumer valueCallback) { callback = valueCallback; return this; @@ -126,7 +129,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour { this.unit = unit; return this; } - + public ScrollValueBehaviour onlyActiveWhen(Supplier condition) { isActive = condition; return this; @@ -168,7 +171,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour { public BehaviourType getType() { return TYPE; } - + public boolean isActive() { return isActive.get(); } @@ -182,7 +185,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour { public void setLabel(ITextComponent label) { this.label = label; } - + public static class StepContext { public int currentValue; public boolean forward; diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/simple/DeferralBehaviour.java b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/simple/DeferralBehaviour.java index 59db72bcf..3f1bb7fd9 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/simple/DeferralBehaviour.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/behaviour/simple/DeferralBehaviour.java @@ -20,6 +20,9 @@ public class DeferralBehaviour extends TileEntityBehaviour { this.callback = callback; } + @Override + public boolean isSafeNBT() { return true; } + @Override public void write(CompoundNBT nbt, boolean clientPacket) { nbt.putBoolean("NeedsUpdate", needsUpdate); @@ -38,7 +41,7 @@ public class DeferralBehaviour extends TileEntityBehaviour { if (needsUpdate && callback.get()) needsUpdate = false; } - + public void scheduleUpdate() { needsUpdate = true; } diff --git a/src/main/java/com/simibubi/create/foundation/utility/IPartialSafeNBT.java b/src/main/java/com/simibubi/create/foundation/utility/IPartialSafeNBT.java new file mode 100644 index 000000000..ae1ca8a9d --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/IPartialSafeNBT.java @@ -0,0 +1,7 @@ +package com.simibubi.create.foundation.utility; + +import net.minecraft.nbt.CompoundNBT; + +public interface IPartialSafeNBT { + public void writeSafe(CompoundNBT compound, boolean clientPacket); +}