From d08cbf6b8705df234077c49608de3c2f7ff0e3fa Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 21 Nov 2019 18:35:14 -0500 Subject: [Update] Codebase updated to 0.2.8 ~ Rewrote files and documentation for cohesion ~ Removed I from the start of interface names --- src/main/java/net/transit/InitTransit.java | 6 +- .../net/transit/network/packet/ArrayPacket.java | 11 +- .../java/net/transit/network/packet/IPacket.java | 23 --- .../net/transit/network/packet/MetaPacket.java | 2 +- .../java/net/transit/network/packet/Packet.java | 23 +++ .../net/transit/network/packet/StaticPacket.java | 2 +- .../java/net/transit/network/swap/Acceptor.java | 44 +++++ .../java/net/transit/network/swap/IAcceptor.java | 31 --- .../java/net/transit/network/swap/IProvider.java | 37 ---- .../java/net/transit/network/swap/Provider.java | 50 +++++ .../java/net/transit/network/system/INode.java | 50 ----- .../java/net/transit/network/system/ISystem.java | 17 -- src/main/java/net/transit/network/system/Node.java | 50 +++++ .../java/net/transit/network/system/System.java | 17 ++ src/main/java/net/transit/type/Type.java | 37 +++- .../java/net/transit/type/group/GroupRegistry.java | 35 ++-- .../java/net/transit/type/group/TypeGroup.java | 217 +++++++++++++++------ .../net/transit/type/group/simple/EnergyGroup.java | 10 - .../net/transit/type/group/simple/FluidGroup.java | 11 -- .../type/group/simple/InitSimpleGroups.java | 22 +++ .../net/transit/type/group/simple/ItemGroup.java | 11 -- .../net/transit/type/group/simple/ManaGroup.java | 10 - .../transit/type/group/simple/SimpleGroups.java | 26 --- .../java/net/transit/type/simple/EnergyType.java | 11 -- .../java/net/transit/type/simple/FluidType.java | 11 -- .../java/net/transit/type/simple/ItemType.java | 11 -- .../java/net/transit/type/simple/ManaType.java | 10 - .../java/net/transit/type/simple/SimpleTypes.java | 25 +-- 28 files changed, 422 insertions(+), 388 deletions(-) delete mode 100644 src/main/java/net/transit/network/packet/IPacket.java create mode 100644 src/main/java/net/transit/network/packet/Packet.java create mode 100644 src/main/java/net/transit/network/swap/Acceptor.java delete mode 100644 src/main/java/net/transit/network/swap/IAcceptor.java delete mode 100644 src/main/java/net/transit/network/swap/IProvider.java create mode 100644 src/main/java/net/transit/network/swap/Provider.java delete mode 100644 src/main/java/net/transit/network/system/INode.java delete mode 100644 src/main/java/net/transit/network/system/ISystem.java create mode 100644 src/main/java/net/transit/network/system/Node.java create mode 100644 src/main/java/net/transit/network/system/System.java delete mode 100644 src/main/java/net/transit/type/group/simple/EnergyGroup.java delete mode 100644 src/main/java/net/transit/type/group/simple/FluidGroup.java create mode 100644 src/main/java/net/transit/type/group/simple/InitSimpleGroups.java delete mode 100644 src/main/java/net/transit/type/group/simple/ItemGroup.java delete mode 100644 src/main/java/net/transit/type/group/simple/ManaGroup.java delete mode 100644 src/main/java/net/transit/type/group/simple/SimpleGroups.java delete mode 100644 src/main/java/net/transit/type/simple/EnergyType.java delete mode 100644 src/main/java/net/transit/type/simple/FluidType.java delete mode 100644 src/main/java/net/transit/type/simple/ItemType.java delete mode 100644 src/main/java/net/transit/type/simple/ManaType.java (limited to 'src') diff --git a/src/main/java/net/transit/InitTransit.java b/src/main/java/net/transit/InitTransit.java index 1d4941e..a89f91a 100644 --- a/src/main/java/net/transit/InitTransit.java +++ b/src/main/java/net/transit/InitTransit.java @@ -1,15 +1,13 @@ package net.transit; import net.fabricmc.api.ModInitializer; -import net.transit.type.group.simple.SimpleGroups; -import net.transit.type.simple.SimpleTypes; +import net.transit.type.group.simple.InitSimpleGroups; public class InitTransit implements ModInitializer { @Override public void onInitialize() { - SimpleGroups.initGroups(); - SimpleTypes.initTypes(); + InitSimpleGroups.init(); } } diff --git a/src/main/java/net/transit/network/packet/ArrayPacket.java b/src/main/java/net/transit/network/packet/ArrayPacket.java index 9cbd300..fbea811 100644 --- a/src/main/java/net/transit/network/packet/ArrayPacket.java +++ b/src/main/java/net/transit/network/packet/ArrayPacket.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import net.transit.type.Type; -public class ArrayPacket implements IPacket +public class ArrayPacket implements Packet { private ArrayList arrayData; private Type type; @@ -13,8 +13,10 @@ public class ArrayPacket implements IPacket { arrayData = new ArrayList(0); arrayData.add(startValue); + type = t; } - + + @Override public D getData() { if(arrayData.size() > 0) return arrayData.get(0); @@ -25,6 +27,11 @@ public class ArrayPacket implements IPacket { arrayData.add(data); } + + public int dataStored() + { + return arrayData.size(); + } public D popAndShift() { diff --git a/src/main/java/net/transit/network/packet/IPacket.java b/src/main/java/net/transit/network/packet/IPacket.java deleted file mode 100644 index deafdf8..0000000 --- a/src/main/java/net/transit/network/packet/IPacket.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.transit.network.packet; - -import net.transit.type.Type; - -/** - * @author Kyle Gunger - * - * @param The data type (Object) that the packet transfers. - */ -public interface IPacket -{ - /**Get the packet's data - * - * @return The packet's data - */ - public D getData(); - - /**Get the packet's type - * - * @return IType<> The type of the packet - */ - public Type getType(); -} \ No newline at end of file diff --git a/src/main/java/net/transit/network/packet/MetaPacket.java b/src/main/java/net/transit/network/packet/MetaPacket.java index e4cffdf..0240519 100644 --- a/src/main/java/net/transit/network/packet/MetaPacket.java +++ b/src/main/java/net/transit/network/packet/MetaPacket.java @@ -2,7 +2,7 @@ package net.transit.network.packet; import net.transit.type.Type; -public class MetaPacket implements IPacket +public class MetaPacket implements Packet { private D data; diff --git a/src/main/java/net/transit/network/packet/Packet.java b/src/main/java/net/transit/network/packet/Packet.java new file mode 100644 index 0000000..0b7c832 --- /dev/null +++ b/src/main/java/net/transit/network/packet/Packet.java @@ -0,0 +1,23 @@ +package net.transit.network.packet; + +import net.transit.type.Type; + +/** + * @author Kyle Gunger + * + * @param The data type (Object) that the packet transfers. + */ +public interface Packet +{ + /**Get the packet's data + * + * @return The packet's data + */ + public D getData(); + + /**Get the packet's type + * + * @return IType<> The type of the packet + */ + public Type getType(); +} \ No newline at end of file diff --git a/src/main/java/net/transit/network/packet/StaticPacket.java b/src/main/java/net/transit/network/packet/StaticPacket.java index 8399041..b14e442 100644 --- a/src/main/java/net/transit/network/packet/StaticPacket.java +++ b/src/main/java/net/transit/network/packet/StaticPacket.java @@ -2,7 +2,7 @@ package net.transit.network.packet; import net.transit.type.Type; -public class StaticPacket implements IPacket +public class StaticPacket implements Packet { private D data; private Type type; diff --git a/src/main/java/net/transit/network/swap/Acceptor.java b/src/main/java/net/transit/network/swap/Acceptor.java new file mode 100644 index 0000000..9778095 --- /dev/null +++ b/src/main/java/net/transit/network/swap/Acceptor.java @@ -0,0 +1,44 @@ +package net.transit.network.swap; + +import net.transit.network.packet.Packet; +import net.transit.network.system.Node; + +/** + * @author Kyle Gunger + * + * @param The type of data accepted (in IPacket<> form) + */ +public interface Acceptor +{ + /** Returns the INode this Acceptor is attached to. All Acceptors and Provider must be attached to an INode to function. + * + * @return INode + */ + public Node getNode(); + + /** + * @return True if the Acceptor can accept packets. + */ + public boolean canAccept(); + + /** + * @param provided The packet provided. + * @return True if the Acceptor accepted the packet. + */ + public boolean accept(Packet provided); + + /** + * @return True if the Acceptor has a Provider assigned to it. + */ + public boolean hasProvider(); + + /**Set the provider of the Acceptor. The Acceptor can decide if it wants to adopt the Provider. + */ + public void setProvider(Provider provider); + + /** Request that the Acceptor close it's connection to a Provider. The Acceptor may also shut itself down if no more Providers are attached. + * + * @param provider The Provider + */ + public void shutdown(Provider provider); +} \ No newline at end of file diff --git a/src/main/java/net/transit/network/swap/IAcceptor.java b/src/main/java/net/transit/network/swap/IAcceptor.java deleted file mode 100644 index 6966b9a..0000000 --- a/src/main/java/net/transit/network/swap/IAcceptor.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.transit.network.swap; - -import net.transit.network.packet.IPacket; - -/** - * @author Kyle Gunger - * - * @param The type of data accepted (in IPacket<> form) - */ -public interface IAcceptor -{ - /** - * @return True if the acceptor can accept packets. - */ - public boolean canAccept(); - - /** - * @param provided The packet provided. - * @return True if the acceptor accepted the packet. - */ - public boolean accept(IPacket provided); - - /** - * @return True if the acceptor has a provider assigned to it. - */ - public boolean hasProvider(); - - /**Set the provider of the acceptor. The acceptor can decide if it wants to adopt the provider. - */ - public void setProvider(IProvider provider); -} \ No newline at end of file diff --git a/src/main/java/net/transit/network/swap/IProvider.java b/src/main/java/net/transit/network/swap/IProvider.java deleted file mode 100644 index 03c9ca9..0000000 --- a/src/main/java/net/transit/network/swap/IProvider.java +++ /dev/null @@ -1,37 +0,0 @@ -package net.transit.network.swap; - -import net.transit.network.packet.IPacket; - -/** - * @author Kyle Gunger - * - * @param The type of data provided (in IPacket<> form) - */ -public interface IProvider -{ - /** - * @return True if the provider can provide a packet. - */ - public boolean canProvide(); - - /**Get the next packet from the provider - * - * @return IPacket<> - */ - public IPacket provide(); - - /**Retain the packet if the acceptor did not accept the packet. - * - * @param rejected The rejected packet - */ - public void retain(IPacket rejected); - - /** - * @return True if the acceptor has a provider assigned to it - */ - public boolean hasAcceptor(); - - /**Set the acceptor of the provider. The provider can decide if it wants to adopt the acceptor. - */ - public void setAcceptor(); -} \ No newline at end of file diff --git a/src/main/java/net/transit/network/swap/Provider.java b/src/main/java/net/transit/network/swap/Provider.java new file mode 100644 index 0000000..20656b8 --- /dev/null +++ b/src/main/java/net/transit/network/swap/Provider.java @@ -0,0 +1,50 @@ +package net.transit.network.swap; + +import net.transit.network.packet.Packet; +import net.transit.network.system.Node; + +/** + * @author Kyle Gunger + * + * @param The type of data provided (in IPacket<> form) + */ +public interface Provider +{ + /** Returns the INode this Provider is attached to. All Acceptors and Providers must be attached to an INode to function. + * + * @return INode + */ + public Node getNode(); + + /** + * @return True if the Provider can provide a packet. + */ + public boolean canProvide(); + + /**Get the next packet from the Provider + * + * @return IPacket<> + */ + public Packet provide(); + + /**Retain the packet if the Acceptor did not accept the packet. + * + * @param rejected The rejected packet + */ + public void retain(Packet rejected); + + /** + * @return True if the Provider has an Acceptor assigned to it + */ + public boolean hasAcceptor(); + + /**Set the Acceptor of the Provider. The Provider can decide if it wants to adopt the Acceptor. + */ + public void setAcceptor(Acceptor acceptor); + + /** Request that the Provider close it's connection to a Acceptor. The Provider may also shut itself down if no more Acceptors are attached. + * + * @param acceptor The Acceptor + */ + public void shutdown(Acceptor acceptor); +} \ No newline at end of file diff --git a/src/main/java/net/transit/network/system/INode.java b/src/main/java/net/transit/network/system/INode.java deleted file mode 100644 index 2dc3686..0000000 --- a/src/main/java/net/transit/network/system/INode.java +++ /dev/null @@ -1,50 +0,0 @@ -package net.transit.network.system; - -import net.transit.network.swap.IAcceptor; -import net.transit.network.swap.IProvider; - -/** - * @author Kyle Gunger - * @apiNote A node inside or outside a system. Provides acceptors and providers to other nodes. - */ -public interface INode -{ - /**Returns the groupIDs of groups the node provide - * - * @return String[] - */ - public String[] groupsProvided(); - - /**Get the system managing the node or {@code null} if there isn't one - * - * @return ISystem - */ - public ISystem getSystem(); - - /**Returns {@code true} if the node provides IAcceptors - * - * @return boolean - */ - public boolean isAcceptor(); - - /**Returns an IAcceptor or {@code null} depending on the requester and group - * - * @param requester The object requesting the IAcceptor - * @param group - * @return IAcceptor - */ - public IAcceptor requestAcceptor(Object requester, String group); - - /**Returns {@code true} if the node provides IProviders - * - */ - public boolean isProvider(); - - /**Returns an IProvider or {@code null} depending on the requester and group - * - * @param requester The object requesting the IProvider - * @param group - * @return IProvider - */ - public IProvider requestProvider(Object requestor, String group); -} diff --git a/src/main/java/net/transit/network/system/ISystem.java b/src/main/java/net/transit/network/system/ISystem.java deleted file mode 100644 index fd2fa51..0000000 --- a/src/main/java/net/transit/network/system/ISystem.java +++ /dev/null @@ -1,17 +0,0 @@ -package net.transit.network.system; - - -/**ISystem - a group of nodes optimized for performance - * - * A node can exist without a system, but a system can not exist without at least one root node. - * - * @param T The object type stored in the system. - */ -public interface ISystem -{ - /**The nodes stored by the system - * - * @return INode[] - */ - public INode[] getNodes(); -} diff --git a/src/main/java/net/transit/network/system/Node.java b/src/main/java/net/transit/network/system/Node.java new file mode 100644 index 0000000..8e0fcbb --- /dev/null +++ b/src/main/java/net/transit/network/system/Node.java @@ -0,0 +1,50 @@ +package net.transit.network.system; + +import net.transit.network.swap.Acceptor; +import net.transit.network.swap.Provider; + +/** + * @author Kyle Gunger + * @apiNote A node inside or outside a system. Provides acceptors and providers to other nodes. + */ +public interface Node +{ + /**Returns the groupIDs of groups the node provide + * + * @return String[] + */ + public String[] groupsProvided(); + + /**Get the system managing the node or {@code null} if there isn't one + * + * @return ISystem + */ + public System getSystem(); + + /**Returns {@code true} if the node provides Acceptors + * + * @return boolean + */ + public boolean isAcceptor(); + + /**Returns an Acceptor or {@code null} depending on the requester and group + * + * @param requester The object requesting the Acceptor + * @param group + * @return Acceptor + */ + public Acceptor requestAcceptor(Object requester, String group); + + /**Returns {@code true} if the node provides Providers + * + */ + public boolean isProvider(); + + /**Returns an Provider or {@code null} depending on the requester and group + * + * @param requester The object requesting the Provider + * @param group + * @return Provider + */ + public Provider requestProvider(Object requestor, String group); +} diff --git a/src/main/java/net/transit/network/system/System.java b/src/main/java/net/transit/network/system/System.java new file mode 100644 index 0000000..c70292a --- /dev/null +++ b/src/main/java/net/transit/network/system/System.java @@ -0,0 +1,17 @@ +package net.transit.network.system; + + +/**ISystem - a group of nodes optimized for performance + * + * A node can exist without a system, but a system can not exist without at least one root node. + * + * @param T The object type stored in the system. + */ +public interface System +{ + /**The nodes stored by the system + * + * @return INode[] + */ + public Node[] getNodes(); +} diff --git a/src/main/java/net/transit/type/Type.java b/src/main/java/net/transit/type/Type.java index 7c7f839..d140da5 100644 --- a/src/main/java/net/transit/type/Type.java +++ b/src/main/java/net/transit/type/Type.java @@ -1,41 +1,60 @@ package net.transit.type; -import net.transit.network.packet.IPacket; +import net.transit.network.packet.Packet; import net.transit.network.packet.StaticPacket; /**@author Kyle Gunger * * @param The type of object transfered by packets of this type */ -public abstract class Type +public class Type { + protected String typeID; + protected String groupID; - /**Return the packet's data formatted in the group's base type. + public Type(String tID, String gID) + { + typeID = tID; + groupID = gID; + } + + /** Return the packet's data formatted in the group's base type. * * @param packet The packet (of this type) + * @param group The group asking for the conversion * @return The packet's data in the default type */ - public T toBase(IPacket packet) + public T toBase(Packet packet, String group) { return packet.getData(); } - /**Create a packet which has the current type from the data in the base type. + /** Create a packet which has the current type from the data in the base type. * * @param base The base data + * @param group The group that the data comes from * @return IPacket<> The packet */ - public IPacket fromBase(T base) + public Packet fromBase(T base, String group) { return new StaticPacket(base, this); } - /** The type identifier. Must be overridden for any class implementing IType + /** The type identifier. Must be overridden for any class implementing IType. + * + * @return String + */ + public final String getType() + { + return typeID; + } + + /** The group identifier. Gives the normal group identity of the type. * * @return String */ - public String getType() + public final String getGroup() { - return "ITYPE"; + return groupID; } } diff --git a/src/main/java/net/transit/type/group/GroupRegistry.java b/src/main/java/net/transit/type/group/GroupRegistry.java index 47ee4db..8f8a612 100644 --- a/src/main/java/net/transit/type/group/GroupRegistry.java +++ b/src/main/java/net/transit/type/group/GroupRegistry.java @@ -5,47 +5,46 @@ import java.util.ArrayList; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -public abstract class GroupRegistry { +import net.transit.type.Type; + +public class GroupRegistry { private static final ArrayList> GROUPS = new ArrayList>(0); - private static final ArrayList GROUPIDS = new ArrayList(0); private static final Logger LOG = LogManager.getFormatterLogger("Transit|GroupRegistry"); + private static final String prefix = "[" + LOG.getName() + "] "; + + private GroupRegistry() {} - public static final boolean addGroup(TypeGroup group, String groupID) + public static final boolean addGroup(TypeGroup group) { - for(String s : GROUPIDS) - { - if(s.equals(groupID)) - { - LOG.warn("Failed to add group " + group.getGroup() + " to the registry. Did another mod add a group with the same name?"); - return false; - } - } - for(TypeGroup g : GROUPS) { - if(g.equals(group)) + if(g.getGroup().equals(group.getGroup())) { - LOG.warn("Failed to add group " + group.getGroup() + " to the registry. Was the group alreay added?"); + LOG.warn(prefix + "Failed to add group " + group.getGroup() + " to the registry. Did another mod add a group with the same name?"); return false; } } GROUPS.add(group); - GROUPIDS.add(groupID); - LOG.info("Successfully added group " + group.getGroup() + " to the registry."); + LOG.info(prefix + "Successfully added group " + group.getGroup() + " to the registry."); return true; } public static final TypeGroup groupByID(String groupID) { - for(String s : GROUPIDS) + for(TypeGroup g : GROUPS) { - if(s.equals(groupID)) return GROUPS.get(GROUPIDS.indexOf(s)); + if(g.getGroup().equals(groupID)) return g; } return null; } + public static final Type typeByIdentity(String groupID, String typeID) + { + return groupByID(groupID).getType(typeID); + } + } diff --git a/src/main/java/net/transit/type/group/TypeGroup.java b/src/main/java/net/transit/type/group/TypeGroup.java index 2602641..8929252 100644 --- a/src/main/java/net/transit/type/group/TypeGroup.java +++ b/src/main/java/net/transit/type/group/TypeGroup.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import net.transit.network.packet.IPacket; +import net.transit.network.packet.Packet; import net.transit.type.Type; /** @@ -14,124 +14,213 @@ import net.transit.type.Type; */ public class TypeGroup { - /** The list of types. */ + // The base Type (provides the group's identifier) + private Type baseType; + + // The list of types. private final ArrayList> TYPES = new ArrayList>(0); - private final ArrayList TYPEIDS = new ArrayList(0); - /** Logger for events in the group. Should not be overridden.*/ + // Logger for events in the TypeGroup. private static final Logger LOG = LogManager.getFormatterLogger("Transit|Group"); + private static final String prefix = "[" + LOG.getName() + "] "; - /**Check if the type is in the group - * - * @param type - * @return boolean + + public TypeGroup(Type base) + { + baseType = base; + addType(base); + } + + + // Type management + + /** Add a Type to the TypeGroup. + * The Type must have a group-type combo not already found in this TypeGroup. + * Returns {@code true} if the group was added. + * + * @param type The Type to add + * @return */ - public boolean isInGroup(Type type) + public boolean addType(Type type) { - return TYPES.contains(type); + if(!isInGroup(type.getGroup(), type.getType()) && !isInGroup(type)) + { + TYPES.add(type); + LOG.info(prefix + "Added type " + type.getType() + " to group " + getGroup()); + return true; + } + + return false; } - /**Check if the type is in the group + /** Remove a Type from the TypeGroup. * - * @param type - * @return boolean + * @param type The Type to remove + * @return */ - public boolean isInGroup(String type) + public boolean removeType(Type type) { - return TYPEIDS.contains(type); + + if(isInGroup(type) && TYPES.indexOf(type) != -1) + { + LOG.info(prefix + "Removed type " + TYPES.remove(TYPES.indexOf(type)).getType() + " from group " + getGroup()); + return true; + } + + LOG.warn(prefix + "[WARN] Failed to remove type " + type.getGroup() + ":" + type.getType() + " from group " + getGroup() + ". Are we sure that the type was added to the group first?"); + return false; } - /**Get the type from the group + /** Remove a type from the group based on it's group-type identifier. * * @param type - * @return IType<> + * @return */ - public Type getType(String type) + public boolean removeType(String groupID, String typeID) { - if(isInGroup(type)) + + for(Type type : TYPES) { - return (Type) TYPES.get(TYPEIDS.indexOf(type)); + if(type.getGroup().equals(groupID) && type.getType().equals(typeID)) + { + LOG.info(prefix + "Removed type " + TYPES.remove(TYPES.indexOf(type)).getType() + " from group " + getGroup()); + return true; + } } - return null; + LOG.warn(prefix + "[WARN] Failed to remove type " + groupID + ":" + typeID + " from group " + getGroup() + ". Are we sure that the type was added to the group first?"); + return false; } - // Type conversion - /**Convert a packet to a new type + /** Remove a type from the group based on it's group-type identifier. * - * @param packet The packet to convert - * @param type The type to convert to - * @return IPacket<> + * @param type + * @return */ - public IPacket convertPacket(IPacket packet, Type type) + public boolean removeType(String typeID) { - if(isInGroup(packet.getType()) && isInGroup(type)) + + return removeType(getGroup(), typeID); + } + + + // Check if a type is in the group + + /**Check if the type is in the group + * + * @param type + * @return boolean + */ + public boolean isInGroup(Type type) + { + for(Type t : TYPES) { - return type.fromBase(packet.getType().toBase(packet)); + if(t.equals(type)) return true; } - - return null; + return false; } - public IPacket convertPacket(IPacket packet, String type) + /**Check if the type is in the group + * + * @param groupID + * @param typeID + * @return boolean + */ + public boolean isInGroup(String groupID, String typeID) { - if(isInGroup(packet.getType()) && isInGroup(type)) + for(Type t : TYPES) { - return getType(type).fromBase(packet.getType().toBase(packet)); + if(t.getGroup().equals(groupID) && t.getType().equals(typeID)) return true; } - - return null; + return false; + } + + /**Check if the type is in the group + * + * @param typeID + * @return boolean + */ + public boolean isInGroup(String typeID) + { + return isInGroup(getGroup(), typeID); } - // Type management + // Get a type in the group - public boolean addType(Type type, String typeID) + /**Get the type from the group + * + * @param type + * @return Type + */ + @SuppressWarnings("unchecked") + public Type getType(String groupID, String typeID) { - if(!isInGroup(type.getType()) && !isInGroup(typeID)) + for(Type t : TYPES) { - TYPES.add(type); - TYPEIDS.add(typeID); - LOG.info("Added type " + typeID + " to group " + this.getGroup()); - return true; + if(t.getGroup().equals(groupID) && t.getType().equals(typeID)) return (Type) t; } - return false; + return null; } + public Type getType(String typeID) + { + return getType(getGroup(), typeID); + } - public boolean removeType(Type type) + + // Type conversion + + /** Actually convert the packet + * + * @param packet + * @param type + * @return + */ + protected Packet convertPacketRaw(Packet packet, Type type) { - - if(isInGroup(type) && TYPES.indexOf(type) != -1) + return type.fromBase(packet.getType().toBase(packet, getGroup()), getGroup()); + } + + /**Convert a packet to a new type + * + * @param packet The packet to convert + * @param type The type to convert to + * @return Packet + */ + public Packet convertPacket(Packet packet, Type type) + { + if(isInGroup(packet.getType()) && isInGroup(type)) { - String id = TYPEIDS.remove(TYPES.indexOf(type)); - TYPES.remove(type); - LOG.info("Removed type " + id + " from group " + this.getGroup()); - return true; + return convertPacketRaw(packet, type); } - LOG.warn("Failed to remove type " + type.getType() + " from group " + this.getGroup() + ". Are we sure that the type was added to the group first?"); - return false; + + return null; } - public boolean removeType(String typeID) + /**Convert a packet to a new type + * + * @param packet The packet to convert + * @param groupID The groupID of the Type to convert to + * @param typeID The typeID of the Type to convert to + * @return Packet + */ + public Packet convertPacket(Packet packet, String groupID, String typeID) { - if(isInGroup(typeID) && TYPEIDS.indexOf(typeID) != -1) + Type toType = getType(groupID, typeID); + if(toType != null) { - TYPES.remove(TYPEIDS.indexOf(typeID)); - TYPEIDS.remove(typeID); - LOG.info("Removed type " + typeID + " from group " + this.getGroup()); - return true; + return convertPacketRaw(packet, toType); } - LOG.warn("Failed to remove type " + typeID + " from group " + this.getGroup() + ". Are we sure that the type was added to the group first?"); - return false; + return null; } - /** The group identifier. Must be overridden for any class implementing ITypeGroup + /** The group identifier. Given by the Base Group * @return String */ - public String getGroup() + public final String getGroup() { - return "ITYPEGROUP"; + return baseType.getGroup(); } } diff --git a/src/main/java/net/transit/type/group/simple/EnergyGroup.java b/src/main/java/net/transit/type/group/simple/EnergyGroup.java deleted file mode 100644 index 2d1eadc..0000000 --- a/src/main/java/net/transit/type/group/simple/EnergyGroup.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.transit.type.group.simple; - -import net.transit.type.group.TypeGroup; - -public class EnergyGroup extends TypeGroup{ - public String getGroup() - { - return "ENERGY"; - } -} diff --git a/src/main/java/net/transit/type/group/simple/FluidGroup.java b/src/main/java/net/transit/type/group/simple/FluidGroup.java deleted file mode 100644 index 3272c94..0000000 --- a/src/main/java/net/transit/type/group/simple/FluidGroup.java +++ /dev/null @@ -1,11 +0,0 @@ -package net.transit.type.group.simple; - -import net.minecraft.block.FluidBlock; -import net.transit.type.group.TypeGroup; - -public class FluidGroup extends TypeGroup{ - public String getGroup() - { - return "FLUID"; - } -} diff --git a/src/main/java/net/transit/type/group/simple/InitSimpleGroups.java b/src/main/java/net/transit/type/group/simple/InitSimpleGroups.java new file mode 100644 index 0000000..50a268a --- /dev/null +++ b/src/main/java/net/transit/type/group/simple/InitSimpleGroups.java @@ -0,0 +1,22 @@ +package net.transit.type.group.simple; + +import net.minecraft.block.FluidBlock; +import net.minecraft.item.Item; +import net.transit.type.group.GroupRegistry; +import net.transit.type.group.TypeGroup; +import net.transit.type.simple.SimpleTypes; + +public final class InitSimpleGroups { + public static final TypeGroup ENERGY_GROUP = new TypeGroup(SimpleTypes.ENERGY); + public static final TypeGroup MANA_GROUP = new TypeGroup(SimpleTypes.MANA); + public static final TypeGroup ITEM_GROUP = new TypeGroup(SimpleTypes.ITEM); + public static final TypeGroup FLUID_GROUP = new TypeGroup(SimpleTypes.FLUID); + + public static final void init() + { + GroupRegistry.addGroup(ENERGY_GROUP); + GroupRegistry.addGroup(MANA_GROUP); + GroupRegistry.addGroup(ITEM_GROUP); + GroupRegistry.addGroup(FLUID_GROUP); + } +} diff --git a/src/main/java/net/transit/type/group/simple/ItemGroup.java b/src/main/java/net/transit/type/group/simple/ItemGroup.java deleted file mode 100644 index b7df90b..0000000 --- a/src/main/java/net/transit/type/group/simple/ItemGroup.java +++ /dev/null @@ -1,11 +0,0 @@ -package net.transit.type.group.simple; - -import net.minecraft.item.Item; -import net.transit.type.group.TypeGroup; - -public class ItemGroup extends TypeGroup{ - public String getGroup() - { - return "ITEM"; - } -} diff --git a/src/main/java/net/transit/type/group/simple/ManaGroup.java b/src/main/java/net/transit/type/group/simple/ManaGroup.java deleted file mode 100644 index 6ed6ff1..0000000 --- a/src/main/java/net/transit/type/group/simple/ManaGroup.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.transit.type.group.simple; - -import net.transit.type.group.TypeGroup; - -public class ManaGroup extends TypeGroup{ - public String getGroup() - { - return "MANA"; - } -} diff --git a/src/main/java/net/transit/type/group/simple/SimpleGroups.java b/src/main/java/net/transit/type/group/simple/SimpleGroups.java deleted file mode 100644 index 57ab2e9..0000000 --- a/src/main/java/net/transit/type/group/simple/SimpleGroups.java +++ /dev/null @@ -1,26 +0,0 @@ -package net.transit.type.group.simple; - -import net.minecraft.block.FluidBlock; -import net.minecraft.item.Item; -import net.transit.type.group.GroupRegistry; -import net.transit.type.group.TypeGroup; - -/** - * @author Kyle Gunger - * - * @apiNote Basic default groups which all mods are welcome to add themselves to. - */ -public class SimpleGroups -{ - public static final TypeGroup ENERGY_GROUP = new EnergyGroup(); - public static final TypeGroup MANA_GROUP = new ManaGroup(); - public static final TypeGroup ITEM_GROUP = new ItemGroup(); - public static final TypeGroup FLUID_GROUP = new FluidGroup(); - public static void initGroups() - { - GroupRegistry.addGroup(ENERGY_GROUP, "ENERGY"); - GroupRegistry.addGroup(MANA_GROUP, "MANA"); - GroupRegistry.addGroup(ITEM_GROUP, "ITEM"); - GroupRegistry.addGroup(FLUID_GROUP, "FLUID"); - } -} diff --git a/src/main/java/net/transit/type/simple/EnergyType.java b/src/main/java/net/transit/type/simple/EnergyType.java deleted file mode 100644 index 113b881..0000000 --- a/src/main/java/net/transit/type/simple/EnergyType.java +++ /dev/null @@ -1,11 +0,0 @@ -package net.transit.type.simple; - -import net.transit.type.Type; - -public class EnergyType extends Type -{ - public String getType() { - return "ENERGY_TYPE"; - } - -} diff --git a/src/main/java/net/transit/type/simple/FluidType.java b/src/main/java/net/transit/type/simple/FluidType.java deleted file mode 100644 index 86a3fb0..0000000 --- a/src/main/java/net/transit/type/simple/FluidType.java +++ /dev/null @@ -1,11 +0,0 @@ -package net.transit.type.simple; - -import net.minecraft.block.FluidBlock; -import net.transit.type.Type; - -public class FluidType extends Type -{ - public String getType() { - return "FLUID_TYPE"; - } -} diff --git a/src/main/java/net/transit/type/simple/ItemType.java b/src/main/java/net/transit/type/simple/ItemType.java deleted file mode 100644 index 6e860fc..0000000 --- a/src/main/java/net/transit/type/simple/ItemType.java +++ /dev/null @@ -1,11 +0,0 @@ -package net.transit.type.simple; - -import net.minecraft.item.Item; -import net.transit.type.Type; - -public class ItemType extends Type -{ - public String getType() { - return "ITEM_TYPE"; - } -} diff --git a/src/main/java/net/transit/type/simple/ManaType.java b/src/main/java/net/transit/type/simple/ManaType.java deleted file mode 100644 index 6959216..0000000 --- a/src/main/java/net/transit/type/simple/ManaType.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.transit.type.simple; - -import net.transit.type.Type; - -public class ManaType extends Type -{ - public String getType() { - return "MANA_TYPE"; - } -} diff --git a/src/main/java/net/transit/type/simple/SimpleTypes.java b/src/main/java/net/transit/type/simple/SimpleTypes.java index e01af77..b1f1b98 100644 --- a/src/main/java/net/transit/type/simple/SimpleTypes.java +++ b/src/main/java/net/transit/type/simple/SimpleTypes.java @@ -3,25 +3,10 @@ package net.transit.type.simple; import net.minecraft.block.FluidBlock; import net.minecraft.item.Item; import net.transit.type.Type; -import net.transit.type.group.simple.SimpleGroups; -/** - * @author Kyle Gunger - * - * @apiNote Simple types which work as the base types for the simple groups. - */ -public class SimpleTypes -{ - public static final Type ENERGY_TYPE = new EnergyType(); - public static final Type MANA_TYPE = new ManaType(); - public static final Type ITEM_TYPE = new ItemType(); - public static final Type FLUID_TYPE = new FluidType(); - public static void initTypes() - { - SimpleGroups.ENERGY_GROUP.addType(ENERGY_TYPE, "ENERGY"); - SimpleGroups.MANA_GROUP.addType(MANA_TYPE, "MANA"); - SimpleGroups.ITEM_GROUP.addType(ITEM_TYPE, "ITEM"); - SimpleGroups.FLUID_GROUP.addType(FLUID_TYPE, "FLUID"); - System.out.println(SimpleGroups.ENERGY_GROUP.isInGroup("ENERGY")); - } +public final class SimpleTypes { + public static final Type ENERGY = new Type("ENERGY", "ENERGY"); + public static final Type MANA = new Type("MANA", "MANA"); + public static final Type ITEM = new Type("ITEM", "ITEM"); + public static final Type FLUID = new Type("FLUID", "FLUID"); } -- cgit v1.2.3