Skip to content

Commit a27a6f1

Browse files
authored
Merge pull request #6 from GlodBlock/bugfix
fix drops leak to item cell when discretizer go offline
2 parents 0937feb + d919914 commit a27a6f1

File tree

5 files changed

+131
-20
lines changed

5 files changed

+131
-20
lines changed

dependencies.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ dependencies {
2222
compileOnly("com.github.GTNewHorizons:EnderIO:2.3.1.29:dev") {transitive = false}
2323
compileOnly("com.github.GTNewHorizons:GTplusplus:1.7.49:dev") {transitive = false}
2424

25-
2625
runtime("com.github.GTNewHorizons:Baubles:1.0.1.14:dev")
2726
}

src/main/java/com/glodblock/github/common/tile/TileFluidDiscretizer.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,6 @@ public void onStorageUpdate(MENetworkStorageEvent event) {
129129
updateState();
130130
}
131131

132-
/*@MENetworkEventSubscribe
133-
public void onBootUp(MENetworkBootingStatusChange event) {
134-
try {
135-
IMEInventory<IAEItemStack> inventory = this.getProxy().getStorage().getItemInventory();
136-
IItemList<IAEItemStack> items = inventory.getAvailableItems(AEApi.instance().storage().createItemList());
137-
IItemList<IAEItemStack> drops = AEApi.instance().storage().createItemList();
138-
for (IAEItemStack item : items) {
139-
if (item != null && item.getItem() instanceof ItemFluidDrop) {
140-
drops.add(item);
141-
}
142-
}
143-
for (IAEItemStack drop : drops) {
144-
inventory.extractItems(drop, Actionable.MODULATE, ownActionSource);
145-
inventory.injectItems(drop, Actionable.MODULATE, ownActionSource);
146-
}
147-
} catch (GridAccessException ignored) {
148-
}
149-
}*/
150-
151132
private class FluidDiscretizingInventory implements IMEInventory<IAEItemStack>, IMEMonitorHandlerReceiver<IAEFluidStack> {
152133

153134
private final MEInventoryHandler<IAEItemStack> invHandler = new MEInventoryHandler<>(this, getChannel());

src/main/java/com/glodblock/github/coremod/hooker/CoreModHooks.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.glodblock.github.coremod.hooker;
22

3+
import appeng.api.config.Actionable;
34
import appeng.api.networking.IGrid;
45
import appeng.api.networking.IGridHost;
56
import appeng.api.networking.IGridNode;
67
import appeng.api.networking.IMachineSet;
8+
import appeng.api.networking.storage.IStorageGrid;
9+
import appeng.api.storage.IMEInventory;
10+
import appeng.api.storage.data.IAEFluidStack;
711
import appeng.api.storage.data.IAEItemStack;
12+
import appeng.crafting.MECraftingInventory;
813
import appeng.me.MachineSet;
14+
import appeng.me.cluster.implementations.CraftingCPUCluster;
915
import appeng.parts.misc.PartInterface;
1016
import appeng.tile.misc.TileInterface;
1117
import appeng.util.InventoryAdaptor;
@@ -16,6 +22,7 @@
1622
import com.glodblock.github.inventory.FluidConvertingInventoryAdaptor;
1723
import com.glodblock.github.inventory.FluidConvertingInventoryCrafting;
1824
import com.glodblock.github.loader.ItemAndBlockHolder;
25+
import com.glodblock.github.util.Ae2Reflect;
1926
import com.glodblock.github.util.SetBackedMachineSet;
2027
import com.glodblock.github.util.Util;
2128
import com.google.common.collect.Sets;
@@ -104,4 +111,51 @@ public static long getFluidSize(IAEItemStack aeStack) {
104111
else return aeStack.getStackSize();
105112
}
106113

114+
public static void storeFluidItem(CraftingCPUCluster instance) {
115+
final IGrid g = Ae2Reflect.getGrid(instance);
116+
117+
if( g == null )
118+
{
119+
return;
120+
}
121+
122+
final IStorageGrid sg = g.getCache( IStorageGrid.class );
123+
final IMEInventory<IAEItemStack> ii = sg.getItemInventory();
124+
final IMEInventory<IAEFluidStack> jj = sg.getFluidInventory();
125+
final MECraftingInventory inventory = Ae2Reflect.getCPUInventory(instance);
126+
127+
for( IAEItemStack is : inventory.getItemList() )
128+
{
129+
is = inventory.extractItems( is.copy(), Actionable.MODULATE, Ae2Reflect.getCPUSource(instance) );
130+
131+
if( is != null )
132+
{
133+
Ae2Reflect.postCPUChange(instance, is, Ae2Reflect.getCPUSource(instance));
134+
if (is.getItem() instanceof ItemFluidDrop) {
135+
IAEFluidStack fluidDrop = ItemFluidDrop.getAeFluidStack(is);
136+
fluidDrop = jj.injectItems(fluidDrop, Actionable.MODULATE, Ae2Reflect.getCPUSource(instance));
137+
if (fluidDrop == null) {
138+
is = null;
139+
} else {
140+
is.setStackSize(fluidDrop.getStackSize());
141+
}
142+
} else {
143+
is = ii.injectItems(is, Actionable.MODULATE, Ae2Reflect.getCPUSource(instance));
144+
}
145+
}
146+
147+
if( is != null )
148+
{
149+
inventory.injectItems( is, Actionable.MODULATE, Ae2Reflect.getCPUSource(instance) );
150+
}
151+
}
152+
153+
if( inventory.getItemList().isEmpty() )
154+
{
155+
Ae2Reflect.setCPUInventory(instance, new MECraftingInventory());
156+
}
157+
158+
Ae2Reflect.markCPUDirty(instance);
159+
}
160+
107161
}

src/main/java/com/glodblock/github/coremod/transform/CraftingCpuTransformer.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ private static class TransformCraftingCPUCluster extends ClassVisitor {
2929
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
3030
if (name.equals("executeCrafting")) {
3131
return new TransformExecuteCrafting(api, super.visitMethod(access, name, desc, signature, exceptions));
32+
} else if (name.equals("cancel")) {
33+
return new TransformStoreItems(api, super.visitMethod(access, name, desc, signature, exceptions));
3234
}
3335
return super.visitMethod(access, name, desc, signature, exceptions);
3436
}
@@ -104,4 +106,27 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc,
104106

105107
}
106108

109+
private static class TransformStoreItems extends MethodVisitor {
110+
111+
TransformStoreItems(int api, MethodVisitor mv) {
112+
super(api, mv);
113+
}
114+
115+
@Override
116+
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
117+
if (opcode == Opcodes.INVOKESPECIAL
118+
&& owner.equals("appeng/me/cluster/implementations/CraftingCPUCluster") && name.equals("storeItems")) {
119+
super.visitMethodInsn(Opcodes.INVOKESTATIC,
120+
"com/glodblock/github/coremod/hooker/CoreModHooks",
121+
"storeFluidItem",
122+
"(Lappeng/me/cluster/implementations/CraftingCPUCluster;)V",
123+
false
124+
);
125+
} else {
126+
super.visitMethodInsn(opcode, owner, name, desc, itf);
127+
}
128+
}
129+
130+
}
131+
107132
}

src/main/java/com/glodblock/github/util/Ae2Reflect.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.glodblock.github.util;
22

33
import appeng.api.implementations.IUpgradeableHost;
4+
import appeng.api.networking.IGrid;
45
import appeng.api.networking.crafting.ICraftingCPU;
6+
import appeng.api.networking.security.BaseActionSource;
7+
import appeng.api.networking.security.MachineSource;
58
import appeng.api.storage.IMEInventory;
9+
import appeng.api.storage.data.IAEItemStack;
610
import appeng.container.implementations.ContainerUpgradeable;
711
import appeng.container.implementations.CraftingCPURecord;
12+
import appeng.crafting.MECraftingInventory;
13+
import appeng.me.cluster.implementations.CraftingCPUCluster;
814
import appeng.me.storage.MEInventoryHandler;
915
import appeng.me.storage.MEPassThrough;
1016
import appeng.util.inv.ItemSlot;
@@ -23,7 +29,12 @@ public class Ae2Reflect {
2329
private static final Field fCPU_myName;
2430
private static final Field fCPU_processors;
2531
private static final Field fCPU_size;
32+
private static final Field fCPU_inventory;
33+
private static final Field fCPU_machineSrc;
2634
private static final Method mItemSlot_setExtractable;
35+
private static final Method mCPU_getGrid;
36+
private static final Method mCPU_postChange;
37+
private static final Method mCPU_markDirty;
2738

2839
static {
2940
try {
@@ -34,7 +45,12 @@ public class Ae2Reflect {
3445
fCPU_myName = Ae2Reflect.reflectField(CraftingCPURecord.class, "myName");
3546
fCPU_processors = Ae2Reflect.reflectField(CraftingCPURecord.class, "processors");
3647
fCPU_size = Ae2Reflect.reflectField(CraftingCPURecord.class, "size");
48+
fCPU_inventory = Ae2Reflect.reflectField(CraftingCPUCluster.class, "inventory");
49+
fCPU_machineSrc = Ae2Reflect.reflectField(CraftingCPUCluster.class, "machineSrc");
3750
mItemSlot_setExtractable = reflectMethod(ItemSlot.class, "setExtractable", boolean.class);
51+
mCPU_getGrid = reflectMethod(CraftingCPUCluster.class, "getGrid");
52+
mCPU_postChange = reflectMethod(CraftingCPUCluster.class, "postChange", IAEItemStack.class, BaseActionSource.class);
53+
mCPU_markDirty = reflectMethod(CraftingCPUCluster.class, "markDirty");
3854
} catch (Exception e) {
3955
throw new IllegalStateException("Failed to initialize AE2 reflection hacks!", e);
4056
}
@@ -114,4 +130,40 @@ public static IUpgradeableHost getUpgradeList(ContainerUpgradeable container) {
114130
return Ae2Reflect.readField(container, fInventory_containerUpgrade);
115131
}
116132

133+
public static IGrid getGrid(CraftingCPUCluster cpu) {
134+
try {
135+
return (IGrid) mCPU_getGrid.invoke(cpu);
136+
} catch (Exception e) {
137+
throw new IllegalStateException("Failed to invoke method: " + mCPU_getGrid, e);
138+
}
139+
}
140+
141+
public static MECraftingInventory getCPUInventory(CraftingCPUCluster cpu) {
142+
return Ae2Reflect.readField(cpu, fCPU_inventory);
143+
}
144+
145+
public static void setCPUInventory(CraftingCPUCluster cpu, MECraftingInventory value) {
146+
Ae2Reflect.writeField(cpu, fCPU_inventory, value);
147+
}
148+
149+
public static MachineSource getCPUSource(CraftingCPUCluster cpu) {
150+
return Ae2Reflect.readField(cpu, fCPU_machineSrc);
151+
}
152+
153+
public static void postCPUChange(CraftingCPUCluster cpu, IAEItemStack stack, MachineSource src) {
154+
try {
155+
mCPU_postChange.invoke(cpu, stack, src);
156+
} catch (Exception e) {
157+
throw new IllegalStateException("Failed to invoke method: " + mCPU_postChange, e);
158+
}
159+
}
160+
161+
public static void markCPUDirty(CraftingCPUCluster cpu) {
162+
try {
163+
mCPU_markDirty.invoke(cpu);
164+
} catch (Exception e) {
165+
throw new IllegalStateException("Failed to invoke method: " + mCPU_markDirty, e);
166+
}
167+
}
168+
117169
}

0 commit comments

Comments
 (0)