Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Cleanup isValidMetaTileEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
miozune authored and Dream-Master committed Oct 30, 2023
1 parent 31d4cad commit 6612c54
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gtPlusPlus.xmod.gregtech.api.metatileentity.implementations.base;

import static gregtech.api.enums.Mods.TecTech;
import static gregtech.api.util.GT_Utility.filterValidMTEs;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -120,12 +121,6 @@ public GregtechMeta_MultiBlockBase(final String aName) {
super(aName);
}

public static boolean isValidMetaTileEntity(final MetaTileEntity aMetaTileEntity) {
return (aMetaTileEntity.getBaseMetaTileEntity() != null)
&& (aMetaTileEntity.getBaseMetaTileEntity().getMetaTileEntity() == aMetaTileEntity)
&& !aMetaTileEntity.getBaseMetaTileEntity().isDead();
}

private static int toStackCount(Entry<ItemStack, Integer> e) {
int tMaxStackSize = e.getKey().getMaxStackSize();
int tStackSize = e.getValue();
Expand Down Expand Up @@ -307,50 +302,40 @@ public String[] getInfoData() {

public int getPollutionReductionForAllMufflers() {
int mPollutionReduction = 0;
for (GT_MetaTileEntity_Hatch_Muffler tHatch : mMufflerHatches) {
if (isValidMetaTileEntity(tHatch)) {
mPollutionReduction = Math.max(calculatePollutionReductionForHatch(tHatch, 100), mPollutionReduction);
}
for (GT_MetaTileEntity_Hatch_Muffler tHatch : filterValidMTEs(mMufflerHatches)) {
mPollutionReduction = Math.max(calculatePollutionReductionForHatch(tHatch, 100), mPollutionReduction);
}
return mPollutionReduction;
}

public long getStoredEnergyInAllEnergyHatches() {
long storedEnergy = 0;
for (GT_MetaTileEntity_Hatch tHatch : mAllEnergyHatches) {
if (isValidMetaTileEntity(tHatch)) {
storedEnergy += tHatch.getBaseMetaTileEntity().getStoredEU();
}
for (GT_MetaTileEntity_Hatch tHatch : filterValidMTEs(mAllEnergyHatches)) {
storedEnergy += tHatch.getBaseMetaTileEntity().getStoredEU();
}
return storedEnergy;
}

public long getMaxEnergyStorageOfAllEnergyHatches() {
long maxEnergy = 0;
for (GT_MetaTileEntity_Hatch tHatch : mAllEnergyHatches) {
if (isValidMetaTileEntity(tHatch)) {
maxEnergy += tHatch.getBaseMetaTileEntity().getEUCapacity();
}
for (GT_MetaTileEntity_Hatch tHatch : filterValidMTEs(mAllEnergyHatches)) {
maxEnergy += tHatch.getBaseMetaTileEntity().getEUCapacity();
}
return maxEnergy;
}

public long getStoredEnergyInAllDynamoHatches() {
long storedEnergy = 0;
for (GT_MetaTileEntity_Hatch tHatch : mAllDynamoHatches) {
if (isValidMetaTileEntity(tHatch)) {
storedEnergy += tHatch.getBaseMetaTileEntity().getStoredEU();
}
for (GT_MetaTileEntity_Hatch tHatch : filterValidMTEs(mAllDynamoHatches)) {
storedEnergy += tHatch.getBaseMetaTileEntity().getStoredEU();
}
return storedEnergy;
}

public long getMaxEnergyStorageOfAllDynamoHatches() {
long maxEnergy = 0;
for (GT_MetaTileEntity_Hatch tHatch : mAllDynamoHatches) {
if (isValidMetaTileEntity(tHatch)) {
maxEnergy += tHatch.getBaseMetaTileEntity().getEUCapacity();
}
for (GT_MetaTileEntity_Hatch tHatch : filterValidMTEs(mAllDynamoHatches)) {
maxEnergy += tHatch.getBaseMetaTileEntity().getEUCapacity();
}
return maxEnergy;
}
Expand Down Expand Up @@ -415,8 +400,8 @@ public long getMaxInputEnergy() {
if (mEnergyHatches.size() == 1) // so it only takes 1 amp is only 1 hatch is present so it works like most gt
// multies
return mEnergyHatches.get(0).getBaseMetaTileEntity().getInputVoltage();
for (GT_MetaTileEntity_Hatch_Energy tHatch : mEnergyHatches)
if (isValidMetaTileEntity(tHatch)) rEnergy += tHatch.getBaseMetaTileEntity().getInputVoltage()
for (GT_MetaTileEntity_Hatch_Energy tHatch : filterValidMTEs(mEnergyHatches))
rEnergy += tHatch.getBaseMetaTileEntity().getInputVoltage()
* tHatch.getBaseMetaTileEntity().getInputAmperage();
return rEnergy;
}
Expand Down Expand Up @@ -522,33 +507,27 @@ public ItemStack findItemInInventory(ItemStack aSearchStack) {
*/
protected boolean depleteInputFromRestrictedHatches(Collection<GT_MetaTileEntity_Hatch_CustomFluidBase> aHatches,
int aAmount) {
for (final GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : aHatches) {
if (isValidMetaTileEntity(tHatch)) {
FluidStack tLiquid = tHatch.getFluid();
if (tLiquid == null || tLiquid.amount < aAmount) {
continue;
}
tLiquid = tHatch.drain(aAmount, false);
if (tLiquid != null && tLiquid.amount >= aAmount) {
tLiquid = tHatch.drain(aAmount, true);
return tLiquid != null && tLiquid.amount >= aAmount;
}
for (final GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : filterValidMTEs(aHatches)) {
FluidStack tLiquid = tHatch.getFluid();
if (tLiquid == null || tLiquid.amount < aAmount) {
continue;
}
tLiquid = tHatch.drain(aAmount, false);
if (tLiquid != null && tLiquid.amount >= aAmount) {
tLiquid = tHatch.drain(aAmount, true);
return tLiquid != null && tLiquid.amount >= aAmount;
}
}
return false;
}

@Override
public void updateSlots() {
for (final GT_MetaTileEntity_Hatch_InputBattery tHatch : this.mChargeHatches) {
if (isValidMetaTileEntity(tHatch)) {
tHatch.updateSlots();
}
for (final GT_MetaTileEntity_Hatch_InputBattery tHatch : filterValidMTEs(this.mChargeHatches)) {
tHatch.updateSlots();
}
for (final GT_MetaTileEntity_Hatch_OutputBattery tHatch : this.mDischargeHatches) {
if (isValidMetaTileEntity(tHatch)) {
tHatch.updateSlots();
}
for (final GT_MetaTileEntity_Hatch_OutputBattery tHatch : filterValidMTEs(this.mDischargeHatches)) {
tHatch.updateSlots();
}
super.updateSlots();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static gregtech.api.enums.GT_Values.V;
import static gregtech.api.util.GT_StructureUtility.buildHatchAdder;
import static gregtech.api.util.GT_Utility.filterValidMTEs;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -183,15 +184,13 @@ public void stopMachine() {
@Override
public boolean depleteInput(FluidStack aLiquid) {
if (aLiquid == null) return false;
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : mSteamInputFluids) {
if (isValidMetaTileEntity(tHatch)) {
FluidStack tLiquid = tHatch.getFluid();
if (tLiquid != null && tLiquid.isFluidEqual(aLiquid)) {
tLiquid = tHatch.drain(aLiquid.amount, false);
if (tLiquid != null && tLiquid.amount >= aLiquid.amount) {
tLiquid = tHatch.drain(aLiquid.amount, true);
return tLiquid != null && tLiquid.amount >= aLiquid.amount;
}
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : filterValidMTEs(mSteamInputFluids)) {
FluidStack tLiquid = tHatch.getFluid();
if (tLiquid != null && tLiquid.isFluidEqual(aLiquid)) {
tLiquid = tHatch.drain(aLiquid.amount, false);
if (tLiquid != null && tLiquid.amount >= aLiquid.amount) {
tLiquid = tHatch.drain(aLiquid.amount, true);
return tLiquid != null && tLiquid.amount >= aLiquid.amount;
}
}
}
Expand All @@ -203,25 +202,21 @@ public boolean depleteInput(ItemStack aStack) {
if (GT_Utility.isStackInvalid(aStack)) return false;
FluidStack aLiquid = GT_Utility.getFluidForFilledItem(aStack, true);
if (aLiquid != null) return depleteInput(aLiquid);
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : mSteamInputFluids) {
if (isValidMetaTileEntity(tHatch)) {
if (GT_Utility.areStacksEqual(aStack, tHatch.getBaseMetaTileEntity().getStackInSlot(0))) {
if (tHatch.getBaseMetaTileEntity().getStackInSlot(0).stackSize >= aStack.stackSize) {
tHatch.getBaseMetaTileEntity().decrStackSize(0, aStack.stackSize);
return true;
}
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : filterValidMTEs(mSteamInputFluids)) {
if (GT_Utility.areStacksEqual(aStack, tHatch.getBaseMetaTileEntity().getStackInSlot(0))) {
if (tHatch.getBaseMetaTileEntity().getStackInSlot(0).stackSize >= aStack.stackSize) {
tHatch.getBaseMetaTileEntity().decrStackSize(0, aStack.stackSize);
return true;
}
}
}
for (GT_MetaTileEntity_Hatch_Steam_BusInput tHatch : mSteamInputs) {
for (GT_MetaTileEntity_Hatch_Steam_BusInput tHatch : filterValidMTEs(mSteamInputs)) {
tHatch.mRecipeMap = getRecipeMap();
if (isValidMetaTileEntity(tHatch)) {
for (int i = tHatch.getBaseMetaTileEntity().getSizeInventory() - 1; i >= 0; i--) {
if (GT_Utility.areStacksEqual(aStack, tHatch.getBaseMetaTileEntity().getStackInSlot(i))) {
if (tHatch.getBaseMetaTileEntity().getStackInSlot(0).stackSize >= aStack.stackSize) {
tHatch.getBaseMetaTileEntity().decrStackSize(0, aStack.stackSize);
return true;
}
for (int i = tHatch.getBaseMetaTileEntity().getSizeInventory() - 1; i >= 0; i--) {
if (GT_Utility.areStacksEqual(aStack, tHatch.getBaseMetaTileEntity().getStackInSlot(i))) {
if (tHatch.getBaseMetaTileEntity().getStackInSlot(0).stackSize >= aStack.stackSize) {
tHatch.getBaseMetaTileEntity().decrStackSize(0, aStack.stackSize);
return true;
}
}
}
Expand All @@ -232,8 +227,8 @@ public boolean depleteInput(ItemStack aStack) {
@Override
public ArrayList<FluidStack> getStoredFluids() {
ArrayList<FluidStack> rList = new ArrayList<>();
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : mSteamInputFluids) {
if (isValidMetaTileEntity(tHatch) && tHatch.getFillableStack() != null) {
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : filterValidMTEs(mSteamInputFluids)) {
if (tHatch.getFillableStack() != null) {
rList.add(tHatch.getFillableStack());
}
}
Expand All @@ -243,13 +238,11 @@ public ArrayList<FluidStack> getStoredFluids() {
@Override
public ArrayList<ItemStack> getStoredInputs() {
ArrayList<ItemStack> rList = new ArrayList<>();
for (GT_MetaTileEntity_Hatch_Steam_BusInput tHatch : mSteamInputs) {
for (GT_MetaTileEntity_Hatch_Steam_BusInput tHatch : filterValidMTEs(mSteamInputs)) {
tHatch.mRecipeMap = getRecipeMap();
if (isValidMetaTileEntity(tHatch)) {
for (int i = tHatch.getBaseMetaTileEntity().getSizeInventory() - 1; i >= 0; i--) {
if (tHatch.getBaseMetaTileEntity().getStackInSlot(i) != null) {
rList.add(tHatch.getBaseMetaTileEntity().getStackInSlot(i));
}
for (int i = tHatch.getBaseMetaTileEntity().getSizeInventory() - 1; i >= 0; i--) {
if (tHatch.getBaseMetaTileEntity().getStackInSlot(i) != null) {
rList.add(tHatch.getBaseMetaTileEntity().getStackInSlot(i));
}
}
}
Expand All @@ -264,15 +257,15 @@ public boolean addOutput(ItemStack aStack) {
while (outputSuccess && aStack.stackSize > 0) {
outputSuccess = false;
ItemStack single = aStack.splitStack(1);
for (GT_MetaTileEntity_Hatch_Steam_BusOutput tHatch : mSteamOutputs) {
if (!outputSuccess && isValidMetaTileEntity(tHatch)) {
for (GT_MetaTileEntity_Hatch_Steam_BusOutput tHatch : filterValidMTEs(mSteamOutputs)) {
if (!outputSuccess) {
for (int i = tHatch.getSizeInventory() - 1; i >= 0 && !outputSuccess; i--) {
if (tHatch.getBaseMetaTileEntity().addStackToSlot(i, single)) outputSuccess = true;
}
}
}
for (GT_MetaTileEntity_Hatch_Output tHatch : mOutputHatches) {
if (!outputSuccess && isValidMetaTileEntity(tHatch) && tHatch.outputsItems()) {
for (GT_MetaTileEntity_Hatch_Output tHatch : filterValidMTEs(mOutputHatches)) {
if (!outputSuccess && tHatch.outputsItems()) {
if (tHatch.getBaseMetaTileEntity().addStackToSlot(1, single)) outputSuccess = true;
}
}
Expand All @@ -283,11 +276,9 @@ public boolean addOutput(ItemStack aStack) {
@Override
public ArrayList<ItemStack> getStoredOutputs() {
ArrayList<ItemStack> rList = new ArrayList<>();
for (GT_MetaTileEntity_Hatch_Steam_BusOutput tHatch : mSteamOutputs) {
if (isValidMetaTileEntity(tHatch)) {
for (int i = tHatch.getBaseMetaTileEntity().getSizeInventory() - 1; i >= 0; i--) {
rList.add(tHatch.getBaseMetaTileEntity().getStackInSlot(i));
}
for (GT_MetaTileEntity_Hatch_Steam_BusOutput tHatch : filterValidMTEs(mSteamOutputs)) {
for (int i = tHatch.getBaseMetaTileEntity().getSizeInventory() - 1; i >= 0; i--) {
rList.add(tHatch.getBaseMetaTileEntity().getStackInSlot(i));
}
}
return rList;
Expand All @@ -296,7 +287,7 @@ public ArrayList<ItemStack> getStoredOutputs() {
@Override
public List<ItemStack> getItemOutputSlots(ItemStack[] toOutput) {
List<ItemStack> ret = new ArrayList<>();
for (final GT_MetaTileEntity_Hatch tBus : filterValidMetaTileEntities(mSteamOutputs)) {
for (final GT_MetaTileEntity_Hatch tBus : filterValidMTEs(mSteamOutputs)) {
final IInventory tBusInv = tBus.getBaseMetaTileEntity();
for (int i = 0; i < tBusInv.getSizeInventory(); i++) {
ret.add(tBus.getStackInSlot(i));
Expand All @@ -307,10 +298,8 @@ public List<ItemStack> getItemOutputSlots(ItemStack[] toOutput) {

@Override
public void updateSlots() {
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : mSteamInputFluids)
if (isValidMetaTileEntity(tHatch)) tHatch.updateSlots();
for (GT_MetaTileEntity_Hatch_Steam_BusInput tHatch : mSteamInputs)
if (isValidMetaTileEntity(tHatch)) tHatch.updateSlots();
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : filterValidMTEs(mSteamInputFluids)) tHatch.updateSlots();
for (GT_MetaTileEntity_Hatch_Steam_BusInput tHatch : filterValidMTEs(mSteamInputs)) tHatch.updateSlots();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static gregtech.api.enums.GT_HatchElement.OutputBus;
import static gregtech.api.enums.GT_HatchElement.OutputHatch;
import static gregtech.api.util.GT_StructureUtility.buildHatchAdder;
import static gregtech.api.util.GT_Utility.filterValidMTEs;

import java.util.ArrayList;

Expand Down Expand Up @@ -157,8 +158,7 @@ private boolean addCryotheumHatch(IGregTechTileEntity aTileEntity, int aBaseCasi

@Override
public void updateSlots() {
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : mCryotheumHatches)
if (isValidMetaTileEntity(tHatch)) tHatch.updateSlots();
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : filterValidMTEs(mCryotheumHatches)) tHatch.updateSlots();
super.updateSlots();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static gregtech.api.enums.GT_HatchElement.OutputBus;
import static gregtech.api.enums.GT_HatchElement.OutputHatch;
import static gregtech.api.util.GT_StructureUtility.buildHatchAdder;
import static gregtech.api.util.GT_Utility.filterValidMTEs;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -428,11 +429,9 @@ public int getMaxParallelRecipes() {
@Override
public ArrayList<ItemStack> getStoredInputs() {
ArrayList<ItemStack> tItems = super.getStoredInputs();
for (GT_MetaTileEntity_Hatch_MillingBalls tHatch : mMillingBallBuses) {
for (GT_MetaTileEntity_Hatch_MillingBalls tHatch : filterValidMTEs(mMillingBallBuses)) {
tHatch.mRecipeMap = getRecipeMap();
if (isValidMetaTileEntity(tHatch)) {
tItems.addAll(tHatch.getContentUsageSlots());
}
tItems.addAll(tHatch.getContentUsageSlots());
}
return tItems;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static gregtech.api.enums.GT_HatchElement.OutputHatch;
import static gregtech.api.util.GT_StructureUtility.buildHatchAdder;
import static gregtech.api.util.GT_StructureUtility.ofCoil;
import static gregtech.api.util.GT_Utility.filterValidMTEs;

import java.util.ArrayList;

Expand All @@ -23,7 +24,6 @@
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -179,28 +179,10 @@ private boolean addPyrotheumHatch(IGregTechTileEntity aTileEntity, int aBaseCasi

@Override
public void updateSlots() {
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : mPyrotheumHatches)
if (isValidMetaTileEntity(tHatch)) tHatch.updateSlots();
for (GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : filterValidMTEs(mPyrotheumHatches)) tHatch.updateSlots();
super.updateSlots();
}

private boolean depleteFuel(int aAmount) {
for (final GT_MetaTileEntity_Hatch_CustomFluidBase tHatch : this.mPyrotheumHatches) {
if (isValidMetaTileEntity(tHatch)) {
FluidStack tLiquid = tHatch.getFluid();
if (tLiquid == null || tLiquid.amount < aAmount) {
continue;
}
tLiquid = tHatch.drain(aAmount, false);
if (tLiquid != null && tLiquid.amount >= aAmount) {
tLiquid = tHatch.drain(aAmount, true);
return tLiquid != null && tLiquid.amount >= aAmount;
}
}
}
return false;
}

@Override
protected IIconContainer getActiveOverlay() {
return TexturesGtBlock.Overlay_Machine_Controller_Advanced_Active;
Expand Down
Loading

0 comments on commit 6612c54

Please sign in to comment.