Skip to content

Types & Groups

Kyle Gunger edited this page Jun 20, 2021 · 9 revisions

Types

package net.cshift.transit.type

At the core of Transit is it's type system. It is one of the only things required to interact with when implementing this mod into your own.

"Type" can be a little misleading, it's more like "Format". The format of MJ vs RF or the format that AE2 uses for it's items vs the format EnderIO uses for it's item pipes would be good examples. All types in the same group are representations of the same thing.

Each group has a base type used for conversion, so a type must be able to convert it's own format of energy/items/fluids to the base format.

The base type class net.cshift.transit.type.Type contains mostly final methods and members, but the few non-final methods should be overridden.

Here are the methods available to you:

  • toBase - formats the provided data as the base format for the group
    • TJoule (Number) for "ENERGY"
    • TMana (TMana) for "MANA"
    • Item (TItem) for "ITEM"
    • Fluid (TFluid) for "FLUID"
  • fromBase - formats data in the type's format from the base format
  • convertFrom - converts between two types using the base as an intermediate
  • convertTo - converts between two types using the base as an intermediate
  • getType() - returns the type's name
  • getGroup() - returns the type's default group
  • toString() - returns <groupID>:<typeID> (i.e. ENERGY:TJoule)

What you can override:

  • toBase(T data, String group)
  • fromBase(T data, String group)
  • convertFrom(Type<T> from, T data, String group)

Example implementation:

Let's say that a new mod wanted to add it's own energy type called "Minecraft Tesla (MT)". This mod would have to figure out the conversion ratio for MT to TJ (let's say one MT is worth 2 TJ), and extend the type class to implement it.

(public class MinecraftTesla<T> extends Type<T>)

@Override
public T toBase(T data, String group)
{
	return data * 2;
}

@Override
public T fromBase(T data, String group)
{
	return data / 2;
}

This would be enough as a simple example, but the modder would then have to create an instance of his/her class.

public static final MinecraftTesla<Number> MT = new MinecraftTesla<Number>("MinecraftTesla", "ENERGY")

Which would produce a type with name "MinecraftTesla" and group "ENERGY" (ENERGY:MinecraftTesla).


Groups

package net.cshift.transit.type.group

Transit uses a registry system as a way to group types. To register a type in one of the simple groups, import net.cshift.transit.type.group.simple.SimpleGroups and use one of the provided groups to add a type.

To add the sample type to the registry, simply call SimpleGroups.ENERGY_GROUP.addType(MJ).

TypeGroups need not be extended, but you may if you must. The constructor TypeGroup(Type<B> base) creates the group using the base group's groupID as the group name. Types may be added to multiple groups as long as the object of transmission is the same. Groups can also convert packets, but this is deprecated in favor of direct type conversions due to overhead.

A group can be added to the registry by using GroupRegistry.addGroup(TypeGroup<?> group).

Adding types to groups isn't strictly necessary and may be deprecated in the future, but it's fine for now.

Clone this wiki locally