TechReborn/src/main/java/techreborn/items/battery/ItemBattery.java
coderbot16 a766cdfa53 Remove almost all explicit uses of CapabilityEnergy (#1643)
* Battery packs (ItemLithiumBackpack/ItemLapotronPack) still use CapEnergy explicitly, but don't charge IC2 items.
 * Note: Before we can remove FE, we have to modify StackInfoHUD and ItemUtils in RebornCore.
2018-12-27 13:58:55 +00:00

113 lines
3.5 KiB
Java

/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.items.battery;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import reborncore.common.util.ItemUtils;
import techreborn.items.ItemTR;
import javax.annotation.Nullable;
public class ItemBattery extends ItemTR implements IEnergyItemInfo {
String name;
int maxEnergy = 0;
int maxTransfer = 0;
public ItemBattery(String name, int maxEnergy, int maxTransfer) {
super();
setMaxStackSize(1);
setMaxDamage(1);
setUnlocalizedName("techreborn." + name);
this.name = name;
this.maxEnergy = maxEnergy;
this.maxTransfer = maxTransfer;
this.addPropertyOverride(new ResourceLocation("techreborn:empty"), new IItemPropertyGetter() {
@Override
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
if (!stack.isEmpty() && new ForgePowerItemManager(stack).getEnergyStored() == 0) {
return 1.0F;
}
return 0.0F;
}
});
}
// Item
@Override
public double getDurabilityForDisplay(ItemStack stack) {
return 1 - ItemUtils.getPowerForDurabilityBar(stack);
}
@Override
public boolean showDurabilityBar(ItemStack stack) {
return true;
}
@Override
public int getRGBDurabilityForDisplay(ItemStack stack) {
return PowerSystem.getDisplayPower().colour;
}
@Override
@Nullable
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
return new PoweredItemContainerProvider(stack);
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxEnergy;
}
@Override
public boolean canAcceptEnergy(ItemStack stack) {
return true;
}
@Override
public boolean canProvideEnergy(ItemStack stack) {
return true;
}
@Override
public double getMaxTransfer(ItemStack stack) {
return maxTransfer;
}
}