From 20f2c0836b309a95cfcf0d9c13fafeefc5c1d5e7 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 16 Nov 2023 13:24:49 -0500 Subject: Change how GroupRegistry deals with Wildcard Generics --- gradle.properties | 2 +- src/main/java/net/cshift/api/transit/Transit.java | 7 +- .../net/cshift/api/transit/network/Channel.java | 12 ++- .../java/net/cshift/api/transit/network/INode.java | 20 +++-- .../net/cshift/api/transit/network/ISystem.java | 15 ---- .../cshift/api/transit/network/PoolManifest.java | 28 +++---- .../api/transit/network/packet/DynamicPacket.java | 14 ++-- .../api/transit/network/packet/IDynamicPacket.java | 13 ++-- .../api/transit/network/packet/IStaticPacket.java | 10 +-- .../transit/network/packet/MetaDynamicPacket.java | 44 ----------- .../api/transit/network/packet/MetaPacket.java | 35 --------- .../api/transit/network/packet/StaticPacket.java | 6 +- .../net/cshift/api/transit/type/SimpleTypes.java | 8 +- .../java/net/cshift/api/transit/type/Type.java | 74 ++++++++++++++---- .../api/transit/type/group/GroupRegistry.java | 88 +++++++++++++++++++--- .../api/transit/type/group/SimpleGroups.java | 8 +- .../cshift/api/transit/type/group/TypeGroup.java | 16 +++- src/main/resources/fabric.mod.json | 2 +- 18 files changed, 216 insertions(+), 186 deletions(-) delete mode 100644 src/main/java/net/cshift/api/transit/network/ISystem.java delete mode 100644 src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java delete mode 100644 src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java diff --git a/gradle.properties b/gradle.properties index 240d22c..9cf48d8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.20.2+build.1 loader_version=0.14.22 # Mod Properties -mod_version=3.0.0-alpha +mod_version=3.0.0-alpha-2 maven_group=net.cshift.api archives_base_name=transit diff --git a/src/main/java/net/cshift/api/transit/Transit.java b/src/main/java/net/cshift/api/transit/Transit.java index adc9f25..6d0523d 100644 --- a/src/main/java/net/cshift/api/transit/Transit.java +++ b/src/main/java/net/cshift/api/transit/Transit.java @@ -26,10 +26,9 @@ package net.cshift.api.transit; import net.fabricmc.api.ModInitializer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.slf4j.*; -import net.cshift.api.transit.type.group.SimpleGroups; +import net.cshift.api.transit.type.group.*; /** * @author Kyle Gunger @@ -38,7 +37,7 @@ import net.cshift.api.transit.type.group.SimpleGroups; public class Transit implements ModInitializer { private static final Logger LOGGER = LoggerFactory.getLogger("Transit API"); - + @Override public void onInitialize() { LOGGER.info("Transit API: Providing a thin wrapper over reality!"); diff --git a/src/main/java/net/cshift/api/transit/network/Channel.java b/src/main/java/net/cshift/api/transit/network/Channel.java index bd4cdf2..3fdae88 100644 --- a/src/main/java/net/cshift/api/transit/network/Channel.java +++ b/src/main/java/net/cshift/api/transit/network/Channel.java @@ -25,6 +25,8 @@ package net.cshift.api.transit.network; import net.cshift.api.transit.network.packet.IStaticPacket; +import net.cshift.api.transit.type.group.GroupRegistry; +import net.cshift.api.transit.type.group.TypeGroup; /** * @author Kyle Gunger @@ -35,6 +37,7 @@ public final class Channel { private INode to; private int id; private String group; + private Class baseClass; /** This constructor should be called by a node approving a connection. The approving node can give the connection an ID and group. * Negative IDs indicate a terminated connection, so do not initialize the class with a negative ID. @@ -43,11 +46,12 @@ public final class Channel { * @param id The channel's id, as assigned by the recieving node. In most cases, this will match the pool ID as a way to match channels to pools. * @param group */ - public Channel(INode node, int id, String group) + public Channel(INode node, int id, String group, Class baseClass) { to = node; this.id = id; this.group = group; + this.baseClass = baseClass; } @@ -78,8 +82,8 @@ public final class Channel { * * @return */ - public String getGroup() { - return group; + public TypeGroup getGroup() { + return GroupRegistry.groupByID(group, baseClass); } /** Returns true if the connection has been terminated @@ -135,7 +139,7 @@ public final class Channel { * @param packet the packet to send * @return The overflow data if the packet is only partially accepted. {@code null} otherwise. */ - public IStaticPacket send(IStaticPacket packet) + public IStaticPacket send(IStaticPacket packet) { return to.accept(packet, this); } diff --git a/src/main/java/net/cshift/api/transit/network/INode.java b/src/main/java/net/cshift/api/transit/network/INode.java index 54701e7..6653e40 100644 --- a/src/main/java/net/cshift/api/transit/network/INode.java +++ b/src/main/java/net/cshift/api/transit/network/INode.java @@ -13,25 +13,29 @@ public interface INode */ public PoolManifest getManifest(); - /** Get the system managing the node or {@code null} if there isn't one - * - */ - public ISystem getSystem(); - // ############### // # Connections # // ############### - /** Call this function to establish a connection with a node. + /** Call this function to establish a specific connection with a node. * * @param The type of connection being asked for * @param poolID The ID of the pool the channel will interface with (see PoolManifest) * @param asker The asking node * @return A channel if the node accepts the request, {@code null} otherwise */ - public Channel connect(int poolID, INode asker); + public Channel connect(int poolID, String group, INode asker); + + /** Call this function to establish a default connection with a node. + * + * @param The type of connection being asked for + * @param group The group that is being connected to + * @param asker The asking node + * @return A channel if the node accepts the request, {@code null} otherwise + */ + public Channel connectDefault(String group, INode asker); /** Accept a packet from a channel (or not). * @@ -41,7 +45,7 @@ public interface INode * @param channel The channel which the packet is coming through * @return The overflow data if the packet is only partially accepted. {@code null} otherwise. */ - public IStaticPacket accept(IStaticPacket packet, Channel channel); + public IStaticPacket accept(IStaticPacket packet, Channel channel); /** Pressure * diff --git a/src/main/java/net/cshift/api/transit/network/ISystem.java b/src/main/java/net/cshift/api/transit/network/ISystem.java deleted file mode 100644 index b7ab9bf..0000000 --- a/src/main/java/net/cshift/api/transit/network/ISystem.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.cshift.api.transit.network; - - -/** - * @author Kyle Gunger - * @apiNote An ISystem represents a system of nodes which has been optimized for performance. A node can exist on its own, but an ISystem can't exist without a node. - */ -public interface ISystem -{ - /**The nodes stored by the system - * - * @return INode[] - */ - public INode[] getNodes(); -} diff --git a/src/main/java/net/cshift/api/transit/network/PoolManifest.java b/src/main/java/net/cshift/api/transit/network/PoolManifest.java index 26cbece..e539ee1 100644 --- a/src/main/java/net/cshift/api/transit/network/PoolManifest.java +++ b/src/main/java/net/cshift/api/transit/network/PoolManifest.java @@ -6,35 +6,37 @@ package net.cshift.api.transit.network; */ public abstract class PoolManifest { - /** Represents the number of pools that the node has access to for the specified resource. - * @apiNote A "pool" in this context represents an independant network of resources. + /** Represents the number of pools that the node has access to. + * @apiNote A "pool" in this context represents an independent network of resources. + * Each network in the pool may represent a different TypeGroup. * Pool zero should be the default group that simple nodes will attempt to connect to. * @param group The TypeGroup that the pool belongs to */ - public abstract int poolCount(String group); + public abstract int poolCount(); - /** The ID of the pool. The INode will use this in a connection attempt with the other INode. - * - * @param group The TypeGroup the pool belongs to - * @param pool Array-like index for pool (gotten from poolCount) - */ - public abstract int poolID(String group, int pool); - - /** If the mod supports named pools, the names can be querried through this function. + /** If the mod supports named pools, the names can be queried through this function. * * @param group The TypeGroup the pool belongs to * @param pool Array-like index for pool (gotten from poolCount) */ - public String poolName(String group, int pool) { + public String poolName(int pool) { return ""; } + + /** Returns true if the pool with the given id supports the given group + * + * @param pool The pool ID to query + * @param group The TypeGroup to query + * @return {@code true} if the pool supports the specified group, {@code false} otherwise + */ + public abstract boolean poolProvides(int pool, String group); /** If the mod supports pool descriptions, they can be accessed by this method. * * @param group The TypeGroup the pool belongs to * @param pool Array-like index for pool (gotten from poolCount) */ - public String poolDescription(String group, int pool) { + public String poolDescription(int pool) { return ""; } } diff --git a/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java index f3a6c47..3a57227 100644 --- a/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java +++ b/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java @@ -3,14 +3,14 @@ package net.cshift.api.transit.network.packet; import net.cshift.api.transit.type.Type; /** - * Simple packet which stores a fluid value. + * Simple packet which stores a value which can change. * + * @param The type parameter of the Type being transported * @author Kyle Gunger - * - * @param The data type (Object) that the packet transfers + * */ public class DynamicPacket implements IDynamicPacket { - private D data; + private Object data; private Type type; /** Constructor. Stores the given data and uses the given type. @@ -18,20 +18,20 @@ public class DynamicPacket implements IDynamicPacket { * @param dat The packet's data * @param t The packet's type */ - public DynamicPacket(D dat, Type t) + public DynamicPacket(Object dat, Type t) { data = dat; type = t; } @Override - public D getData() + public Object getData() { return data; } @Override - public void setData(D dat) + public void setData(Object dat) { data = dat; } diff --git a/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java index 334d5f6..6d80b3b 100644 --- a/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java +++ b/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java @@ -1,14 +1,11 @@ package net.cshift.api.transit.network.packet; -/** Interface describing a fluid packet. +/** Interface describing a packet where the data can change as it's transferred. + * + * @param The type parameter of the Type being transported * @author Kyle Gunger - * - * @param The data type (Object) that the packet transfers */ public interface IDynamicPacket extends IStaticPacket{ - /**Set the packet's data. - * - * @return The packet's data - */ - public void setData(D dat); + /** Set the packet's data. */ + public void setData(Object dat); } diff --git a/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java b/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java index ba1b17f..cdad90a 100644 --- a/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java +++ b/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java @@ -3,21 +3,21 @@ package net.cshift.api.transit.network.packet; import net.cshift.api.transit.type.*; /** Interface describing an unchanging packet. - * @author Kyle Gunger * - * @param The data type (Object) that the packet transfers. + * @param The type parameter of the Type being transported + * @author Kyle Gunger */ public interface IStaticPacket { /**Get the packet's data. * - * @return The packet's data + * @return The packet's data */ - public D getData(); + public Object getData(); /**Get the packet's type. * - * @return IType<> The type of the packet + * @return IType The type of the packet */ public Type getType(); } diff --git a/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java deleted file mode 100644 index d382645..0000000 --- a/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java +++ /dev/null @@ -1,44 +0,0 @@ -package net.cshift.api.transit.network.packet; - -import net.cshift.api.transit.type.Type; - -/** Static packet with extra data attached. - * @author Kyle Gunger - * - * @param The data type (Object) that the packet transfers - * @param The type of metadata -*/ -public class MetaDynamicPacket extends DynamicPacket -{ - private M metaData; - - /** Constructor - * - * @param dat The data to store - * @param t The Type of the data - * @param meta The metadata to store - */ - public MetaDynamicPacket(D dat, Type t, M meta) - { - super(dat, t); - metaData = meta; - } - - /** Get the metadata of the packet. - * - * @return The packet's metadata - */ - public M getMetaData() - { - return metaData; - } - - /** Set the metadata of the packet. - * - * @param meta The packet's new metadata - */ - public void setMetaData(M meta) - { - metaData = meta; - } -} diff --git a/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java b/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java deleted file mode 100644 index aea1b83..0000000 --- a/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.cshift.api.transit.network.packet; - -import net.cshift.api.transit.type.*; - -/** Static packet with extra data attached. - * @author Kyle Gunger - * - * @param The data type (Object) that the packet transfers - * @param The type of metadata -*/ -public class MetaPacket extends StaticPacket -{ - private M metaData; - - /** Constructor - * - * @param dat The data to store - * @param t The Type of the data - * @param meta The metadata to store - */ - public MetaPacket(D dat, Type t, M meta) - { - super(dat, t); - metaData = meta; - } - - /** Get the metadata of the packet. - * - * @return The packet's metadata - */ - public M getMetaData() - { - return metaData; - } -} diff --git a/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java b/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java index 2c6a16a..5ffed26 100644 --- a/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java +++ b/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java @@ -9,7 +9,7 @@ import net.cshift.api.transit.type.*; */ public class StaticPacket implements IStaticPacket { - private D data; + private Object data; private Type type; /** Constructor. Stores the given data and uses the given type. @@ -17,14 +17,14 @@ public class StaticPacket implements IStaticPacket * @param dat The packet's data * @param t The packet's type */ - public StaticPacket(D dat, Type t) + public StaticPacket(Object dat, Type t) { data = dat; type = t; } @Override - public D getData() + public Object getData() { return data; } diff --git a/src/main/java/net/cshift/api/transit/type/SimpleTypes.java b/src/main/java/net/cshift/api/transit/type/SimpleTypes.java index 54106c7..5b4a6b8 100644 --- a/src/main/java/net/cshift/api/transit/type/SimpleTypes.java +++ b/src/main/java/net/cshift/api/transit/type/SimpleTypes.java @@ -27,17 +27,17 @@ package net.cshift.api.transit.type; public final class SimpleTypes { /** Transfers energy. Energy is stored as a numeric. */ - public static final Type TransitJoule = new Type("Joule", "ENERGY"); + public static final Type TransitJoule = new Type("Joule", "ENERGY", Long.class); /** Transfers mana. TMana stores mana count and type. */ - public static final Type TransitMana = new Type("TMana", "MANA"); + public static final Type TransitMana = new Type("TMana", "MANA", TMana.class); /** Transfers items. TItem stores an item and an item count. */ - public static final Type TransitItem = new Type("TItem", "ITEM"); + public static final Type TransitItem = new Type("TItem", "ITEM", TItem.class); /** Transfers fluid. TFluid stores fluid and mB. */ - public static final Type TransitFluid = new Type("TFluid", "FLUID"); + public static final Type TransitFluid = new Type("TFluid", "FLUID", TFluid.class); } diff --git a/src/main/java/net/cshift/api/transit/type/Type.java b/src/main/java/net/cshift/api/transit/type/Type.java index d47cd08..f544e8f 100644 --- a/src/main/java/net/cshift/api/transit/type/Type.java +++ b/src/main/java/net/cshift/api/transit/type/Type.java @@ -1,3 +1,27 @@ +/* + MIT License + + Copyright (c) 2023 Kyle Gunger + + 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 net.cshift.api.transit.type; import net.cshift.api.transit.network.packet.*; @@ -10,12 +34,14 @@ public class Type { protected final String typeID; protected final String groupID; + private final Class underlying; // Should not override, but you can if you must - public Type(String tID, String gID) + public Type(String tID, String gID, Class underlying) { typeID = tID; groupID = gID; + this.underlying = underlying; } @@ -31,9 +57,13 @@ public class Type * @param group The group asking for the conversion * @return The data in the group's base type */ - public T toBase(T data, String group) + @SuppressWarnings("unchecked") + public T toBase(Object data, String group) { - return data; + // Assume the underlying is the same as T for this very simple case + if (data.getClass().equals(this.getUnderlying())) + return (T) data; + return null; } /** Return the data formatted in the default group's base type. @@ -41,7 +71,7 @@ public class Type * @param data The data (of this type) * @return The data in the group's base type */ - public final T toBase(T data) + public final T toBase(Object data) { return this.toBase(data, this.groupID); } @@ -54,7 +84,9 @@ public class Type */ public final T toBase(IStaticPacket packet, String group) { - return this.toBase(packet.getData(), group); + if(packet.getType().equals(this)) + return this.toBase(packet.getData(), group); + return null; } /** Return the packet's data formatted in the default group's base type. @@ -78,19 +110,20 @@ public class Type * * @param data The base data * @param group The group asking for the conversion - * @return The packet's data in the group's base type + * @return The converted data, in the Type's underlying */ - public T fromBase(T data, String group) + public Object fromBase(T data, String group) { + // Assume T is the same as the underlying return data; } /** Convert data from the default group's base type to this type. * * @param data The base data - * @return The packet's data in the group's base type + * @return The converted data, in the Type's underlying */ - public final T fromBase(T data) + public final Object fromBase(T data) { return fromBase(data, this.groupID); } @@ -99,7 +132,7 @@ public class Type * * @param data The base data * @param group The group asking for the conversion - * @return The packet's data in the group's base type + * @return A packet containing the converted data in the Type's underlying */ public final IStaticPacket packetFromBase(T data, String group) { @@ -130,7 +163,7 @@ public class Type * @param data The data to convert * @return The data formatted in this type */ - public T convertFrom(Type from, T data, String group) { + public Object convertFrom(Type from, Object data, String group) { return this.fromBase(from.toBase(data, group), group); } @@ -140,7 +173,7 @@ public class Type * @param data The data to convert * @return The data formatted in this type */ - public final T convertFrom(Type from, T data) { + public final Object convertFrom(Type from, Object data) { return convertFrom(from, data, from.groupID); } @@ -159,7 +192,7 @@ public class Type * @param packet The packet to convert * @return The new packet formatted in this type */ - public final IStaticPacket convertFrom(IStaticPacket packet) { + public final IStaticPacket convertFrom(IStaticPacket packet) throws ClassCastException { return this.convertFrom(packet, packet.getType().groupID); } @@ -176,7 +209,7 @@ public class Type * @param data The data to convert * @return The data formatted in this type */ - public final T convertTo(Type to, T data, String group) { + public final Object convertTo(Type to, Object data, String group) { return to.convertFrom(this, data, group); } @@ -186,7 +219,7 @@ public class Type * @param data The data to convert * @return The data formatted in this type */ - public final T convertTo(Type to, T data) { + public final Object convertTo(Type to, Object data) { return to.convertFrom(this, data, this.groupID); } @@ -232,7 +265,16 @@ public class Type { return groupID; } - + + /** The underlying class which this Type uses to implement functionality within the mod + * + * @return Class + */ + public final Class getUnderlying() + { + return underlying; + } + @Override public final String toString() { return groupID + ":" + typeID; diff --git a/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java b/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java index 5e09e71..f74e9c5 100644 --- a/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java +++ b/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java @@ -1,3 +1,27 @@ +/* + MIT License + + Copyright (c) 2023 Kyle Gunger + + 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 net.cshift.api.transit.type.group; import java.security.InvalidKeyException; @@ -14,8 +38,7 @@ public final class GroupRegistry { private GroupRegistry() {} - public static final boolean addGroup(TypeGroup group) - { + public static final boolean addGroup(TypeGroup group) { if (GROUPS.containsKey(group.getGroup())) { LOGGER.warn("Failed to add group " + group + " to the registry. Did another mod add a group with the same name?"); return false; @@ -27,24 +50,67 @@ public final class GroupRegistry { return true; } - public static final TypeGroup groupByID(String groupID) - { + /** Get the requested type group via ID + * + * @param The base type of the type group + * @param groupID + * @param baseClass + * @throws ClassCastException if the group is not of the provided class type + * @return The group (properly casted) if it exists, or null otherwise + */ + @SuppressWarnings("unchecked") + public static final TypeGroup groupByID(String groupID, Class baseClass) throws ClassCastException { if (GROUPS.containsKey(groupID)) { TypeGroup g = GROUPS.get(groupID); - return (TypeGroup) g; + if (g.getBaseClass().equals(baseClass)) { + return (TypeGroup) g; + } else { + throw new ClassCastException( + "Group " + g.getGroup() + " was requested as TypeGroup<" + + baseClass.getName() + ">, but the group is actually TypeGroup<" + + g.getBaseClass().getName() + ">" + ); + } } return null; } - public static final Type typeByIdentity(String groupID, String typeID) throws InvalidKeyException - { - TypeGroup group = GroupRegistry.groupByID(groupID); + /** Get the wildcard generic TypeGroup via the groupID + * + * @param groupID The ID of the group to get + * @return The group if it exists, or null otherwise + */ + public static final TypeGroup groupByID(String groupID) { + if (GROUPS.containsKey(groupID)) { + return GROUPS.get(groupID); + } - if(group != null) - return group.getType(typeID); - return null; } + /** Get the requested type via + * + * @param + * @param groupID + * @param typeID + * @param baseClass + * @return + * @throws ClassCastException + */ + public static final Type typeByIdentity(String groupID, String typeID, Class baseClass) throws ClassCastException { + TypeGroup group = GroupRegistry.groupByID(groupID, baseClass); + + if(group != null && group.isInGroup(typeID)) { + try { + return group.getType(typeID); + } catch (InvalidKeyException e) { + // Should never happen, since we just checked that the type is in the group + LOGGER.error("Unreachable in typeByIdentity: trying to get type '" + groupID + ":" + typeID + "'"); + e.printStackTrace(); + } + } + + return null; + } } diff --git a/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java b/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java index 66d0121..3f7d8a1 100644 --- a/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java +++ b/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java @@ -31,19 +31,19 @@ import net.cshift.api.transit.type.*; public final class SimpleGroups { /** Transfers energy. Basic unit is TJoule (Transit Joule). */ - public static final TypeGroup ENERGY = new TypeGroup(SimpleTypes.TransitJoule); + public static final TypeGroup ENERGY = new TypeGroup(SimpleTypes.TransitJoule, Long.class); /** Transfers mana. Basic unit is TMana (Transit Mana). */ - public static final TypeGroup MANA = new TypeGroup(SimpleTypes.TransitMana); + public static final TypeGroup MANA = new TypeGroup(SimpleTypes.TransitMana, TMana.class); /** Transfers items. Basic unit is TItem. */ - public static final TypeGroup ITEM = new TypeGroup(SimpleTypes.TransitItem); + public static final TypeGroup ITEM = new TypeGroup(SimpleTypes.TransitItem, TItem.class); /** Transfers fluids. Basic unit is TFluid. */ - public static final TypeGroup FLUID = new TypeGroup(SimpleTypes.TransitFluid); + public static final TypeGroup FLUID = new TypeGroup(SimpleTypes.TransitFluid, TFluid.class); private static final Logger LOGGER = LoggerFactory.getLogger("Transit API/Simple Groups"); diff --git a/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java b/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java index fcbb3ef..426f0a8 100644 --- a/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java +++ b/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java @@ -25,7 +25,6 @@ package net.cshift.api.transit.type.group; import java.security.InvalidKeyException; -import java.sql.Types; import java.util.HashMap; import org.slf4j.*; @@ -38,6 +37,9 @@ import net.cshift.api.transit.type.Type; */ public final class TypeGroup { + // The basic class of the base type which is being transferred + private Class baseClass; + // The base Type (provides the group's identifier) private Type baseType; @@ -48,10 +50,11 @@ public final class TypeGroup private static final Logger LOGGER = LoggerFactory.getLogger("Transit API/Type Group"); - public TypeGroup(Type base) + public TypeGroup(Type base, Class baseClass) { TYPES = new HashMap>(); baseType = base; + this.baseClass = baseClass; addType(base); } @@ -207,11 +210,18 @@ public final class TypeGroup } /** The base group type - * @return Type + * @return Type */ public final Type getBase() { return baseType; } + + /** A Class object which represents the type that the base type uses to transfer data + * @return Class + */ + public final Class getBaseClass() { + return baseClass; + } @Override public final String toString() { diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 6f2fbd5..94912c3 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "id": "transit-api", - "version": "3.0.0-alpha", + "version": "3.0.0-alpha-2", "name": "Transit API", "description": "Move things about!", -- cgit v1.2.3