Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
reobf committed Feb 9, 2024
1 parent 002929c commit c59a191
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) };

}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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++) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -1119,7 +1156,7 @@ public boolean justUpdated() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Optional<IDualInputInventory> 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();
Expand All @@ -1131,7 +1168,7 @@ private Predicate<DualInvBuffer> not(Predicate<DualInvBuffer> s) {

@Override
public Iterator<? extends IDualInputInventory> inventories() {

dirty=true;
return inv0.stream()
.filter(not(DualInvBuffer::isEmpty))
.iterator();
Expand Down Expand Up @@ -1252,4 +1289,12 @@ public void getWailaBody(ItemStack itemStack, List<String> currenttip, IWailaDat


}


@Override
public void updateSlots() {
inv0.forEach(DualInvBuffer::updateSlots);
super.updateSlots();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) };

}

Expand All @@ -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) {
Expand Down Expand Up @@ -513,7 +513,7 @@ public void program() {
if (decrStackSize(slot, 64).stackSize == 0) {
continue;
}

markDirty();
setInventorySlotContents(
getCircuitSlot(),
ItemProgrammingCircuit.getCircuit(is)
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Runnable> 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);

}






}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public void initTierBasedField() {

ArrayList<FluidTank> 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;
}
Expand Down
Loading

0 comments on commit c59a191

Please sign in to comment.