Started work in switching powered items to capability
This commit is contained in:
parent
c7dcc7a5e3
commit
c4c5f2135c
2 changed files with 90 additions and 156 deletions
|
@ -22,37 +22,47 @@
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package techreborn.client;
|
package techreborn.events;
|
||||||
|
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.BlockContainer;
|
import net.minecraft.block.BlockContainer;
|
||||||
import net.minecraft.block.ITileEntityProvider;
|
import net.minecraft.block.ITileEntityProvider;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.text.TextFormatting;
|
import net.minecraft.util.text.TextFormatting;
|
||||||
|
import net.minecraftforge.energy.CapabilityEnergy;
|
||||||
|
import net.minecraftforge.energy.IEnergyStorage;
|
||||||
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||||
|
import net.minecraftforge.fml.relauncher.Side;
|
||||||
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||||
|
|
||||||
import org.lwjgl.input.Keyboard;
|
import org.lwjgl.input.Keyboard;
|
||||||
import reborncore.api.IListInfoProvider;
|
import reborncore.api.IListInfoProvider;
|
||||||
import reborncore.api.power.IEnergyInterfaceItem;
|
import reborncore.api.power.IEnergyItemInfo;
|
||||||
import reborncore.common.powerSystem.PowerSystem;
|
import reborncore.common.powerSystem.PowerSystem;
|
||||||
import techreborn.Core;
|
import techreborn.Core;
|
||||||
|
|
||||||
public class StackToolTipEvent {
|
public class StackToolTipEvent {
|
||||||
|
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void handleItemTooltipEvent(ItemTooltipEvent event) {
|
public void handleItemTooltipEvent(ItemTooltipEvent event) {
|
||||||
if (event.getItemStack().getItem() instanceof IListInfoProvider) {
|
if (event.getEntityPlayer() == null) {
|
||||||
((IListInfoProvider) event.getItemStack().getItem()).addInfo(event.getToolTip(), false);
|
return;
|
||||||
} else if (event.getItemStack().getItem() instanceof IEnergyInterfaceItem) {
|
}
|
||||||
event.getToolTip().add(1, TextFormatting.GOLD + PowerSystem.getLocaliszedPowerFormattedNoSuffix(
|
Item item = event.getItemStack().getItem();
|
||||||
(int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getEnergy(event.getItemStack()))
|
if (item instanceof IListInfoProvider) {
|
||||||
+ "/" + PowerSystem.getLocaliszedPowerFormattedNoSuffix((int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getMaxPower(event.getItemStack()))
|
((IListInfoProvider) item).addInfo(event.getToolTip(), false);
|
||||||
+ " " + PowerSystem.getDisplayPower().abbreviation);
|
} else if (event.getItemStack().getItem() instanceof IEnergyItemInfo) {
|
||||||
|
IEnergyStorage capEnergy = event.getItemStack().getCapability(CapabilityEnergy.ENERGY, null);
|
||||||
|
event.getToolTip().add(1,
|
||||||
|
TextFormatting.GOLD + PowerSystem.getLocaliszedPowerFormattedNoSuffix(capEnergy.getEnergyStored())
|
||||||
|
+ "/" + PowerSystem.getLocaliszedPowerFormattedNoSuffix(capEnergy.getMaxEnergyStored())
|
||||||
|
+ " " + PowerSystem.getDisplayPower().abbreviation);
|
||||||
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
|
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
|
||||||
int percentage = percentage(
|
int percentage = percentage(capEnergy.getMaxEnergyStored(), capEnergy.getEnergyStored());
|
||||||
(int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getMaxPower(event.getItemStack()),
|
|
||||||
(int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getEnergy(event.getItemStack()));
|
|
||||||
TextFormatting color;
|
TextFormatting color;
|
||||||
if (percentage <= 10) {
|
if (percentage <= 10) {
|
||||||
color = TextFormatting.RED;
|
color = TextFormatting.RED;
|
||||||
|
@ -62,14 +72,18 @@ public class StackToolTipEvent {
|
||||||
color = TextFormatting.YELLOW;
|
color = TextFormatting.YELLOW;
|
||||||
}
|
}
|
||||||
event.getToolTip().add(2, color + "" + percentage + "%" + TextFormatting.GRAY + " Charged");
|
event.getToolTip().add(2, color + "" + percentage + "%" + TextFormatting.GRAY + " Charged");
|
||||||
event.getToolTip().add(3, TextFormatting.GRAY + "I/O Rate: " + TextFormatting.GOLD + PowerSystem.getLocaliszedPowerFormatted((int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getMaxTransfer(event.getItemStack())));
|
event.getToolTip().add(3,
|
||||||
|
TextFormatting.GRAY + "I/O Rate: "
|
||||||
|
+ TextFormatting.GOLD
|
||||||
|
+ PowerSystem.getLocaliszedPowerFormatted((int) ((IEnergyItemInfo) item).getMaxTransfer(event.getItemStack())));
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
try {
|
try {
|
||||||
Block block = Block.getBlockFromItem(event.getItemStack().getItem());
|
Block block = Block.getBlockFromItem(item);
|
||||||
if (block != null && (block instanceof BlockContainer || block instanceof ITileEntityProvider)
|
if (block != null && (block instanceof BlockContainer || block instanceof ITileEntityProvider)
|
||||||
&& block.getRegistryName().getResourceDomain().contains("techreborn")) {
|
&& block.getRegistryName().getResourceDomain().contains("techreborn")) {
|
||||||
TileEntity tile = block.createTileEntity(Minecraft.getMinecraft().world,
|
TileEntity tile = block.createTileEntity(Minecraft.getMinecraft().world,
|
||||||
block.getStateFromMeta(event.getItemStack().getItemDamage()));
|
block.getStateFromMeta(event.getItemStack().getItemDamage()));
|
||||||
if (tile instanceof IListInfoProvider) {
|
if (tile instanceof IListInfoProvider) {
|
||||||
((IListInfoProvider) tile).addInfo(event.getToolTip(), false);
|
((IListInfoProvider) tile).addInfo(event.getToolTip(), false);
|
|
@ -38,21 +38,20 @@ import net.minecraftforge.energy.CapabilityEnergy;
|
||||||
import net.minecraftforge.energy.IEnergyStorage;
|
import net.minecraftforge.energy.IEnergyStorage;
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
import net.minecraftforge.fml.relauncher.Side;
|
||||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||||
import reborncore.api.power.IEnergyInterfaceItem;
|
|
||||||
import reborncore.api.power.IEnergyItemInfo;
|
import reborncore.api.power.IEnergyItemInfo;
|
||||||
import reborncore.common.powerSystem.PowerSystem;
|
import reborncore.common.powerSystem.PowerSystem;
|
||||||
import reborncore.common.powerSystem.PoweredItem;
|
|
||||||
import reborncore.common.powerSystem.PoweredItemContainerProvider;
|
import reborncore.common.powerSystem.PoweredItemContainerProvider;
|
||||||
|
import reborncore.common.powerSystem.forge.ForgePowerItemManager;
|
||||||
import techreborn.client.TechRebornCreativeTab;
|
import techreborn.client.TechRebornCreativeTab;
|
||||||
import techreborn.config.ConfigTechReborn;
|
import techreborn.config.ConfigTechReborn;
|
||||||
import techreborn.init.ModItems;
|
import techreborn.init.ModItems;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IEnergyInterfaceItem {
|
public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo {
|
||||||
|
|
||||||
public static final int maxCharge = ConfigTechReborn.LithiumBatpackCharge;
|
public static final int maxCharge = ConfigTechReborn.LithiumBatpackCharge;
|
||||||
public double transferLimit = 10000;
|
public double transferLimit = 10_000;
|
||||||
|
|
||||||
public ItemLithiumBatpack() {
|
public ItemLithiumBatpack() {
|
||||||
super(ItemArmor.ArmorMaterial.DIAMOND, 7, EntityEquipmentSlot.CHEST);
|
super(ItemArmor.ArmorMaterial.DIAMOND, 7, EntityEquipmentSlot.CHEST);
|
||||||
|
@ -61,30 +60,54 @@ public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IE
|
||||||
setCreativeTab(TechRebornCreativeTab.instance);
|
setCreativeTab(TechRebornCreativeTab.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void distributePowerToInventory(World world, EntityPlayer player, ItemStack itemStack, int maxSend) {
|
||||||
|
if (world.isRemote) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!itemStack.hasCapability(CapabilityEnergy.ENERGY, null)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
IEnergyStorage capEnergy = itemStack.getCapability(CapabilityEnergy.ENERGY, null);
|
||||||
|
|
||||||
|
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
|
||||||
|
if (player.inventory.getStackInSlot(i) != ItemStack.EMPTY) {
|
||||||
|
ItemStack item = player.inventory.getStackInSlot(i);
|
||||||
|
if (!item.hasCapability(CapabilityEnergy.ENERGY, null)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
IEnergyStorage itemPower = item.getCapability(CapabilityEnergy.ENERGY, null);
|
||||||
|
capEnergy.extractEnergy(itemPower.receiveEnergy(Math.min(capEnergy.getEnergyStored(), maxSend), false), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Item
|
||||||
@Override
|
@Override
|
||||||
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {
|
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {
|
||||||
distributePowerToInventory(world, player, itemStack, (int) transferLimit);
|
distributePowerToInventory(world, player, itemStack, (int) transferLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void distributePowerToInventory(World world, EntityPlayer player, ItemStack itemStack, int maxSend){
|
@Override
|
||||||
if(world.isRemote){
|
public double getDurabilityForDisplay(ItemStack stack) {
|
||||||
return;
|
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
|
||||||
}
|
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
|
||||||
if(!itemStack.hasCapability(CapabilityEnergy.ENERGY, null)){
|
return 1 - charge;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
IEnergyStorage backpackPower = itemStack.getCapability(CapabilityEnergy.ENERGY, null);
|
|
||||||
|
|
||||||
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
|
@Override
|
||||||
if (player.inventory.getStackInSlot(i) != ItemStack.EMPTY) {
|
public boolean showDurabilityBar(ItemStack stack) {
|
||||||
ItemStack item = player.inventory.getStackInSlot(i);
|
return true;
|
||||||
if(!item.hasCapability(CapabilityEnergy.ENERGY, null)){
|
}
|
||||||
continue;
|
|
||||||
}
|
@Override
|
||||||
IEnergyStorage itemPower = item.getCapability(CapabilityEnergy.ENERGY, null);
|
public int getRGBDurabilityForDisplay(ItemStack stack) {
|
||||||
backpackPower.extractEnergy(itemPower.receiveEnergy(Math.min(backpackPower.getEnergyStored(), maxSend), false), false);
|
return PowerSystem.getDisplayPower().colour;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
|
||||||
|
return new PoweredItemContainerProvider(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -93,6 +116,21 @@ public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IE
|
||||||
return "techreborn:" + "textures/models/lithiumbatpack.png";
|
return "techreborn:" + "textures/models/lithiumbatpack.png";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
|
@Override
|
||||||
|
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
|
||||||
|
if (!isInCreativeTab(par2CreativeTabs)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ItemStack uncharged = new ItemStack(ModItems.LITHIUM_BATTERY_PACK);
|
||||||
|
ItemStack charged = new ItemStack(ModItems.LITHIUM_BATTERY_PACK);
|
||||||
|
ForgePowerItemManager capEnergy = (ForgePowerItemManager) charged.getCapability(CapabilityEnergy.ENERGY, null);
|
||||||
|
capEnergy.setEnergyStored(capEnergy.getMaxEnergyStored());
|
||||||
|
itemList.add(uncharged);
|
||||||
|
itemList.add(charged);
|
||||||
|
}
|
||||||
|
|
||||||
|
// IEnergyItemInfo
|
||||||
@Override
|
@Override
|
||||||
public double getMaxPower(ItemStack stack) {
|
public double getMaxPower(ItemStack stack) {
|
||||||
return maxCharge;
|
return maxCharge;
|
||||||
|
@ -112,122 +150,4 @@ public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IE
|
||||||
public double getMaxTransfer(ItemStack stack) {
|
public double getMaxTransfer(ItemStack stack) {
|
||||||
return transferLimit;
|
return transferLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SideOnly(Side.CLIENT)
|
|
||||||
@Override
|
|
||||||
public void getSubItems(CreativeTabs par2CreativeTabs, NonNullList<ItemStack> itemList) {
|
|
||||||
if (!isInCreativeTab(par2CreativeTabs)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ItemStack uncharged = new ItemStack(ModItems.LITHIUM_BATTERY_PACK);
|
|
||||||
ItemStack charged = new ItemStack(ModItems.LITHIUM_BATTERY_PACK);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue