diff --git a/src/main/java/reobf/proghatches/gt/metatileentity/BufferedDualInputHatch.java b/src/main/java/reobf/proghatches/gt/metatileentity/BufferedDualInputHatch.java index 19177f8..41a09fd 100644 --- a/src/main/java/reobf/proghatches/gt/metatileentity/BufferedDualInputHatch.java +++ b/src/main/java/reobf/proghatches/gt/metatileentity/BufferedDualInputHatch.java @@ -65,18 +65,23 @@ import gregtech.api.interfaces.ITexture; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.interfaces.tileentity.IMachineProgress; +import gregtech.api.metatileentity.BaseMetaTileEntity; import gregtech.api.metatileentity.MetaTileEntity; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch_OutputBus; +import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase; +import gregtech.api.recipe.check.CheckRecipeResult; +import gregtech.api.recipe.check.CheckRecipeResultRegistry; import gregtech.api.util.GT_TooltipDataCache.TooltipData; import gregtech.api.util.GT_Utility; import gregtech.api.util.extensions.ArrayExt; import gregtech.common.tileentities.machines.IDualInputInventory; +import gregtech.common.tileentities.machines.IRecipeProcessingAwareHatch; import mcp.mobius.waila.api.IWailaConfigHandler; import mcp.mobius.waila.api.IWailaDataAccessor; import reobf.proghatches.item.ItemProgrammingCircuit; import reobf.proghatches.main.MyMod; -public class BufferedDualInputHatch extends DualInputHatch { +public class BufferedDualInputHatch extends DualInputHatch { public Widget circuitSlot(IItemHandlerModifiable inventory, int slot) { @@ -220,15 +225,15 @@ public BufferedDualInputHatch(int id, String name, String nameRegional, int tier public void initTierBasedField() { if (mMultiFluid) { - mStoredFluid = new FluidTank[] { + mStoredFluid = new ListeningFluidTank[] { - new FluidTank((int) (1000 * Math.pow(2, mTier))), new FluidTank((int) (1000 * Math.pow(2, mTier))), - new FluidTank((int) (1000 * Math.pow(2, mTier))), new FluidTank((int) (1000 * Math.pow(2, mTier))) + new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this), new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this), + new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this), new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this) }; } else { - mStoredFluid = new FluidTank[] { new FluidTank((int) (4000 * Math.pow(2, mTier))) }; + mStoredFluid = new ListeningFluidTank[] { new ListeningFluidTank((int) (4000 * Math.pow(2, mTier)),this) }; } @@ -283,7 +288,16 @@ public boolean full() { return false; } - +public void updateSlots(){ + for (int i = 0; i < this.i ; i++) + if (mStoredItemInternal[i] != null && mStoredItemInternal[i].stackSize <= 0){ + mStoredItemInternal[i] = null;} + for (int i = 0; i < this.f ; i++) + if(Optional.ofNullable(mStoredFluidInternal[i].getFluid()).filter(s->s.amount==0).isPresent()){ + mStoredFluidInternal[i].setFluid(null); + } + +} public NBTTagCompound toTag() { NBTTagCompound tag = new NBTTagCompound(); @@ -414,15 +428,17 @@ private boolean fluidEquals(FluidTank a, FluidTank b) { return true; } - - public void firstCalssify(FluidTank[] fin, ItemStack[] iin) { +/** + * classify() with less check, for better performance + * */ + public void firstClassify(ListeningFluidTank[] fin, ItemStack[] iin) { for (int ix = 0; ix < f; ix++) { mStoredFluidInternal[ix].setFluid( Optional.ofNullable(fin[ix].getFluid()) .map(FluidStack::copy) .orElse(null)); - fin[ix].setFluid(null); + fin[ix].setFluidDirect(null); } for (int ix = 0; ix < i; ix++) { @@ -467,7 +483,7 @@ private void programLocal() { } - public void classify(FluidTank[] fin, ItemStack[] iin) { + public void classify(ListeningFluidTank[] fin, ItemStack[] iin) { boolean hasJob = false; for (int ix = 0; ix < f; ix++) { if (fin[ix].getFluidAmount() > 0) { @@ -491,7 +507,7 @@ public void classify(FluidTank[] fin, ItemStack[] iin) { for (int ix = 0; ix < f; ix++) { mStoredFluidInternal[ix].fill(mStoredFluidInternalSingle[ix].getFluid(), true); - fin[ix].setFluid(null); + fin[ix].setFluidDirect(null); } for (int ix = 0; ix < i; ix++) { @@ -505,7 +521,7 @@ public void classify(FluidTank[] fin, ItemStack[] iin) { } - public void recordRecipeOrCalssify(FluidTank[] fin, ItemStack[] iin) { + public boolean recordRecipeOrClassify(ListeningFluidTank[] fin, ItemStack[] iin) { boolean isEmpty = clearRecipeIfNeeded(); // clearRecipeIfNeeded(); if (recipeLocked == false && isEmpty == true) { @@ -523,9 +539,10 @@ public void recordRecipeOrCalssify(FluidTank[] fin, ItemStack[] iin) { } } recipeLocked = actuallyFound; - firstCalssify(fin, iin); + if(actuallyFound) firstClassify(fin, iin); + return actuallyFound; } - + return false; } @Override @@ -608,23 +625,32 @@ public static class CallerCheck { } } } - +public boolean highEfficiencyMode(){return false;} @Override public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) { super.onPostTick(aBaseMetaTileEntity, aTick); if (aBaseMetaTileEntity.getWorld().isRemote) return; - if(this.getBaseMetaTileEntity().isAllowedToWork()) + + if(dirty){updateSlots();markDirty();} + dirty=dirty||(!highEfficiencyMode()); + boolean on=(this.getBaseMetaTileEntity().isAllowedToWork()); for (DualInvBuffer inv0 : this.sortByEmpty()) { - if (inv0.full() == false) { - inv0.recordRecipeOrCalssify(this.mStoredFluid, mInventory); + if(on&&dirty){ + if (inv0.full() == false) { + if(!inv0.recordRecipeOrClassify(this.mStoredFluid, mInventory)){ inv0.classify(this.mStoredFluid, mInventory); + } + } } + + + + inv0.clearRecipeIfNeeded(); } - - // BaseMetaTileEntity h; - // h.add4by4Slots(builder, background); - } + + dirty=false; + } @Override public ItemStack getStackInSlot(int aIndex) { @@ -657,12 +683,20 @@ public void classify() { } } - + boolean dirty; +@Override +public void onFill() { + //Thread.dumpStack(); + classify(); + markDirty(); + dirty=true; +} @Override public void setInventorySlotContents(int aIndex, ItemStack aStack) { super.setInventorySlotContents(aIndex, aStack); classify(); + markDirty();dirty=true; } public void add1by1Slot(ModularWindow.Builder builder, int index, IDrawable... background) { @@ -970,6 +1004,8 @@ private NBTTagCompound cv(String s) { thiz.disableWorking(); } else { thiz.enableWorking(); + BufferedDualInputHatch bff =(BufferedDualInputHatch) (thiz).getMetaTileEntity(); + bff.dirty=true; } }) .setPlayClickSoundResource( () -> thiz.isAllowedToWork() ? SoundResource.GUI_BUTTON_UP.resourceLocation @@ -990,7 +1026,7 @@ private NBTTagCompound cv(String s) { }), builder) .addTooltip(StatCollector.translateToLocal("GT5U.gui.button.power_switch")) .setTooltipShowUpDelay(TOOLTIP_DELAY) - .setPos(new Pos2d(getGUIWidth()-18-3,3)) + .setPos(new Pos2d(getGUIWidth()-18-3,5)) .setSize(16, 16); return (ButtonWidget) button; } @@ -1018,7 +1054,7 @@ public int moveButtons() { @Override public void loadNBTData(NBTTagCompound aNBT) { - // inv0.clear(); + dirty=aNBT.getBoolean("dirty"); for (int i = 0; i < bufferNum; i++) { final int ii = i; inv0.get(i) @@ -1030,6 +1066,7 @@ public void loadNBTData(NBTTagCompound aNBT) { @Override public void saveNBTData(NBTTagCompound aNBT) { + aNBT.setBoolean("dirty", dirty); for (int i = 0; i < bufferNum; i++) aNBT.setTag( @@ -1119,7 +1156,7 @@ public boolean justUpdated() { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Optional getFirstNonEmptyInventory() { - + dirty=true;//IRecipeProcessingAwareHatch NOT working on dualhatch, so assume multi-te will consume items after calling this return (Optional) inv0.stream() .filter(not(DualInvBuffer::isEmpty)) .findAny(); @@ -1131,7 +1168,7 @@ private Predicate not(Predicate s) { @Override public Iterator inventories() { - + dirty=true; return inv0.stream() .filter(not(DualInvBuffer::isEmpty)) .iterator(); @@ -1252,4 +1289,12 @@ public void getWailaBody(ItemStack itemStack, List currenttip, IWailaDat } + + +@Override +public void updateSlots() { + inv0.forEach(DualInvBuffer::updateSlots); + super.updateSlots(); +} + } diff --git a/src/main/java/reobf/proghatches/gt/metatileentity/DualInputHatch.java b/src/main/java/reobf/proghatches/gt/metatileentity/DualInputHatch.java index e76ba9b..bc8328b 100644 --- a/src/main/java/reobf/proghatches/gt/metatileentity/DualInputHatch.java +++ b/src/main/java/reobf/proghatches/gt/metatileentity/DualInputHatch.java @@ -111,16 +111,16 @@ public void initTierBasedField() { if (mMultiFluid) { - mStoredFluid = new FluidTank[] { + mStoredFluid = new ListeningFluidTank[] { - new FluidTank((int) (1000 * Math.pow(2, mTier))), new FluidTank((int) (1000 * Math.pow(2, mTier))), - new FluidTank((int) (1000 * Math.pow(2, mTier))), new FluidTank((int) (1000 * Math.pow(2, mTier))) + new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this), new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this), + new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this), new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this) }; } else { - mStoredFluid = new FluidTank[] { new FluidTank((int) (4000 * Math.pow(2, mTier))) }; + mStoredFluid = new ListeningFluidTank[] { new ListeningFluidTank((int) (4000 * Math.pow(2, mTier)),this) }; } @@ -143,7 +143,7 @@ public DualInputHatch(String aName, int aTier, int aSlots, String[] aDescription } - public FluidTank[] mStoredFluid; + public ListeningFluidTank[] mStoredFluid; @Override public void saveNBTData(NBTTagCompound aNBT) { @@ -513,7 +513,7 @@ public void program() { if (decrStackSize(slot, 64).stackSize == 0) { continue; } - + markDirty(); setInventorySlotContents( getCircuitSlot(), ItemProgrammingCircuit.getCircuit(is) @@ -541,13 +541,14 @@ public void onFirstTick(IGregTechTileEntity aBaseMetaTileEntity) { public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) { super.onPostTick(aBaseMetaTileEntity, aTick); if (aBaseMetaTileEntity.getWorld().isRemote) return; - markDirty(); + if (program) program(); if (aBaseMetaTileEntity.isServerSide()) { for (int i = 0; i < getMaxType(); i++) { if (mStoredFluid[i].getFluid() != null && mStoredFluid[i].getFluidAmount() <= 0) { mStoredFluid[i].setFluid(null); + markDirty(); } } } @@ -559,7 +560,7 @@ public void setInventorySlotContents(int aIndex, ItemStack aStack) { super.setInventorySlotContents(aIndex, aStack); if (program) program(); } - + public void onFill(){} @Override public int fill(FluidStack aFluid, boolean doFill) { if (aFluid == null || aFluid.getFluid() diff --git a/src/main/java/reobf/proghatches/gt/metatileentity/ListeningFluidTank.java b/src/main/java/reobf/proghatches/gt/metatileentity/ListeningFluidTank.java new file mode 100644 index 0000000..e84649a --- /dev/null +++ b/src/main/java/reobf/proghatches/gt/metatileentity/ListeningFluidTank.java @@ -0,0 +1,55 @@ +package reobf.proghatches.gt.metatileentity; + +import java.util.Optional; + +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTank; +import net.minecraftforge.fluids.FluidTankInfo; + +public class ListeningFluidTank extends FluidTank{ + + public ListeningFluidTank(int capacity,DualInputHatch thiz) { + super(capacity); + addListener(thiz::onFill); + } + public ListeningFluidTank(FluidStack stack, int capacity,DualInputHatch thiz) { + super(stack, capacity); + addListener(thiz::onFill); + } + public ListeningFluidTank(Fluid fluid, int amount, int capacity,DualInputHatch thiz) { + super(fluid, amount, capacity); + addListener(thiz::onFill); + } + Optional callback=Optional.empty(); + + public ListeningFluidTank addListener(Runnable c){ + if(callback.isPresent())throw new RuntimeException("callback exists"); + callback=Optional.of(c);return this; + } + @Override + public int fill(FluidStack resource, boolean doFill) { + try{return super.fill(resource, doFill);}finally{callback.ifPresent(Runnable::run);} + } + @Override + public void setFluid(FluidStack fluid) { + super.setFluid(fluid); + callback.ifPresent(Runnable::run); + } + + + public int fillDirect(FluidStack resource, boolean doFill) { + return super.fill(resource, doFill); + } + + public void setFluidDirect(FluidStack fluid) { + super.setFluid(fluid); + + } + + + + + + +} diff --git a/src/main/java/reobf/proghatches/gt/metatileentity/SuperfluidHatch.java b/src/main/java/reobf/proghatches/gt/metatileentity/SuperfluidHatch.java index 3fbb544..7ef895e 100644 --- a/src/main/java/reobf/proghatches/gt/metatileentity/SuperfluidHatch.java +++ b/src/main/java/reobf/proghatches/gt/metatileentity/SuperfluidHatch.java @@ -88,11 +88,11 @@ public void initTierBasedField() { ArrayList arr = new ArrayList<>(); for (int i = 0; i < 24; i++) { - arr.add(new FluidTank((int) (1000 * Math.pow(2, mTier)))); + arr.add(new ListeningFluidTank((int) (1000 * Math.pow(2, mTier)),this)); } - mStoredFluid = arr.toArray(new FluidTank[0]); + mStoredFluid = arr.toArray(new ListeningFluidTank[0]); return; } diff --git a/src/main/java/reobf/proghatches/main/mixin/MixinPlugin.java b/src/main/java/reobf/proghatches/main/mixin/MixinPlugin.java index 0e3a2d6..65c5525 100644 --- a/src/main/java/reobf/proghatches/main/mixin/MixinPlugin.java +++ b/src/main/java/reobf/proghatches/main/mixin/MixinPlugin.java @@ -101,10 +101,10 @@ public List getMixins() { // GregTech5RecipeProcessor.class.getDeclaredFields(); ArrayList ret = new ArrayList<>(); - if(!"true".equals(pp.get("noRecipeFilterForDualHatch"))) - ret.add("MixinGTRecipeFilter"); + if(!"true".equals(pp.get("noRecipeFilterForDualHatch"))){ + ret.add("MixinGTRecipeFilter"); // GT Multiblock will not set recipe filter of DualInputHatch, set it via mixin - // ret.add("MixinAddProgCircuitExemptToInputFilter"); + ret.add("MixinAddProgCircuitExemptToInputFilter");} // Crafting CPU cannot recognize empty-input pattern // bypass the check anyway ret.add("MixinCanCraftExempt");