2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("ItemEnergy.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.items.base;
|
2015-07-07 20:20:24 +02:00
|
|
|
|
2016-11-26 19:00:02 +01:00
|
|
|
import cofh.api.energy.IEnergyContainerItem;
|
|
|
|
import cofh.api.energy.IEnergyStorage;
|
2015-07-07 20:20:24 +02:00
|
|
|
import cofh.api.energy.ItemEnergyContainer;
|
2016-01-08 20:51:03 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-10-29 12:23:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
|
2016-03-18 18:38:39 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
2016-07-25 02:07:16 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.ItemTeslaWrapper;
|
2015-07-07 20:20:24 +02:00
|
|
|
import net.minecraft.creativetab.CreativeTabs;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-11-26 19:00:02 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-11-19 21:11:17 +01:00
|
|
|
import net.minecraft.util.NonNullList;
|
|
|
|
import net.minecraft.util.math.MathHelper;
|
2016-11-26 19:00:02 +01:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2016-07-25 02:07:16 +02:00
|
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
2016-11-26 19:00:02 +01:00
|
|
|
import net.minecraftforge.energy.CapabilityEnergy;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-07-07 20:20:24 +02:00
|
|
|
|
2016-11-26 19:00:02 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
2016-10-29 12:23:01 +02:00
|
|
|
import java.text.NumberFormat;
|
2015-07-07 20:20:24 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
public abstract class ItemEnergy extends ItemEnergyContainer{
|
2015-07-07 20:20:24 +02:00
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
private final String name;
|
2015-12-03 20:15:07 +01:00
|
|
|
|
|
|
|
public ItemEnergy(int maxPower, int transfer, String name){
|
2015-07-07 20:20:24 +02:00
|
|
|
super(maxPower, transfer);
|
|
|
|
this.setHasSubtypes(true);
|
|
|
|
this.setMaxStackSize(1);
|
2015-12-03 20:15:07 +01:00
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
this.register();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void register(){
|
2016-03-18 18:38:39 +01:00
|
|
|
ItemUtil.registerItem(this, this.getBaseName(), this.shouldAddCreative());
|
2016-01-08 16:52:53 +01:00
|
|
|
|
2016-01-08 20:51:03 +01:00
|
|
|
this.registerRendering();
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:15:07 +01:00
|
|
|
protected String getBaseName(){
|
|
|
|
return this.name;
|
2015-07-07 20:20:24 +02:00
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
public boolean shouldAddCreative(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
protected void registerRendering(){
|
2016-06-01 00:37:28 +02:00
|
|
|
ActuallyAdditions.proxy.addRenderRegister(new ItemStack(this), this.getRegistryName(), "inventory");
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
2015-07-07 20:20:24 +02:00
|
|
|
@Override
|
2015-10-03 10:19:40 +02:00
|
|
|
public boolean getShareTag(){
|
2015-07-07 20:20:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-05 19:55:28 +02:00
|
|
|
@Override
|
|
|
|
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool){
|
2016-10-29 12:23:01 +02:00
|
|
|
NumberFormat format = NumberFormat.getInstance();
|
2016-11-26 15:09:15 +01:00
|
|
|
list.add(format.format(this.getEnergyStored(stack))+"/"+format.format(this.getMaxEnergyStored(stack))+" Crystal Flux");
|
2015-10-05 19:55:28 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public boolean hasEffect(ItemStack stack){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-07 20:20:24 +02:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2016-11-19 21:11:17 +01:00
|
|
|
public void getSubItems(Item item, CreativeTabs tabs, NonNullList list){
|
2015-07-07 20:20:24 +02:00
|
|
|
ItemStack stackFull = new ItemStack(this);
|
|
|
|
this.setEnergy(stackFull, this.getMaxEnergyStored(stackFull));
|
|
|
|
list.add(stackFull);
|
|
|
|
|
|
|
|
ItemStack stackEmpty = new ItemStack(this);
|
|
|
|
this.setEnergy(stackEmpty, 0);
|
|
|
|
list.add(stackEmpty);
|
|
|
|
}
|
|
|
|
|
2015-10-03 10:19:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean showDurabilityBar(ItemStack itemStack){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getDurabilityForDisplay(ItemStack stack){
|
2016-05-02 17:46:53 +02:00
|
|
|
double maxAmount = this.getMaxEnergyStored(stack);
|
2016-07-25 13:59:23 +02:00
|
|
|
double energyDif = maxAmount-this.getEnergyStored(stack);
|
2015-10-03 10:19:40 +02:00
|
|
|
return energyDif/maxAmount;
|
|
|
|
}
|
|
|
|
|
2016-11-19 21:11:17 +01:00
|
|
|
@Override
|
|
|
|
public int getRGBDurabilityForDisplay(ItemStack stack){
|
|
|
|
int currEnergy = this.getEnergyStored(stack);
|
|
|
|
int maxEnergy = this.getMaxEnergyStored(stack);
|
|
|
|
return MathHelper.hsvToRGB(Math.max(0.0F, (float)currEnergy/maxEnergy)/3.0F, 1.0F, 1.0F);
|
|
|
|
}
|
|
|
|
|
2015-07-07 20:20:24 +02:00
|
|
|
public void setEnergy(ItemStack stack, int energy){
|
|
|
|
NBTTagCompound compound = stack.getTagCompound();
|
2015-10-03 10:16:18 +02:00
|
|
|
if(compound == null){
|
|
|
|
compound = new NBTTagCompound();
|
|
|
|
}
|
2015-07-07 20:20:24 +02:00
|
|
|
compound.setInteger("Energy", energy);
|
|
|
|
stack.setTagCompound(compound);
|
|
|
|
}
|
2016-07-25 02:07:16 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt){
|
2016-11-26 19:00:02 +01:00
|
|
|
return new EnergyCapabilityProvider(stack, this);
|
2016-07-25 02:07:16 +02:00
|
|
|
}
|
2016-11-23 18:10:55 +01:00
|
|
|
|
|
|
|
public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate){
|
|
|
|
int before = this.maxExtract;
|
|
|
|
this.setMaxExtract(Integer.MAX_VALUE);
|
|
|
|
|
|
|
|
int toReturn = this.extractEnergy(stack, maxExtract, simulate);
|
|
|
|
|
|
|
|
this.setMaxExtract(before);
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int receiveEnergyInternal(ItemStack stack, int maxReceive, boolean simulate){
|
|
|
|
int before = this.maxReceive;
|
|
|
|
this.setMaxReceive(Integer.MAX_VALUE);
|
|
|
|
|
|
|
|
int toReturn = this.receiveEnergy(stack, maxReceive, simulate);
|
|
|
|
|
|
|
|
this.setMaxReceive(before);
|
|
|
|
return toReturn;
|
|
|
|
}
|
2016-11-26 19:00:02 +01:00
|
|
|
|
|
|
|
private static class EnergyCapabilityProvider implements ICapabilityProvider{
|
|
|
|
|
|
|
|
private final Object forgeUnitsWrapper;
|
|
|
|
private Object teslaWrapper;
|
|
|
|
|
|
|
|
private final IEnergyContainerItem item;
|
|
|
|
private final ItemStack stack;
|
|
|
|
|
|
|
|
public EnergyCapabilityProvider(final ItemStack stack, final IEnergyContainerItem item){
|
|
|
|
this.stack = stack;
|
|
|
|
this.item = item;
|
|
|
|
|
|
|
|
this.forgeUnitsWrapper = new IEnergyStorage(){
|
|
|
|
@Override
|
|
|
|
public int receiveEnergy(int maxReceive, boolean simulate){
|
|
|
|
return item.receiveEnergy(stack, maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int extractEnergy(int maxExtract, boolean simulate){
|
|
|
|
return item.extractEnergy(stack, maxExtract, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(){
|
|
|
|
return item.getEnergyStored(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(){
|
|
|
|
return item.getMaxEnergyStored(stack);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCapability(Capability<?> capability, EnumFacing facing){
|
|
|
|
return this.getCapability(capability, facing) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing){
|
|
|
|
if(capability == CapabilityEnergy.ENERGY){
|
|
|
|
return (T)this.forgeUnitsWrapper;
|
|
|
|
}
|
|
|
|
else if(ActuallyAdditions.teslaLoaded){
|
|
|
|
if(this.teslaWrapper == null){
|
|
|
|
this.teslaWrapper = new ItemTeslaWrapper(this.stack, this.item);
|
|
|
|
}
|
|
|
|
return (T)this.teslaWrapper;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 20:20:24 +02:00
|
|
|
}
|