fixed lazy optionals

This commit is contained in:
Ell 2020-10-19 21:31:23 +02:00
parent 0aa78a2171
commit 3dcda2f69a
4 changed files with 19 additions and 5 deletions

View file

@ -55,6 +55,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
private final ListMultimap<BlockPos, IPipeItem> pipeItems = ArrayListMultimap.create(); private final ListMultimap<BlockPos, IPipeItem> pipeItems = ArrayListMultimap.create();
private final ListMultimap<BlockPos, NetworkLock> networkLocks = ArrayListMultimap.create(); private final ListMultimap<BlockPos, NetworkLock> networkLocks = ArrayListMultimap.create();
private final World world; private final World world;
private final LazyOptional<PipeNetwork> lazyThis = LazyOptional.of(() -> this);
public PipeNetwork(World world) { public PipeNetwork(World world) {
this.world = world; this.world = world;
@ -66,7 +67,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
@Nonnull @Nonnull
@Override @Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
return cap == Registry.pipeNetworkCapability ? LazyOptional.of(() -> (T) this) : LazyOptional.empty(); return cap == Registry.pipeNetworkCapability ? this.lazyThis.cast() : LazyOptional.empty();
} }
@Override @Override

View file

@ -73,6 +73,7 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
protected List<IPipeItem> items; protected List<IPipeItem> items;
private int lastItemAmount; private int lastItemAmount;
private int priority; private int priority;
private final LazyOptional<PipeTileEntity> lazyThis = LazyOptional.of(() -> this);
public PipeTileEntity() { public PipeTileEntity() {
this(Registry.pipeTileEntity); this(Registry.pipeTileEntity);
@ -351,6 +352,7 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
PipeNetwork network = PipeNetwork.get(this.world); PipeNetwork network = PipeNetwork.get(this.world);
for (NetworkLock lock : this.craftIngredientRequests) for (NetworkLock lock : this.craftIngredientRequests)
network.resolveNetworkLock(lock); network.resolveNetworkLock(lock);
this.lazyThis.invalidate();
} }
@Override @Override
@ -374,7 +376,7 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
@Override @Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
if (cap == Registry.pipeConnectableCapability) if (cap == Registry.pipeConnectableCapability)
return LazyOptional.of(() -> (T) this); return this.lazyThis.cast();
return LazyOptional.empty(); return LazyOptional.empty();
} }

View file

@ -36,6 +36,8 @@ import javax.annotation.Nullable;
public class PressurizerTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity, IPipeConnectable { public class PressurizerTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity, IPipeConnectable {
private final ModifiableEnergyStorage storage = new ModifiableEnergyStorage(64000, 512, 0); private final ModifiableEnergyStorage storage = new ModifiableEnergyStorage(64000, 512, 0);
private final LazyOptional<IEnergyStorage> lazyStorage = LazyOptional.of(() -> this.storage);
private final LazyOptional<IPipeConnectable> lazyThis = LazyOptional.of(() -> this);
private int lastEnergy; private int lastEnergy;
public PressurizerTileEntity() { public PressurizerTileEntity() {
@ -100,14 +102,21 @@ public class PressurizerTileEntity extends TileEntity implements INamedContainer
@Override @Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
if (cap == CapabilityEnergy.ENERGY) { if (cap == CapabilityEnergy.ENERGY) {
return LazyOptional.of(() -> (T) this.storage); return this.lazyStorage.cast();
} else if (cap == Registry.pipeConnectableCapability) { } else if (cap == Registry.pipeConnectableCapability) {
return LazyOptional.of(() -> (T) this); return this.lazyThis.cast();
} else { } else {
return LazyOptional.empty(); return LazyOptional.empty();
} }
} }
@Override
public void remove() {
super.remove();
this.lazyStorage.invalidate();
this.lazyThis.invalidate();
}
@Override @Override
public void tick() { public void tick() {
if (this.world.isRemote) if (this.world.isRemote)

View file

@ -60,6 +60,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
}; };
protected Map<EquatableItemStack, NetworkItem> networkItems; protected Map<EquatableItemStack, NetworkItem> networkItems;
private final Queue<NetworkLock> existingRequests = new LinkedList<>(); private final Queue<NetworkLock> existingRequests = new LinkedList<>();
private final LazyOptional<IPipeConnectable> lazyThis = LazyOptional.of(() -> this);
protected ItemTerminalTileEntity(TileEntityType<?> tileEntityTypeIn) { protected ItemTerminalTileEntity(TileEntityType<?> tileEntityTypeIn) {
super(tileEntityTypeIn); super(tileEntityTypeIn);
@ -113,6 +114,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
PipeNetwork network = PipeNetwork.get(this.world); PipeNetwork network = PipeNetwork.get(this.world);
for (NetworkLock lock : this.existingRequests) for (NetworkLock lock : this.existingRequests)
network.resolveNetworkLock(lock); network.resolveNetworkLock(lock);
this.lazyThis.invalidate();
} }
public PipeTileEntity getConnectedPipe() { public PipeTileEntity getConnectedPipe() {
@ -217,7 +219,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
@Override @Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
if (cap == Registry.pipeConnectableCapability) if (cap == Registry.pipeConnectableCapability)
return LazyOptional.of(() -> (T) this); return this.lazyThis.cast();
return LazyOptional.empty(); return LazyOptional.empty();
} }