From ded594c86c701a0ff1f2282b4d6bff9cc7b1b9f6 Mon Sep 17 00:00:00 2001 From: Dimach Date: Sun, 27 Aug 2017 01:55:20 +0300 Subject: [PATCH 1/2] Patched ME energy tunnel, now they can emit GT energy. --- .../java/appeng/parts/p2p/PartP2PGTPower.java | 86 +++++++++++++++++++ src/main/java/gregtech/GT_Mod.java | 1 + .../postload/GT_AE2EnergyTunnelLoader.java | 29 +++++++ 3 files changed, 116 insertions(+) create mode 100644 src/main/java/appeng/parts/p2p/PartP2PGTPower.java create mode 100644 src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java diff --git a/src/main/java/appeng/parts/p2p/PartP2PGTPower.java b/src/main/java/appeng/parts/p2p/PartP2PGTPower.java new file mode 100644 index 00000000..a4fc3ce2 --- /dev/null +++ b/src/main/java/appeng/parts/p2p/PartP2PGTPower.java @@ -0,0 +1,86 @@ +package appeng.parts.p2p; + +import appeng.api.networking.IGridNode; +import appeng.api.networking.ticking.IGridTickable; +import appeng.api.networking.ticking.TickRateModulation; +import appeng.api.networking.ticking.TickingRequest; +import gregtech.api.interfaces.tileentity.IEnergyConnected; +import gregtech.api.util.GT_Utility; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +public class PartP2PGTPower extends PartP2PIC2Power implements IGridTickable { + public PartP2PGTPower(ItemStack is) { + super(is); + } + + public final World getWorld() { + return tile.getWorldObj(); + } + + public final int getXCoord() { + return tile.xCoord; + } + + public final short getYCoord() { + return (short) tile.yCoord; + } + + public final int getZCoord() { + return tile.zCoord; + } + + public final int getOffsetX(byte aSide, int aMultiplier) { + return getXCoord() + ForgeDirection.getOrientation(aSide).offsetX * aMultiplier; + } + + public final short getOffsetY(byte aSide, int aMultiplier) { + return (short) (getYCoord() + ForgeDirection.getOrientation(aSide).offsetY * aMultiplier); + } + + public final int getOffsetZ(byte aSide, int aMultiplier) { + return getZCoord() + ForgeDirection.getOrientation(aSide).offsetZ * aMultiplier; + } + + public final TileEntity getTileEntity(int aX, int aY, int aZ) { + return getWorld().getTileEntity(aX, aY, aZ); + } + + public final TileEntity getTileEntityAtSide(byte aSide) { + int tX = getOffsetX(aSide, 1), tY = getOffsetY(aSide, 1), tZ = getOffsetZ(aSide, 1); + return getWorld().getTileEntity(tX, tY, tZ); + } + + public boolean outputEnergy() { + if (OutputEnergyA == 0) { + return false; + } + TileEntity t = getTileEntityAtSide((byte) side.ordinal()); + if (t instanceof IEnergyConnected) { + long l = ((IEnergyConnected) t).injectEnergyUnits(GT_Utility.getOppositeSide(side.ordinal()), (long) OutputVoltageA, ((long) OutputEnergyA) / ((long) OutputVoltageA)); + if (l > 0) { + OutputEnergyA -= ((long) OutputVoltageA) * l; + if (OutputEnergyA < 1) { + OutputEnergyA = OutputEnergyB; + OutputVoltageA = OutputVoltageB; + OutputEnergyB = 0; + OutputVoltageB = 0; + } + return true; + } + } + return false; + } + + @Override + public TickingRequest getTickingRequest(IGridNode iGridNode) { + return new TickingRequest(1, 10, false, false); + } + + @Override + public TickRateModulation tickingRequest(IGridNode iGridNode, int i) { + return outputEnergy() ? TickRateModulation.FASTER : TickRateModulation.SLOWER; + } +} diff --git a/src/main/java/gregtech/GT_Mod.java b/src/main/java/gregtech/GT_Mod.java index 7ab912b7..5c3a660a 100644 --- a/src/main/java/gregtech/GT_Mod.java +++ b/src/main/java/gregtech/GT_Mod.java @@ -597,6 +597,7 @@ public class GT_Mod implements IGT_Mod { new GT_CropLoader().run(); new GT_Worldgenloader().run(); new GT_CoverLoader().run(); + new GT_AE2EnergyTunnelLoader().run(); LoadArmorComponents.init(); GT_RecipeRegistrator.registerUsagesForMaterials(new ItemStack(Blocks.planks, 1), null, false); diff --git a/src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java b/src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java new file mode 100644 index 00000000..c6f753d7 --- /dev/null +++ b/src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java @@ -0,0 +1,29 @@ +package gregtech.loaders.postload; + +import appeng.items.parts.PartType; +import appeng.parts.p2p.PartP2PGTPower; +import gregtech.GT_Mod; +import gregtech.api.util.GT_Log; + +import java.lang.reflect.Field; + +public class GT_AE2EnergyTunnelLoader implements Runnable { + @Override + public void run() { + if (GT_Mod.gregtechproxy.mAE2Integration) { + try { + load(); + } catch (Throwable e) { + GT_Log.out.println("Failed to load P2P tunnel for GT electricity"); + e.printStackTrace(GT_Log.out); + } + } + } + + public void load() throws Throwable { + Field f = PartType.class.getDeclaredField("myPart"); + f.setAccessible(true); + f.set(PartType.P2PTunnelEU, PartP2PGTPower.class); + } +} + From c6d39f12fdf85edbeadc725e0b398fa70eb15bd0 Mon Sep 17 00:00:00 2001 From: Dimach Date: Wed, 6 Sep 2017 10:47:03 +0300 Subject: [PATCH 2/2] Replaced protected fields access with methods calls, now code more safe. --- .../postload/GT_AE2EnergyTunnelLoader.java | 1 - .../loaders/postload}/PartP2PGTPower.java | 22 +++++++++---------- 2 files changed, 10 insertions(+), 13 deletions(-) rename src/main/java/{appeng/parts/p2p => gregtech/loaders/postload}/PartP2PGTPower.java (78%) diff --git a/src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java b/src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java index c6f753d7..bdaa43c4 100644 --- a/src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java +++ b/src/main/java/gregtech/loaders/postload/GT_AE2EnergyTunnelLoader.java @@ -1,7 +1,6 @@ package gregtech.loaders.postload; import appeng.items.parts.PartType; -import appeng.parts.p2p.PartP2PGTPower; import gregtech.GT_Mod; import gregtech.api.util.GT_Log; diff --git a/src/main/java/appeng/parts/p2p/PartP2PGTPower.java b/src/main/java/gregtech/loaders/postload/PartP2PGTPower.java similarity index 78% rename from src/main/java/appeng/parts/p2p/PartP2PGTPower.java rename to src/main/java/gregtech/loaders/postload/PartP2PGTPower.java index a4fc3ce2..602b4816 100644 --- a/src/main/java/appeng/parts/p2p/PartP2PGTPower.java +++ b/src/main/java/gregtech/loaders/postload/PartP2PGTPower.java @@ -1,9 +1,10 @@ -package appeng.parts.p2p; +package gregtech.loaders.postload; import appeng.api.networking.IGridNode; import appeng.api.networking.ticking.IGridTickable; import appeng.api.networking.ticking.TickRateModulation; import appeng.api.networking.ticking.TickingRequest; +import appeng.parts.p2p.PartP2PIC2Power; import gregtech.api.interfaces.tileentity.IEnergyConnected; import gregtech.api.util.GT_Utility; import net.minecraft.item.ItemStack; @@ -54,20 +55,17 @@ public class PartP2PGTPower extends PartP2PIC2Power implements IGridTickable { } public boolean outputEnergy() { - if (OutputEnergyA == 0) { + if (getOfferedEnergy() == 0) { return false; } TileEntity t = getTileEntityAtSide((byte) side.ordinal()); if (t instanceof IEnergyConnected) { - long l = ((IEnergyConnected) t).injectEnergyUnits(GT_Utility.getOppositeSide(side.ordinal()), (long) OutputVoltageA, ((long) OutputEnergyA) / ((long) OutputVoltageA)); - if (l > 0) { - OutputEnergyA -= ((long) OutputVoltageA) * l; - if (OutputEnergyA < 1) { - OutputEnergyA = OutputEnergyB; - OutputVoltageA = OutputVoltageB; - OutputEnergyB = 0; - OutputVoltageB = 0; - } + long voltage = 8 << (getSourceTier() * 2); + if (voltage > getOfferedEnergy()) { + voltage = (long) getOfferedEnergy(); + } + if (((IEnergyConnected) t).injectEnergyUnits(GT_Utility.getOppositeSide(side.ordinal()), voltage, 1) > 0) { + drawEnergy(voltage); return true; } } @@ -76,7 +74,7 @@ public class PartP2PGTPower extends PartP2PIC2Power implements IGridTickable { @Override public TickingRequest getTickingRequest(IGridNode iGridNode) { - return new TickingRequest(1, 10, false, false); + return new TickingRequest(1, 20, false, false); } @Override