TechReborn/src/main/java/techreborn/tiles/idsu/TileIDSU.java

195 lines
4.1 KiB
Java
Raw Normal View History

package techreborn.tiles.idsu;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-11-23 15:45:16 +01:00
import net.minecraft.util.EnumFacing;
import org.apache.commons.lang3.StringUtils;
import reborncore.api.power.EnumPowerTier;
2016-10-08 21:46:16 +02:00
import reborncore.common.IWrenchable;
import reborncore.common.powerSystem.TilePowerAcceptor;
2015-06-21 15:17:47 +02:00
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModBlocks;
2016-10-08 21:46:16 +02:00
public class TileIDSU extends TilePowerAcceptor implements IWrenchable {
2016-03-25 10:47:34 +01:00
public String ownerUdid;
public int tier;
public int output;
public double maxStorage;
private double euLastTick = 0;
private double euChange;
private int ticks;
2016-10-08 21:46:16 +02:00
public TileIDSU(int tier1, int output1, int maxStorage1) {
super(tier1);
2016-03-25 10:47:34 +01:00
this.tier = tier1;
this.output = output1;
this.maxStorage = maxStorage1;
}
2016-10-08 21:46:16 +02:00
public TileIDSU() {
2016-03-25 10:47:34 +01:00
this(5, 2048, 100000000);
}
@Override
2016-10-08 21:46:16 +02:00
public double getEnergy() {
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid)) {
2016-03-25 10:47:34 +01:00
return 0.0;
}
2016-11-19 13:50:08 +01:00
return IDSUManager.INSTANCE.getSaveDataForWorld(world, ownerUdid).storedPower;
2016-03-25 10:47:34 +01:00
}
@Override
2016-10-08 21:46:16 +02:00
public void setEnergy(double energy) {
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid)) {
2016-03-25 10:47:34 +01:00
return;
}
2016-11-19 13:50:08 +01:00
IDSUManager.INSTANCE.getSaveDataForWorld(world, ownerUdid).storedPower = energy;
2016-03-25 10:47:34 +01:00
}
@Override
2016-10-08 21:46:16 +02:00
public void readFromNBTWithoutCoords(NBTTagCompound tag) {
}
@Override
2016-10-08 21:46:16 +02:00
public double getMaxPower() {
2016-03-25 10:47:34 +01:00
return 1000000000;
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canAcceptEnergy(EnumFacing direction) {
return getFacingEnum() != direction;
}
@Override
2016-10-08 21:46:16 +02:00
public boolean canProvideEnergy(EnumFacing direction) {
return getFacingEnum() == direction;
}
@Override
2016-10-08 21:46:16 +02:00
public double getMaxOutput() {
return output;
}
@Override
2016-10-08 21:46:16 +02:00
public double getMaxInput() {
return maxStorage;
}
@Override
2016-10-08 21:46:16 +02:00
public EnumPowerTier getTier() {
return EnumPowerTier.EXTREME;
2016-03-25 10:47:34 +01:00
}
2016-10-08 21:46:16 +02:00
public float getChargeLevel() {
2016-03-25 10:47:34 +01:00
float ret = (float) this.getEnergy() / (float) this.maxStorage;
2016-10-08 21:46:16 +02:00
if (ret > 1.0F) {
2016-03-25 10:47:34 +01:00
ret = 1.0F;
}
return ret;
}
2016-10-08 21:46:16 +02:00
public void readFromNBT(NBTTagCompound nbttagcompound) {
2016-03-25 10:47:34 +01:00
super.readFromNBT(nbttagcompound);
this.ownerUdid = nbttagcompound.getString("ownerUdid");
}
2016-10-08 21:46:16 +02:00
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
2016-03-25 10:47:34 +01:00
super.writeToNBT(nbttagcompound);
2016-10-08 21:46:16 +02:00
if (ownerUdid == null && StringUtils.isBlank(ownerUdid) || StringUtils.isEmpty(ownerUdid)) {
2016-05-18 17:53:54 +02:00
return nbttagcompound;
2016-03-25 10:47:34 +01:00
}
nbttagcompound.setString("ownerUdid", this.ownerUdid);
2016-05-18 17:53:54 +02:00
return nbttagcompound;
2016-03-25 10:47:34 +01:00
}
2016-10-08 21:46:16 +02:00
public void updateEntity() {
super.updateEntity();
2016-03-25 10:47:34 +01:00
2016-10-08 21:46:16 +02:00
if (ticks == ConfigTechReborn.AverageEuOutTickTime) {
2016-03-25 10:47:34 +01:00
euChange = -1;
ticks = 0;
2016-10-08 21:46:16 +02:00
} else {
2016-03-25 10:47:34 +01:00
ticks++;
euChange += getEnergy() - euLastTick;
2016-10-08 21:46:16 +02:00
if (euLastTick == getEnergy()) {
2016-03-25 10:47:34 +01:00
euChange = 0;
}
}
euLastTick = getEnergy();
boolean needsInvUpdate = false;
2016-10-08 21:46:16 +02:00
if (needsInvUpdate) {
this.markDirty();
2016-03-25 10:47:34 +01:00
}
2016-03-25 10:47:34 +01:00
}
2016-10-08 21:46:16 +02:00
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
2016-03-25 10:47:34 +01:00
return false;
}
2016-10-08 21:46:16 +02:00
@Override
public boolean wrenchCanSetFacing(EntityPlayer p0, EnumFacing p1) {
return true;
}
2016-10-08 21:46:16 +02:00
@Override
public EnumFacing getFacing() {
return getFacingEnum();
}
2016-10-08 21:46:16 +02:00
@Override
public boolean wrenchCanRemove(EntityPlayer p0) {
return true;
}
2016-10-08 21:46:16 +02:00
@Override
public float getWrenchDropRate() {
return 1.0F;
}
2016-10-08 21:46:16 +02:00
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
2016-03-25 10:47:34 +01:00
NBTTagCompound tileEntity = new NBTTagCompound();
ItemStack dropStack = new ItemStack(ModBlocks.INTERDIMENSIONAL_SU, 1);
2016-03-25 10:47:34 +01:00
writeToNBT(tileEntity);
dropStack.setTagCompound(new NBTTagCompound());
dropStack.getTagCompound().setTag("tileEntity", tileEntity);
return dropStack;
}
2016-10-08 21:46:16 +02:00
public double getEuChange() {
if (euChange == -1) {
2016-03-25 10:47:34 +01:00
return -1;
}
return (euChange / ticks);
}
2016-10-08 21:46:16 +02:00
public void handleGuiInputFromClient(int id) {
if (id == 0) {
2016-03-25 10:47:34 +01:00
output += 256;
}
2016-10-08 21:46:16 +02:00
if (id == 1) {
2016-03-25 10:47:34 +01:00
output += 64;
}
2016-10-08 21:46:16 +02:00
if (id == 2) {
2016-03-25 10:47:34 +01:00
output -= 64;
}
2016-10-08 21:46:16 +02:00
if (id == 3) {
2016-03-25 10:47:34 +01:00
output -= 256;
}
2016-10-08 21:46:16 +02:00
if (output > 4096) {
2016-03-25 10:47:34 +01:00
output = 4096;
}
2016-10-08 21:46:16 +02:00
if (output <= -1) {
2016-03-25 10:47:34 +01:00
output = 0;
}
}
}