Skip to content

Commit

Permalink
Less onContentChanged (#1732)
Browse files Browse the repository at this point in the history
  • Loading branch information
gateguardian523 authored Aug 16, 2024
1 parent 6d64666 commit 6aad35c
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,43 +73,47 @@ public void autoIO() {
if (!this.shouldSyncME()) return;

if (this.updateMEStatus()) {
MEStorage aeNetwork = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEItemSlot aeSlot : this.aeItemHandler.getInventory()) {
// Try to clear the wrong item
GenericStack exceedItem = aeSlot.exceedStack();
if (exceedItem != null) {
long total = exceedItem.amount();
long inserted = aeNetwork.insert(exceedItem.what(), exceedItem.amount(), Actionable.MODULATE,
this.actionSource);
if (inserted > 0) {
aeSlot.extractItem(0, (int) inserted, false);
continue;
} else {
aeSlot.extractItem(0, (int) total, false);
}
this.syncME();
this.updateInventorySubscription();
}
}

protected void syncME() {
MEStorage networkInv = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEItemSlot aeSlot : this.aeItemHandler.getInventory()) {
// Try to clear the wrong item
GenericStack exceedItem = aeSlot.exceedStack();
if (exceedItem != null) {
long total = exceedItem.amount();
long inserted = networkInv.insert(exceedItem.what(), exceedItem.amount(), Actionable.MODULATE,
this.actionSource);
if (inserted > 0) {
aeSlot.extractItem(0, (int) inserted, false);
continue;
} else {
aeSlot.extractItem(0, (int) total, false);
}
// Fill it
GenericStack reqItem = aeSlot.requestStack();
if (reqItem != null) {
long extracted = aeNetwork.extract(reqItem.what(), reqItem.amount(), Actionable.MODULATE,
this.actionSource);
if (extracted != 0) {
aeSlot.addStack(new GenericStack(reqItem.what(), extracted));
}
}
// Fill it
GenericStack reqItem = aeSlot.requestStack();
if (reqItem != null) {
long extracted = networkInv.extract(reqItem.what(), reqItem.amount(), Actionable.MODULATE,
this.actionSource);
if (extracted != 0) {
aeSlot.addStack(new GenericStack(reqItem.what(), extracted));
}
}
this.updateInventorySubscription();
}
}

protected void flushInventory() {
if (this.updateMEStatus()) {
MEStorage storage = this.getMainNode().getGrid().getStorageService().getInventory();

var grid = getMainNode().getGrid();
if (grid != null) {
for (var aeSlot : aeItemHandler.getInventory()) {
GenericStack stock = aeSlot.getStock();
if (stock != null) {
storage.insert(stock.what(), stock.amount(), Actionable.MODULATE, actionSource);
grid.getStorageService().getInventory().insert(stock.what(), stock.amount(), Actionable.MODULATE,
actionSource);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,43 +72,47 @@ protected void autoIO() {
if (!this.shouldSyncME()) return;

if (this.updateMEStatus()) {
MEStorage aeNetwork = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEFluidSlot aeTank : this.aeFluidHandler.getInventory()) {
// Try to clear the wrong fluid
GenericStack exceedFluid = aeTank.exceedStack();
if (exceedFluid != null) {
long total = exceedFluid.amount();
long inserted = aeNetwork.insert(exceedFluid.what(), exceedFluid.amount(), Actionable.MODULATE,
this.actionSource);
if (inserted > 0) {
aeTank.drain(inserted, false);
continue;
} else {
aeTank.drain(total, false);
}
this.syncME();
this.updateTankSubscription();
}
}

protected void syncME() {
MEStorage networkInv = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEFluidSlot aeTank : this.aeFluidHandler.getInventory()) {
// Try to clear the wrong fluid
GenericStack exceedFluid = aeTank.exceedStack();
if (exceedFluid != null) {
long total = exceedFluid.amount();
long inserted = networkInv.insert(exceedFluid.what(), exceedFluid.amount(), Actionable.MODULATE,
this.actionSource);
if (inserted > 0) {
aeTank.drain(inserted, false);
continue;
} else {
aeTank.drain(total, false);
}
// Fill it
GenericStack reqFluid = aeTank.requestStack();
if (reqFluid != null) {
long extracted = aeNetwork.extract(reqFluid.what(), reqFluid.amount(), Actionable.MODULATE,
this.actionSource);
if (extracted > 0) {
aeTank.addStack(new GenericStack(reqFluid.what(), extracted));
}
}
// Fill it
GenericStack reqFluid = aeTank.requestStack();
if (reqFluid != null) {
long extracted = networkInv.extract(reqFluid.what(), reqFluid.amount(), Actionable.MODULATE,
this.actionSource);
if (extracted > 0) {
aeTank.addStack(new GenericStack(reqFluid.what(), extracted));
}
}
this.updateTankSubscription();
}
}

protected void flushInventory() {
if (this.updateMEStatus()) {
MEStorage storage = this.getMainNode().getGrid().getStorageService().getInventory();

var grid = getMainNode().getGrid();
if (grid != null) {
for (var aeSlot : aeFluidHandler.getInventory()) {
GenericStack stock = aeSlot.getStock();
if (stock != null) {
storage.insert(stock.what(), stock.amount(), Actionable.MODULATE, actionSource);
grid.getStorageService().getInventory().insert(stock.what(), stock.amount(), Actionable.MODULATE,
actionSource);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gregtechceu.gtceu.api.capability.recipe.IO;
import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.api.machine.feature.IMachineLife;
import com.gregtechceu.gtceu.api.machine.trait.NotifiableItemStackHandler;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.integration.ae2.gui.widget.list.AEListGridWidget;
Expand All @@ -19,6 +20,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;

import appeng.api.config.Actionable;
import appeng.api.stacks.AEItemKey;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;
Expand All @@ -34,7 +36,7 @@
*/
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class MEOutputBusPartMachine extends MEBusPartMachine {
public class MEOutputBusPartMachine extends MEBusPartMachine implements IMachineLife {

protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(
MEOutputBusPartMachine.class, MEBusPartMachine.MANAGED_FIELD_HOLDER);
Expand All @@ -56,6 +58,17 @@ protected NotifiableItemStackHandler createInventory(Object... args) {
return new InaccessibleInfiniteHandler(this);
}

@Override
public void onMachineRemoved() {
var grid = getMainNode().getGrid();
if (grid != null && !internalBuffer.isEmpty()) {
for (var entry : internalBuffer) {
grid.getStorageService().getInventory().insert(entry.getKey(), entry.getLongValue(),
Actionable.MODULATE, actionSource);
}
}
}

@Override
public ManagedFieldHolder getFieldHolder() {
return MANAGED_FIELD_HOLDER;
Expand All @@ -76,7 +89,7 @@ public void autoIO() {

if (this.updateMEStatus()) {
var grid = getMainNode().getGrid();
if (grid != null && !internalBuffer.storage.isEmpty()) {
if (grid != null && !internalBuffer.isEmpty()) {
internalBuffer.insertInventory(grid.getStorageService().getInventory(), actionSource);
}
this.updateInventorySubscription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gregtechceu.gtceu.api.capability.recipe.IO;
import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.api.machine.feature.IMachineLife;
import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.ingredient.FluidIngredient;
Expand All @@ -19,9 +20,8 @@

import net.minecraft.MethodsReturnNonnullByDefault;

import appeng.api.networking.IInWorldGridNodeHost;
import appeng.api.config.Actionable;
import appeng.api.stacks.AEFluidKey;
import appeng.me.helpers.IGridConnectedBlockEntity;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -30,8 +30,7 @@

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class MEOutputHatchPartMachine extends MEHatchPartMachine
implements IInWorldGridNodeHost, IGridConnectedBlockEntity {
public class MEOutputHatchPartMachine extends MEHatchPartMachine implements IMachineLife {

protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(
MEOutputHatchPartMachine.class, MEHatchPartMachine.MANAGED_FIELD_HOLDER);
Expand All @@ -54,6 +53,17 @@ protected NotifiableFluidTank createTank(long initialCapacity, int slots, Object
return new InaccessibleInfiniteTank(this);
}

@Override
public void onMachineRemoved() {
var grid = getMainNode().getGrid();
if (grid != null && !internalBuffer.isEmpty()) {
for (var entry : internalBuffer) {
grid.getStorageService().getInventory().insert(entry.getKey(), entry.getLongValue(),
Actionable.MODULATE, actionSource);
}
}
}

@Override
public ManagedFieldHolder getFieldHolder() {
return MANAGED_FIELD_HOLDER;
Expand All @@ -74,7 +84,7 @@ protected void autoIO() {

if (this.updateMEStatus()) {
var grid = getMainNode().getGrid();
if (grid != null && !internalBuffer.storage.isEmpty()) {
if (grid != null && !internalBuffer.isEmpty()) {
internalBuffer.insertInventory(grid.getStorageService().getInventory(), actionSource);
}
this.updateTankSubscription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,31 @@ public ManagedFieldHolder getFieldHolder() {

@Override
public void autoIO() {
if (!this.isWorkingEnabled()) return;
if (this.updateMEStatus()) {
// Update the visual display for the fake items. This also is important for the item handler's
// getStackInSlot() method, as it uses the cached items set here.
MEStorage aeNetwork = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEItemSlot slot : this.aeItemHandler.getInventory()) {
var config = slot.getConfig();
if (config == null) {
slot.setStock(null);
} else {
// Try to fill the slot
var key = config.what();
long result = aeNetwork.extract(key, Long.MAX_VALUE, Actionable.SIMULATE, actionSource);
slot.setStock(new GenericStack(key, result));
slot.onContentsChanged();
super.autoIO();
if (autoPull && getOffsetTimer() % 100 == 0) {
refreshList();
syncME();
}
}

@Override
protected void syncME() {
// Update the visual display for the fake items. This also is important for the item handler's
// getStackInSlot() method, as it uses the cached items set here.
MEStorage networkInv = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEItemSlot slot : this.aeItemHandler.getInventory()) {
var config = slot.getConfig();
if (config != null) {
// Try to fill the slot
var key = config.what();
long extracted = networkInv.extract(key, Long.MAX_VALUE, Actionable.SIMULATE, actionSource);
if (extracted > 0) {
slot.setStock(new GenericStack(key, extracted));
continue;
}
}

this.updateInventorySubscription();
slot.setStock(null);
}
if (!autoPull || !(getOffsetTimer() % 100 == 0)) return;
refreshList();
}

@Override
Expand Down Expand Up @@ -205,7 +208,6 @@ private void refreshList() {
var slot = this.aeItemHandler.getInventory()[index];
slot.setConfig(new GenericStack(what, 1));
slot.setStock(new GenericStack(what, request));
slot.onContentsChanged();
index++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,29 @@ public ManagedFieldHolder getFieldHolder() {

@Override
public void autoIO() {
if (!this.isWorkingEnabled()) return;

if (this.updateMEStatus()) {
MEStorage aeNetwork = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEFluidSlot slot : aeFluidHandler.getInventory()) {
var config = slot.getConfig();
if (config == null) {
slot.setStock(null);
} else {
// Try to fill the slot
var key = config.what();
long result = aeNetwork.extract(key, Long.MAX_VALUE, Actionable.SIMULATE, actionSource);
slot.setStock(new GenericStack(key, result));
slot.onContentsChanged();
super.autoIO();
if (autoPull && getOffsetTimer() % 100 == 0) {
refreshList();
syncME();
}
}

@Override
protected void syncME() {
MEStorage networkInv = this.getMainNode().getGrid().getStorageService().getInventory();
for (ExportOnlyAEFluidSlot slot : aeFluidHandler.getInventory()) {
var config = slot.getConfig();
if (config != null) {
// Try to fill the slot
var key = config.what();
long extracted = networkInv.extract(key, Long.MAX_VALUE, Actionable.SIMULATE, actionSource);
if (extracted > 0) {
slot.setStock(new GenericStack(key, extracted));
continue;
}
}
this.updateTankSubscription();
slot.setStock(null);
}

if (!autoPull || !(getOffsetTimer() % 100 == 0)) return;
refreshList();
}

@Override
Expand Down Expand Up @@ -189,7 +191,6 @@ private void refreshList() {
var slot = this.aeFluidHandler.getInventory()[index];
slot.setConfig(new GenericStack(what, 1));
slot.setStock(new GenericStack(what, request));
slot.onContentsChanged();
index++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ public boolean supportsFill(int tank) {
return false;
}

@Override
public List<FluidIngredient> handleRecipeInner(IO io, GTRecipe recipe, List<FluidIngredient> left,
@Nullable String slotName, boolean simulate) {
return handleIngredient(io, recipe, left, simulate, this.handlerIO, getStorages());
}

@Override
public FluidStack drainInternal(long maxDrain, boolean simulate) {
if (maxDrain == 0) {
Expand All @@ -94,6 +88,12 @@ public FluidStack drainInternal(long maxDrain, boolean simulate) {
return totalDrained == null ? FluidStack.empty() : totalDrained;
}

@Override
public List<FluidIngredient> handleRecipeInner(IO io, GTRecipe recipe, List<FluidIngredient> left,
@Nullable String slotName, boolean simulate) {
return handleIngredient(io, recipe, left, simulate, this.handlerIO, getStorages());
}

@Override
public IConfigurableSlot getConfigurableSlot(int index) {
return inventory[index];
Expand Down
Loading

0 comments on commit 6aad35c

Please sign in to comment.