Update to latest Forge, use Capability standards. Need to port to new fluid system

This commit is contained in:
aidancbrady committed 2016-06-23 16:34:27 -04:00
1 parent 07e307d9b5
commit e6da7fdc9c
106 files changed
+1101 -196

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
package net.darkhax.tesla;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.darkhax.tesla.api.implementation.BaseTeslaContainer;
import net.darkhax.tesla.capability.TeslaCapabilities.CapabilityTeslaConsumer;
import net.darkhax.tesla.capability.TeslaCapabilities.CapabilityTeslaHolder;
import net.darkhax.tesla.capability.TeslaCapabilities.CapabilityTeslaProducer;
import net.darkhax.tesla.lib.Constants;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
@Mod(modid = Constants.MOD_ID, name = Constants.MOD_NAME, version = Constants.VERSION_NUMBER)
public class Tesla {
@Mod.Instance(Constants.MOD_ID)
public static Tesla instance;
@EventHandler
public void preInit (FMLPreInitializationEvent event) {
CapabilityManager.INSTANCE.register(ITeslaConsumer.class, new CapabilityTeslaConsumer<ITeslaConsumer>(), BaseTeslaContainer.class);
CapabilityManager.INSTANCE.register(ITeslaProducer.class, new CapabilityTeslaProducer<ITeslaProducer>(), BaseTeslaContainer.class);
CapabilityManager.INSTANCE.register(ITeslaHolder.class, new CapabilityTeslaHolder<ITeslaHolder>(), BaseTeslaContainer.class);
}
}
@@ -0,0 +1,225 @@
package net.darkhax.tesla.api;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.INBTSerializable;
/**
* A basic Tesla container that serves as a consumer, producer and holder. Custom
* implementations do not need to use all three. The INBTSerializable interface is also
* optional.
*
* @deprecated Moved to net.darkhax.tesla.api.implementation
*/
@Deprecated
public class BaseTeslaContainer implements ITeslaConsumer, ITeslaProducer, ITeslaHolder, INBTSerializable<NBTTagCompound> {
/**
* The amount of stored Tesla power.
*/
private long stored;
/**
* The maximum amount of Tesla power that can be stored.
*/
private long capacity;
/**
* The maximum amount of Tesla power that can be accepted.
*/
private long inputRate;
/**
* The maximum amount of Tesla power that can be extracted
*/
private long outputRate;
/**
* Default constructor. Sets capacity to 5000 and transfer rate to 50. This constructor
* will not set the amount of stored power. These values are arbitrary and should not be
* taken as a base line for balancing.
*/
public BaseTeslaContainer() {
this(5000, 50, 50);
}
/**
* Constructor for setting the basic values. Will not construct with any stored power.
*
* @param capacity The maximum amount of Tesla power that the container should hold.
* @param input The maximum rate of power that can be accepted at a time.
* @param output The maximum rate of power that can be extracted at a time.
*/
public BaseTeslaContainer(long capacity, long input, long output) {
this(0, capacity, input, output);
}
/**
* Constructor for setting all of the base values, including the stored power.
*
* @param power The amount of stored power to initialize the container with.
* @param capacity The maximum amount of Tesla power that the container should hold.
* @param input The maximum rate of power that can be accepted at a time.
* @param output The maximum rate of power that can be extracted at a time.
*/
public BaseTeslaContainer(long power, long capacity, long input, long output) {
this.stored = power;
this.capacity = capacity;
this.inputRate = input;
this.outputRate = output;
}
/**
* Constructor for creating an instance directly from a compound tag. This expects that the
* compound tag has some of the required data. @See {@link #deserializeNBT(NBTTagCompound)}
* for precise info on what is expected. This constructor will only set the stored power if
* it has been written on the compound tag.
*
* @param dataTag The NBTCompoundTag to read the important data from.
*/
public BaseTeslaContainer(NBTTagCompound dataTag) {
this.deserializeNBT(dataTag);
}
@Override
public long getStoredPower () {
return this.stored;
}
@Override
public long givePower (long Tesla, boolean simulated) {
final long acceptedTesla = Math.min(this.capacity - this.stored, Math.min(this.inputRate, Tesla));
if (!simulated)
this.stored += acceptedTesla;
return acceptedTesla;
}
@Override
public long takePower (long Tesla, boolean simulated) {
final long removedPower = Math.min(this.stored, Math.min(this.outputRate, Tesla));
if (!simulated)
this.stored -= removedPower;
return removedPower;
}
@Override
public long getCapacity () {
return this.capacity;
}
@Override
public NBTTagCompound serializeNBT () {
final NBTTagCompound dataTag = new NBTTagCompound();
dataTag.setLong("TeslaPower", this.stored);
dataTag.setLong("TeslaCapacity", this.capacity);
dataTag.setLong("TeslaInput", this.inputRate);
dataTag.setLong("TeslaOutput", this.outputRate);
return dataTag;
}
@Override
public void deserializeNBT (NBTTagCompound nbt) {
this.stored = nbt.getLong("TeslaPower");
if (nbt.hasKey("TeslaCapacity"))
this.capacity = nbt.getLong("TeslaCapacity");
if (nbt.hasKey("TeslaInput"))
this.inputRate = nbt.getLong("TeslaInput");
if (nbt.hasKey("TeslaOutput"))
this.outputRate = nbt.getLong("TeslaOutput");
if (this.stored > this.capacity)
this.stored = this.capacity;
}
/**
* Sets the capacity of the the container. If the existing stored power is more than the
* new capacity, the stored power will be decreased to match the new capacity.
*
* @param capacity The new capacity for the container.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setCapacity (long capacity) {
this.capacity = capacity;
if (this.stored > capacity)
this.stored = capacity;
return this;
}
/**
* Gets the maximum amount of Tesla power that can be accepted by the container.
*
* @return The amount of Tesla power that can be accepted at any time.
*/
public long getInputRate () {
return this.inputRate;
}
/**
* Sets the maximum amount of Tesla power that can be accepted by the container.
*
* @param rate The amount of Tesla power to accept at a time.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setInputRate (long rate) {
this.inputRate = rate;
return this;
}
/**
* Gets the maximum amount of Tesla power that can be pulled from the container.
*
* @return The amount of Tesla power that can be extracted at any time.
*/
public long getOutputRate () {
return this.outputRate;
}
/**
* Sets the maximum amount of Tesla power that can be pulled from the container.
*
* @param rate The amount of Tesla power that can be extracted.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setOutputRate (long rate) {
this.outputRate = rate;
return this;
}
/**
* Sets both the input and output rates of the container at the same time. Both rates will
* be the same.
*
* @param rate The input/output rate for the Tesla container.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setTransferRate (long rate) {
this.setInputRate(rate);
this.setOutputRate(rate);
return this;
}
}
@@ -0,0 +1,14 @@
package net.darkhax.tesla.api;
public interface ITeslaConsumer {
/**
* Offers power to the Tesla Consumer.
*
* @param power The amount of power to offer.
* @param simulated Whether or not this is being called as part of a simulation.
* Simulations are used to get information without affecting the Tesla Producer.
* @return The amount of power that the consumer accepts.
*/
long givePower (long power, boolean simulated);
}
@@ -0,0 +1,18 @@
package net.darkhax.tesla.api;
public interface ITeslaHolder {
/**
* Gets the amount of Tesla power stored being stored.
*
* @return The amount of Tesla power being stored.
*/
long getStoredPower ();
/**
* Gets the maximum amount of Tesla power that can be held.
*
* @return The maximum amount of Tesla power that can be held.
*/
long getCapacity ();
}
@@ -0,0 +1,14 @@
package net.darkhax.tesla.api;
public interface ITeslaProducer {
/**
* Requests an amount of power from the Tesla Producer.
*
* @param power The amount of power to request.
* @param simulated Whether or not this is being called as part of a simulation.
* Simulations are used to get information without affecting the Tesla Producer.
* @return The amount of power that the Tesla Producer will give.
*/
long takePower (long power, boolean simulated);
}
@@ -0,0 +1,16 @@
package net.darkhax.tesla.api;
/**
* Logic for a Tesla Consumer that will consume infinite amounts of power.
*
* @deprecated Moved to net.darkhax.tesla.api.implementation
*/
@Deprecated
public class InfiniteTeslaConsumer implements ITeslaConsumer {
@Override
public long givePower (long power, boolean simulated) {
return power;
}
}
@@ -0,0 +1,16 @@
package net.darkhax.tesla.api;
/**
* Logic for a Tesla Producer that will produce an infinite amount of energy.
*
* @deprecated Moved to net.darkhax.tesla.api.implementation
*/
@Deprecated
public class InfiniteTeslaProducer implements ITeslaProducer {
@Override
public long takePower (long power, boolean simulated) {
return power;
}
}
@@ -0,0 +1,225 @@
package net.darkhax.tesla.api.implementation;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.INBTSerializable;
/**
* A basic Tesla container that serves as a consumer, producer and holder. Custom
* implementations do not need to use all three. The INBTSerializable interface is also
* optional.
*/
public class BaseTeslaContainer implements ITeslaConsumer, ITeslaProducer, ITeslaHolder, INBTSerializable<NBTTagCompound> {
/**
* The amount of stored Tesla power.
*/
private long stored;
/**
* The maximum amount of Tesla power that can be stored.
*/
private long capacity;
/**
* The maximum amount of Tesla power that can be accepted.
*/
private long inputRate;
/**
* The maximum amount of Tesla power that can be extracted
*/
private long outputRate;
/**
* Default constructor. Sets capacity to 5000 and transfer rate to 50. This constructor
* will not set the amount of stored power. These values are arbitrary and should not be
* taken as a base line for balancing.
*/
public BaseTeslaContainer() {
this(5000, 50, 50);
}
/**
* Constructor for setting the basic values. Will not construct with any stored power.
*
* @param capacity The maximum amount of Tesla power that the container should hold.
* @param input The maximum rate of power that can be accepted at a time.
* @param output The maximum rate of power that can be extracted at a time.
*/
public BaseTeslaContainer(long capacity, long input, long output) {
this(0, capacity, input, output);
}
/**
* Constructor for setting all of the base values, including the stored power.
*
* @param power The amount of stored power to initialize the container with.
* @param capacity The maximum amount of Tesla power that the container should hold.
* @param input The maximum rate of power that can be accepted at a time.
* @param output The maximum rate of power that can be extracted at a time.
*/
public BaseTeslaContainer(long power, long capacity, long input, long output) {
this.stored = power;
this.capacity = capacity;
this.inputRate = input;
this.outputRate = output;
}
/**
* Constructor for creating an instance directly from a compound tag. This expects that the
* compound tag has some of the required data. @See {@link #deserializeNBT(NBTTagCompound)}
* for precise info on what is expected. This constructor will only set the stored power if
* it has been written on the compound tag.
*
* @param dataTag The NBTCompoundTag to read the important data from.
*/
public BaseTeslaContainer(NBTTagCompound dataTag) {
this.deserializeNBT(dataTag);
}
@Override
public long getStoredPower () {
return this.stored;
}
@Override
public long givePower (long Tesla, boolean simulated) {
final long acceptedTesla = Math.min(this.capacity - this.stored, Math.min(this.inputRate, Tesla));
if (!simulated)
this.stored += acceptedTesla;
return acceptedTesla;
}
@Override
public long takePower (long Tesla, boolean simulated) {
final long removedPower = Math.min(this.stored, Math.min(this.outputRate, Tesla));
if (!simulated)
this.stored -= removedPower;
return removedPower;
}
@Override
public long getCapacity () {
return this.capacity;
}
@Override
public NBTTagCompound serializeNBT () {
final NBTTagCompound dataTag = new NBTTagCompound();
dataTag.setLong("TeslaPower", this.stored);
dataTag.setLong("TeslaCapacity", this.capacity);
dataTag.setLong("TeslaInput", this.inputRate);
dataTag.setLong("TeslaOutput", this.outputRate);
return dataTag;
}
@Override
public void deserializeNBT (NBTTagCompound nbt) {
this.stored = nbt.getLong("TeslaPower");
if (nbt.hasKey("TeslaCapacity"))
this.capacity = nbt.getLong("TeslaCapacity");
if (nbt.hasKey("TeslaInput"))
this.inputRate = nbt.getLong("TeslaInput");
if (nbt.hasKey("TeslaOutput"))
this.outputRate = nbt.getLong("TeslaOutput");
if (this.stored > this.capacity)
this.stored = this.capacity;
}
/**
* Sets the capacity of the the container. If the existing stored power is more than the
* new capacity, the stored power will be decreased to match the new capacity.
*
* @param capacity The new capacity for the container.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setCapacity (long capacity) {
this.capacity = capacity;
if (this.stored > capacity)
this.stored = capacity;
return this;
}
/**
* Gets the maximum amount of Tesla power that can be accepted by the container.
*
* @return The amount of Tesla power that can be accepted at any time.
*/
public long getInputRate () {
return this.inputRate;
}
/**
* Sets the maximum amount of Tesla power that can be accepted by the container.
*
* @param rate The amount of Tesla power to accept at a time.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setInputRate (long rate) {
this.inputRate = rate;
return this;
}
/**
* Gets the maximum amount of Tesla power that can be pulled from the container.
*
* @return The amount of Tesla power that can be extracted at any time.
*/
public long getOutputRate () {
return this.outputRate;
}
/**
* Sets the maximum amount of Tesla power that can be pulled from the container.
*
* @param rate The amount of Tesla power that can be extracted.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setOutputRate (long rate) {
this.outputRate = rate;
return this;
}
/**
* Sets both the input and output rates of the container at the same time. Both rates will
* be the same.
*
* @param rate The input/output rate for the Tesla container.
* @return The instance of the container being updated.
*/
public BaseTeslaContainer setTransferRate (long rate) {
this.setInputRate(rate);
this.setOutputRate(rate);
return this;
}
}
@@ -0,0 +1,57 @@
package net.darkhax.tesla.api.implementation;
import net.darkhax.tesla.capability.TeslaCapabilities;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
/**
* A capability provider for the base Tesla container implementation.
*/
public class BaseTeslaContainerProvider implements INBTSerializable<NBTTagCompound>, ICapabilityProvider {
/**
* The capability being provided.
*/
private final BaseTeslaContainer container;
/**
* Constructor for setting the BaseTeslaContainer for the provider to provide.
*
* @param container The BaseTeslaContainer to provide.
*/
public BaseTeslaContainerProvider(BaseTeslaContainer container) {
this.container = container;
}
@Override
public boolean hasCapability (Capability<?> capability, EnumFacing facing) {
return capability == TeslaCapabilities.CAPABILITY_CONSUMER || capability == TeslaCapabilities.CAPABILITY_PRODUCER || capability == TeslaCapabilities.CAPABILITY_HOLDER;
}
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability (Capability<T> capability, EnumFacing facing) {
if (capability == TeslaCapabilities.CAPABILITY_CONSUMER || capability == TeslaCapabilities.CAPABILITY_PRODUCER || capability == TeslaCapabilities.CAPABILITY_HOLDER)
return (T) this.container;
return null;
}
@Override
public NBTTagCompound serializeNBT () {
return this.container.serializeNBT();
}
@Override
public void deserializeNBT (NBTTagCompound nbt) {
this.container.deserializeNBT(nbt);
}
}
@@ -0,0 +1,15 @@
package net.darkhax.tesla.api.implementation;
import net.darkhax.tesla.api.ITeslaConsumer;
/**
* Logic for a Tesla Consumer that will consume infinite amounts of power.
*/
public class InfiniteTeslaConsumer implements ITeslaConsumer {
@Override
public long givePower (long power, boolean simulated) {
return power;
}
}
@@ -0,0 +1,25 @@
package net.darkhax.tesla.api.implementation;
import net.darkhax.tesla.capability.TeslaCapabilities;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
/**
* A capability provider for the infinite Tesla consumer capability.
*/
public class InfiniteTeslaConsumerProvider implements ICapabilityProvider {
@Override
public boolean hasCapability (Capability<?> capability, EnumFacing facing) {
return capability == TeslaCapabilities.CAPABILITY_CONSUMER;
}
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability (Capability<T> capability, EnumFacing facing) {
return capability == TeslaCapabilities.CAPABILITY_CONSUMER ? (T) new InfiniteTeslaConsumer() : null;
}
}
@@ -0,0 +1,15 @@
package net.darkhax.tesla.api.implementation;
import net.darkhax.tesla.api.ITeslaProducer;
/**
* Logic for a Tesla Producer that will produce an infinite amount of energy.
*/
public class InfiniteTeslaProducer implements ITeslaProducer {
@Override
public long takePower (long power, boolean simulated) {
return power;
}
}
@@ -0,0 +1,25 @@
package net.darkhax.tesla.api.implementation;
import net.darkhax.tesla.capability.TeslaCapabilities;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
/**
* A capability provider for the infinite Tesla producer capability.
*/
public class InfiniteTeslaProducerProvider implements ICapabilityProvider {
@Override
public boolean hasCapability (Capability<?> capability, EnumFacing facing) {
return capability == TeslaCapabilities.CAPABILITY_PRODUCER;
}
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability (Capability<T> capability, EnumFacing facing) {
return capability == TeslaCapabilities.CAPABILITY_PRODUCER ? (T) new InfiniteTeslaConsumer() : null;
}
}
@@ -0,0 +1,64 @@
package net.darkhax.tesla.capability;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;
import net.minecraftforge.common.capabilities.CapabilityInject;
public class TeslaCapabilities {
@CapabilityInject(ITeslaConsumer.class)
public static Capability<ITeslaConsumer> CAPABILITY_CONSUMER = null;
@CapabilityInject(ITeslaProducer.class)
public static Capability<ITeslaProducer> CAPABILITY_PRODUCER = null;
@CapabilityInject(ITeslaHolder.class)
public static Capability<ITeslaHolder> CAPABILITY_HOLDER = null;
public static class CapabilityTeslaConsumer<T extends ITeslaConsumer> implements IStorage<ITeslaConsumer> {
@Override
public NBTBase writeNBT (Capability<ITeslaConsumer> capability, ITeslaConsumer instance, EnumFacing side) {
return null;
}
@Override
public void readNBT (Capability<ITeslaConsumer> capability, ITeslaConsumer instance, EnumFacing side, NBTBase nbt) {
}
}
public static class CapabilityTeslaProducer<T extends ITeslaProducer> implements IStorage<ITeslaProducer> {
@Override
public NBTBase writeNBT (Capability<ITeslaProducer> capability, ITeslaProducer instance, EnumFacing side) {
return null;
}
@Override
public void readNBT (Capability<ITeslaProducer> capability, ITeslaProducer instance, EnumFacing side, NBTBase nbt) {
}
}
public static class CapabilityTeslaHolder<T extends ITeslaHolder> implements IStorage<ITeslaHolder> {
@Override
public NBTBase writeNBT (Capability<ITeslaHolder> capability, ITeslaHolder instance, EnumFacing side) {
return null;
}
@Override
public void readNBT (Capability<ITeslaHolder> capability, ITeslaHolder instance, EnumFacing side, NBTBase nbt) {
}
}
}
@@ -0,0 +1,8 @@
package net.darkhax.tesla.lib;
public class Constants {
public static final String MOD_ID = "Tesla";
public static final String MOD_NAME = "TESLA";
public static final String VERSION_NUMBER = "1.1.0.0";
}
@@ -0,0 +1,166 @@
package net.darkhax.tesla.lib;
import java.util.ArrayList;
import java.util.List;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaProducer;
import net.darkhax.tesla.capability.TeslaCapabilities;
import net.minecraft.client.resources.I18n;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
public class TeslaUtils {
/**
* The smallest unit of power measurement.
*/
public static final int TESLA = 1;
/**
* The amount of Tesla in a KiloTesla. One thousand.
*/
public static final int KILOTESLA = 1000;
/**
* The amount of Telsa in a MegaTesla. One Million.
*/
public static final int MEGATESLA = 1000000;
/**
* The amount of Tesla in a GigaTesla. One Billion.
*/
public static final int GIGATESLA = 1000000000;
/**
* The amount of Telsa in a TeraTesla. One Trillion.
*/
public static final long TERATESLA = 1000000000000L;
/**
* The amount of Tesla in a PentaTesla. One Quadrilli.on
*/
public static final long PENTATESLA = 1000000000000000L;
/**
* The amount of Tesla in a ExaTesla. One Quintilian.
*
* The ExaTesla should not be treated as the largest unit of Tesla. The naming scheme can
* go on indefinitely. The next unit would be a ZettaTesla, followed by YottaTesla,
* BronoTesla, GeopTesla and so on. While it is possible to define these measurements,
* there really isn't a point.
*/
public static final long EXATESLA = 1000000000000000000L;
/**
* Converts an amount of Tesla units into a human readable String. The string amount is
* only rounded to one decimal place.
*
* @param tesla The amount of Tesla units to display.
* @return A human readable display of the Tesla units.
*/
public static String getDisplayableTeslaCount (long tesla) {
if (tesla < 1000)
return tesla + " T";
final int exp = (int) (Math.log(tesla) / Math.log(1000));
final char unitType = "KMGTPE".charAt(exp - 1);
return String.format("%.1f %sT", tesla / Math.pow(1000, exp), unitType);
}
/**
* Gets the abbreviated name of the best unit to describe the provided power. For example,
* anything less than 1000 will return t for tesla, while anything between 999 and one
* million will return kt for kilo tesla. This method has support for up to Exatesla.
*
* @param tesla The amount of Tesla to get the unit for.
* @return The abbreviated name for the unit used to describe the provided power amount.
*/
public static String getUnitType (long tesla) {
if (tesla < 1000)
return tesla + "t";
final int exp = (int) (Math.log(tesla) / Math.log(1000));
return "kmgtpe".charAt(exp - 1) + "t";
}
/**
* Gets the name of the the power unit that best represents the amount of provided power.
* The name will be translated to the local language, or english if that language is not
* yet supported.
*
* @param tesla The amount of Tesla to get the unit name for.
* @return The name of the power unit that best represents the amount of power provided.
*/
public static String getLocalizedUnitType (long tesla) {
return I18n.format("unit.tesla." + getUnitType(tesla));
}
/**
* Gets a list of all capabilities that touch a BlockPos. This will search for tile
* entities touching the BlockPos and then query them for access to their capabilities.
*
* @param capability The capability you want to retrieve.
* @param world The world that this is happening in.
* @param pos The position to search around.
* @return A list of all capabilities that are being held by connected blocks.
*/
public static <T> List<T> getConnectedCapabilities (Capability<T> capability, World world, BlockPos pos) {
final List<T> capabilities = new ArrayList<T>();
for (final EnumFacing side : EnumFacing.values()) {
final TileEntity tile = world.getTileEntity(pos.offset(side));
if (tile != null && !tile.isInvalid() && tile.hasCapability(capability, side.getOpposite()))
capabilities.add(tile.getCapability(capability, side.getOpposite()));
}
return capabilities;
}
/**
* Attempts to give power to all consumers touching the given BlockPos.
*
* @param world The world that this is happening in.
* @param pos The position to search around.
* @param amount The amount of power to offer to each individual face.
* @param simulated Whether or not this is being ran as part of a simulation.
* @return The amount of power that was consumed.
*/
public static long distributePowerToAllFaces (World world, BlockPos pos, long amount, boolean simulated) {
long consumedPower = 0L;
for (final ITeslaConsumer consumer : getConnectedCapabilities(TeslaCapabilities.CAPABILITY_CONSUMER, world, pos))
consumedPower += consumer.givePower(amount, simulated);
return consumedPower;
}
/**
* Attempts to consume power from all producers touching the given BlockPos.
*
* @param world The world that this is happening in.
* @param pos The position to search around.
* @param amount The amount of power to request from each individual face.
* @param simulated Whether or not this is being ran as part of a simulation.
* @return The amount of power that was successfully consumed.
*/
public static long consumePowerFromAllFaces (World world, BlockPos pos, long amount, boolean simulated) {
long recievedPower = 0L;
for (final ITeslaProducer producer : getConnectedCapabilities(TeslaCapabilities.CAPABILITY_PRODUCER, world, pos))
recievedPower += producer.takePower(amount, simulated);
return recievedPower;
}
}