Tomson124 Posted April 15, 2017 Posted April 15, 2017 Hey everyone, I am trying to implement ForgeEnergy as a second energy system in addtion to RF (and later also TELSA). I want to implement it in an item class wich extends the ItemArmor class. The page on ReadTheDocs about capabilities only describes it for TEs and ItemStacks... I am completly new to the capability system so I have absolutly no plan what to do. Maybe someone can explain it to me in a few sentences and can even provide an example. If it helps, the class I am talking about is this one: https://github.com/CyberdyneCC/SimplyJetpacks-2/blob/TomsonDev/src/main/java/tonius/simplyjetpacks/item/rewrite/ItemFluxpack.java Tomson124 Quote
Choonster Posted April 15, 2017 Posted April 15, 2017 (edited) Item capabilities are handled differently to Entity/TileEntity capabilities because Items are singletons, i.e. there's only one instance per item type. Instead of Item implementing ICapabilityProvider directly, it has a method (Item#initCapabilities) that creates and returns a separate ICapabilityProvider instance. ItemStack (which implements ICapabilityProvider) calls this method from its constructors and stores the returned ICapabilityProvider, using it in its own implementation of the interface. You can see some examples of items with capabilities here, here, here, here and here. These all use one of two ICapabilityProvider implementations: CapabilityProviderSimple (only implements ICapabilityProvider, doesn't save/load capability data) and CapabilityProviderSerializable (implements both ICapabilityProvider and INBTSerialializable so it saves/loads capability data). Edited April 15, 2017 by Choonster 1 Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Tomson124 Posted April 16, 2017 Author Posted April 16, 2017 On 4/15/2017 at 1:02 PM, Choonster said: These all use one of two ICapabilityProvider implementations: CapabilityProviderSimple (only implements ICapabilityProvider, doesn't save/load capability data) and CapabilityProviderSerializable (implements both ICapabilityProvider and INBTSerialializable so it saves/loads capability data). Expand Thank you. If I am right, I don't have to make my own ICapabilityProviders if I just use the default implementation from ForgeEnergy, right? Or do I need to do the exchange between ForgeEnergy and RF in the CapabilityProvider Implementation? Quote
Choonster Posted April 16, 2017 Posted April 16, 2017 On 4/16/2017 at 11:55 AM, Tomson124 said: Thank you. If I am right, I don't have to make my own ICapabilityProviders if I just use the default implementation from ForgeEnergy, right? Or do I need to do the exchange between ForgeEnergy and RF in the CapabilityProvider Implementation? Expand The ICapabilityProvider is responsible for storing the IEnergyStorage instance and providing it to external code. The IEnergyStorage implementation should be a completely separate class that stores the energy value and allows interaction with it through the IEnergyStorage methods. You will need to create your own ICapabilityProvider implementation, Forge doesn't provide a stand-alone implementation for your use-case. The only implementations apart from vanilla classes are CapabilityDispatcher, which is designed to wrap multiple ICapabilityProviders into one; and FluidHandlerItemStack, FluidHandlerItemStackSimple and FluidBucketWrapper, which implement IFluidHandlerItem in various ways. If you want the RF IEnergyContainerItem and Forge IEnergyStorage interfaces to interact with the same energy value, you'll need to implement that yourself. There are two ways to do this: Keep the current IEnergyContainerItem implementation and create an IEnergyStorage implementation that delegates to it Use the default IEnergyStorage implementation (EnergyStorage) and change the IEnergyContainerItem implementation to delegate to it Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Tomson124 Posted April 16, 2017 Author Posted April 16, 2017 So to sum this up, I have to create my own CapabilityProvider which is then used in initCapabilities and I have to make a custom implementation of IEnergyStorage (like EnergyStorage) which also includes interaction with IEngeryContainerItem. But how do I tell the ForgeEnergy Capability that it has to use my custom IEnergyStorage implementation instead of the default one? Quote
Choonster Posted April 16, 2017 Posted April 16, 2017 On 4/16/2017 at 8:10 PM, Tomson124 said: So to sum this up, I have to create my own CapabilityProvider which is then used in initCapabilities and I have to make a custom implementation of IEnergyStorage (like EnergyStorage) which also includes interaction with IEngeryContainerItem. But how do I tell the ForgeEnergy Capability that it has to use my custom IEnergyStorage implementation instead of the default one? Expand Your ICapabilityProvider class should store an instance of this custom IEnergyStorage implementation and return it from ICapabilityProvider#getCapability when the Capability argument is CapabilityEnergy.ENERGY. This is what allows other mods to access your IEnergyStorage instance. You can either create the IEnergyStorage instance in the ICapabilityProvider or create it in the Item#initCapabilities override and pass it to the ICapabilityProvider's constructor. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Tomson124 Posted April 16, 2017 Author Posted April 16, 2017 (edited) So I think I got it right, at least I did so until I ran into the first errors It currently looks like this: Reveal hidden contents CapabilityProviderEnergy: Reveal hidden contents package tonius.simplyjetpacks.capability; import net.minecraft.nbt.NBTBase; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ICapabilitySerializable; import javax.annotation.Nullable; public class CapabilityProviderEnergy<HANDLER> implements ICapabilitySerializable { protected final HANDLER instance; protected final Capability<HANDLER> capability; protected final EnumFacing facing; public CapabilityProviderEnergy(final Capability<HANDLER> capability, @Nullable final EnumFacing facing) { this(capability.getDefaultInstance(), capability, facing); } public CapabilityProviderEnergy(final HANDLER instance, Capability<HANDLER> capability, @Nullable EnumFacing facing) { this.instance = instance; this.capability = capability; this.facing = facing; } @Override public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) { return capability == getCapability(); } @Override public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) { if (capability == getCapability()) { return getCapability().cast(getInstance()); } return null; } @Override public NBTBase serializeNBT() { return getCapability().writeNBT(getInstance(), getFacing()); } @Override public void deserializeNBT(NBTBase nbt) { getCapability().readNBT(getInstance(), getFacing(), nbt); } public final Capability<HANDLER> getCapability() { return capability; } @Nullable public EnumFacing getFacing() { return facing; } public final HANDLER getInstance() { return instance; } } Custom EnergyStorage: Reveal hidden contents package tonius.simplyjetpacks.capability; import cofh.api.energy.IEnergyContainerItem; import net.minecraft.item.ItemStack; import net.minecraftforge.energy.EnergyStorage; import net.minecraftforge.energy.IEnergyStorage; public class EnergyConversionStorage extends EnergyStorage implements IEnergyContainerItem { protected int energy; protected int capacity; protected int maxReceive; protected int maxExtract; public IEnergyStorage energyStorage; /*public EnergyConversionStorage(int capacity) { this(capacity, capacity, capacity); } public EnergyConversionStorage(int capacity, int maxTransfer) { this(capacity, maxTransfer, maxTransfer); }*/ public EnergyConversionStorage(int capacity) { super(capacity); //this.capacity = capacity; //this.maxReceive = maxReceive; //this.maxExtract = maxExtract; } //RF API @Override public int receiveEnergy(ItemStack container, int maxReceive, boolean simulate) { return receiveEnergy(maxReceive, simulate); } @Override public int extractEnergy(ItemStack container, int maxExtract, boolean simulate) { return extractEnergy(maxExtract, simulate); } @Override public int getEnergyStored(ItemStack container) { return getEnergyStored(); } @Override public int getMaxEnergyStored(ItemStack container) { return getMaxEnergyStored(); } } ItemFluxpack: Reveal hidden contents package tonius.simplyjetpacks.item.rewrite; import tonius.simplyjetpacks.SimplyJetpacks; import tonius.simplyjetpacks.capability.CapabilityProviderEnergy; import tonius.simplyjetpacks.capability.EnergyConversionStorage; import tonius.simplyjetpacks.client.model.PackModelType; import tonius.simplyjetpacks.client.util.RenderUtils; import tonius.simplyjetpacks.config.Config; import tonius.simplyjetpacks.item.IHUDInfoProvider; import tonius.simplyjetpacks.setup.FuelType; import tonius.simplyjetpacks.setup.ModCreativeTab; import tonius.simplyjetpacks.setup.ModEnchantments; import tonius.simplyjetpacks.setup.ModItems; import tonius.simplyjetpacks.util.EquipmentSlotHelper; import tonius.simplyjetpacks.util.NBTHelper; import tonius.simplyjetpacks.util.SJStringHelper; import cofh.api.energy.IEnergyContainerItem; import net.minecraft.client.model.ModelBiped; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.EntityEquipmentSlot; import net.minecraft.item.EnumRarity; import net.minecraft.item.Item; import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.DamageSource; import net.minecraft.util.math.MathHelper; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; import net.minecraftforge.common.ISpecialArmor; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import java.util.List; public class ItemFluxpack extends ItemArmor implements ISpecialArmor, /*IEnergyContainerItem,*/ IHUDInfoProvider { public boolean hasFuelIndicator = true; public boolean hasStateIndicators = true; public FuelType fuelType = FuelType.ENERGY; public boolean isFluxBased = false; public boolean showTier = true; public static final String TAG_ENERGY = "Energy"; public static final String TAG_ON = "PackOn"; public String name; private final int numItems; public ItemFluxpack(String name) { super(ArmorMaterial.IRON, 2, EntityEquipmentSlot.CHEST); this.name = name; this.setUnlocalizedName(SimplyJetpacks.PREFIX + name); this.setHasSubtypes(true); this.setMaxDamage(0); this.setCreativeTab(ModCreativeTab.instance); this.setRegistryName(name); numItems = Fluxpack.values().length; } @Override @SideOnly(Side.CLIENT) public void getSubItems(Item item, CreativeTabs creativeTabs, List<ItemStack> List) { if (ModItems.integrateEIO) { for (Fluxpack pack : Fluxpack.ALL_FLUXPACKS) { ItemStack stack; if (pack.usesFuel) { List.add(new ItemStack(item, 1, pack.ordinal())); } else { stack = new ItemStack(item, 1, pack.ordinal()); if (item instanceof ItemFluxpack) { ((ItemFluxpack) item).addFuel(stack, ((ItemFluxpack) item).getMaxEnergyStored(stack), false); } List.add(stack); } } } else { Fluxpack pack = Fluxpack.CREATIVE_FLUXPACK; ItemStack stack; if (pack.usesFuel) { List.add(new ItemStack(item, 1, pack.ordinal())); } else { stack = new ItemStack(item, 1, pack.ordinal()); if (item instanceof ItemFluxpack) { ((ItemFluxpack) item).addFuel(stack, ((ItemFluxpack) item).getMaxEnergyStored(stack), false); } List.add(stack); } } } @Override public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) { } @Override public void onArmorTick(World world, EntityPlayer player, ItemStack stack) { if(this.isOn(stack)) { this.chargeInventory(player, stack, this); } } public void toggleState(boolean on, ItemStack stack, String type, String tag, EntityPlayer player, boolean showInChat) { NBTHelper.setBoolean(stack, tag, !on); if (player != null && showInChat) { String color = on ? TextFormatting.RED.toString() : TextFormatting.GREEN.toString(); type = type != null && !type.equals("") ? "chat." + this.name + "." + type + ".on" : "chat." + this.name + ".on"; String msg = SJStringHelper.localize(type) + " " + color + SJStringHelper.localize("chat." + (on ? "disabled" : "enabled")); player.addChatMessage(new TextComponentString(msg)); } } protected void chargeInventory(EntityLivingBase user, ItemStack stack, ItemFluxpack item) { int i = MathHelper.clamp_int(stack.getItemDamage(), 0, numItems - 1); if(this.fuelType == FuelType.ENERGY) { for(int j = 0; j <= 5; j++) { ItemStack currentStack = user.getItemStackFromSlot(EquipmentSlotHelper.fromSlot(j)); if(currentStack != null && currentStack != stack && currentStack.getItem() instanceof IEnergyContainerItem) { IEnergyContainerItem heldEnergyItem = (IEnergyContainerItem) currentStack.getItem(); if(Fluxpack.values()[i].usesFuel) { int energyToAdd = Math.min(item.useFuel(stack, Fluxpack.values()[i].getFuelPerTickOut(), true), heldEnergyItem.receiveEnergy(currentStack, Fluxpack.values()[i].getFuelPerTickOut(), true)); item.useFuel(stack, energyToAdd, false); heldEnergyItem.receiveEnergy(currentStack, energyToAdd, false); } else { heldEnergyItem.receiveEnergy(currentStack, Fluxpack.values()[i].getFuelPerTickOut(), false); } } } } } @Override public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) { information(stack, this, tooltip); if (SJStringHelper.canShowDetails()) { shiftInformation(stack, tooltip); } else { tooltip.add(SJStringHelper.getShiftText()); } } @SideOnly(Side.CLIENT) @SuppressWarnings("unchecked") public void information(ItemStack stack, ItemFluxpack item, List list) { int i = MathHelper.clamp_int(stack.getItemDamage(), 0, numItems - 1); if (this.showTier) { list.add(SJStringHelper.getTierText(Fluxpack.values()[i].getTier())); } list.add(SJStringHelper.getFuelText(this.fuelType, item.getFuelStored(stack), Fluxpack.values()[i].getFuelCapacity(), !Fluxpack.values()[i].usesFuel)); } @SideOnly(Side.CLIENT) @SuppressWarnings("unchecked") public void shiftInformation(ItemStack stack, List list) { int i = MathHelper.clamp_int(stack.getItemDamage(), 0, numItems - 1); list.add(SJStringHelper.getStateText(this.isOn(stack))); list.add(SJStringHelper.getEnergySendText(Fluxpack.values()[i].getFuelPerTickOut())); if (Fluxpack.values()[i].getFuelPerTickIn() > 0) { list.add(SJStringHelper.getEnergyReceiveText(Fluxpack.values()[i].getFuelPerTickIn())); } SJStringHelper.addDescriptionLines(list, "fluxpack", TextFormatting.GREEN.toString()); } // HUD info @Override @SideOnly(Side.CLIENT) public void addHUDInfo(List<String> list, ItemStack stack, boolean showFuel, boolean showState) { if (showFuel && this.hasFuelIndicator) { list.add(this.getHUDFuelInfo(stack, this)); } if (showState && this.hasStateIndicators) { list.add(this.getHUDStatesInfo(stack)); } } @SideOnly(Side.CLIENT) public String getHUDFuelInfo(ItemStack stack, ItemFluxpack item) { int fuel = item.getFuelStored(stack); int maxFuel = item.getMaxFuelStored(stack); int percent = (int) Math.ceil((double) fuel / (double) maxFuel * 100D); return SJStringHelper.getHUDFuelText(this.name, percent, this.fuelType, fuel); } @SideOnly(Side.CLIENT) public String getHUDStatesInfo(ItemStack stack) { return SJStringHelper.getHUDStateText(null, null, this.isOn(stack)); } public boolean isOn(ItemStack stack) { return NBTHelper.getBooleanFallback(stack, TAG_ON, true); } @Override public EnumRarity getRarity(ItemStack stack) { int i = MathHelper.clamp_int(stack.getItemDamage(), 0, numItems - 1); if (Fluxpack.values()[i].getRarity() != null) { return Fluxpack.values()[i].getRarity(); } return super.getRarity(stack); } @Override public boolean showDurabilityBar(ItemStack stack) { int i = MathHelper.clamp_int(stack.getItemDamage(), 0, numItems - 1); if (!Fluxpack.values()[i].usesFuel) { return false; } return this.hasFuelIndicator; } @Override public double getDurabilityForDisplay(ItemStack stack) { double stored = this.getMaxFuelStored(stack) - this.getFuelStored(stack) + 1; double max = this.getMaxFuelStored(stack) + 1; return stored / max; } @Override public String getUnlocalizedName(ItemStack itemStack) { int i = MathHelper.clamp_int(itemStack.getItemDamage(), 0, numItems - 1); return Fluxpack.values()[i].unlocalisedName; } // fuel public int getFuelStored(ItemStack stack) { return this.getEnergyStored(stack); } public int getMaxFuelStored(ItemStack stack) { return this.getMaxEnergyStored(stack); } public int addFuel(ItemStack stack, int maxAdd, boolean simulate) { int energy = this.getEnergyStored(stack); int energyReceived = Math.min(this.getMaxEnergyStored(stack) - energy, maxAdd); if (!simulate) { energy += energyReceived; NBTHelper.setInt(stack, TAG_ENERGY, energy); } return energyReceived; } public int useFuel(ItemStack stack, int maxUse, boolean simulate) { int energy = this.getEnergyStored(stack); int energyExtracted = Math.min(energy, maxUse); if (!simulate) { energy -= energyExtracted; NBTHelper.setInt(stack, TAG_ENERGY, energy); } return energyExtracted; } @Override public int receiveEnergy(ItemStack container, int maxReceive, boolean simulate) { int i = MathHelper.clamp_int(container.getItemDamage(), 0, numItems - 1); int energy = this.getEnergyStored(container); int energyReceived = Math.min(this.getMaxEnergyStored(container) - energy, Math.min(maxReceive, Fluxpack.values()[i].getFuelPerTickIn())); if (!simulate) { energy += energyReceived; NBTHelper.setInt(container, TAG_ENERGY, energy); } return energyReceived; } @Override public int extractEnergy(ItemStack container, int maxExtract, boolean simulate) { int i = MathHelper.clamp_int(container.getItemDamage(), 0, numItems - 1); int energy = this.getEnergyStored(container); int energyExtracted = Math.min(energy, Math.min(maxExtract, Fluxpack.values()[i].getFuelPerTickOut())); if (!simulate) { energy -= energyExtracted; NBTHelper.setInt(container, TAG_ENERGY, energy); } return energyExtracted; } /* @Override public int getEnergyStored(ItemStack container) { return NBTHelper.getInt(container, TAG_ENERGY); } @Override public int getMaxEnergyStored(ItemStack container) { int i = MathHelper.clamp_int(container.getItemDamage(), 0, numItems - 1); return Fluxpack.values()[i].getFuelCapacity(); }*/ public int getMaxEnergyStored(ItemStack armor) { int i = MathHelper.clamp_int(armor.getItemDamage(), 0, numItems - 1); return Fluxpack.values()[i].getFuelCapacity(); } public int getEnergyStored(ItemStack container) { return NBTHelper.getInt(container, TAG_ENERGY); } @Override public ArmorProperties getProperties(EntityLivingBase player, ItemStack armor, DamageSource source, double damage, int slot) { int i = MathHelper.clamp_int(armor.getItemDamage(), 0, numItems - 1); if (Fluxpack.values()[i].getIsArmored() && !source.isUnblockable()) { if (this.isFluxBased && source.damageType.equals("flux")) { return new ArmorProperties(0, 0.5D, Integer.MAX_VALUE); } int energyPerDamage = this.getFuelPerDamage(armor); int maxAbsorbed = energyPerDamage > 0 ? 25 * (this.getFuelStored(armor) / energyPerDamage) : 0; return new ArmorProperties(0, 0.85D * (Fluxpack.values()[i].getArmorReduction() / 20.0D), maxAbsorbed); } return new ArmorProperties(0, 1, 0); } @Override public int getArmorDisplay(EntityPlayer player, ItemStack armor, int slot) { int i = MathHelper.clamp_int(armor.getItemDamage(), 0, numItems - 1); if (Fluxpack.values()[i].getIsArmored()) { if (this.getFuelStored(armor) >= Fluxpack.values()[i].getArmorFuelPerHit()) { return Fluxpack.values()[i].getArmorReduction(); } } return 0; } @Override public ModelBiped getArmorModel(EntityLivingBase entityLiving, ItemStack itemStack, EntityEquipmentSlot armorSlot, ModelBiped _default) { int i = MathHelper.clamp_int(itemStack.getItemDamage(), 0, numItems - 1); if (Config.enableArmor3DModels) { ModelBiped model = RenderUtils.getArmorModel(Fluxpack.values()[i], entityLiving); if (model != null) { return model; } } return super.getArmorModel(entityLiving, itemStack, armorSlot, _default); } @Override public String getArmorTexture(ItemStack stack, Entity entity, EntityEquipmentSlot slot, String type) { int i = MathHelper.clamp_int(stack.getItemDamage(), 0, numItems - 1); String flat = Config.enableArmor3DModels || Fluxpack.values()[i].armorModel == PackModelType.FLAT ? "" : ".flat"; return SimplyJetpacks.RESOURCE_PREFIX + "textures/armor/" + Fluxpack.values()[i].getBaseName() + flat + ".png"; } @Override public void damageArmor(EntityLivingBase entity, ItemStack armor, DamageSource source, int damage, int slot) { int i = MathHelper.clamp_int(armor.getItemDamage(), 0, numItems - 1); if (Fluxpack.values()[i].getIsArmored() && Fluxpack.values()[i].usesFuel) { if (this.fuelType == FuelType.ENERGY && this.isFluxBased && source.damageType.equals("flux")) { this.addFuel(armor, damage * (source.getEntity() == null ? Fluxpack.values()[i].getArmorFuelPerHit() / 2 : this.getFuelPerDamage(armor)), false); } else { this.useFuel(armor, damage * this.getFuelPerDamage(armor), false); } } } // armor protected int getFuelPerDamage(ItemStack stack) { int i = MathHelper.clamp_int(stack.getItemDamage(), 0, numItems - 1); if (ModEnchantments.fuelEffeciency == null) { return Fluxpack.values()[i].getArmorFuelPerHit(); } int fuelEfficiencyLevel = MathHelper.clamp_int(EnchantmentHelper.getEnchantmentLevel(ModEnchantments.fuelEffeciency, stack), 0, 4); return (int) Math.round(Fluxpack.values()[i].getArmorFuelPerHit() * (5 - fuelEfficiencyLevel) / 5.0D); } @Override public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt) { return new CapabilityProviderEnergy<>(new EnergyConversionStorage(1000), CapabilityEnergy.ENERGY, null); } public void registerItemModel() { for (int i = 0; i < numItems; i++) { SimplyJetpacks.proxy.registerItemRenderer(this, i, Fluxpack.getTypeFromMeta(i).getBaseName()); } } } Edited April 16, 2017 by Tomson124 Adjusted Spoilers Quote
Choonster Posted April 17, 2017 Posted April 17, 2017 The RF API requires you to implement IEnergyContainerItem on an Item, it doesn't support capabilities. Why does EnergyConversionStorage have an IEnergyStorage field? If you're delegating the IEnergyStorage methods to the IEnergyContainerItem methods, EnergyConversionStorage should only implement IEnergyStorage without extending EnergyStorage. It should have an ItemStack field so it can call the IEnergyContainerItem methods on the Item. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Tomson124 Posted April 17, 2017 Author Posted April 17, 2017 On 4/17/2017 at 7:57 AM, Choonster said: The RF API requires you to implement IEnergyContainerItem on an Item, it doesn't support capabilities. Why does EnergyConversionStorage have an IEnergyStorage field? If you're delegating the IEnergyStorage methods to the IEnergyContainerItem methods, EnergyConversionStorage should only implement IEnergyStorage without extending EnergyStorage. It should have an ItemStack field so it can call the IEnergyContainerItem methods on the Item. Expand The field was just a remaing piece of code from an earlier attempt... I now changed the EnergyConversionStorage like you said: Reveal hidden contents package tonius.simplyjetpacks.capability; import cofh.api.energy.IEnergyContainerItem; import net.minecraft.item.ItemStack; import net.minecraftforge.energy.IEnergyStorage; public class EnergyConversionStorage implements IEnergyStorage { public ItemStack itemStack; public IEnergyContainerItem itemRF; public EnergyConversionStorage(IEnergyContainerItem itemRF, ItemStack itemStack) { this.itemStack = itemStack; this.itemRF = itemRF; } @Override public int receiveEnergy(int maxReceive, boolean simulate) { return itemRF.receiveEnergy(itemStack, maxReceive, simulate); } @Override public int extractEnergy(int maxExtract, boolean simulate) { return itemRF.extractEnergy(itemStack, maxExtract, simulate); } @Override public int getEnergyStored() { return itemRF.getEnergyStored(itemStack); } @Override public int getMaxEnergyStored() { return itemRF.getMaxEnergyStored(itemStack); } @Override public boolean canExtract() { return true; } @Override public boolean canReceive() { return true; } } And I adjusted the initCapabilities accordingly: @Override public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt) { return new CapabilityProviderEnergy<>(new EnergyConversionStorage(this, stack), CapabilityEnergy.ENERGY, null); } But when I start minecraft, or more specifically when I load a world it crashes with the error: Reveal hidden contents Description: Loading entity NBT java.lang.IllegalArgumentException: Can not deserialize to an instance that isn't the default implementation at net.minecraftforge.energy.CapabilityEnergy$1.readNBT(CapabilityEnergy.java:30) at net.minecraftforge.energy.CapabilityEnergy$1.readNBT(CapabilityEnergy.java:19) at net.minecraftforge.common.capabilities.Capability.readNBT(Capability.java:99) at tonius.simplyjetpacks.capability.CapabilityProviderEnergy.deserializeNBT(CapabilityProviderEnergy.java:49) at net.minecraftforge.common.capabilities.CapabilityDispatcher.deserializeNBT(CapabilityDispatcher.java:133) at net.minecraft.item.ItemStack.setItem(ItemStack.java:1087) at net.minecraft.item.ItemStack.readFromNBT(ItemStack.java:221) at net.minecraft.item.ItemStack.loadItemStackFromNBT(ItemStack.java:113) at net.minecraft.entity.player.InventoryPlayer.readFromNBT(InventoryPlayer.java:640) at net.minecraft.entity.player.EntityPlayer.readEntityFromNBT(EntityPlayer.java:958) at net.minecraft.entity.player.EntityPlayerMP.readEntityFromNBT(EntityPlayerMP.java:205) at net.minecraft.entity.Entity.readFromNBT(Entity.java:1923) at net.minecraft.server.management.PlayerList.readPlayerDataFromFile(PlayerList.java:332) at net.minecraft.server.management.PlayerList.initializeConnectionToPlayer(PlayerList.java:122) at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:263) at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.access$100(NetworkDispatcher.java:73) at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:212) at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:309) at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:197) at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:807) at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:688) at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:537) at java.lang.Thread.run(Thread.java:745) Quote
Choonster Posted April 17, 2017 Posted April 17, 2017 (edited) On 4/17/2017 at 12:30 PM, Tomson124 said: java.lang.IllegalArgumentException: Can not deserialize to an instance that isn't the default implementation at net.minecraftforge.energy.CapabilityEnergy$1.readNBT(CapabilityEnergy.java:30) at net.minecraftforge.energy.CapabilityEnergy$1.readNBT(CapabilityEnergy.java:19) at net.minecraftforge.common.capabilities.Capability.readNBT(Capability.java:99) at tonius.simplyjetpacks.capability.CapabilityProviderEnergy.deserializeNBT(CapabilityProviderEnergy.java:49) Expand The IStorage for the energy capability (used by Capability#readNBT) can only desrialise from NBT into an instance of EnergyStorage. This is a general rule for most capabilities: The IStorage is only guaranteed to work with the capability's default implementation. If you're using a custom implementation, you'll need to read from/write to NBT yourself (or use that implementation's methods if it implements INBTSerializable). Since your IEnergyStorage implementation isn't actually storing the energy value itself, the ICapabilityProvider doesn't need to implement INBTSerializable (or ICapabilitySerializable, which is simply a combination of the two interfaces). Edited April 17, 2017 by Choonster 1 Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Tomson124 Posted April 17, 2017 Author Posted April 17, 2017 Thanks Now it works... kind of... I did the Forge Energy implementation because EnderIO items can only be charged by Forge Energy or TESLA, even after the implementation my fluxpacks don't charge EnderIO items I don't know why... Is there a way to check if a capability is loaded correctly? And I assume that I still can use the "normal" IEnergyContainerItem methods for energy usage, as I delegated them to the Capability in my EnergyConversionStorage Quote
Choonster Posted April 17, 2017 Posted April 17, 2017 (edited) On 4/17/2017 at 1:04 PM, Tomson124 said: Thanks Now it works... kind of... I did the Forge Energy implementation because EnderIO items can only be charged by Forge Energy or TESLA, even after the implementation my fluxpacks don't charge EnderIO items I don't know why... Is there a way to check if a capability is loaded correctly? Expand You've exposed the IEnergyStorage capability on your own item, which allows other mods to insert/extract energy from it; but are you accessing the IEnergyStorage capability on the items you're trying to charge? Quote And I assume that I still can use the "normal" IEnergyContainerItem methods for energy usage, as I delegated them to the Capability in my EnergyConversionStorage Expand Yes, they'll still work. Edited April 17, 2017 by Choonster Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Tomson124 Posted April 17, 2017 Author Posted April 17, 2017 On 4/17/2017 at 1:13 PM, Choonster said: You've exposed the IEnergyStorage capability on your own item, which allows other mods to insert/extract energy from it; but are you accessing the IEnergyStorage capability on the items you're trying to charge? Expand I assume that I don't do that, as I did no changes to the Item classe except overriding the initCapabilities... I thought I access the IEnergyStorage through the IEnergyContainerItem methods as they are delegated to the ForgeEnergy Capability, but probably I am understanding something wrong here. How would I access the IEnergyStorage then? By using the ForgeEnergy methods? If so, how would I use them? just returning the IEnergyContainerITem method or calculating it again? Quote
Choonster Posted April 17, 2017 Posted April 17, 2017 On 4/17/2017 at 1:20 PM, Tomson124 said: I assume that I don't do that, as I did no changes to the Item classe except overriding the initCapabilities... I thought I access the IEnergyStorage through the IEnergyContainerItem methods as they are delegated to the ForgeEnergy Capability, but probably I am understanding something wrong here. How would I access the IEnergyStorage then? By using the ForgeEnergy methods? If so, how would I use them? just returning the IEnergyContainerITem method or calculating it again? Expand All you've done is expose your own item's energy storage through the Forge Energy API, with an IEnergyStorage that calls the IEnergyContainerItem methods. This doesn't affect the energy stored in other items. When you charge another item, you'll need to get its IEnergyStorage through the ItemStack's ICapabilityProvider methods and use IEnergyStorage#receiveEnergy to add energy to it. If it doesn't have one, check if it's an instance of IEnergyContainerItem and use the corresponding method to add energy to it. If it doesn't implement IEnergyContainerItem, To simplify your code, you may want to create a method to get an IEnergyStorage from an ItemStack that does what I said above but returns an instance of EnergyConversionStorage if the item doesn't have an IEnergyStorage and implements IEnergyContainerItem. This way you can use IEnergyStorage#receiveEnergy regardless of which API the item supports. You can use either API to extract energy from the Flux Pack (since you know they both use the same value). 1 Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Tomson124 Posted April 17, 2017 Author Posted April 17, 2017 Thank you so much it is working now I wrote a small method which returns the IEnergyStorage of the item which should be charged: public IEnergyStorage getIEnergyStorage(ItemStack chargeItem) { if (chargeItem.hasCapability(CapabilityEnergy.ENERGY, null)) { return chargeItem.getCapability(CapabilityEnergy.ENERGY, null); } else if (chargeItem.getItem() instanceof IEnergyContainerItem) { return new EnergyConversionStorage((IEnergyContainerItem) chargeItem.getItem(), chargeItem); } return null; } I am reffering to that method when charging the Items, and it works perfectly fine. Just have to see if it also works with Items that do not implement ForgeEnergy, as I am not sure about that part: else if (chargeItem.getItem() instanceof IEnergyContainerItem) { return new EnergyConversionStorage((IEnergyContainerItem) chargeItem.getItem(), chargeItem); } But thank you a lot, I now also have a much better knowledge of Capabilites, too. Thank you for your patience with me ^^ Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.