From 3fb52be97384274656c8f4c8dce2abf3d279687c Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:45:56 -0700 Subject: [PATCH] strip IItemHandler and IFluidHandler from IDualHandler improve proxy capability slightly improve qcontroller update and isPowered --- .../gregtech/api/capability/IDualHandler.java | 7 +- .../storage/MetaTileEntityQuantumProxy.java | 26 +++-- ...etaTileEntityQuantumStorageController.java | 96 ++++--------------- 3 files changed, 37 insertions(+), 92 deletions(-) diff --git a/src/main/java/gregtech/api/capability/IDualHandler.java b/src/main/java/gregtech/api/capability/IDualHandler.java index b1d0b1d2dca..595efc6a9f2 100644 --- a/src/main/java/gregtech/api/capability/IDualHandler.java +++ b/src/main/java/gregtech/api/capability/IDualHandler.java @@ -1,11 +1,14 @@ package gregtech.api.capability; -import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.items.IItemHandler; -public interface IDualHandler extends IItemHandler, IFluidHandler { +public interface IDualHandler { boolean hasFluidTanks(); boolean hasItemHandlers(); + + IMultipleTankHandler getFluidTanks(); + + IItemHandler getItemHandlers(); } diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumProxy.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumProxy.java index abddcefdb19..3c21fec1d2f 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumProxy.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumProxy.java @@ -2,6 +2,7 @@ import gregtech.api.capability.GregtechDataCodes; import gregtech.api.capability.IDualHandler; +import gregtech.api.capability.IQuantumController; import gregtech.api.gui.ModularUI; import gregtech.api.metatileentity.MetaTileEntity; import gregtech.api.metatileentity.interfaces.IGregTechTileEntity; @@ -23,6 +24,7 @@ import codechicken.lib.vec.Matrix4; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.Nullable; public class MetaTileEntityQuantumProxy extends MetaTileEntityQuantumStorage { @@ -66,15 +68,13 @@ public boolean hasFrontFacing() { return false; } - @SuppressWarnings("unchecked") @Override public T getCapability(Capability capability, EnumFacing side) { - if (getTypeValue() == null) return super.getCapability(capability, side); - - if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { - return (T) getTypeValue(); - } else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) { - return (T) getTypeValue(); + if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || + capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) { + var controller = getPoweredController(); + if (controller != null) + return controller.getCapability(capability, side); } return super.getCapability(capability, side); } @@ -84,13 +84,19 @@ public Type getType() { return Type.PROXY; } - @SuppressWarnings("DataFlowIssue") @Override public IDualHandler getTypeValue() { + IQuantumController c = getPoweredController(); + if (c == null) return null; + return c.getHandler(); + } + + @Nullable + private IQuantumController getPoweredController() { if (!isConnected()) return null; var controller = getQuantumController(); - if (!controller.isPowered()) return null; - return controller.getHandler(); + if (controller == null || !controller.isPowered()) return null; + return controller; } @Override diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java index 57362076d29..58f3efd28e9 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java @@ -6,7 +6,6 @@ import gregtech.api.capability.IEnergyContainer; import gregtech.api.capability.IQuantumController; import gregtech.api.capability.IQuantumStorage; -import gregtech.api.capability.impl.EnergyContainerHandler; import gregtech.api.capability.impl.EnergyContainerList; import gregtech.api.capability.impl.FluidTankList; import gregtech.api.capability.impl.ItemHandlerList; @@ -30,10 +29,8 @@ import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.Constants; -import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; -import net.minecraftforge.fluids.capability.IFluidTankProperties; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; @@ -88,16 +85,18 @@ public void update() { if (getWorld().isRemote) return; if (getOffsetTimer() % 10 == 0) { - if (isPowered) { - energyContainer.removeEnergy(energyConsumption); + boolean isPowered = energyContainer.getEnergyStored() > energyConsumption && energyConsumption > 0; + if (isPowered) energyContainer.removeEnergy(energyConsumption); + + if (isPowered != this.isPowered) { + this.isPowered = isPowered; + writeCustomData(GregtechDataCodes.UPDATE_ENERGY, buf -> buf.writeBoolean(this.isPowered)); } - isPowered = energyContainer.getEnergyStored() >= energyConsumption && energyConsumption > 0; - writeCustomData(GregtechDataCodes.UPDATE_ENERGY, buf -> buf.writeBoolean(isPowered)); } } public boolean isPowered() { - return isPowered && energyConsumption > 0; + return isPowered; } @Override @@ -334,6 +333,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) { list.appendTag(new NBTTagLong(pos.toLong())); } tagCompound.setTag("StorageInstances", list); + tagCompound.setBoolean("isPowered", this.isPowered); return tagCompound; } @@ -344,6 +344,7 @@ public void readFromNBT(NBTTagCompound data) { for (int i = 0; i < list.tagCount(); i++) { storagePositions.add(BlockPos.fromLong(((NBTTagLong) list.get(i)).getLong())); } + this.isPowered = data.getBoolean("isPowered"); } @SuppressWarnings("unchecked") @@ -351,9 +352,9 @@ public void readFromNBT(NBTTagCompound data) { public T getCapability(@NotNull Capability capability, EnumFacing side) { if (isPowered()) { if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && handler.hasItemHandlers()) { - return (T) handler; + return (T) handler.getItemHandlers(); } else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY && handler.hasFluidTanks()) { - return (T) handler; + return (T) handler.getFluidTanks(); } } @@ -376,6 +377,7 @@ public IEnergyContainer getEnergyContainer() { return this.energyContainer; } + // todo use DualHandler instead once the multis ability pr is merged private class QuantumControllerHandler implements IDualHandler { // IFluidHandler saved values @@ -386,7 +388,7 @@ private class QuantumControllerHandler implements IDualHandler { private void invalidate() { fluidTanks = new FluidTankList(false); - itemHandlers = new ItemHandlerList(new ArrayList<>()); + itemHandlers = new ItemHandlerList(Collections.emptyList()); } private void rebuildCache() { @@ -416,9 +418,8 @@ public boolean hasItemHandlers() { return !getItemHandlers().getBackingHandlers().isEmpty(); } - // IFluidHandler - - protected FluidTankList getFluidTanks() { + @Override + public FluidTankList getFluidTanks() { if (fluidTanks == null) { rebuildCache(); } @@ -426,76 +427,11 @@ protected FluidTankList getFluidTanks() { } @Override - public IFluidTankProperties[] getTankProperties() { - return fluidTanks.getTankProperties(); - } - - @Override - public int fill(FluidStack stack, boolean doFill) { - return fluidTanks.fill(stack, doFill); - } - - @Nullable - @Override - public FluidStack drain(FluidStack stack, boolean doFill) { - var f = fluidTanks.drain(stack, doFill); - - if (f != null && f.amount > stack.amount) - f.amount = stack.amount; - - return f; - } - - @Nullable - @Override - public FluidStack drain(int maxDrain, boolean doFill) { - var f = fluidTanks.drain(maxDrain, doFill); - - if (f != null && f.amount > maxDrain) - f.amount = maxDrain; - - return f; - } - - // IItemHandler - - protected ItemHandlerList getItemHandlers() { + public ItemHandlerList getItemHandlers() { if (itemHandlers == null) { rebuildCache(); } return itemHandlers; } - - @Override - public int getSlots() { - return getItemHandlers().getSlots(); - } - - @NotNull - @Override - public ItemStack getStackInSlot(int slot) { - return itemHandlers.getStackInSlot(slot); - } - - @NotNull - @Override - public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) { - return itemHandlers.insertItem(slot, stack, simulate); - } - - @NotNull - @Override - public ItemStack extractItem(int slot, int amount, boolean simulate) { - var s = itemHandlers.extractItem(slot, amount, simulate); - if (s.getCount() > amount) - s.setCount(amount); - - return s; - } - - @Override - public int getSlotLimit(int slot) { - return getItemHandlers().getSlotLimit(slot); - } } }