Renew Replenisher (#435)

This commit is contained in:
lc-1337 authored and GitHub committed 2026-05-27 08:41:28 +02:00
1 parent 4a030a52b6
commit 0a84d208f3
6 files changed
+243 -220

No files matched your search

@@ -1,6 +1,5 @@
package com.glodblock.github.common.block;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
@@ -47,15 +46,6 @@ public class BlockSuperStockReplenisher extends FCBaseBlock {
return false;
}
@Override
public void breakBlock(World w, int x, int y, int z, Block a, int b) {
final TileSuperStockReplenisher tss = this.getTileEntity(w, x, y, z);
if (tss != null) {
tss.fullRefund();
}
super.breakBlock(w, x, y, z, a, b);
}
@Override
public BlockSuperStockReplenisher register() {
GameRegistry.registerBlock(this, AEBaseItemBlock.class, NameConst.BLOCK_SUPER_STOCK_REPLENISHER);
@@ -15,12 +15,16 @@ import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import org.jetbrains.annotations.Nullable;
import com.glodblock.github.common.item.FCBaseItemCell;
import com.glodblock.github.common.item.ItemFluidPacket;
import com.glodblock.github.inventory.AEFluidInventory;
import com.glodblock.github.inventory.IAEFluidInventory;
import com.glodblock.github.inventory.IAEFluidTank;
import com.gtnewhorizon.gtnhlib.item.ItemStackNBT;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
import appeng.api.implementations.IPowerChannelState;
@@ -36,8 +40,10 @@ import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.ITerminalHost;
import appeng.api.storage.StorageName;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
import appeng.api.util.IConfigManager;
@@ -47,28 +53,48 @@ import appeng.me.GridAccessException;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkInvTile;
import appeng.tile.inventory.AppEngInternalAEInventory;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.BiggerAppEngInventory;
import appeng.tile.inventory.IAEStackInventory;
import appeng.tile.inventory.IIAEStackInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.Platform;
import appeng.util.item.AEFluidStack;
import appeng.util.item.AEItemStack;
import io.netty.buffer.ByteBuf;
public class TileSuperStockReplenisher extends AENetworkInvTile
implements IAEFluidInventory, IFluidHandler, IPowerChannelState, IGridTickable, ITerminalHost {
public class TileSuperStockReplenisher extends AENetworkInvTile implements IAEFluidInventory, IFluidHandler,
IPowerChannelState, IGridTickable, ITerminalHost, IIAEStackInventory {
private final AppEngInternalInventory cell = new AppEngInternalInventory(this, 1);
private final BiggerAppEngInventory invItems = new BiggerAppEngInventory(this, 63);
private final AEFluidInventory invFluids = new AEFluidInventory(this, 9, Integer.MAX_VALUE);
private final AppEngInternalAEInventory configFluids = new AppEngInternalAEInventory(this, 9);
private final AppEngInternalAEInventory configItems = new AppEngInternalAEInventory(this, 63);
private final IAEStackInventory configFluids = new IAEStackInventory(this, 9, StorageName.NONE) {
@Override
public void readFromNBT(@Nullable NBTTagCompound data, String name) {
super.readFromNBT(data, name);
for (int i = 0; i < this.getSizeInventory(); i++) {
final IAEStack<?> aes = this.getAEStackInSlot(i);
if (aes instanceof IAEItemStack ais && ais.getItem() instanceof ItemFluidPacket) {
final IAEFluidStack ifs = ItemFluidPacket.getFluidAEStack(ais);
if (ifs != null && ifs.getStackSize() > Integer.MAX_VALUE) ifs.setStackSize(Integer.MAX_VALUE);
this.putAEStackInSlot(i, ifs);
}
}
}
};
private final IAEStackInventory configItems = new IAEStackInventory(this, 63, StorageName.CONFIG);
private final BaseActionSource source;
private boolean isPowered;
private long totalBytes;
private long storedFluidCount;
private long storedItemCount;
private boolean needReCountStoredFluids = true;
private boolean needReCountStoredItems = true;
public TileSuperStockReplenisher() {
super(false);
getProxy().setIdlePowerUsage(4D);
@@ -78,18 +104,28 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
}
private TickRateModulation doWork() {
this.fletchFluids();
this.fletchItems();
this.needReCountStoredFluids = true;
this.needReCountStoredItems = true;
return TickRateModulation.SAME;
}
private void fletchFluids() {
for (int i = 0; i < 9; i++) {
IAEFluidStack invFluid = invFluids.getFluidInSlot(i);
IAEItemStack configFluid = configFluids.getAEStackInSlot(i);
if (configFluid != null) {
IAEFluidStack fs = AEFluidStack.create(ItemFluidPacket.getFluidStack(configFluid));
if (invFluid == null) requestFluid(fs, i);
else if (invFluid.equals(fs)) {
IAEStack<?> configFluid = configFluids.getAEStackInSlot(i);
if (configFluid instanceof IAEFluidStack fs) {
IAEFluidStack ifs = fs.copy();
if (invFluid == null) requestFluid(ifs, i);
else if (invFluid.equals(ifs)) {
long invSize = invFluid.getStackSize();
long confSize = fs.getStackSize();
long confSize = ifs.getStackSize();
if (invSize < confSize / 2f) {
fs.setStackSize(confSize - invSize);
requestFluid(fs, i);
ifs.setStackSize(confSize - invSize);
requestFluid(ifs, i);
} else if (invSize > confSize) {
returnFluid(i, invSize - confSize);
}
@@ -100,13 +136,14 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
returnFluid(i, Long.MAX_VALUE);
}
}
}
private void fletchItems() {
for (int i = 0; i < 63; i++) {
ItemStack invItem = invItems.getStackInSlot(i);
IAEItemStack configItem = configItems.getAEStackInSlot(i);
if (configItem != null) {
IAEItemStack is = configItem.copy();
IAEStack<?> configItem = configItems.getAEStackInSlot(i);
if (configItem instanceof IAEItemStack ais) {
IAEItemStack is = ais.copy();
if (invItem == null) requestItem(is, i);
else if (is.equals(invItem)) {
int confSize = (int) is.getStackSize();
@@ -123,8 +160,28 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
returnItem(i, Integer.MAX_VALUE);
}
}
}
return TickRateModulation.SAME;
private void countFluids() {
if (this.needReCountStoredFluids) {
this.needReCountStoredFluids = false;
this.storedFluidCount = 0;
for (int i = 0; i < this.invFluids.getSlots(); i++) {
final IAEFluidStack fs = this.invFluids.getFluidInSlot(i);
if (fs != null) this.storedFluidCount += fs.getStackSize();
}
}
}
private void countItems() {
if (this.needReCountStoredItems) {
this.needReCountStoredItems = false;
this.storedItemCount = 0;
for (int i = 0; i < this.invItems.getSizeInventory(); i++) {
final ItemStack is = this.invItems.getStackInSlot(i);
if (is != null) this.storedItemCount += is.stackSize;
}
}
}
private void returnFluid(int index, long amount) {
@@ -161,21 +218,23 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
is.stackSize = is.stackSize - amount;
is = is.copy();
is.stackSize = amount;
storedItemCount -= is.stackSize;
} else {
invItems.setInventorySlotContents(index, null);
}
this.storedItemCount -= is.stackSize;
IAEItemStack notInserted = this.getProxy().getStorage().getItemInventory()
.injectItems(AEItemStack.create(is), Actionable.MODULATE, this.source);
if (notInserted != null) {
ItemStack tempStack = invItems.getStackInSlot(index);
if (tempStack != null) {
tempStack.stackSize = tempStack.stackSize + (int) notInserted.getStackSize();
storedItemCount += notInserted.getStackSize();
saveChanges();
} else invItems.setInventorySlotContents(index, notInserted.getItemStack());
this.storedItemCount += notInserted.getStackSize();
}
} catch (final GridAccessException ignored) {}
}
@@ -191,10 +250,11 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
ItemStack tempStack = invItems.getStackInSlot(index);
if (tempStack != null) {
tempStack.stackSize = tempStack.stackSize + (int) extracted.getStackSize();
storedItemCount += extracted.getStackSize();
saveChanges();
} else invItems.setInventorySlotContents(index, extracted.getItemStack());
this.storedItemCount += extracted.getStackSize();
}
} catch (final GridAccessException ignored) {}
}
@@ -210,12 +270,12 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
return invItems;
}
public AppEngInternalAEInventory getConfigFluid() {
return configFluids;
public IAEStackInventory getConfigFluids() {
return this.configFluids;
}
public AppEngInternalAEInventory getConfigItems() {
return configItems;
public IAEStackInventory getConfigItems() {
return this.configItems;
}
public AppEngInternalInventory getCell() {
@@ -223,6 +283,9 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
}
public long getFreeBytes() {
this.countFluids();
this.countItems();
return totalBytes - (((storedItemCount + getUnusedItemCount()) / 8)
+ ((storedFluidCount + getUnusedFluidCount()) / 2048));
}
@@ -273,14 +336,6 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
totalBytes = 0;
getProxy().setIdlePowerUsage(4d);
}
} else if (inv == invItems) {
if (added != null) {
storedItemCount += added.stackSize;
}
if (removed != null) {
storedItemCount -= removed.stackSize;
}
}
}
case decreaseStackSize -> {
@@ -288,10 +343,6 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
totalBytes = 0;
getProxy().setIdlePowerUsage(4d);
}
if (inv == invItems) {
storedItemCount -= removed.stackSize;
}
}
case markDirty -> markDirty();
}
@@ -319,12 +370,26 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
@Override
public void getDrops(World w, int x, int y, int z, List<ItemStack> drops) {
this.fullRefund();
if (cell.getStackInSlot(0) != null) drops.add(cell.getStackInSlot(0));
final ItemStack container = AEApi.instance().definitions().items().itemMEStackPacket().maybeStack(1).get();
for (int i = 0; i < 9; i++) {
ItemStack ifp = ItemFluidPacket.newStack(invFluids.getFluidStackInSlot(i));
if (ifp != null) drops.add(ifp);
final FluidStack fs = this.invFluids.getFluidStackInSlot(i);
if (fs == null) continue;
final ItemStack temp = container.copy();
Platform.writeStackNBT(AEFluidStack.create(fs), ItemStackNBT.get(temp));
drops.add(temp);
}
for (int i = 0; i < 63; i++) {
final ItemStack is = this.invItems.getStackInSlot(i);
if (is == null) continue;
final ItemStack temp = container.copy();
Platform.writeStackNBT(AEItemStack.create(is), ItemStackNBT.get(temp));
drops.add(temp);
}
super.getDrops(w, x, y, z, drops);
}
@Override
@@ -339,16 +404,12 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
FluidStack fs = invFluids.drain(from, resource, doDrain);
if (doDrain && fs != null) storedFluidCount -= fs.amount;
return fs;
return invFluids.drain(from, resource, doDrain);
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
FluidStack fs = invFluids.drain(from, maxDrain, doDrain);
if (doDrain && fs != null) storedFluidCount -= fs.amount;
return fs;
return invFluids.drain(from, maxDrain, doDrain);
}
@Override
@@ -368,7 +429,7 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
@Override
public void onFluidInventoryChanged(IAEFluidTank inv, int slot) {
saveChanges();
this.markDirty();
}
@Override
@@ -396,8 +457,6 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
configFluids.readFromNBT(data, "configFluids");
cell.readFromNBT(data, "cellHolder");
totalBytes = data.getLong("totalBytes");
storedFluidCount = data.getLong("storedFluidCount");
storedItemCount = data.getLong("storedItemCount");
getProxy().setIdlePowerUsage(data.getDouble("powerDraw"));
}
@@ -409,8 +468,6 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
configFluids.writeToNBT(data, "configFluids");
cell.writeToNBT(data, "cellHolder");
data.setLong("totalBytes", totalBytes);
data.setLong("storedFluidCount", storedFluidCount);
data.setLong("storedItemCount", storedItemCount);
data.setDouble("powerDraw", getProxy().getIdlePowerUsage());
return data;
}
@@ -482,4 +539,18 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
public IConfigManager getConfigManager() {
return null;
}
@Override
public void saveAEStackInv() {
this.markDirty();
}
@Override
public IAEStackInventory getAEInventoryByName(StorageName name) {
return switch (name) {
case NONE -> this.configFluids;
case CONFIG -> this.configItems;
default -> null;
};
}
}