Better support for partially safe NBT writing

This commit is contained in:
reidbhuntley 2021-05-21 20:33:28 -04:00
parent 68b3ba7215
commit c2cc124239
10 changed files with 73 additions and 32 deletions

View file

@ -416,23 +416,26 @@ public class ArmTileEntity extends KineticTileEntity {
markDirty(); 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 @Override
public void write(CompoundNBT compound, boolean clientPacket) { public void write(CompoundNBT compound, boolean clientPacket) {
super.write(compound, clientPacket); super.write(compound, clientPacket);
if (updateInteractionPoints) { writeInteractionPoints(compound);
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);
}
NBTHelper.writeEnum(compound, "Phase", phase); NBTHelper.writeEnum(compound, "Phase", phase);
compound.putBoolean("Powered", redstoneLocked); compound.putBoolean("Powered", redstoneLocked);
@ -441,6 +444,13 @@ public class ArmTileEntity extends KineticTileEntity {
compound.putFloat("MovementProgress", chasedPointProgress); compound.putFloat("MovementProgress", chasedPointProgress);
} }
@Override
public void writeSafe(CompoundNBT compound, boolean clientPacket) {
super.writeSafe(compound, clientPacket);
writeInteractionPoints(compound);
}
@Override @Override
protected void fromTag(BlockState state, CompoundNBT compound, boolean clientPacket) { protected void fromTag(BlockState state, CompoundNBT compound, boolean clientPacket) {
int previousIndex = chasedPointIndex; int previousIndex = chasedPointIndex;

View file

@ -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.BehaviourType;
import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour; import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringBehaviour;
import com.simibubi.create.foundation.utility.BlockHelper; 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.Iterate;
import com.simibubi.create.foundation.utility.NBTProcessors; import com.simibubi.create.foundation.utility.NBTProcessors;
@ -502,13 +503,10 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC
if (AllBlockTags.SAFE_NBT.matches(blockState)) { if (AllBlockTags.SAFE_NBT.matches(blockState)) {
data = tile.write(new CompoundNBT()); data = tile.write(new CompoundNBT());
data = NBTProcessors.process(tile, data, true); data = NBTProcessors.process(tile, data, true);
} else if (tile instanceof SmartTileEntity) { } else if (tile instanceof IPartialSafeNBT) {
FilteringBehaviour filtering = ((SmartTileEntity)tile).getBehaviour(FilteringBehaviour.TYPE); data = new CompoundNBT();
if (filtering != null) { ((IPartialSafeNBT) tile).writeSafe(data, false);
data = new CompoundNBT(); data = NBTProcessors.process(tile, data, true);
filtering.write(data, false);
data = NBTProcessors.process(tile, data, true);
}
} }
} }

View file

@ -8,6 +8,8 @@ import java.util.function.Consumer;
import com.simibubi.create.foundation.tileEntity.behaviour.BehaviourType; import com.simibubi.create.foundation.tileEntity.behaviour.BehaviourType;
import com.simibubi.create.foundation.utility.IPartialSafeNBT;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.ITickableTileEntity;
@ -16,7 +18,7 @@ import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.items.CapabilityItemHandler; 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<BehaviourType<?>, TileEntityBehaviour> behaviours; private final Map<BehaviourType<?>, TileEntityBehaviour> behaviours;
// Internally maintained to be identical to behaviorMap.values() in order to improve iteration performance. // 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)); 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 @Override
public void remove() { public void remove() {
forEachBehaviour(TileEntityBehaviour::remove); forEachBehaviour(TileEntityBehaviour::remove);

View file

@ -42,6 +42,8 @@ public abstract class TileEntityBehaviour {
} }
public boolean isSafeNBT() { return false; }
public void onBlockChanged(BlockState oldState) { public void onBlockChanged(BlockState oldState) {
} }
@ -94,5 +96,4 @@ public abstract class TileEntityBehaviour {
SmartTileEntity ste = (SmartTileEntity) te; SmartTileEntity ste = (SmartTileEntity) te;
return ste.getBehaviour(type); return ste.getBehaviour(type);
} }
} }

View file

@ -57,6 +57,9 @@ public class FilteringBehaviour extends TileEntityBehaviour {
fluidFilter = false; fluidFilter = false;
} }
@Override
public boolean isSafeNBT() { return true; }
@Override @Override
public void write(CompoundNBT nbt, boolean clientPacket) { public void write(CompoundNBT nbt, boolean clientPacket) {
nbt.put("Filter", getFilter().serializeNBT()); nbt.put("Filter", getFilter().serializeNBT());

View file

@ -58,6 +58,9 @@ public class SidedFilteringBehaviour extends FilteringBehaviour {
removeFilter(d); removeFilter(d);
} }
@Override
public boolean isSafeNBT() { return true; }
@Override @Override
public void write(CompoundNBT nbt, boolean clientPacket) { public void write(CompoundNBT nbt, boolean clientPacket) {
nbt.put("Filters", NBTHelper.writeCompoundList(sidedFilters.entrySet(), entry -> { nbt.put("Filters", NBTHelper.writeCompoundList(sidedFilters.entrySet(), entry -> {

View file

@ -115,6 +115,9 @@ public class LinkBehaviour extends TileEntityBehaviour {
getHandler().removeFromNetwork(this); getHandler().removeFromNetwork(this);
} }
@Override
public boolean isSafeNBT() { return true; }
@Override @Override
public void write(CompoundNBT nbt, boolean clientPacket) { public void write(CompoundNBT nbt, boolean clientPacket) {
super.write(nbt, clientPacket); super.write(nbt, clientPacket);

View file

@ -54,6 +54,9 @@ public class ScrollValueBehaviour extends TileEntityBehaviour {
ticksUntilScrollPacket = -1; ticksUntilScrollPacket = -1;
} }
@Override
public boolean isSafeNBT() { return true; }
@Override @Override
public void write(CompoundNBT nbt, boolean clientPacket) { public void write(CompoundNBT nbt, boolean clientPacket) {
nbt.putInt("ScrollValue", value); nbt.putInt("ScrollValue", value);

View file

@ -20,6 +20,9 @@ public class DeferralBehaviour extends TileEntityBehaviour {
this.callback = callback; this.callback = callback;
} }
@Override
public boolean isSafeNBT() { return true; }
@Override @Override
public void write(CompoundNBT nbt, boolean clientPacket) { public void write(CompoundNBT nbt, boolean clientPacket) {
nbt.putBoolean("NeedsUpdate", needsUpdate); nbt.putBoolean("NeedsUpdate", needsUpdate);

View file

@ -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);
}