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

@ -122,14 +122,14 @@ public class ArmTileEntity extends KineticTileEntity {
} }
if (world.isRemote) if (world.isRemote)
return; return;
if (phase == Phase.MOVE_TO_INPUT) if (phase == Phase.MOVE_TO_INPUT)
collectItem(); collectItem();
else if (phase == Phase.MOVE_TO_OUTPUT) else if (phase == Phase.MOVE_TO_OUTPUT)
depositItem(); depositItem();
else if (phase == Phase.SEARCH_INPUTS || phase == Phase.DANCING) else if (phase == Phase.SEARCH_INPUTS || phase == Phase.DANCING)
searchForItem(); searchForItem();
if (targetReached) if (targetReached)
lazyTick(); lazyTick();
} }
@ -142,7 +142,7 @@ public class ArmTileEntity extends KineticTileEntity {
return; return;
if (chasedPointProgress < .5f) if (chasedPointProgress < .5f)
return; return;
if (phase == Phase.SEARCH_INPUTS || phase == Phase.DANCING) if (phase == Phase.SEARCH_INPUTS || phase == Phase.DANCING)
checkForMusic(); checkForMusic();
if (phase == Phase.SEARCH_OUTPUTS) if (phase == Phase.SEARCH_OUTPUTS)
searchForDestination(); searchForDestination();
@ -175,7 +175,7 @@ public class ArmTileEntity extends KineticTileEntity {
} }
private boolean tickMovementProgress() { private boolean tickMovementProgress() {
boolean targetReachedPreviously = chasedPointProgress >= 1; boolean targetReachedPreviously = chasedPointProgress >= 1;
chasedPointProgress += Math.min(256, Math.abs(getSpeed())) / 1024f; chasedPointProgress += Math.min(256, Math.abs(getSpeed())) / 1024f;
if (chasedPointProgress > 1) if (chasedPointProgress > 1)
chasedPointProgress = 1; chasedPointProgress = 1;
@ -349,7 +349,7 @@ public class ArmTileEntity extends KineticTileEntity {
chasedPointIndex = -1; chasedPointIndex = -1;
sendData(); sendData();
markDirty(); markDirty();
if (!prevHeld.isItemEqual(heldItem)) if (!prevHeld.isItemEqual(heldItem))
world.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, .125f, world.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, .125f,
.5f + Create.random.nextFloat() * .25f); .5f + Create.random.nextFloat() * .25f);
@ -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);
@ -95,7 +98,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour {
clientCallback = valueCallback; clientCallback = valueCallback;
return this; return this;
} }
public ScrollValueBehaviour withCallback(Consumer<Integer> valueCallback) { public ScrollValueBehaviour withCallback(Consumer<Integer> valueCallback) {
callback = valueCallback; callback = valueCallback;
return this; return this;
@ -126,7 +129,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour {
this.unit = unit; this.unit = unit;
return this; return this;
} }
public ScrollValueBehaviour onlyActiveWhen(Supplier<Boolean> condition) { public ScrollValueBehaviour onlyActiveWhen(Supplier<Boolean> condition) {
isActive = condition; isActive = condition;
return this; return this;
@ -168,7 +171,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour {
public BehaviourType<?> getType() { public BehaviourType<?> getType() {
return TYPE; return TYPE;
} }
public boolean isActive() { public boolean isActive() {
return isActive.get(); return isActive.get();
} }
@ -182,7 +185,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour {
public void setLabel(ITextComponent label) { public void setLabel(ITextComponent label) {
this.label = label; this.label = label;
} }
public static class StepContext { public static class StepContext {
public int currentValue; public int currentValue;
public boolean forward; public boolean forward;

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);
@ -38,7 +41,7 @@ public class DeferralBehaviour extends TileEntityBehaviour {
if (needsUpdate && callback.get()) if (needsUpdate && callback.get())
needsUpdate = false; needsUpdate = false;
} }
public void scheduleUpdate() { public void scheduleUpdate() {
needsUpdate = true; needsUpdate = true;
} }

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