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

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);
@ -95,7 +98,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour {
clientCallback = valueCallback;
return this;
}
public ScrollValueBehaviour withCallback(Consumer<Integer> valueCallback) {
callback = valueCallback;
return this;
@ -126,7 +129,7 @@ public class ScrollValueBehaviour extends TileEntityBehaviour {
this.unit = unit;
return this;
}
public ScrollValueBehaviour onlyActiveWhen(Supplier<Boolean> 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;

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