Started work on capability for powered item

This commit is contained in:
drcrazy 2018-05-23 00:27:28 +03:00
parent c4c5f2135c
commit f203bbbefd
19 changed files with 603 additions and 1424 deletions

View file

@ -33,7 +33,6 @@ import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.tuple.Pair;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.recipe.IRecipeCrafterProvider;
import reborncore.api.tile.IUpgrade;
import reborncore.api.tile.IUpgradeable;
@ -91,7 +90,7 @@ public class ContainerTileInventoryBuilder {
public ContainerTileInventoryBuilder energySlot(final int index, final int x, final int y) {
this.parent.slots.add(new FilteredSlot(this.tile, index, x, y)
.setFilter(stack -> stack.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.UP)
|| stack.getItem() instanceof IEnergyInterfaceItem || (CompatManager.isIC2Loaded && IC2ItemCharger.isIC2PoweredItem(stack))));
|| (CompatManager.isIC2Loaded && IC2ItemCharger.isIC2PoweredItem(stack))));
return this;
}

View file

@ -28,8 +28,6 @@ import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraftforge.common.MinecraftForge;
import reborncore.RebornRegistry;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.util.BucketHandler;
import reborncore.common.util.OreUtil;
@ -350,12 +348,6 @@ public class ModItems {
public static void registerItem(Item item, String name) {
item.setRegistryName(name);
RebornRegistry.registerItem(item);
if (item.getClass().isInstance(IEnergyItemInfo.class)) {
if (!item.getClass().isInstance(IEnergyInterfaceItem.class)) {
Core.logHelper.error(name + " was not patched with the power mixin. This is a error, the item may not work as intended.");
Core.logHelper.error("Please check that the reborn core loading plugin has been registerd.");
}
}
}
}

View file

@ -28,30 +28,30 @@ import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.client.TechRebornCreativeTab;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModItems;
import javax.annotation.Nullable;
public class ItemLapotronPack extends ItemArmor implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemLapotronPack extends ItemArmor implements IEnergyItemInfo {
public static final int maxCharge = ConfigTechReborn.LapotronPackCharge;
public double transferLimit = 100000;
public double transferLimit = 100_000;
public ItemLapotronPack() {
super(ItemArmor.ArmorMaterial.DIAMOND, 7, EntityEquipmentSlot.CHEST);
@ -60,12 +60,56 @@ public class ItemLapotronPack extends ItemArmor implements IEnergyItemInfo, IEne
setMaxStackSize(1);
}
// Item
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.LAPOTRONIC_ORB_PACK);
ItemStack charged = new ItemStack(ModItems.LAPOTRONIC_ORB_PACK);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);
}
@Override
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {
ItemLithiumBatpack.distributePowerToInventory(world, player, itemStack, (int) transferLimit);
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@Override
@SideOnly(Side.CLIENT)
public String getArmorTexture(ItemStack stack, Entity entity, EntityEquipmentSlot slot, String type) {
return "techreborn:" + "textures/models/lapotronpack.png";
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -85,127 +129,4 @@ public class ItemLapotronPack extends ItemArmor implements IEnergyItemInfo, IEne
public double getMaxTransfer(ItemStack stack) {
return transferLimit;
}
@SideOnly(Side.CLIENT)
public void getSubItems(Item item,
CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.LAPOTRONIC_ORB_PACK);
ItemStack charged = new ItemStack(ModItems.LAPOTRONIC_ORB_PACK);
PoweredItem.setEnergy(getMaxPower(charged), charged);
itemList.add(uncharged);
itemList.add(charged);
}
@Override
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {
ItemLithiumBatpack.distributePowerToInventory(world, player, itemStack, (int) transferLimit);
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
}

View file

@ -31,18 +31,18 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import techreborn.items.ItemTR;
import javax.annotation.Nullable;
public class ItemBattery extends ItemTR implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemBattery extends ItemTR implements IEnergyItemInfo {
String name;
int maxEnergy = 0;
@ -60,7 +60,7 @@ public class ItemBattery extends ItemTR implements IEnergyItemInfo, IEnergyInter
@Override
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
if (!stack.isEmpty() && PoweredItem.getEnergy(stack) == 0.0) {
if (!stack.isEmpty() && stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() == 0) {
return 1.0F;
}
return 0.0F;
@ -68,6 +68,31 @@ public class ItemBattery extends ItemTR implements IEnergyItemInfo, IEnergyInter
});
}
// Item
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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;
@ -88,106 +113,6 @@ public class ItemBattery extends ItemTR implements IEnergyItemInfo, IEnergyInter
return maxTransfer;
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
}

View file

@ -27,9 +27,10 @@ package techreborn.items.battery;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.init.ModItems;
public class ItemEnergyCrystal extends ItemBattery {
@ -47,7 +48,8 @@ public class ItemEnergyCrystal extends ItemBattery {
ItemStack stack = new ItemStack(ModItems.ENERGY_CRYSTAL);
ItemStack uncharged = stack.copy();
ItemStack charged = stack.copy();
PoweredItem.setEnergy(getMaxPower(charged), charged);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);

View file

@ -27,9 +27,10 @@ package techreborn.items.battery;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.init.ModItems;
public class ItemLapotronCrystal extends ItemBattery {
@ -48,7 +49,8 @@ public class ItemLapotronCrystal extends ItemBattery {
ItemStack stack = new ItemStack(ModItems.LAPOTRONIC_CRYSTAL);
ItemStack uncharged = stack.copy();
ItemStack charged = stack.copy();
PoweredItem.setEnergy(getMaxPower(charged), charged);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);

View file

@ -27,9 +27,10 @@ package techreborn.items.battery;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModItems;
@ -49,7 +50,8 @@ public class ItemLapotronicOrb extends ItemBattery {
ItemStack stack = new ItemStack(ModItems.LAPOTRONIC_ORB);
ItemStack uncharged = stack.copy();
ItemStack charged = stack.copy();
PoweredItem.setEnergy(getMaxPower(charged), charged);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);

View file

@ -27,9 +27,10 @@ package techreborn.items.battery;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.init.ModItems;
public class ItemLithiumBattery extends ItemBattery {
@ -48,7 +49,8 @@ public class ItemLithiumBattery extends ItemBattery {
ItemStack stack = new ItemStack(ModItems.LITHIUM_BATTERY);
ItemStack uncharged = stack.copy();
ItemStack charged = stack.copy();
PoweredItem.setEnergy(getMaxPower(charged), charged);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);

View file

@ -27,9 +27,10 @@ package techreborn.items.battery;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.init.ModItems;
public class ItemReBattery extends ItemBattery {
@ -48,7 +49,8 @@ public class ItemReBattery extends ItemBattery {
ItemStack stack = new ItemStack(ModItems.RE_BATTERY);
ItemStack uncharged = stack.copy();
ItemStack charged = stack.copy();
PoweredItem.setEnergy(getMaxPower(charged), charged);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);

View file

@ -33,9 +33,11 @@ import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModItems;
@ -56,7 +58,8 @@ public class ItemAdvancedChainsaw extends ItemChainsaw {
ItemStack stack = new ItemStack(ModItems.ADVANCED_CHAINSAW);
ItemStack uncharged = stack.copy();
ItemStack charged = stack.copy();
PoweredItem.setEnergy(getMaxPower(charged), charged);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);
@ -83,7 +86,8 @@ public class ItemAdvancedChainsaw extends ItemChainsaw {
if (oldPos == pos) {
return;
}
if (!PoweredItem.canUseEnergy(cost, stack)) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
if (capEnergy.getEnergyStored() < cost) {
return;
}
IBlockState blockState = world.getBlockState(pos);
@ -93,10 +97,7 @@ public class ItemAdvancedChainsaw extends ItemChainsaw {
if(!(entityLiving instanceof EntityPlayer)){
return;
}
if(!PoweredItem.canUseEnergy(cost, stack)){
return;
}
PoweredItem.useEnergy(cost, stack);
capEnergy.extractEnergy(cost, false);
blockState.getBlock().harvestBlock(world, (EntityPlayer) entityLiving, pos, blockState, world.getTileEntity(pos), stack);
world.setBlockToAir(pos);
world.removeTileEntity(pos);

View file

@ -38,19 +38,19 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import techreborn.client.TechRebornCreativeTab;
import javax.annotation.Nullable;
import java.util.Random;
public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo {
public int maxCharge = 1;
public int cost = 250;
@ -59,8 +59,7 @@ public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo, IEnergyInt
public double transferLimit = 100;
public boolean isBreaking = false;
public ItemChainsaw(ToolMaterial material, String unlocalizedName, int energyCapacity,
float unpoweredSpeed) {
public ItemChainsaw(ToolMaterial material, String unlocalizedName, int energyCapacity, float unpoweredSpeed) {
super(material);
setCreativeTab(TechRebornCreativeTab.instance);
setMaxStackSize(1);
@ -71,12 +70,9 @@ public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo, IEnergyInt
this.addPropertyOverride(new ResourceLocation("techreborn:animated"), new IItemPropertyGetter() {
@Override
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack,
@Nullable
World worldIn,
@Nullable
EntityLivingBase entityIn) {
if (!stack.isEmpty() && PoweredItem.canUseEnergy(cost, stack) && entityIn != null && entityIn.getHeldItemMainhand().equals(stack)) {
public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
if (!stack.isEmpty() && stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() >= cost
&& entityIn != null && entityIn.getHeldItemMainhand().equals(stack)) {
return 1.0F;
}
return 0.0F;
@ -84,22 +80,23 @@ public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo, IEnergyInt
});
}
// ItemAxe
@Override
public float getDestroySpeed(ItemStack stack, IBlockState state) {
if (PoweredItem.canUseEnergy(cost, stack) && (state.getBlock().isToolEffective("axe", state) || state.getMaterial() == Material.WOOD)) {
if (stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() >= cost
&& (state.getBlock().isToolEffective("axe", state) || state.getMaterial() == Material.WOOD)) {
return this.poweredSpeed;
}
else {
} else {
return super.getDestroySpeed(stack, state);
}
}
// ItemTool
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos,
EntityLivingBase entityLiving) {
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
PoweredItem.useEnergy(cost, stack);
stack.getCapability(CapabilityEnergy.ENERGY, null).extractEnergy(cost, false);
}
return true;
}
@ -109,11 +106,46 @@ public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo, IEnergyInt
return true;
}
// Item
@Override
public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, EntityPlayer player) {
return super.onBlockStartBreak(itemstack, pos, player);
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
@Override
public boolean isRepairable() {
return false;
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -129,129 +161,9 @@ public class ItemChainsaw extends ItemAxe implements IEnergyItemInfo, IEnergyInt
return false;
}
@Override
public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, EntityPlayer player) {
return super.onBlockStartBreak(itemstack, pos, player);
}
@Override
public double getMaxTransfer(ItemStack stack) {
return transferLimit;
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
/**
* Called when the player is mining a block and the item in his hand changes.
* Allows to not reset blockbreaking if only NBT or similar changes.
* @param oldStack The old stack that was used for mining. Item in players main hand
* @param newStack The new stack
* @return True to reset block break progress
*/
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
}

View file

@ -34,13 +34,14 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import reborncore.common.registration.RebornRegistry;
import reborncore.common.registration.impl.ConfigRegistry;
import techreborn.api.Reference;
@ -53,7 +54,7 @@ import techreborn.lib.ModInfo;
import javax.annotation.Nullable;
@RebornRegistry(modID = ModInfo.MOD_ID)
public class ItemCloakingDevice extends ItemTRArmour implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemCloakingDevice extends ItemTRArmour implements IEnergyItemInfo {
@ConfigRegistry(config = "items", category = "cloacking_device", key = "ClockingDeviceEnergyUsage", comment = "Cloacking device energy usesage (Value in EU)")
public static int usage = 10;
@ -69,6 +70,7 @@ public class ItemCloakingDevice extends ItemTRArmour implements IEnergyItemInfo,
setCreativeTab(TechRebornCreativeTab.instance);
}
// Item
@Override
@SideOnly(Side.CLIENT)
public String getArmorTexture(ItemStack stack, Entity entity, EntityEquipmentSlot slot, String type) {
@ -76,9 +78,10 @@ public class ItemCloakingDevice extends ItemTRArmour implements IEnergyItemInfo,
}
@Override
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {
if (PoweredItem.canUseEnergy(usage, itemStack)) {
PoweredItem.useEnergy(usage, itemStack);
public void onArmorTick(World world, EntityPlayer player, ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
if (capEnergy != null && capEnergy.getEnergyStored() >= usage) {
capEnergy.extractEnergy(usage, false);
player.setInvisible(true);
} else {
if (!player.isPotionActive(MobEffects.INVISIBILITY)) {
@ -87,6 +90,44 @@ public class ItemCloakingDevice extends ItemTRArmour implements IEnergyItemInfo,
}
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.CLOAKING_DEVICE);
ItemStack charged = new ItemStack(ModItems.CLOAKING_DEVICE);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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 MaxCharge;
@ -106,132 +147,4 @@ public class ItemCloakingDevice extends ItemTRArmour implements IEnergyItemInfo,
public double getMaxTransfer(ItemStack stack) {
return Limit;
}
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
ItemStack itemstack1 = player.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
if (itemstack1.isEmpty()) {
player.setItemStackToSlot(EntityEquipmentSlot.CHEST, itemStack.copy());
itemStack.setCount(0);
}
return itemStack;
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.CLOAKING_DEVICE);
ItemStack charged = new ItemStack(ModItems.CLOAKING_DEVICE);
PoweredItem.setEnergy(getMaxPower(charged), charged);
itemList.add(uncharged);
itemList.add(charged);
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
}

View file

@ -35,17 +35,17 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import reborncore.api.power.IEnergyInterfaceItem;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import techreborn.client.TechRebornCreativeTab;
import javax.annotation.Nullable;
import java.util.Random;
public class ItemDrill extends ItemPickaxe implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemDrill extends ItemPickaxe implements IEnergyItemInfo {
public int maxCharge = 1;
public int cost = 250;
@ -62,19 +62,10 @@ public class ItemDrill extends ItemPickaxe implements IEnergyItemInfo, IEnergyIn
this.unpoweredSpeed = unpoweredSpeed;
}
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos,
EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
PoweredItem.useEnergy(cost, stack);
}
return true;
}
// ItemPickaxe
@Override
public float getDestroySpeed(ItemStack stack, IBlockState state) {
if (!PoweredItem.canUseEnergy(cost, stack)) {
if (stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() < cost) {
return unpoweredSpeed;
}
if (Items.WOODEN_PICKAXE.getDestroySpeed(stack, state) > 1.0F
@ -85,17 +76,56 @@ public class ItemDrill extends ItemPickaxe implements IEnergyItemInfo, IEnergyIn
}
}
// ItemTool
@Override
public boolean hitEntity(ItemStack itemstack, EntityLivingBase entityliving,
EntityLivingBase entityliving1) {
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
stack.getCapability(CapabilityEnergy.ENERGY, null).extractEnergy(cost, false);
}
return true;
}
@Override
public boolean hitEntity(ItemStack itemstack, EntityLivingBase entityliving, EntityLivingBase entityliving1) {
return true;
}
//Item
@Override
public boolean isRepairable() {
return false;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -115,120 +145,4 @@ public class ItemDrill extends ItemPickaxe implements IEnergyItemInfo, IEnergyIn
public double getMaxTransfer(ItemStack stack) {
return transferLimit;
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
/**
* Called when the player is mining a block and the item in his hand changes.
* Allows to not reset blockbreaking if only NBT or similar changes.
* @param oldStack The old stack that was used for mining. Item in players main hand
* @param newStack The new stack
* @return True to reset block break progress
*/
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
}

View file

@ -38,13 +38,14 @@ import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.client.TechRebornCreativeTab;
import techreborn.compat.CompatManager;
import techreborn.init.ModItems;
@ -55,7 +56,7 @@ import javax.annotation.Nullable;
/**
* Created by modmuss50 on 05/11/2016.
*/
public class ItemElectricTreetap extends ItemTR implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemElectricTreetap extends ItemTR implements IEnergyItemInfo {
public static final int maxCharge = 10000;
public static final int tier = 1;
@ -67,18 +68,59 @@ public class ItemElectricTreetap extends ItemTR implements IEnergyItemInfo, IEne
setMaxStackSize(1);
}
// Item
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
IBlockState state = worldIn.getBlockState(pos);
Block block = state.getBlock();
if (CompatManager.isIC2Loaded && block == Block.getBlockFromName("ic2:rubber_wood") && PoweredItem.canUseEnergy(20, playerIn.getHeldItem(hand)))
IEnergyStorage capEnergy = playerIn.getHeldItem(hand).getCapability(CapabilityEnergy.ENERGY, null);
if (CompatManager.isIC2Loaded && block == Block.getBlockFromName("ic2:rubber_wood") && capEnergy.getEnergyStored() >= cost)
if (ItemTreetap.attemptExtract(playerIn, worldIn, pos, side, state, null) && !worldIn.isRemote) {
PoweredItem.useEnergy(20, playerIn.getHeldItem(hand));
capEnergy.extractEnergy(cost, false);
return EnumActionResult.SUCCESS;
}
return EnumActionResult.PASS;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.ELECTRIC_TREE_TAP);
ItemStack charged = new ItemStack(ModItems.ELECTRIC_TREE_TAP);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -98,122 +140,4 @@ public class ItemElectricTreetap extends ItemTR implements IEnergyItemInfo, IEne
public double getMaxTransfer(ItemStack stack) {
return 200;
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.ELECTRIC_TREE_TAP);
ItemStack charged = new ItemStack(ModItems.ELECTRIC_TREE_TAP);
PoweredItem.setEnergy(getMaxPower(charged), charged);
itemList.add(uncharged);
itemList.add(charged);
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
}

View file

@ -26,7 +26,6 @@ package techreborn.items.tools;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Blocks;
@ -34,16 +33,13 @@ import net.minecraft.init.Enchantments;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
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.IEnergyInterfaceItem;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import techreborn.client.TechRebornCreativeTab;
import techreborn.utils.OreDictUtils;
@ -51,7 +47,7 @@ import techreborn.utils.OreDictUtils;
import javax.annotation.Nullable;
import java.util.Random;
public class ItemJackhammer extends ItemPickaxe implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemJackhammer extends ItemPickaxe implements IEnergyItemInfo {
public int maxCharge = 1;
public int cost = 250;
@ -67,41 +63,73 @@ public class ItemJackhammer extends ItemPickaxe implements IEnergyItemInfo, IEne
this.maxCharge = energyCapacity;
}
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos,
EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
PoweredItem.useEnergy(cost, stack);
}
return true;
}
@Override
public boolean canHarvestBlock(final IBlockState state, final ItemStack stack) {
return OreDictUtils.isOre(state, "stone")
|| state.getMaterial() == Material.ROCK && PoweredItem.canUseEnergy(this.cost, stack);
}
// ItemPicjaxe
@Override
public float getDestroySpeed(ItemStack stack, IBlockState state) {
if ((OreDictUtils.isOre(state, "stone") || state.getBlock() == Blocks.STONE) && PoweredItem.canUseEnergy(cost, stack)) {
if ((OreDictUtils.isOre(state, "stone") || state.getBlock() == Blocks.STONE)
&& stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() >= cost) {
return efficiency;
} else {
return 0.5F;
}
}
// ItemTool
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
stack.getCapability(CapabilityEnergy.ENERGY, null).extractEnergy(cost, false);
}
return true;
}
@Override
public boolean hitEntity(ItemStack itemstack, EntityLivingBase entityliving, EntityLivingBase entityliving1) {
return true;
}
// Item
@Override
public boolean canHarvestBlock(final IBlockState state, final ItemStack stack) {
return OreDictUtils.isOre(state, "stone")
|| state.getMaterial() == Material.ROCK && stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() >= cost;
}
@Override
public boolean isRepairable() {
return false;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -121,133 +149,4 @@ public class ItemJackhammer extends ItemPickaxe implements IEnergyItemInfo, IEne
public double getMaxTransfer(ItemStack stack) {
return transferLimit;
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack itemStack = new ItemStack(this, 1);
itemList.add(itemStack);
ItemStack charged = new ItemStack(this, 1);
PoweredItem.setEnergy(getMaxPower(charged), charged);
itemList.add(charged);
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
/**
* Called when the player is mining a block and the item in his hand changes.
* Allows to not reset blockbreaking if only NBT or similar changes.
* @param oldStack The old stack that was used for mining. Item in players main hand
* @param newStack The new stack
* @return True to reset block break progress
*/
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
}

View file

@ -44,13 +44,14 @@ import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import reborncore.common.util.ChatUtils;
import techreborn.client.TechRebornCreativeTab;
import techreborn.init.ModItems;
@ -59,7 +60,7 @@ import techreborn.lib.MessageIDs;
import javax.annotation.Nullable;
import java.util.List;
public class ItemNanosaber extends ItemSword implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemNanosaber extends ItemSword implements IEnergyItemInfo {
public int cost = 250;
public ItemNanosaber() {
@ -71,14 +72,12 @@ public class ItemNanosaber extends ItemSword implements IEnergyItemInfo, IEnergy
this.addPropertyOverride(new ResourceLocation("techreborn:active"), new IItemPropertyGetter() {
@Override
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack,
@Nullable
World worldIn,
@Nullable
EntityLivingBase entityIn) {
if (!stack.isEmpty() && stack.hasTagCompound() && stack.getTagCompound().hasKey("isActive") && stack.getTagCompound().getBoolean("isActive")) {
if (PoweredItem.getMaxPower(stack) - PoweredItem.getEnergy(stack) >= 0.9 * PoweredItem.getMaxPower(stack))
public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
if (isActive(stack)) {
ForgePowerItemManager capEnergy = (ForgePowerItemManager) stack.getCapability(CapabilityEnergy.ENERGY, null);
if (capEnergy.getMaxEnergyStored() - capEnergy.getEnergyStored() >= 0.9 * capEnergy.getMaxEnergyStored()) {
return 0.5F;
}
return 1.0F;
}
return 0.0F;
@ -86,14 +85,33 @@ public class ItemNanosaber extends ItemSword implements IEnergyItemInfo, IEnergy
});
}
private boolean isActive(ItemStack stack) {
if (!stack.isEmpty() && stack.getTagCompound() != null && stack.getTagCompound().getBoolean("isActive")) {
return true;
}
return false;
}
// ItemSword
@Override
public Multimap<String, AttributeModifier> getAttributeModifiers(EntityEquipmentSlot slot,
ItemStack stack) {
public boolean hitEntity(ItemStack stack, EntityLivingBase entityliving, EntityLivingBase entityliving1) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
if (capEnergy.getEnergyStored() >= cost) {
capEnergy.extractEnergy(cost, false);
return true;
} else {
return false;
}
}
// Item
@Override
public Multimap<String, AttributeModifier> getAttributeModifiers(EntityEquipmentSlot slot, ItemStack stack) {
Multimap<String, AttributeModifier> multimap = HashMultimap.<String, AttributeModifier>create();
int modifier = 0;
if (!stack.isEmpty() && stack.getTagCompound() != null && stack.getTagCompound().getBoolean("isActive"))
if (isActive(stack)) {
modifier = 9;
}
if (slot == EntityEquipmentSlot.MAINHAND) {
multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(),
new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", (double) modifier, 0));
@ -104,64 +122,16 @@ public class ItemNanosaber extends ItemSword implements IEnergyItemInfo, IEnergy
}
@Override
public boolean hitEntity(ItemStack itemstack, EntityLivingBase entityliving,
EntityLivingBase entityliving1) {
if (PoweredItem.canUseEnergy(cost, itemstack)) {
PoweredItem.useEnergy(cost, itemstack);
return true;
} else {
return false;
}
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(
CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack inactiveUncharged = new ItemStack(ModItems.NANOSABER);
inactiveUncharged.setTagCompound(new NBTTagCompound());
inactiveUncharged.getTagCompound().setBoolean("isActive", false);
ItemStack inactiveCharged = new ItemStack(ModItems.NANOSABER);
inactiveCharged.setTagCompound(new NBTTagCompound());
inactiveCharged.getTagCompound().setBoolean("isActive", false);
PoweredItem.setEnergy(getMaxPower(inactiveCharged), inactiveCharged);
ItemStack activeCharged = new ItemStack(ModItems.NANOSABER);
activeCharged.setTagCompound(new NBTTagCompound());
activeCharged.getTagCompound().setBoolean("isActive", true);
PoweredItem.setEnergy(getMaxPower(activeCharged), activeCharged);
itemList.add(inactiveUncharged);
itemList.add(inactiveCharged);
itemList.add(activeCharged);
}
@SideOnly(Side.CLIENT)
@Override
public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
if (stack.getTagCompound() == null || !stack.getTagCompound().getBoolean("isActive")) {
tooltip.add(TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberInactive"));
} else {
tooltip.add(TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberActive"));
}
}
@Override
public ActionResult<ItemStack> onItemRightClick(final World world, final EntityPlayer player,
final EnumHand hand) {
public ActionResult<ItemStack> onItemRightClick(final World world, final EntityPlayer player, final EnumHand hand) {
final ItemStack stack = player.getHeldItem(hand);
if (player.isSneaking()) {
if (!PoweredItem.canUseEnergy(this.cost, stack)) {
if (stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() < cost) {
ChatUtils.sendNoSpamMessages(MessageIDs.nanosaberID, new TextComponentString(
TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberEnergyErrorTo") + " "
+ TextFormatting.GOLD + I18n
.format("techreborn.message.nanosaberActivate")));
} else {
if (stack.getTagCompound() == null || !stack.getTagCompound().getBoolean("isActive")) {
if (!isActive(stack)) {
if (stack.getTagCompound() == null) {
stack.setTagCompound(new NBTTagCompound());
}
@ -189,7 +159,7 @@ public class ItemNanosaber extends ItemSword implements IEnergyItemInfo, IEnergy
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
if (stack.getTagCompound() != null && stack.getTagCompound().getBoolean("isActive") && !PoweredItem.canUseEnergy(cost, stack)) {
if (isActive(stack) && stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() < cost) {
if(worldIn.isRemote){
ChatUtils.sendNoSpamMessages(MessageIDs.nanosaberID, new TextComponentString(
TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberEnergyError") + " "
@ -205,9 +175,71 @@ public class ItemNanosaber extends ItemSword implements IEnergyItemInfo, IEnergy
return false;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(
CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack inactiveUncharged = new ItemStack(ModItems.NANOSABER);
inactiveUncharged.setTagCompound(new NBTTagCompound());
inactiveUncharged.getTagCompound().setBoolean("isActive", false);
ItemStack inactiveCharged = new ItemStack(ModItems.NANOSABER);
inactiveCharged.setTagCompound(new NBTTagCompound());
inactiveCharged.getTagCompound().setBoolean("isActive", false);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) inactiveCharged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
ItemStack activeCharged = new ItemStack(ModItems.NANOSABER);
activeCharged.setTagCompound(new NBTTagCompound());
activeCharged.getTagCompound().setBoolean("isActive", true);
ForgePowerItemManager capEnergy2 = (ForgePowerItemManager) activeCharged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy2.setEnergyStored(capEnergy2.getMaxEnergyStored());
itemList.add(inactiveUncharged);
itemList.add(inactiveCharged);
itemList.add(activeCharged);
}
@SideOnly(Side.CLIENT)
@Override
public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
if (!isActive(stack)) {
tooltip.add(TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberInactive"));
} else {
tooltip.add(TextFormatting.GRAY + I18n.format("techreborn.message.nanosaberActive"));
}
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return 40000;
return 160_000;
}
@Override
@ -222,110 +254,6 @@ public class ItemNanosaber extends ItemSword implements IEnergyItemInfo, IEnergy
@Override
public double getMaxTransfer(ItemStack stack) {
return 512;
return 1000;
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
}

View file

@ -38,13 +38,14 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import reborncore.common.util.TorchHelper;
import techreborn.client.TechRebornCreativeTab;
import techreborn.config.ConfigTechReborn;
@ -53,7 +54,7 @@ import techreborn.init.ModItems;
import javax.annotation.Nullable;
import java.util.List;
public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo {
public static final int maxCharge = ConfigTechReborn.OmniToolCharge;
public int cost = 100;
@ -68,13 +69,7 @@ public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo, IEnerg
setUnlocalizedName("techreborn.omniTool");
}
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos,
EntityLivingBase entityLiving) {
PoweredItem.useEnergy(cost, stack);
return true;
}
// ItemPickaxe
@Override
public boolean canHarvestBlock(IBlockState state) {
return Items.DIAMOND_AXE.canHarvestBlock(state) || Items.DIAMOND_SWORD.canHarvestBlock(state)
@ -82,10 +77,20 @@ public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo, IEnerg
|| Items.SHEARS.canHarvestBlock(state);
}
// ItemTool
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
stack.getCapability(CapabilityEnergy.ENERGY, null).extractEnergy(cost, false);
return true;
}
// @Override
// public float getDigSpeed(ItemStack stack, IBlockState state) {
// if (PoweredItem.canUseEnergy(cost, stack)) {
// PoweredItem.useEnergy(cost, stack);
// IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
// if (capEnergy.getEnergyStored() >= cost) {
// capEnergy.extractEnergy(cost, false);
// return 5.0F;
// }
//
@ -101,14 +106,16 @@ public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo, IEnerg
// }
@Override
public boolean hitEntity(ItemStack itemstack, EntityLivingBase entityliving, EntityLivingBase attacker) {
if (PoweredItem.canUseEnergy(hitCost, itemstack)) {
PoweredItem.useEnergy(hitCost, itemstack);
public boolean hitEntity(ItemStack stack, EntityLivingBase entityliving, EntityLivingBase attacker) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
if (capEnergy.getEnergyStored() >= hitCost) {
capEnergy.extractEnergy(hitCost, false);
entityliving.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer) attacker), 8F);
}
return false;
}
// Item
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos,
EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
@ -120,6 +127,51 @@ public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo, IEnerg
return false;
}
@Override
public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag) {
tooltip.add(TextFormatting.RED + "WIP Coming Soon");
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(
CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.OMNI_TOOL);
ItemStack charged = new ItemStack(ModItems.OMNI_TOOL);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -137,129 +189,6 @@ public class ItemOmniTool extends ItemPickaxe implements IEnergyItemInfo, IEnerg
@Override
public double getMaxTransfer(ItemStack stack) {
return 200;
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(
CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.OMNI_TOOL);
ItemStack charged = new ItemStack(ModItems.OMNI_TOOL);
PoweredItem.setEnergy(getMaxPower(charged), charged);
itemList.add(uncharged);
itemList.add(charged);
}
@Override
public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag) {
tooltip.add(TextFormatting.RED + "WIP Coming Soon");
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
return 1000;
}
}

View file

@ -39,13 +39,14 @@ import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PoweredItem;
import reborncore.common.powerSystem.PoweredItemContainerProvider;
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
import techreborn.client.TechRebornCreativeTab;
import techreborn.config.ConfigTechReborn;
import techreborn.init.ModItems;
@ -53,7 +54,7 @@ import techreborn.init.ModItems;
import javax.annotation.Nullable;
import java.util.Random;
public class ItemRockCutter extends ItemPickaxe implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemRockCutter extends ItemPickaxe implements IEnergyItemInfo {
public static final int maxCharge = ConfigTechReborn.RockCutterCharge;
public int cost = 500;
@ -66,16 +67,7 @@ public class ItemRockCutter extends ItemPickaxe implements IEnergyItemInfo, IEne
efficiency = 16F;
}
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos,
EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
PoweredItem.useEnergy(cost, stack);
}
return true;
}
// ItemPickaxe
@Override
public boolean canHarvestBlock(IBlockState state) {
if (Items.DIAMOND_PICKAXE.canHarvestBlock(state)) {
@ -84,28 +76,34 @@ public class ItemRockCutter extends ItemPickaxe implements IEnergyItemInfo, IEne
return false;
}
@Override
public int getHarvestLevel(ItemStack stack,
String toolClass,
@Nullable
EntityPlayer player,
@Nullable
IBlockState blockState) {
if (!stack.isItemEnchanted()) {
stack.addEnchantment(Enchantment.getEnchantmentByID(33), 1);
}
return super.getHarvestLevel(stack, toolClass, player, blockState);
}
@Override
public float getDestroySpeed(ItemStack stack, IBlockState state) {
if (!PoweredItem.canUseEnergy(cost, stack)) {
if (stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored() < cost) {
return 2F;
} else {
return Items.DIAMOND_PICKAXE.getDestroySpeed(stack, state);
}
}
// ItemTool
@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
Random rand = new Random();
if (rand.nextInt(EnchantmentHelper.getEnchantmentLevel(Enchantments.UNBREAKING, stack) + 1) == 0) {
stack.getCapability(CapabilityEnergy.ENERGY, null).extractEnergy(cost, false);
}
return true;
}
@Override
public int getHarvestLevel(ItemStack stack, String toolClass, @Nullable EntityPlayer player, @Nullable IBlockState blockState) {
if (!stack.isItemEnchanted()) {
stack.addEnchantment(Enchantment.getEnchantmentByID(33), 1);
}
return super.getHarvestLevel(stack, toolClass, player, blockState);
}
// Item
@Override
public boolean isRepairable() {
return false;
@ -116,6 +114,50 @@ public class ItemRockCutter extends ItemPickaxe implements IEnergyItemInfo, IEne
stack.addEnchantment(Enchantments.SILK_TOUCH, 1);
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
@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);
}
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.ROCK_CUTTER);
ItemStack charged = new ItemStack(ModItems.ROCK_CUTTER);
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
itemList.add(uncharged);
itemList.add(charged);
}
// IEnergyItemInfo
@Override
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -133,137 +175,6 @@ public class ItemRockCutter extends ItemPickaxe implements IEnergyItemInfo, IEne
@Override
public double getMaxTransfer(ItemStack stack) {
return 200;
return 1000;
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(
CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
if (!isInCreativeTab(par2CreativeTabs)) {
return;
}
ItemStack uncharged = new ItemStack(ModItems.ROCK_CUTTER);
ItemStack charged = new ItemStack(ModItems.ROCK_CUTTER);
PoweredItem.setEnergy(getMaxPower(charged), charged);
itemList.add(uncharged);
itemList.add(charged);
}
@Override
public double getEnergy(ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
if (tagCompound.hasKey("charge")) {
return tagCompound.getDouble("charge");
}
return 0;
}
@Override
public void setEnergy(double energy, ItemStack stack) {
NBTTagCompound tagCompound = getOrCreateNbtData(stack);
tagCompound.setDouble("charge", energy);
if (this.getEnergy(stack) > getMaxPower(stack)) {
this.setEnergy(getMaxPower(stack), stack);
} else if (this.getEnergy(stack) < 0) {
this.setEnergy(0, stack);
}
}
@Override
public double addEnergy(double energy, ItemStack stack) {
return addEnergy(energy, false, stack);
}
@Override
public double addEnergy(double energy, boolean simulate, ItemStack stack) {
double energyReceived = Math.min(getMaxPower(stack) - energy, Math.min(this.getMaxPower(stack), energy));
if (!simulate) {
setEnergy(energy + energyReceived, stack);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double input, ItemStack stack) {
return input <= getEnergy(stack);
}
@Override
public double useEnergy(double energy, ItemStack stack) {
return useEnergy(energy, false, stack);
}
@Override
public double useEnergy(double extract, boolean simulate, ItemStack stack) {
double energyExtracted = Math.min(extract, Math.min(this.getMaxTransfer(stack), extract));
if (!simulate) {
setEnergy(getEnergy(stack) - energyExtracted, stack);
}
return energyExtracted;
}
@Override
public boolean canAddEnergy(double energy, ItemStack stack) {
return this.getEnergy(stack) + energy <= getMaxPower(stack);
}
public NBTTagCompound getOrCreateNbtData(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
itemStack.setTagCompound(tagCompound);
}
return tagCompound;
}
@Override
public double getDurabilityForDisplay(ItemStack stack) {
double charge = (PoweredItem.getEnergy(stack) / getMaxPower(stack));
return 1 - charge;
}
@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);
}
/**
* Called when the player is mining a block and the item in his hand changes.
* Allows to not reset blockbreaking if only NBT or similar changes.
* @param oldStack The old stack that was used for mining. Item in players main hand
* @param newStack The new stack
* @return True to reset block break progress
*/
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return !(newStack.isItemEqual(oldStack));
}
}

View file

@ -50,6 +50,7 @@ import techreborn.client.render.ModelDynamicCell;
import techreborn.client.render.entitys.RenderNukePrimed;
import techreborn.entities.EntityNukePrimed;
import techreborn.events.FluidBlockModelHandler;
import techreborn.events.StackToolTipEvent;
import techreborn.init.ModBlocks;
import techreborn.items.ItemFrequencyTransmitter;
import techreborn.lib.ModInfo;