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) {
TileEntity tile = player.world.getTileEntity(pos);
if (tile instanceof TileEntityImpl) {
IItemHandlerModifiable handler = ((TileEntityImpl) tile).getItemHandler(null);
IItemHandlerModifiable handler = ((TileEntityImpl) tile).getItemHandler();
if (handler != null) {
ItemStack handStack = player.getHeldItem(hand);
if (!handStack.isEmpty()) {
@ -176,18 +176,17 @@ public final class Helper {
public static ICapabilityProvider makeRechargeProvider(ItemStack stack, boolean needsSelected) {
return new ICapabilityProvider() {
private final IAuraRecharge recharge = (container, containerSlot, itemSlot, isSelected) -> {
if (isSelected || !needsSelected) {
private final LazyOptional<IAuraRecharge> recharge = LazyOptional.of(() -> (container, containerSlot, itemSlot, isSelected) -> {
if (isSelected || !needsSelected)
return rechargeAuraItem(stack, container, 300);
}
return false;
};
});
@Nullable
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
if (capability == NaturesAuraAPI.capAuraRecharge)
return LazyOptional.of(() -> (T) this.recharge);
return this.recharge.cast();
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)) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityAncientLeaves) {
if (((TileEntityAncientLeaves) tile).getAuraContainer(null).getStoredAura() > 0) {
if (((TileEntityAncientLeaves) tile).getAuraContainer().getStoredAura() > 0) {
NaturesAuraAPI.instance().spawnMagicParticle(
pos.getX() + rand.nextDouble(), pos.getY(), pos.getZ() + rand.nextDouble(),
0F, 0F, 0F, 0xCC4780,
@ -82,7 +82,7 @@ public class BlockAncientLeaves extends LeavesBlock implements IModItem, IColorP
if (!worldIn.isRemote) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityAncientLeaves) {
if (((TileEntityAncientLeaves) tile).getAuraContainer(null).getStoredAura() <= 0) {
if (((TileEntityAncientLeaves) tile).getAuraContainer().getStoredAura() <= 0) {
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) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityGratedChute) {
IItemHandler handler = ((TileEntityGratedChute) tile).getItemHandler(null);
IItemHandler handler = ((TileEntityGratedChute) tile).getItemHandler();
ItemStack stack = handler.getStackInSlot(0);
if (stack.isEmpty())
return 0;

View file

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

View file

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

View file

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

View file

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

View file

@ -80,7 +80,7 @@ public class TileEntityEnderCrate extends TileEntityImpl implements INamedContai
}
@Override
public IItemHandlerModifiable getItemHandler(Direction facing) {
public IItemHandlerModifiable getItemHandler() {
if (this.canOpen())
return this.wrappedEnderStorage;
return null;
@ -146,6 +146,6 @@ public class TileEntityEnderCrate extends TileEntityImpl implements INamedContai
@Nullable
@Override
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
public IItemHandlerModifiable getItemHandler(Direction facing) {
public IItemHandlerModifiable getItemHandler() {
return this.items;
}
}

View file

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

View file

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

View file

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

View file

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

View file

@ -27,7 +27,7 @@ import net.minecraftforge.fluids.capability.templates.FluidTank;
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;
public TileEntitySpring() {
@ -124,7 +124,7 @@ public class TileEntitySpring extends TileEntityImpl implements ITickableTileEnt
@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction facing) {
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
return LazyOptional.of(() -> (T) this.tank);
return this.tank.cast();
return LazyOptional.empty();
}

View file

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

View file

@ -27,7 +27,7 @@ public class RenderAuraTimer extends TileEntityRenderer<TileEntityAuraTimer> {
@Override
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())
return;
stack.push();

View file

@ -17,6 +17,7 @@ import javax.annotation.Nullable;
public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<CompoundNBT> {
private final Chunk chunk;
private final LazyOptional<IAuraChunk> lazyChunk = LazyOptional.of(this::getAuraChunk);
private IAuraChunk auraChunk;
public AuraChunkProvider(Chunk chunk) {
@ -32,7 +33,7 @@ public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<
@Nullable
@Override
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

View file

@ -60,12 +60,7 @@ public class CuriosCompat implements ICompat {
ItemStack stack = event.getObject();
if (TYPES.containsKey(stack.getItem())) {
event.addCapability(new ResourceLocation(NaturesAura.MOD_ID, "curios"), new ICapabilityProvider() {
@Nonnull
@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() {
private final LazyOptional<ICurio> curio = LazyOptional.of(() -> new ICurio() {
@Override
public void curioTick(String identifier, int index, LivingEntity livingEntity) {
stack.getItem().inventoryTick(stack, livingEntity.world, livingEntity, -1, false);
@ -82,6 +77,13 @@ public class CuriosCompat implements ICompat {
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);
if (tile instanceof TileEntityNatureAltar) {
ItemStack tileStack = ((TileEntityNatureAltar) tile).getItemHandler(null).getStackInSlot(0);
ItemStack tileStack = ((TileEntityNatureAltar) tile).getItemHandler().getStackInSlot(0);
if (!tileStack.isEmpty()) {
IAuraContainer stackCont = tileStack.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
if (stackCont != null) {
@ -405,7 +405,7 @@ public class ClientEvents {
storage.getEnergyStored() + " / " + storage.getMaxEnergyStored() + " RF");
} else if (tile instanceof TileEntityGratedChute) {
TileEntityGratedChute chute = (TileEntityGratedChute) tile;
ItemStack itemStack = chute.getItemHandler(null).getStackInSlot(0);
ItemStack itemStack = chute.getItemHandler().getStackInSlot(0);
if (itemStack.isEmpty())
mc.fontRenderer.drawStringWithShadow(stack,
@ -430,7 +430,7 @@ public class ClientEvents {
GlStateManager.enableDepthTest();
} else if (tile instanceof TileEntityAuraTimer) {
TileEntityAuraTimer timer = (TileEntityAuraTimer) tile;
ItemStack itemStack = timer.getItemHandler(null).getStackInSlot(0);
ItemStack itemStack = timer.getItemHandler().getStackInSlot(0);
if (!itemStack.isEmpty()) {
Helper.renderItemInGui(itemStack, x - 20, y - 20, 1);
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
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
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
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
if (capability == NaturesAuraAPI.capAuraContainer) {
return LazyOptional.of(() -> (T) this.container);
return this.container.cast();
} else {
return LazyOptional.empty();
}

View file

@ -31,11 +31,12 @@ public class WorldData implements IWorldData {
public final List<BlockPos> recentlyConvertedMossStones = new ArrayList<>();
private final Map<String, ItemStackHandlerNA> enderStorages = new HashMap<>();
public final Set<TileEntitySpawnLamp> spawnLamps = new HashSet<>();
private final LazyOptional<WorldData> lazyThis = LazyOptional.of(() -> this);
@Nullable
@Override
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

View file

@ -254,7 +254,7 @@ public final class ModRegistry {
IForgeContainerType.create((windowId, inv, data) -> {
TileEntity tile = inv.player.world.getTileEntity(data.readBlockPos());
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;
}).setRegistryName("ender_crate"),
IForgeContainerType.create((windowId, inv, data) -> {