Started work in switching powered items to capability

This commit is contained in:
drcrazy 2018-05-23 00:26:30 +03:00
parent c7dcc7a5e3
commit c4c5f2135c
2 changed files with 90 additions and 156 deletions

View file

@ -22,37 +22,47 @@
* SOFTWARE.
*/
package techreborn.client;
package techreborn.events;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.client.Minecraft;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
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.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Keyboard;
import reborncore.api.IListInfoProvider;
import reborncore.api.power.IEnergyInterfaceItem;
import reborncore.api.power.IEnergyItemInfo;
import reborncore.common.powerSystem.PowerSystem;
import techreborn.Core;
public class StackToolTipEvent {
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void handleItemTooltipEvent(ItemTooltipEvent event) {
if (event.getItemStack().getItem() instanceof IListInfoProvider) {
((IListInfoProvider) event.getItemStack().getItem()).addInfo(event.getToolTip(), false);
} else if (event.getItemStack().getItem() instanceof IEnergyInterfaceItem) {
event.getToolTip().add(1, TextFormatting.GOLD + PowerSystem.getLocaliszedPowerFormattedNoSuffix(
(int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getEnergy(event.getItemStack()))
+ "/" + PowerSystem.getLocaliszedPowerFormattedNoSuffix((int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getMaxPower(event.getItemStack()))
+ " " + PowerSystem.getDisplayPower().abbreviation);
if (event.getEntityPlayer() == null) {
return;
}
Item item = event.getItemStack().getItem();
if (item instanceof IListInfoProvider) {
((IListInfoProvider) item).addInfo(event.getToolTip(), false);
} 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)) {
int percentage = percentage(
(int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getMaxPower(event.getItemStack()),
(int) ((IEnergyInterfaceItem) event.getItemStack().getItem()).getEnergy(event.getItemStack()));
int percentage = percentage(capEnergy.getMaxEnergyStored(), capEnergy.getEnergyStored());
TextFormatting color;
if (percentage <= 10) {
color = TextFormatting.RED;
@ -62,14 +72,18 @@ public class StackToolTipEvent {
color = TextFormatting.YELLOW;
}
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 {
Block block = Block.getBlockFromItem(event.getItemStack().getItem());
Block block = Block.getBlockFromItem(item);
if (block != null && (block instanceof BlockContainer || block instanceof ITileEntityProvider)
&& block.getRegistryName().getResourceDomain().contains("techreborn")) {
TileEntity tile = block.createTileEntity(Minecraft.getMinecraft().world,
TileEntity tile = block.createTileEntity(Minecraft.getMinecraft().world,
block.getStateFromMeta(event.getItemStack().getItemDamage()));
if (tile instanceof IListInfoProvider) {
((IListInfoProvider) tile).addInfo(event.getToolTip(), false);

View file

@ -38,21 +38,20 @@ 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 ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IEnergyInterfaceItem {
public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo {
public static final int maxCharge = ConfigTechReborn.LithiumBatpackCharge;
public double transferLimit = 10000;
public double transferLimit = 10_000;
public ItemLithiumBatpack() {
super(ItemArmor.ArmorMaterial.DIAMOND, 7, EntityEquipmentSlot.CHEST);
@ -61,30 +60,54 @@ public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IE
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
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {
distributePowerToInventory(world, player, itemStack, (int) transferLimit);
}
public static void distributePowerToInventory(World world, EntityPlayer player, ItemStack itemStack, int maxSend){
if(world.isRemote){
return;
}
if(!itemStack.hasCapability(CapabilityEnergy.ENERGY, null)){
return;
}
IEnergyStorage backpackPower = itemStack.getCapability(CapabilityEnergy.ENERGY, null);
@Override
public double getDurabilityForDisplay(ItemStack stack) {
IEnergyStorage capEnergy = stack.getCapability(CapabilityEnergy.ENERGY, null);
double charge = (capEnergy.getEnergyStored() / capEnergy.getMaxEnergyStored());
return 1 - charge;
}
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);
backpackPower.extractEnergy(itemPower.receiveEnergy(Math.min(backpackPower.getEnergyStored(), maxSend), false), false);
}
}
@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
@ -93,6 +116,21 @@ public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IE
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
public double getMaxPower(ItemStack stack) {
return maxCharge;
@ -112,122 +150,4 @@ public class ItemLithiumBatpack extends ItemArmor implements IEnergyItemInfo, IE
public double getMaxTransfer(ItemStack stack) {
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);
}
}