diff --git a/src/main/java/techreborn/events/TRTickHandler.java b/src/main/java/techreborn/events/TRTickHandler.java index ad90eb73c..72a788594 100644 --- a/src/main/java/techreborn/events/TRTickHandler.java +++ b/src/main/java/techreborn/events/TRTickHandler.java @@ -30,10 +30,12 @@ import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; import techreborn.init.ModItems; +import techreborn.power.PowerDestroyEvent; import techreborn.power.PowerTickEvent; public class TRTickHandler { @@ -64,7 +66,9 @@ public class TRTickHandler { } long start = System.nanoTime(); + e.world.theProfiler.startSection("TechRebornPowerNet"); MinecraftForge.EVENT_BUS.post(new PowerTickEvent(e.world)); + e.world.theProfiler.endSection(); long elapsed = System.nanoTime() - start; tickTime += elapsed; ticksCounted++; @@ -76,4 +80,8 @@ public class TRTickHandler { } } + @SubscribeEvent + public void unload(WorldEvent.Unload event){ + MinecraftForge.EVENT_BUS.post(new PowerDestroyEvent(event.getWorld())); + } } diff --git a/src/main/java/techreborn/parts/powerCables/CableMultipart.java b/src/main/java/techreborn/parts/powerCables/CableMultipart.java index 88769070e..1d6fa463c 100644 --- a/src/main/java/techreborn/parts/powerCables/CableMultipart.java +++ b/src/main/java/techreborn/parts/powerCables/CableMultipart.java @@ -410,13 +410,13 @@ public abstract class CableMultipart extends Multipart } } if (network == null) { - network = new TRPowerNet(getCableType()); + network = new TRPowerNet(getCableType(), world); network.addElement(this); } for (EnumFacing dir : EnumFacing.VALUES) { TileEntity te = getNeighbourTile(dir); if (te instanceof IEnergyInterfaceTile) { - network.addConnection((IEnergyInterfaceTile) te, dir.getOpposite()); + network.addConnection((IEnergyInterfaceTile) te, dir); } } } diff --git a/src/main/java/techreborn/power/PowerDestroyEvent.java b/src/main/java/techreborn/power/PowerDestroyEvent.java new file mode 100644 index 000000000..6da9a6aef --- /dev/null +++ b/src/main/java/techreborn/power/PowerDestroyEvent.java @@ -0,0 +1,39 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2017 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package techreborn.power; + +import net.minecraft.world.World; +import net.minecraftforge.fml.common.eventhandler.Event; + +public class PowerDestroyEvent extends Event { + World world; + public PowerDestroyEvent(World world) { + this.world = world; + } + + public World getWorld() { + return world; + } +} diff --git a/src/main/java/techreborn/power/TRPowerNet.java b/src/main/java/techreborn/power/TRPowerNet.java index d94be3475..cda60bf4b 100644 --- a/src/main/java/techreborn/power/TRPowerNet.java +++ b/src/main/java/techreborn/power/TRPowerNet.java @@ -28,6 +28,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -45,10 +46,13 @@ public class TRPowerNet { EnumCableType cableType; private ArrayList cables = new ArrayList(); private int energy = 0; + private World world; - public TRPowerNet(EnumCableType cableType) { + + public TRPowerNet(EnumCableType cableType, World world) { this.cableType = cableType; MinecraftForge.EVENT_BUS.register(this); + this.world = world; } public static void buildEndpoint(TRPowerNet net) { @@ -83,7 +87,7 @@ public class TRPowerNet { @SubscribeEvent public synchronized void tick(PowerTickEvent evt) { - evt.getWorld().theProfiler.startSection("TechRebornPowerNet"); + if (tick < 20) { tick++; return; @@ -100,6 +104,7 @@ public class TRPowerNet { it.remove(); } } + buildEndpoint(this); } if (!cables.isEmpty()) { ArrayList collectibles = new ArrayList(); @@ -126,7 +131,14 @@ public class TRPowerNet { MinecraftForge.EVENT_BUS.unregister(this); } tick++; - evt.getWorld().theProfiler.endSection(); + } + + @SubscribeEvent + public void destory(PowerDestroyEvent event){ + if(event.getWorld() == world){ + world = null; + MinecraftForge.EVENT_BUS.unregister(this); + } } public void addElement(CableMultipart te) { diff --git a/src/main/java/techreborn/tiles/storage/TileEnergyStorage.java b/src/main/java/techreborn/tiles/storage/TileEnergyStorage.java index 8b9d0ed0c..3167f1312 100644 --- a/src/main/java/techreborn/tiles/storage/TileEnergyStorage.java +++ b/src/main/java/techreborn/tiles/storage/TileEnergyStorage.java @@ -129,8 +129,7 @@ public class TileEnergyStorage extends TilePowerAcceptor implements IWrenchable, @Override public boolean canAcceptEnergy(EnumFacing direction) { - - return getFacingEnum() != direction; + return getFacing() != direction; } @Override @@ -182,4 +181,4 @@ public class TileEnergyStorage extends TilePowerAcceptor implements IWrenchable, return true; } -} \ No newline at end of file +}