finally fixed the lazy optionals

This commit is contained in:
Ell 2020-10-19 21:26:32 +02:00
parent 398d0680b6
commit 48dfb8fe96
22 changed files with 83 additions and 56 deletions

View file

@ -140,7 +140,7 @@ public final class Helper {
public static ActionResultType putStackOnTile(PlayerEntity player, Hand hand, BlockPos pos, int slot, boolean sound) { public static ActionResultType putStackOnTile(PlayerEntity player, Hand hand, BlockPos pos, int slot, boolean sound) {
TileEntity tile = player.world.getTileEntity(pos); TileEntity tile = player.world.getTileEntity(pos);
if (tile instanceof TileEntityImpl) { if (tile instanceof TileEntityImpl) {
IItemHandlerModifiable handler = ((TileEntityImpl) tile).getItemHandler(null); IItemHandlerModifiable handler = ((TileEntityImpl) tile).getItemHandler();
if (handler != null) { if (handler != null) {
ItemStack handStack = player.getHeldItem(hand); ItemStack handStack = player.getHeldItem(hand);
if (!handStack.isEmpty()) { if (!handStack.isEmpty()) {
@ -176,18 +176,17 @@ public final class Helper {
public static ICapabilityProvider makeRechargeProvider(ItemStack stack, boolean needsSelected) { public static ICapabilityProvider makeRechargeProvider(ItemStack stack, boolean needsSelected) {
return new ICapabilityProvider() { return new ICapabilityProvider() {
private final IAuraRecharge recharge = (container, containerSlot, itemSlot, isSelected) -> { private final LazyOptional<IAuraRecharge> recharge = LazyOptional.of(() -> (container, containerSlot, itemSlot, isSelected) -> {
if (isSelected || !needsSelected) { if (isSelected || !needsSelected)
return rechargeAuraItem(stack, container, 300); return rechargeAuraItem(stack, container, 300);
}
return false; return false;
}; });
@Nullable @Nullable
@Override @Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) { public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
if (capability == NaturesAuraAPI.capAuraRecharge) if (capability == NaturesAuraAPI.capAuraRecharge)
return LazyOptional.of(() -> (T) this.recharge); return this.recharge.cast();
return LazyOptional.empty(); return LazyOptional.empty();
} }
}; };

View file

@ -63,7 +63,7 @@ public class BlockAncientLeaves extends LeavesBlock implements IModItem, IColorP
if (rand.nextFloat() >= 0.95F && !worldIn.getBlockState(pos.down()).isOpaqueCube(worldIn, pos)) { if (rand.nextFloat() >= 0.95F && !worldIn.getBlockState(pos.down()).isOpaqueCube(worldIn, pos)) {
TileEntity tile = worldIn.getTileEntity(pos); TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityAncientLeaves) { if (tile instanceof TileEntityAncientLeaves) {
if (((TileEntityAncientLeaves) tile).getAuraContainer(null).getStoredAura() > 0) { if (((TileEntityAncientLeaves) tile).getAuraContainer().getStoredAura() > 0) {
NaturesAuraAPI.instance().spawnMagicParticle( NaturesAuraAPI.instance().spawnMagicParticle(
pos.getX() + rand.nextDouble(), pos.getY(), pos.getZ() + rand.nextDouble(), pos.getX() + rand.nextDouble(), pos.getY(), pos.getZ() + rand.nextDouble(),
0F, 0F, 0F, 0xCC4780, 0F, 0F, 0F, 0xCC4780,
@ -82,7 +82,7 @@ public class BlockAncientLeaves extends LeavesBlock implements IModItem, IColorP
if (!worldIn.isRemote) { if (!worldIn.isRemote) {
TileEntity tile = worldIn.getTileEntity(pos); TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityAncientLeaves) { if (tile instanceof TileEntityAncientLeaves) {
if (((TileEntityAncientLeaves) tile).getAuraContainer(null).getStoredAura() <= 0) { if (((TileEntityAncientLeaves) tile).getAuraContainer().getStoredAura() <= 0) {
worldIn.setBlockState(pos, ModBlocks.DECAYED_LEAVES.getDefaultState()); worldIn.setBlockState(pos, ModBlocks.DECAYED_LEAVES.getDefaultState());
} }
} }

View file

@ -131,7 +131,7 @@ public class BlockGratedChute extends BlockContainerImpl implements ICustomBlock
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) { public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
TileEntity tile = worldIn.getTileEntity(pos); TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityGratedChute) { if (tile instanceof TileEntityGratedChute) {
IItemHandler handler = ((TileEntityGratedChute) tile).getItemHandler(null); IItemHandler handler = ((TileEntityGratedChute) tile).getItemHandler();
ItemStack stack = handler.getStackInSlot(0); ItemStack stack = handler.getStackInSlot(0);
if (stack.isEmpty()) if (stack.isEmpty())
return 0; return 0;

View file

@ -29,7 +29,7 @@ public class TileEntityAncientLeaves extends TileEntityImpl {
} }
@Override @Override
public IAuraContainer getAuraContainer(Direction facing) { public IAuraContainer getAuraContainer() {
return this.container; return this.container;
} }

View file

@ -101,7 +101,7 @@ public class TileEntityAuraTimer extends TileEntityImpl implements ITickableTile
} }
@Override @Override
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
return this.itemHandler; return this.itemHandler;
} }

View file

@ -80,7 +80,7 @@ public class TileEntityBlastFurnaceBooster extends TileEntityImpl implements ITi
} }
@Override @Override
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
TileEntity below = this.world.getTileEntity(this.pos.down()); TileEntity below = this.world.getTileEntity(this.pos.down());
if (!(below instanceof BlastFurnaceTileEntity)) if (!(below instanceof BlastFurnaceTileEntity))
return null; return null;

View file

@ -105,7 +105,7 @@ public class TileEntityEndFlower extends TileEntityImpl implements ITickableTile
} }
@Override @Override
public IAuraContainer getAuraContainer(Direction facing) { public IAuraContainer getAuraContainer() {
return this.container; return this.container;
} }

View file

@ -80,7 +80,7 @@ public class TileEntityEnderCrate extends TileEntityImpl implements INamedContai
} }
@Override @Override
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
if (this.canOpen()) if (this.canOpen())
return this.wrappedEnderStorage; return this.wrappedEnderStorage;
return null; return null;
@ -146,6 +146,6 @@ public class TileEntityEnderCrate extends TileEntityImpl implements INamedContai
@Nullable @Nullable
@Override @Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) { public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) {
return new ContainerEnderCrate(ModContainers.ENDER_CRATE, window, player, this.getItemHandler(null)); return new ContainerEnderCrate(ModContainers.ENDER_CRATE, window, player, this.getItemHandler());
} }
} }

View file

@ -145,7 +145,7 @@ public class TileEntityGratedChute extends TileEntityImpl implements ITickableTi
} }
@Override @Override
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
return this.items; return this.items;
} }
} }

View file

@ -28,6 +28,8 @@ import java.util.stream.Stream;
public class TileEntityImpl extends TileEntity { public class TileEntityImpl extends TileEntity {
public int redstonePower; public int redstonePower;
private LazyOptional<IItemHandler> itemHandler;
private LazyOptional<IAuraContainer> auraContainer;
public TileEntityImpl(TileEntityType<?> tileEntityTypeIn) { public TileEntityImpl(TileEntityType<?> tileEntityTypeIn) {
super(tileEntityTypeIn); super(tileEntityTypeIn);
@ -96,11 +98,11 @@ public class TileEntityImpl extends TileEntity {
entities.forEach(e -> e.connection.sendPacket(packet)); entities.forEach(e -> e.connection.sendPacket(packet));
} }
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
return null; return null;
} }
public IAuraContainer getAuraContainer(Direction facing) { public IAuraContainer getAuraContainer() {
return null; return null;
} }
@ -108,18 +110,33 @@ public class TileEntityImpl extends TileEntity {
@Override @Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) { public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
IItemHandler handler = this.getItemHandler(facing); if (this.itemHandler == null) {
return handler == null ? LazyOptional.empty() : LazyOptional.of(() -> (T) handler); IItemHandler handler = this.getItemHandler();
this.itemHandler = handler == null ? LazyOptional.empty() : LazyOptional.of(() -> handler);
}
return this.itemHandler.cast();
} else if (capability == NaturesAuraAPI.capAuraContainer) { } else if (capability == NaturesAuraAPI.capAuraContainer) {
IAuraContainer container = this.getAuraContainer(facing); if (this.auraContainer == null) {
return container == null ? LazyOptional.empty() : LazyOptional.of(() -> (T) container); IAuraContainer container = this.getAuraContainer();
this.auraContainer = container == null ? LazyOptional.empty() : LazyOptional.of(() -> container);
}
return this.auraContainer.cast();
} else { } else {
return super.getCapability(capability, facing); return super.getCapability(capability, facing);
} }
} }
@Override
public void remove() {
super.remove();
if (this.itemHandler != null)
this.itemHandler.invalidate();
if (this.auraContainer != null)
this.auraContainer.invalidate();
}
public void dropInventory() { public void dropInventory() {
IItemHandler handler = this.getItemHandler(null); IItemHandler handler = this.getItemHandler();
if (handler != null) { if (handler != null) {
for (int i = 0; i < handler.getSlots(); i++) { for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i); ItemStack stack = handler.getStackInSlot(i);

View file

@ -259,12 +259,12 @@ public class TileEntityNatureAltar extends TileEntityImpl implements ITickableTi
} }
@Override @Override
public IAuraContainer getAuraContainer(Direction facing) { public IAuraContainer getAuraContainer() {
return this.container; return this.container;
} }
@Override @Override
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
return this.items; return this.items;
} }

View file

@ -139,7 +139,7 @@ public class TileEntityOfferingTable extends TileEntityImpl implements ITickable
} }
@Override @Override
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
return this.items; return this.items;
} }
} }

View file

@ -22,6 +22,7 @@ import javax.annotation.Nullable;
public class TileEntityRFConverter extends TileEntityImpl implements ITickableTileEntity { public class TileEntityRFConverter extends TileEntityImpl implements ITickableTileEntity {
public final RFStorage storage = new RFStorage(); public final RFStorage storage = new RFStorage();
private final LazyOptional<IEnergyStorage> storageOptional = LazyOptional.of(() -> this.storage);
private int lastEnergy; private int lastEnergy;
public TileEntityRFConverter() { public TileEntityRFConverter() {
@ -92,11 +93,17 @@ public class TileEntityRFConverter extends TileEntityImpl implements ITickableTi
@Override @Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) { public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
if (capability == CapabilityEnergy.ENERGY) if (capability == CapabilityEnergy.ENERGY)
return LazyOptional.of(() -> (T) this.storage); return this.storageOptional.cast();
else else
return super.getCapability(capability, facing); return super.getCapability(capability, facing);
} }
@Override
public void remove() {
super.remove();
this.storageOptional.invalidate();
}
public static class RFStorage extends EnergyStorage { public static class RFStorage extends EnergyStorage {
public RFStorage() { public RFStorage() {

View file

@ -27,7 +27,7 @@ import net.minecraftforge.fluids.capability.templates.FluidTank;
public class TileEntitySpring extends TileEntityImpl implements ITickableTileEntity { public class TileEntitySpring extends TileEntityImpl implements ITickableTileEntity {
private final IFluidHandler tank = new InfiniteTank(); private final LazyOptional<IFluidHandler> tank = LazyOptional.of(() -> new InfiniteTank());
private AABBTicket waterTicket; private AABBTicket waterTicket;
public TileEntitySpring() { public TileEntitySpring() {
@ -124,7 +124,7 @@ public class TileEntitySpring extends TileEntityImpl implements ITickableTileEnt
@Override @Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction facing) { public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction facing) {
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
return LazyOptional.of(() -> (T) this.tank); return this.tank.cast();
return LazyOptional.empty(); return LazyOptional.empty();
} }

View file

@ -220,7 +220,7 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickableTile
} }
@Override @Override
public IItemHandlerModifiable getItemHandler(Direction facing) { public IItemHandlerModifiable getItemHandler() {
return this.items; return this.items;
} }
} }

View file

@ -27,7 +27,7 @@ public class RenderAuraTimer extends TileEntityRenderer<TileEntityAuraTimer> {
@Override @Override
public void render(TileEntityAuraTimer tile, float partialTicks, MatrixStack stack, IRenderTypeBuffer buffer, int combinedLightIn, int combinedOverlayIn) { public void render(TileEntityAuraTimer tile, float partialTicks, MatrixStack stack, IRenderTypeBuffer buffer, int combinedLightIn, int combinedOverlayIn) {
ItemStack bottle = tile.getItemHandler(null).getStackInSlot(0); ItemStack bottle = tile.getItemHandler().getStackInSlot(0);
if (bottle.isEmpty()) if (bottle.isEmpty())
return; return;
stack.push(); stack.push();

View file

@ -17,6 +17,7 @@ import javax.annotation.Nullable;
public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<CompoundNBT> { public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<CompoundNBT> {
private final Chunk chunk; private final Chunk chunk;
private final LazyOptional<IAuraChunk> lazyChunk = LazyOptional.of(this::getAuraChunk);
private IAuraChunk auraChunk; private IAuraChunk auraChunk;
public AuraChunkProvider(Chunk chunk) { public AuraChunkProvider(Chunk chunk) {
@ -32,7 +33,7 @@ public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<
@Nullable @Nullable
@Override @Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) { public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
return capability == NaturesAuraAPI.capAuraChunk ? LazyOptional.of(() -> (T) this.getAuraChunk()) : LazyOptional.empty(); return capability == NaturesAuraAPI.capAuraChunk ? this.lazyChunk.cast() : LazyOptional.empty();
} }
@Override @Override

View file

@ -60,12 +60,7 @@ public class CuriosCompat implements ICompat {
ItemStack stack = event.getObject(); ItemStack stack = event.getObject();
if (TYPES.containsKey(stack.getItem())) { if (TYPES.containsKey(stack.getItem())) {
event.addCapability(new ResourceLocation(NaturesAura.MOD_ID, "curios"), new ICapabilityProvider() { event.addCapability(new ResourceLocation(NaturesAura.MOD_ID, "curios"), new ICapabilityProvider() {
@Nonnull private final LazyOptional<ICurio> curio = LazyOptional.of(() -> new ICurio() {
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
if (cap != CuriosCapability.ITEM)
return LazyOptional.empty();
return LazyOptional.of(() -> (T) new ICurio() {
@Override @Override
public void curioTick(String identifier, int index, LivingEntity livingEntity) { public void curioTick(String identifier, int index, LivingEntity livingEntity) {
stack.getItem().inventoryTick(stack, livingEntity.world, livingEntity, -1, false); stack.getItem().inventoryTick(stack, livingEntity.world, livingEntity, -1, false);
@ -82,6 +77,13 @@ public class CuriosCompat implements ICompat {
return true; return true;
} }
}); });
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
if (cap != CuriosCapability.ITEM)
return LazyOptional.empty();
return this.curio.cast();
} }
}); });
} }

View file

@ -389,7 +389,7 @@ public class ClientEvents {
mc, res, 35, blockStack.getDisplayName().getString(), null); mc, res, 35, blockStack.getDisplayName().getString(), null);
if (tile instanceof TileEntityNatureAltar) { if (tile instanceof TileEntityNatureAltar) {
ItemStack tileStack = ((TileEntityNatureAltar) tile).getItemHandler(null).getStackInSlot(0); ItemStack tileStack = ((TileEntityNatureAltar) tile).getItemHandler().getStackInSlot(0);
if (!tileStack.isEmpty()) { if (!tileStack.isEmpty()) {
IAuraContainer stackCont = tileStack.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null); IAuraContainer stackCont = tileStack.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
if (stackCont != null) { if (stackCont != null) {
@ -405,7 +405,7 @@ public class ClientEvents {
storage.getEnergyStored() + " / " + storage.getMaxEnergyStored() + " RF"); storage.getEnergyStored() + " / " + storage.getMaxEnergyStored() + " RF");
} else if (tile instanceof TileEntityGratedChute) { } else if (tile instanceof TileEntityGratedChute) {
TileEntityGratedChute chute = (TileEntityGratedChute) tile; TileEntityGratedChute chute = (TileEntityGratedChute) tile;
ItemStack itemStack = chute.getItemHandler(null).getStackInSlot(0); ItemStack itemStack = chute.getItemHandler().getStackInSlot(0);
if (itemStack.isEmpty()) if (itemStack.isEmpty())
mc.fontRenderer.drawStringWithShadow(stack, mc.fontRenderer.drawStringWithShadow(stack,
@ -430,7 +430,7 @@ public class ClientEvents {
GlStateManager.enableDepthTest(); GlStateManager.enableDepthTest();
} else if (tile instanceof TileEntityAuraTimer) { } else if (tile instanceof TileEntityAuraTimer) {
TileEntityAuraTimer timer = (TileEntityAuraTimer) tile; TileEntityAuraTimer timer = (TileEntityAuraTimer) tile;
ItemStack itemStack = timer.getItemHandler(null).getStackInSlot(0); ItemStack itemStack = timer.getItemHandler().getStackInSlot(0);
if (!itemStack.isEmpty()) { if (!itemStack.isEmpty()) {
Helper.renderItemInGui(itemStack, x - 20, y - 20, 1); Helper.renderItemInGui(itemStack, x - 20, y - 20, 1);
mc.fontRenderer.drawStringWithShadow(stack, TextFormatting.GRAY + this.createTimeString(timer.getTotalTime()), x + 5, y - 11, 0xFFFFFF); mc.fontRenderer.drawStringWithShadow(stack, TextFormatting.GRAY + this.createTimeString(timer.getTotalTime()), x + 5, y - 11, 0xFFFFFF);

View file

@ -99,13 +99,13 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
@Override @Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) { public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
return new ICapabilityProvider() { return new ICapabilityProvider() {
private final ItemAuraContainer container = new ItemAuraContainer(stack, null, ItemAuraCache.this.capacity); private final LazyOptional<ItemAuraContainer> container = LazyOptional.of(() -> new ItemAuraContainer(stack, null, ItemAuraCache.this.capacity));
@Nonnull @Nonnull
@Override @Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) { public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
if (capability == NaturesAuraAPI.capAuraContainer) { if (capability == NaturesAuraAPI.capAuraContainer) {
return LazyOptional.of(() -> (T) this.container); return this.container.cast();
} else { } else {
return LazyOptional.empty(); return LazyOptional.empty();
} }

View file

@ -31,11 +31,12 @@ public class WorldData implements IWorldData {
public final List<BlockPos> recentlyConvertedMossStones = new ArrayList<>(); public final List<BlockPos> recentlyConvertedMossStones = new ArrayList<>();
private final Map<String, ItemStackHandlerNA> enderStorages = new HashMap<>(); private final Map<String, ItemStackHandlerNA> enderStorages = new HashMap<>();
public final Set<TileEntitySpawnLamp> spawnLamps = new HashSet<>(); public final Set<TileEntitySpawnLamp> spawnLamps = new HashSet<>();
private final LazyOptional<WorldData> lazyThis = LazyOptional.of(() -> this);
@Nullable @Nullable
@Override @Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) { public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
return capability == NaturesAuraAPI.capWorldData ? LazyOptional.of(() -> (T) this) : LazyOptional.empty(); return capability == NaturesAuraAPI.capWorldData ? this.lazyThis.cast() : LazyOptional.empty();
} }
@Override @Override

View file

@ -254,7 +254,7 @@ public final class ModRegistry {
IForgeContainerType.create((windowId, inv, data) -> { IForgeContainerType.create((windowId, inv, data) -> {
TileEntity tile = inv.player.world.getTileEntity(data.readBlockPos()); TileEntity tile = inv.player.world.getTileEntity(data.readBlockPos());
if (tile instanceof TileEntityEnderCrate) if (tile instanceof TileEntityEnderCrate)
return new ContainerEnderCrate(ModContainers.ENDER_CRATE, windowId, inv.player, ((TileEntityEnderCrate) tile).getItemHandler(null)); return new ContainerEnderCrate(ModContainers.ENDER_CRATE, windowId, inv.player, ((TileEntityEnderCrate) tile).getItemHandler());
return null; return null;
}).setRegistryName("ender_crate"), }).setRegistryName("ender_crate"),
IForgeContainerType.create((windowId, inv, data) -> { IForgeContainerType.create((windowId, inv, data) -> {