From a7c8a3c6e4bf53d23c2f78598efe67ce09a56daf Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 29 Oct 2015 17:36:30 +0100 Subject: [PATCH] Actually fixed Laser Network bugging sometimes for real now instead of thinking of stupid workarounds that don't really work --- .../actuallyadditions/ActuallyAdditions.java | 15 +- .../actuallyadditions/event/InitEvents.java | 1 - .../event/WorldLoadEvent.java | 26 ---- .../misc/LaserRelayConnectionHandler.java | 131 +++++++++--------- .../actuallyadditions/misc/WorldData.java | 23 ++- .../tile/TileEntityLaserRelay.java | 27 ++-- 6 files changed, 97 insertions(+), 126 deletions(-) delete mode 100644 src/main/java/ellpeck/actuallyadditions/event/WorldLoadEvent.java diff --git a/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java b/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java index 2081d1d51..11b502f6b 100644 --- a/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java +++ b/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java @@ -31,10 +31,7 @@ import ellpeck.actuallyadditions.items.InitItems; import ellpeck.actuallyadditions.items.ItemCoffee; import ellpeck.actuallyadditions.material.InitArmorMaterials; import ellpeck.actuallyadditions.material.InitToolMaterials; -import ellpeck.actuallyadditions.misc.DispenserHandlerEmptyBucket; -import ellpeck.actuallyadditions.misc.DispenserHandlerFertilize; -import ellpeck.actuallyadditions.misc.DispenserHandlerFillBucket; -import ellpeck.actuallyadditions.misc.WorldData; +import ellpeck.actuallyadditions.misc.*; import ellpeck.actuallyadditions.network.PacketHandler; import ellpeck.actuallyadditions.ore.InitOreDict; import ellpeck.actuallyadditions.proxy.IProxy; @@ -119,6 +116,16 @@ public class ActuallyAdditions{ Util.registerDispenserHandler(Items.bucket, new DispenserHandlerFillBucket()); Util.registerDispenserHandler(InitItems.itemFertilizer, new DispenserHandlerFertilize()); + if(LaserRelayConnectionHandler.getInstance() == null){ + LaserRelayConnectionHandler.setInstance(new LaserRelayConnectionHandler()); + } + WorldData.init(event.getServer()); } + + @EventHandler + public void serverStopped(FMLServerStoppedEvent event){ + //Clear all Network Connections so that they won't be carried over into other worlds + LaserRelayConnectionHandler.getInstance().networks.clear(); + } } diff --git a/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java b/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java index 04bb7365b..20e36c5ed 100644 --- a/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java +++ b/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java @@ -31,7 +31,6 @@ public class InitEvents{ Util.registerEvent(new EntityLivingEvent()); Util.registerEvent(new BucketFillEvent()); Util.registerEvent(new LogoutEvent()); - Util.registerEvent(new WorldLoadEvent()); MinecraftForge.TERRAIN_GEN_BUS.register(new WorldDecorationEvent()); } diff --git a/src/main/java/ellpeck/actuallyadditions/event/WorldLoadEvent.java b/src/main/java/ellpeck/actuallyadditions/event/WorldLoadEvent.java deleted file mode 100644 index add130643..000000000 --- a/src/main/java/ellpeck/actuallyadditions/event/WorldLoadEvent.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * This file ("WorldLoadEvent.java") is part of the Actually Additions Mod for Minecraft. - * It is created and owned by Ellpeck and distributed - * under the Actually Additions License to be found at - * http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md - * View the source code at https://github.com/Ellpeck/ActuallyAdditions - * - * © 2015 Ellpeck - */ - -package ellpeck.actuallyadditions.event; - -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import ellpeck.actuallyadditions.misc.LaserRelayConnectionHandler; -import net.minecraftforge.event.world.WorldEvent; - -public class WorldLoadEvent{ - - @SubscribeEvent - public void onLoad(WorldEvent.Load event){ - if(LaserRelayConnectionHandler.getInstance() == null){ - LaserRelayConnectionHandler.setInstance(new LaserRelayConnectionHandler()); - } - } - -} diff --git a/src/main/java/ellpeck/actuallyadditions/misc/LaserRelayConnectionHandler.java b/src/main/java/ellpeck/actuallyadditions/misc/LaserRelayConnectionHandler.java index d46aa23b8..b718a61e8 100644 --- a/src/main/java/ellpeck/actuallyadditions/misc/LaserRelayConnectionHandler.java +++ b/src/main/java/ellpeck/actuallyadditions/misc/LaserRelayConnectionHandler.java @@ -15,23 +15,20 @@ import ellpeck.actuallyadditions.config.values.ConfigIntValues; import ellpeck.actuallyadditions.tile.TileEntityLaserRelay; import ellpeck.actuallyadditions.util.WorldPos; import ellpeck.actuallyadditions.util.WorldUtil; +import io.netty.util.internal.ConcurrentSet; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.ForgeDirection; -import java.util.ArrayList; - -//This is probably like punching any experienced programmer in the face, but it works. -@SuppressWarnings("ForLoopReplaceableByForEach") public class LaserRelayConnectionHandler{ private static LaserRelayConnectionHandler instance; /** - * An ArrayList of all of the networks a world has - * (Every place contains an ArrayList of ConnectionPairs, that is a single network!) + * All of the Networks */ - public ArrayList> networks = new ArrayList>(); + public ConcurrentSet networks = new ConcurrentSet(); public static LaserRelayConnectionHandler getInstance(){ return instance; @@ -41,40 +38,32 @@ public class LaserRelayConnectionHandler{ instance = i; } - public void writeNetworkToNBT(ArrayList network, NBTTagCompound tag, String name){ - NBTTagCompound compound = new NBTTagCompound(); - compound.setInteger("NetworkSize", network.size()); - - for(int pair = 0; pair < network.size(); pair++){ - network.get(pair).writeToNBT(compound, "Pair"+pair); + public NBTTagCompound writeNetworkToNBT(Network network){ + NBTTagList list = new NBTTagList(); + for(ConnectionPair pair : network.connections){ + list.appendTag(pair.writeToNBT()); } - - tag.setTag(name, compound); + NBTTagCompound compound = new NBTTagCompound(); + compound.setTag("Network", list); + return compound; } - public ArrayList readNetworkFromNBT(NBTTagCompound tag, String name){ - NBTTagCompound compound = tag.getCompoundTag(name); - - int networkSize = compound.getInteger("NetworkSize"); - - ArrayList network = new ArrayList(); - for(int pair = 0; pair < networkSize; pair++){ - network.add(ConnectionPair.readFromNBT(compound, "Pair"+pair)); + public Network readNetworkFromNBT(NBTTagCompound tag){ + NBTTagList list = tag.getTagList("Network", 10); + Network network = new Network(); + for(int i = 0; i < list.tagCount(); i++){ + network.connections.add(ConnectionPair.readFromNBT(list.getCompoundTagAt(i))); } - return network; } /** * Gets all Connections for a Relay */ - public ArrayList getConnectionsFor(WorldPos relay){ - ArrayList allPairs = new ArrayList(); - ArrayList> networks1 = this.networks; - for(int i = 0; i < networks1.size(); i++){ - ArrayList aNetwork = networks1.get(i); - for(int i1 = 0; i1 < aNetwork.size(); i1++){ - ConnectionPair pair = aNetwork.get(i1); + public ConcurrentSet getConnectionsFor(WorldPos relay){ + ConcurrentSet allPairs = new ConcurrentSet(); + for(Network aNetwork : this.networks){ + for(ConnectionPair pair : aNetwork.connections){ if(pair.contains(relay)){ allPairs.add(pair); } @@ -87,16 +76,16 @@ public class LaserRelayConnectionHandler{ * Removes a Relay from its Network */ public void removeRelayFromNetwork(WorldPos relay){ - ArrayList network = this.getNetworkFor(relay); + Network network = this.getNetworkFor(relay); if(network != null){ //Setup new network (so that splitting a network will cause it to break into two) this.networks.remove(network); - for(int i = 0; i < network.size(); i++){ - ConnectionPair pair = network.get(i); + for(ConnectionPair pair : network.connections){ if(!pair.contains(relay)){ this.addConnection(pair.firstRelay, pair.secondRelay); } } + System.out.println("Removing a Relay from the Network!"); } WorldData.makeDirty(); } @@ -104,12 +93,9 @@ public class LaserRelayConnectionHandler{ /** * Gets a Network for a Relay */ - public ArrayList getNetworkFor(WorldPos relay){ - ArrayList> networks1 = this.networks; - for(int i = 0; i < networks1.size(); i++){ - ArrayList aNetwork = networks1.get(i); - for(int i1 = 0; i1 < aNetwork.size(); i1++){ - ConnectionPair pair = aNetwork.get(i1); + public Network getNetworkFor(WorldPos relay){ + for(Network aNetwork : this.networks){ + for(ConnectionPair pair : aNetwork.connections){ if(pair.contains(relay)){ return aNetwork; } @@ -128,14 +114,14 @@ public class LaserRelayConnectionHandler{ return false; } - ArrayList firstNetwork = this.getNetworkFor(firstRelay); - ArrayList secondNetwork = this.getNetworkFor(secondRelay); + Network firstNetwork = this.getNetworkFor(firstRelay); + Network secondNetwork = this.getNetworkFor(secondRelay); //No Network exists if(firstNetwork == null && secondNetwork == null){ - firstNetwork = new ArrayList(); + firstNetwork = new Network(); this.networks.add(firstNetwork); - firstNetwork.add(new ConnectionPair(firstRelay, secondRelay)); + firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay)); } //The same Network else if(firstNetwork == secondNetwork){ @@ -144,19 +130,20 @@ public class LaserRelayConnectionHandler{ //Both relays have networks else if(firstNetwork != null && secondNetwork != null){ this.mergeNetworks(firstNetwork, secondNetwork); - firstNetwork.add(new ConnectionPair(firstRelay, secondRelay)); + firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay)); } //Only first network exists else if(firstNetwork != null){ - firstNetwork.add(new ConnectionPair(firstRelay, secondRelay)); + firstNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay)); } //Only second network exists else if(secondNetwork != null){ - secondNetwork.add(new ConnectionPair(firstRelay, secondRelay)); + secondNetwork.connections.add(new ConnectionPair(firstRelay, secondRelay)); } WorldData.makeDirty(); - //System.out.println("Connected "+firstRelay.toString()+" to "+secondRelay.toString()); - //System.out.println(firstNetwork == null ? secondNetwork.toString() : firstNetwork.toString()); + System.out.println("Connected "+firstRelay.toString()+" to "+secondRelay.toString()); + System.out.println(firstNetwork == null ? secondNetwork.toString() : firstNetwork.toString()); + System.out.println(this.networks); return true; } @@ -164,21 +151,19 @@ public class LaserRelayConnectionHandler{ * Merges two networks together * (Actually puts everything from the second network into the first one and removes the second one) */ - public void mergeNetworks(ArrayList firstNetwork, ArrayList secondNetwork){ - for(int i = 0; i < secondNetwork.size(); i++){ - ConnectionPair secondPair = secondNetwork.get(i); - firstNetwork.add(secondPair); + public void mergeNetworks(Network firstNetwork, Network secondNetwork){ + for(ConnectionPair secondPair : secondNetwork.connections){ + firstNetwork.connections.add(secondPair); } this.networks.remove(secondNetwork); WorldData.makeDirty(); - //System.out.println("Merged Two Networks!"); + System.out.println("Merged Two Networks!"); } - public int transferEnergyToReceiverInNeed(ArrayList network, int maxTransfer, boolean simulate){ + public int transferEnergyToReceiverInNeed(Network network, int maxTransfer, boolean simulate){ int transmitted = 0; //Go through all of the connections in the network - for(int i1 = 0; i1 < network.size(); i1++){ - ConnectionPair pair = network.get(i1); + for(ConnectionPair pair : network.connections){ WorldPos[] relays = new WorldPos[]{pair.firstRelay, pair.secondRelay}; //Go through both relays in the connection for(WorldPos relay : relays){ @@ -217,13 +202,13 @@ public class LaserRelayConnectionHandler{ this.secondRelay = secondRelay; } - public static ConnectionPair readFromNBT(NBTTagCompound compound, String name){ + public static ConnectionPair readFromNBT(NBTTagCompound compound){ WorldPos[] pos = new WorldPos[2]; for(int i = 0; i < pos.length; i++){ - int anX = compound.getInteger("x"+name+i); - int aY = compound.getInteger("y"+name+i); - int aZ = compound.getInteger("z"+name+i); - pos[i] = new WorldPos(compound.getInteger("world"+name+i), anX, aY, aZ); + int anX = compound.getInteger("x"+i); + int aY = compound.getInteger("y"+i); + int aZ = compound.getInteger("z"+i); + pos[i] = new WorldPos(compound.getInteger("world"+i), anX, aY, aZ); } return new ConnectionPair(pos[0], pos[1]); } @@ -237,14 +222,26 @@ public class LaserRelayConnectionHandler{ return (this.firstRelay == null ? "-" : this.firstRelay.toString())+" | "+(this.secondRelay == null ? "-" : this.secondRelay.toString()); } - public void writeToNBT(NBTTagCompound compound, String name){ + public NBTTagCompound writeToNBT(){ + NBTTagCompound compound = new NBTTagCompound(); for(int i = 0; i < 2; i++){ WorldPos relay = i == 0 ? this.firstRelay : this.secondRelay; - compound.setInteger("world"+name+i, relay.getWorldID()); - compound.setInteger("x"+name+i, relay.getX()); - compound.setInteger("y"+name+i, relay.getY()); - compound.setInteger("z"+name+i, relay.getZ()); + compound.setInteger("world"+i, relay.getWorldID()); + compound.setInteger("x"+i, relay.getX()); + compound.setInteger("y"+i, relay.getY()); + compound.setInteger("z"+i, relay.getZ()); } + return compound; + } + } + + public static class Network{ + + public ConcurrentSet connections = new ConcurrentSet(); + + @Override + public String toString(){ + return this.connections.toString(); } } } \ No newline at end of file diff --git a/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java b/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java index c88dbaa20..968472cd9 100644 --- a/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java +++ b/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java @@ -13,12 +13,11 @@ package ellpeck.actuallyadditions.misc; import ellpeck.actuallyadditions.util.ModUtil; import ellpeck.actuallyadditions.util.playerdata.PersistentServerData; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.server.MinecraftServer; import net.minecraft.world.World; import net.minecraft.world.WorldSavedData; -import java.util.ArrayList; - public class WorldData extends WorldSavedData{ public static final String DATA_TAG = ModUtil.MOD_ID+"WorldData"; @@ -55,13 +54,10 @@ public class WorldData extends WorldSavedData{ @Override public void readFromNBT(NBTTagCompound compound){ //Laser World Data - int netAmount = compound.getInteger("LaserNetworkAmount"); - LaserRelayConnectionHandler.getInstance().networks.clear(); - for(int i = 0; i < netAmount; i++){ - ArrayList network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(compound, "LaserNetwork"+i); - if(network != null){ - LaserRelayConnectionHandler.getInstance().networks.add(network); - } + NBTTagList list = compound.getTagList("Networks", 10); + for(int i = 0; i < list.tagCount(); i++){ + LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(list.getCompoundTagAt(i)); + LaserRelayConnectionHandler.getInstance().networks.add(network); } //Player Data @@ -78,12 +74,11 @@ public class WorldData extends WorldSavedData{ @Override public void writeToNBT(NBTTagCompound compound){ //Laser World Data - int netAmount = LaserRelayConnectionHandler.getInstance().networks.size(); - compound.setInteger("LaserNetworkAmount", netAmount); - - for(int i = 0; i < netAmount; i++){ - LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(LaserRelayConnectionHandler.getInstance().networks.get(i), compound, "LaserNetwork"+i); + NBTTagList list = new NBTTagList(); + for(LaserRelayConnectionHandler.Network network : LaserRelayConnectionHandler.getInstance().networks){ + list.appendTag(LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(network)); } + compound.setTag("Networks", list); //Player Data compound.setInteger("PersistentDataSize", PersistentServerData.playerSaveData.size()); diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityLaserRelay.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityLaserRelay.java index c7c0b5b3b..0c0740780 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityLaserRelay.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityLaserRelay.java @@ -16,16 +16,16 @@ import cpw.mods.fml.relauncher.SideOnly; import ellpeck.actuallyadditions.config.values.ConfigBoolValues; import ellpeck.actuallyadditions.misc.LaserRelayConnectionHandler; import ellpeck.actuallyadditions.util.WorldPos; +import io.netty.util.internal.ConcurrentSet; import net.minecraft.client.Minecraft; import net.minecraft.client.particle.EntityReddustFX; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.network.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraftforge.common.util.ForgeDirection; -import java.util.ArrayList; - public class TileEntityLaserRelay extends TileEntityBase implements IEnergyReceiver{ @Override @@ -41,15 +41,13 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord)); } - @SuppressWarnings("ForLoopReplaceableByForEach") @SideOnly(Side.CLIENT) public void renderParticles(){ if(this.worldObj.rand.nextInt(2) == 0){ WorldPos thisPos = new WorldPos(this.getWorldObj(), this.xCoord, this.yCoord, this.zCoord); - ArrayList network = LaserRelayConnectionHandler.getInstance().getNetworkFor(thisPos); + LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(thisPos); if(network != null){ - for(int i1 = 0; i1 < network.size(); i1++){ - LaserRelayConnectionHandler.ConnectionPair aPair = network.get(i1); + for(LaserRelayConnectionHandler.ConnectionPair aPair : network.connections){ if(aPair.contains(thisPos) && thisPos.isEqual(aPair.firstRelay)){ if(Minecraft.getMinecraft().thePlayer.getDistance(aPair.firstRelay.getX(), aPair.firstRelay.getY(), aPair.firstRelay.getZ()) <= 64){ int difX = aPair.firstRelay.getX()-aPair.secondRelay.getX(); @@ -72,13 +70,14 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei NBTTagCompound compound = new NBTTagCompound(); WorldPos thisPos = new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord); - ArrayList connections = LaserRelayConnectionHandler.getInstance().getConnectionsFor(thisPos); + ConcurrentSet connections = LaserRelayConnectionHandler.getInstance().getConnectionsFor(thisPos); if(connections != null){ - compound.setInteger("ConnectionAmount", connections.size()); - for(int i = 0; i < connections.size(); i++){ - connections.get(i).writeToNBT(compound, "Connection"+i); + NBTTagList list = new NBTTagList(); + for(LaserRelayConnectionHandler.ConnectionPair pair : connections){ + list.appendTag(pair.writeToNBT()); } + compound.setTag("Connections", list); return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 3, compound); } return null; @@ -90,9 +89,9 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord)); - int amount = compound.getInteger("ConnectionAmount"); - for(int i = 0; i < amount; i++){ - LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(compound, "Connection"+i); + NBTTagList list = compound.getTagList("Connections", 10); + for(int i = 0; i < list.tagCount(); i++){ + LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(list.getCompoundTagAt(i)); LaserRelayConnectionHandler.getInstance().addConnection(pair.firstRelay, pair.secondRelay); } } @@ -115,7 +114,7 @@ public class TileEntityLaserRelay extends TileEntityBase implements IEnergyRecei public int transmitEnergy(int maxTransmit, boolean simulate){ int transmitted = 0; if(maxTransmit > 0){ - ArrayList network = LaserRelayConnectionHandler.getInstance().getNetworkFor(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord)); + LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord)); if(network != null){ transmitted = LaserRelayConnectionHandler.getInstance().transferEnergyToReceiverInNeed(network, maxTransmit, simulate); }