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, NetworkLock> networkLocks = ArrayListMultimap.create();
private final World world;
private final LazyOptional<PipeNetwork> lazyThis = LazyOptional.of(() -> this);
public PipeNetwork(World world) {
this.world = world;
@ -66,7 +67,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
@Nonnull
@Override
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

View file

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

View file

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

View file

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