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

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

View file

@ -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<BehaviourType<?>, 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);

View file

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

View file

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

View file

@ -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 -> {

View file

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

View file

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

View file

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

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