From 4d17561a37cd17179c0f8a0a2446892f79679802 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 9 Nov 2013 11:27:56 -0500 Subject: [PATCH] Working on item networks for AL --- APIs/basiccomponents/api/BasicRegistry.java | 57 --------- src/dark/api/parts/IMotionPath.java | 22 ++++ src/dark/api/parts/ITileNetwork.java | 3 + src/dark/core/prefab/ModPrefab.java | 33 +++-- .../prefab/tilenetwork/NetworkItemSupply.java | 119 ++++++++++++++++++ .../tilenetwork/NetworkResourceSupply.java | 29 +++++ .../tilenetwork/NetworkTileEntities.java | 13 +- ...Handler.java => NetworkUpdateHandler.java} | 40 +++--- 8 files changed, 230 insertions(+), 86 deletions(-) delete mode 100644 APIs/basiccomponents/api/BasicRegistry.java create mode 100644 src/dark/api/parts/IMotionPath.java create mode 100644 src/dark/core/prefab/tilenetwork/NetworkItemSupply.java create mode 100644 src/dark/core/prefab/tilenetwork/NetworkResourceSupply.java rename src/dark/core/prefab/tilenetwork/{NetworkHandler.java => NetworkUpdateHandler.java} (76%) diff --git a/APIs/basiccomponents/api/BasicRegistry.java b/APIs/basiccomponents/api/BasicRegistry.java deleted file mode 100644 index 0ea72ed3b..000000000 --- a/APIs/basiccomponents/api/BasicRegistry.java +++ /dev/null @@ -1,57 +0,0 @@ -package basiccomponents.api; - -import java.util.HashSet; -import java.util.Set; - -/** - * This should be the only class you include in your mod. If your mod is a coremod, feel free to - * download Basic Components Core directly during run-time. - * - * @author Calclavia - */ - -public class BasicRegistry -{ - public static final Set requests = new HashSet(); - - /** - * @param request - Name of the item/block to register. Use the EXACT FIELD NAME of the - * BasicComponents.java field. - */ - public static void register(String request) - { - requests.add(request); - } - - /** - * Requests all items in Basic Components. - */ - public static void requestAll() - { - register("ingotCopper"); - register("ingotTin"); - - register("oreCopper"); - register("oreTin"); - - register("ingotSteel"); - register("dustSteel"); - register("plateSteel"); - - register("ingotBronze"); - register("dustBronze"); - register("plateBronze"); - - register("plateCopper"); - register("plateTin"); - register("plateIron"); - register("plateGold"); - - register("circuitBasic"); - register("circuitAdvanced"); - register("circuitElite"); - - register("motor"); - register("wrench"); - } -} diff --git a/src/dark/api/parts/IMotionPath.java b/src/dark/api/parts/IMotionPath.java new file mode 100644 index 000000000..a1b39880e --- /dev/null +++ b/src/dark/api/parts/IMotionPath.java @@ -0,0 +1,22 @@ +package dark.api.parts; + +import universalelectricity.core.vector.Vector3; +import net.minecraft.entity.Entity; +import net.minecraftforge.common.ForgeDirection; + +public interface IMotionPath +{ + + /** Gets the motion applied to the entity while its on the tile **/ + public Vector3 getMotion(Entity entity); + + /** Can the path controller move the entity over this tile. Make sure to check the position of + * the tile as the controller doesn't know the range of control of the tile. Though it does + * limit itself to blocks around the entity. So if your tile only effects entities above it + * within its bound then make sure the tile is inside those bounds + * + * @param entity - entity in question + * @param from - direction the entity came from + * @return true if it can, false if something is wrong like no power, or solid side */ + public boolean canMoveEntity(Entity entity); +} diff --git a/src/dark/api/parts/ITileNetwork.java b/src/dark/api/parts/ITileNetwork.java index a16f594fb..b2c91f9dc 100644 --- a/src/dark/api/parts/ITileNetwork.java +++ b/src/dark/api/parts/ITileNetwork.java @@ -28,6 +28,9 @@ public interface ITileNetwork /** Called when this network is just created */ public void onCreated(); + /** How many ticks should base between updates, return 0 or bellow for no ticks */ + public int getUpdateRate(); + /** Called every so many ticks so the network has a chance to update */ public void updateTick(); diff --git a/src/dark/core/prefab/ModPrefab.java b/src/dark/core/prefab/ModPrefab.java index da6ef7d31..2ec67cbde 100644 --- a/src/dark/core/prefab/ModPrefab.java +++ b/src/dark/core/prefab/ModPrefab.java @@ -19,9 +19,11 @@ import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.registry.TickRegistry; import cpw.mods.fml.relauncher.Side; import dark.core.common.ExternalModHandler; import dark.core.prefab.helpers.FluidHelper; +import dark.core.prefab.tilenetwork.NetworkUpdateHandler; import dark.core.registration.ModObjectRegistry; import dark.core.save.SaveManager; @@ -48,6 +50,8 @@ public abstract class ModPrefab private static Triple date; + private static boolean preInit, init, postInit; + public abstract String getDomain(); /** Gets the next unused ID in the block list. Does not prevent config file issues after the file @@ -92,26 +96,41 @@ public abstract class ModPrefab public void preInit(FMLPreInitializationEvent event) { this.loadModMeta(); + Modstats.instance().getReporter().registerMod(this); MinecraftForge.EVENT_BUS.register(this); - MinecraftForge.EVENT_BUS.register(new FluidHelper()); - MinecraftForge.EVENT_BUS.register(SaveManager.instance()); - UniversalElectricity.initiate(); - Compatibility.initiate(); + if (!preInit) + { + MinecraftForge.EVENT_BUS.register(new FluidHelper()); + MinecraftForge.EVENT_BUS.register(SaveManager.instance()); + TickRegistry.registerTickHandler(NetworkUpdateHandler.instance(), Side.SERVER); + UniversalElectricity.initiate(); + Compatibility.initiate(); + preInit = true; + } } @EventHandler public void init(FMLInitializationEvent event) { - ExternalModHandler.init(); - ModObjectRegistry.masterBlockConfig.load(); + if (!init) + { + ExternalModHandler.init(); + ModObjectRegistry.masterBlockConfig.load(); + init = true; + } this.registerObjects(); - ModObjectRegistry.masterBlockConfig.save(); + } @EventHandler public void postInit(FMLPostInitializationEvent event) { + if (!postInit) + { + ModObjectRegistry.masterBlockConfig.save(); + postInit = true; + } this.loadRecipes(); } diff --git a/src/dark/core/prefab/tilenetwork/NetworkItemSupply.java b/src/dark/core/prefab/tilenetwork/NetworkItemSupply.java new file mode 100644 index 000000000..13b01b199 --- /dev/null +++ b/src/dark/core/prefab/tilenetwork/NetworkItemSupply.java @@ -0,0 +1,119 @@ +package dark.core.prefab.tilenetwork; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import net.minecraft.entity.Entity; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.ForgeDirection; +import universalelectricity.core.vector.Vector3; + +import com.builtbroken.common.Pair; + +import dark.api.parts.IMotionPath; +import dark.api.parts.INetworkPart; + +/** Class that acts like the redpower pipes system. Each item is marked with a destination. Intended + * use it to improve the assembly line network + * + * @author DarkGuardsman */ +public class NetworkItemSupply extends NetworkTileEntities +{ + List> trackingList = new ArrayList>(); + List ignoreList = new ArrayList(); + /** Same as valid directions from forge direction enum but Unknown was added so that is gets check and checked first */ + public static final ForgeDirection[] VALID_DIRECTIONS = {ForgeDirection.UNKNOWN, ForgeDirection.DOWN, ForgeDirection.UP, ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.WEST, ForgeDirection.EAST}; + + public NetworkItemSupply(INetworkPart... parts) + { + super(parts); + } + + @Override + public void updateTick() + { + Iterator> it = trackingList.iterator(); + while (it.hasNext()) + { + Pair entry = it.next(); + if (entry.left() == null || !this.isOnPath(entry.left())) + { + it.remove(); + } + else + { + if (entry.right() == null) + { + entry.setRight(new Vector3(entry.left())); + } + else + { + entry.left().setPosition(entry.right().x, entry.right().y, entry.right().z); + } + } + } + + } + + @Override + public int getUpdateRate() + { + return 1; + } + + /** Remove an entity from the tracking list */ + public void removeEntity(Entity entity) + { + this.trackingList.remove(entity); + } + + /** Ignores an entity so that is can be controlled by something else for a while. Eg armbots, and + * drones */ + public void ignoreEntity(Entity entity) + { + if (!this.ignoreList.contains(entity)) + { + this.ignoreList.add(entity); + } + } + + /** Add and entity to the tracking list */ + public void addEntity(Entity entity) + { + if (!this.trackingList.contains(entity)) + { + this.trackingList.add(new Pair(entity, new Vector3(entity))); + } + } + + public boolean isTrackingEntity(Entity entity) + { + return this.trackingList.contains(entity); + } + + public boolean isOnPath(Entity entity) + { + if (entity != null) + { + Vector3 ent = new Vector3(entity); + //Check all directions including the current position of the entity + for (ForgeDirection direction : this.VALID_DIRECTIONS) + { + TileEntity a = ent.clone().modifyPositionFromSide(direction).getTileEntity(entity.worldObj); + if (a instanceof IMotionPath && ((IMotionPath) a).canMoveEntity(entity) && this.networkMembers.contains(a)) + { + return true; + } + } + } + return false; + } + + @Override + public boolean isValidMember(INetworkPart part) + { + return super.isValidMember(part) && part instanceof IMotionPath; + } + +} diff --git a/src/dark/core/prefab/tilenetwork/NetworkResourceSupply.java b/src/dark/core/prefab/tilenetwork/NetworkResourceSupply.java new file mode 100644 index 000000000..6d1310086 --- /dev/null +++ b/src/dark/core/prefab/tilenetwork/NetworkResourceSupply.java @@ -0,0 +1,29 @@ +package dark.core.prefab.tilenetwork; + +import java.util.HashMap; +import java.util.List; + +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.ForgeDirection; +import dark.api.parts.INetworkPart; + +/** Network that supplies resources to tiles that demand a set resource + * + * @param C - Storage class used to handle what the network transports + * @param I - Base acceptor class + * @author DarkGuardsman */ +public class NetworkResourceSupply extends NetworkTileEntities +{ + protected C storage; + protected HashMap> acceptors = new HashMap(); + + public NetworkResourceSupply(INetworkPart... parts) + { + super(parts); + } + + public boolean isValidAcceptor(TileEntity entity) + { + return entity != null && !entity.isInvalid(); + } +} diff --git a/src/dark/core/prefab/tilenetwork/NetworkTileEntities.java b/src/dark/core/prefab/tilenetwork/NetworkTileEntities.java index 4dfc125d4..89f6f8021 100644 --- a/src/dark/core/prefab/tilenetwork/NetworkTileEntities.java +++ b/src/dark/core/prefab/tilenetwork/NetworkTileEntities.java @@ -19,11 +19,12 @@ public class NetworkTileEntities implements ITileNetwork public NetworkTileEntities() { - + NetworkUpdateHandler.instance().registerNetwork(this); } public NetworkTileEntities(INetworkPart... parts) { + this(); if (parts != null) { for (INetworkPart part : parts) @@ -56,6 +57,12 @@ public class NetworkTileEntities implements ITileNetwork this.cleanUpMembers(); } + @Override + public int getUpdateRate() + { + return -1; + } + @Override public void updateTick() { @@ -170,7 +177,7 @@ public class NetworkTileEntities implements ITileNetwork /** Merges the two networks together */ protected void mergeDo(ITileNetwork network) { - ITileNetwork newNetwork = NetworkHandler.createNewNetwork(NetworkHandler.getID(this.getClass())); + ITileNetwork newNetwork = NetworkUpdateHandler.createNewNetwork(NetworkUpdateHandler.getID(this.getClass())); newNetwork.getMembers().addAll(this.getMembers()); newNetwork.getMembers().addAll(network.getMembers()); newNetwork.onCreated(); @@ -200,7 +207,7 @@ public class NetworkTileEntities implements ITileNetwork { this.save(); /* NO LONGER CONNECTED ELSE WHERE SO SPLIT AND REFRESH */ - ITileNetwork newNetwork = NetworkHandler.createNewNetwork(NetworkHandler.getID(this.getClass())); + ITileNetwork newNetwork = NetworkUpdateHandler.createNewNetwork(NetworkUpdateHandler.getID(this.getClass())); if (newNetwork != null) { for (Vector3 node : finder.closedSet) diff --git a/src/dark/core/prefab/tilenetwork/NetworkHandler.java b/src/dark/core/prefab/tilenetwork/NetworkUpdateHandler.java similarity index 76% rename from src/dark/core/prefab/tilenetwork/NetworkHandler.java rename to src/dark/core/prefab/tilenetwork/NetworkUpdateHandler.java index 0b648ca42..dae5fb4ac 100644 --- a/src/dark/core/prefab/tilenetwork/NetworkHandler.java +++ b/src/dark/core/prefab/tilenetwork/NetworkUpdateHandler.java @@ -7,42 +7,49 @@ import java.util.Iterator; import java.util.Set; import cpw.mods.fml.common.IScheduledTickHandler; +import cpw.mods.fml.common.ITickHandler; import cpw.mods.fml.common.TickType; import dark.api.parts.ITileNetwork; /** Manages all the tile networks making sure they get world save events, and updates every so often - * + * * @author DarkGuardsman */ -public class NetworkHandler implements IScheduledTickHandler +public class NetworkUpdateHandler implements ITickHandler { private static HashMap> nameToClassMap = new HashMap>(); private static HashMap, String> classToNameMap = new HashMap, String>(); - private byte count = 0; + private int count = 0; + private static int refreshTicks = 6000; - private Set networks = new HashSet(); + private Set activeNetworks = new HashSet(); + private Set allNetworks = new HashSet(); - private static NetworkHandler instance; + private static NetworkUpdateHandler instance; static { registerNetworkClass("base", NetworkTileEntities.class); } - public static NetworkHandler instance() + public static NetworkUpdateHandler instance() { if (instance == null) { - instance = new NetworkHandler(); + instance = new NetworkUpdateHandler(); } return instance; } public void registerNetwork(ITileNetwork network) { - if (!networks.contains(network)) + if (network != null && !activeNetworks.contains(network)) { - this.networks.add(network); + this.allNetworks.add(network); + if (network.getUpdateRate() > 0) + { + this.activeNetworks.add(network); + } } } @@ -95,10 +102,10 @@ public class NetworkHandler implements IScheduledTickHandler @Override public void tickStart(EnumSet type, Object... tickData) { - if (count + 1 >= Byte.MAX_VALUE) + if (count + 1 >= this.refreshTicks) { count = 0; - for (ITileNetwork network : networks) + for (ITileNetwork network : allNetworks) { if (!network.isInvalid()) { @@ -109,7 +116,7 @@ public class NetworkHandler implements IScheduledTickHandler else { count++; - for (ITileNetwork network : networks) + for (ITileNetwork network : activeNetworks) { if (!network.isInvalid()) { @@ -123,7 +130,7 @@ public class NetworkHandler implements IScheduledTickHandler @Override public void tickEnd(EnumSet type, Object... tickData) { - Iterator it = networks.iterator(); + Iterator it = activeNetworks.iterator(); while (it.hasNext()) { ITileNetwork network = it.next(); @@ -131,6 +138,7 @@ public class NetworkHandler implements IScheduledTickHandler { network.invalidate(); it.remove(); + allNetworks.remove(network); } } @@ -147,10 +155,4 @@ public class NetworkHandler implements IScheduledTickHandler { return "[CoreMachine]TileNetworkHandler"; } - - @Override - public int nextTickSpacing() - { - return 20; - } }