Update Tesla API, render fixes

This commit is contained in:
aidancbrady committed 2016-08-04 15:19:36 -04:00
1 parent fa871c55f0
commit 309ed88a1e
20 files changed
+287 -288

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ 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)
@Mod(modid = Constants.MOD_ID, name = Constants.MOD_NAME, version = Constants.VERSION_NUMBER, acceptedMinecraftVersions = "[1.9.4,1.10.2]")
public class Tesla {
@Mod.Instance(Constants.MOD_ID)
@@ -1,225 +0,0 @@
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;
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ public interface ITeslaConsumer {
*
* @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.
* 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);
View File
File mode changed.
+1 -1
View File
@@ -7,7 +7,7 @@ public interface ITeslaProducer {
*
* @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.
* 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);
@@ -1,16 +0,0 @@
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;
}
}
@@ -1,16 +0,0 @@
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;
}
}
+4 -4
View File
@@ -93,7 +93,7 @@ public class BaseTeslaContainer implements ITeslaConsumer, ITeslaProducer, ITesl
@Override
public long givePower (long Tesla, boolean simulated) {
final long acceptedTesla = Math.min(this.capacity - this.stored, Math.min(this.inputRate, Tesla));
final long acceptedTesla = Math.min(this.getCapacity() - this.stored, Math.min(this.getInputRate(), Tesla));
if (!simulated)
this.stored += acceptedTesla;
@@ -104,7 +104,7 @@ public class BaseTeslaContainer implements ITeslaConsumer, ITeslaProducer, ITesl
@Override
public long takePower (long Tesla, boolean simulated) {
final long removedPower = Math.min(this.stored, Math.min(this.outputRate, Tesla));
final long removedPower = Math.min(this.stored, Math.min(this.getOutputRate(), Tesla));
if (!simulated)
this.stored -= removedPower;
@@ -144,8 +144,8 @@ public class BaseTeslaContainer implements ITeslaConsumer, ITeslaProducer, ITesl
if (nbt.hasKey("TeslaOutput"))
this.outputRate = nbt.getLong("TeslaOutput");
if (this.stored > this.capacity)
this.stored = this.capacity;
if (this.stored > this.getCapacity())
this.stored = this.getCapacity();
}
/**
File mode changed.
File mode changed.
File mode changed.
File mode changed.
File mode changed.
+9
View File
@@ -11,12 +11,21 @@ import net.minecraftforge.common.capabilities.CapabilityInject;
public class TeslaCapabilities {
/**
* Access to the consumer capability. Can be used for making checks.
*/
@CapabilityInject(ITeslaConsumer.class)
public static Capability<ITeslaConsumer> CAPABILITY_CONSUMER = null;
/**
* Access to the producer capability. Can be used for making checks.
*/
@CapabilityInject(ITeslaProducer.class)
public static Capability<ITeslaProducer> CAPABILITY_PRODUCER = null;
/**
* Access to the holder capability. Can be used for making checks.
*/
@CapabilityInject(ITeslaHolder.class)
public static Capability<ITeslaHolder> CAPABILITY_HOLDER = null;
+2 -2
View File
@@ -2,7 +2,7 @@ package net.darkhax.tesla.lib;
public class Constants {
public static final String MOD_ID = "Tesla";
public static final String MOD_ID = "tesla";
public static final String MOD_NAME = "TESLA";
public static final String VERSION_NUMBER = "1.1.0.0";
public static final String VERSION_NUMBER = "1.2.1.0";
}
+103
View File
@@ -0,0 +1,103 @@
package net.darkhax.tesla.lib;
import net.darkhax.tesla.api.ITeslaHolder;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class PowerBar {
/**
* Constant reference to the included gui element sheet.
*/
private static final ResourceLocation TEXTURE_SHEET = new ResourceLocation("tesla", "textures/gui/elements.png");
/**
* The x position of the power bar on the screen.
*/
private final int x;
/**
* The y position of the power bar on the screen.
*/
private final int y;
/**
* The screen to draw the power bar on.
*/
private final GuiScreen screen;
/**
* The type of background the use when drawing the power bar.
*/
private final BackgroundType background;
/**
* Object that can be used to draw a power bar on the screen.
*
* @param screen The screen to draw the power bar on.
* @param x The x position of the power bar on the screen.
* @param y The y position of the power bar on the screen.
* @param type The background type to use for the power bar.
*/
public PowerBar(GuiScreen screen, int x, int y, BackgroundType type) {
this.background = type;
this.screen = screen;
this.x = x;
this.y = y;
}
/**
* Draws a power bar that represents the power held within an ITeslaHolder.
*
* @param holder The holder to represent.
*/
public void draw (ITeslaHolder holder) {
draw(holder.getStoredPower(), holder.getCapacity());
}
/**
* Draws a power bard that represents the based power information.
*
* @param power The amount of power to represent.
* @param capacity The capacity to represent.
*/
public void draw (long power, long capacity) {
screen.mc.getTextureManager().bindTexture(TEXTURE_SHEET);
if (this.background == BackgroundType.LIGHT)
screen.drawTexturedModalRect(x, y, 3, 1, 14, 50);
else if (this.background == BackgroundType.DARK)
screen.drawTexturedModalRect(x, y, 3, 53, 14, 50);
long powerOffset = (power * 51) / capacity;
screen.drawTexturedModalRect(x + 1, (int) (y + 50 - powerOffset), 18, (int) (51 - powerOffset), 14, (int) (powerOffset + 2));
}
/**
* Describes a background image to be rendered for a power bar.
*/
public enum BackgroundType {
/**
* No background texture.
*/
NONE,
/**
* The lighter background texture.
*/
LIGHT,
/**
* The darker background texture.
*/
DARK,
}
}
+136
View File
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.darkhax.tesla.capability.TeslaCapabilities;
import net.minecraft.client.resources.I18n;
@@ -12,6 +13,7 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
public class TeslaUtils {
@@ -102,6 +104,140 @@ public class TeslaUtils {
return I18n.format("unit.tesla." + getUnitType(tesla));
}
/**
* Checks if a capability provider has support for Tesla.
*
* @param provider The provider to check. ItemStack/TileEntity/Entity and so on.
* @param side The side to check.
* @return Whether or not the provider has support for Tesla.
*/
public static boolean hasTeslaSupport (ICapabilityProvider provider, EnumFacing side) {
return isTeslaHolder(provider, side) || isTeslaConsumer(provider, side) || isTeslaProducer(provider, side);
}
/**
* Checks if a capability provider is a Tesla holder.
*
* @param provider The provider to check. ItemStack/TileEntity/Entity and so on.
* @param side The side to check.
* @return Whether or not the provider is a Tesla holder.
*/
public static boolean isTeslaHolder (ICapabilityProvider provider, EnumFacing side) {
return provider.hasCapability(TeslaCapabilities.CAPABILITY_HOLDER, side);
}
/**
* Checks if a capability provider is a Tesla consumer.
*
* @param provider The provider to check. ItemStack/TileEntity/Entity and so on.
* @param side The side to check.
* @return Whether or not the provider is a Tesla consumer.
*/
public static boolean isTeslaConsumer (ICapabilityProvider provider, EnumFacing side) {
return provider.hasCapability(TeslaCapabilities.CAPABILITY_CONSUMER, side);
}
/**
* Checks if a capability provider is a Tesla producer.
*
* @param provider The provider to check. ItemStack/TileEntity/Entity and so on.
* @param side The side to check.
* @return Whether or not the provider is a Tesla producer.
*/
public static boolean isTeslaProducer (ICapabilityProvider provider, EnumFacing side) {
return provider.hasCapability(TeslaCapabilities.CAPABILITY_PRODUCER, side);
}
/**
* Gets an ITeslaHolder implementation from the capability provider.
*
* @param provider The provider to get the capability from.
* @param side The side to access;
* @return A Tesla holder implementation, or null if none could be found.
*/
public static ITeslaHolder getTeslaHolder (ICapabilityProvider provider, EnumFacing side) {
return (ITeslaHolder) provider.getCapability(TeslaCapabilities.CAPABILITY_HOLDER, side);
}
/**
* Gets an ITeslaConsumer implementation from the capability provider.
*
* @param provider The provider to get the capability from.
* @param side The side to access;
* @return A Tesla consumer implementation, or null if none could be found.
*/
public static ITeslaConsumer getTeslaConsumer (ICapabilityProvider provider, EnumFacing side) {
return (ITeslaConsumer) provider.getCapability(TeslaCapabilities.CAPABILITY_HOLDER, side);
}
/**
* Gets an ITeslaProducer implementation from the capability provider.
*
* @param provider The provider to get the capability from.
* @param side The side to access;
* @return A Tesla producer implementation, or null if none could be found.
*/
public static ITeslaProducer getTeslaProducer (ICapabilityProvider provider, EnumFacing side) {
return (ITeslaProducer) provider.getCapability(TeslaCapabilities.CAPABILITY_HOLDER, side);
}
/**
* Gets the capacity of a provider.
*
* @param provider The provider to access.
* @param side The side to access.
* @return The capacity of the provider. 0 if it has no capacity.
*/
public static long getCapacity (ICapabilityProvider provider, EnumFacing side) {
return isTeslaHolder(provider, side) ? getTeslaHolder(provider, side).getCapacity() : 0;
}
/**
* Gets the stored power of a provider.
*
* @param provider The provider to access.
* @param side The side to access.
* @return The amount of power stored.
*/
public static long getStoredPower (ICapabilityProvider provider, EnumFacing side) {
return isTeslaHolder(provider, side) ? getTeslaHolder(provider, side).getStoredPower() : 0;
}
/**
* Attempt to give power to a provider.
*
* @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.
*/
public static long givePower (ICapabilityProvider provider, EnumFacing side, long power, boolean simulated) {
return isTeslaConsumer(provider, side) ? getTeslaConsumer(provider, side).givePower(power, simulated) : 0;
}
/**
* Attempts to take power from a provider.
*
* @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.
*/
public static long takePower (ICapabilityProvider provider, EnumFacing side, long power, boolean simulated) {
return isTeslaProducer(provider, side) ? getTeslaProducer(provider, side).takePower(power, simulated) : 0;
}
/**
* 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.