Posted November 6, 20196 yr Hello, I am currently working on a mod similar to TeamCofH's Redstone Arsenal mod (but without being able to empower tools). I am somewhat new to modding; I've created small, simple mods for personal use in the past, but I wanted to try to branch out with this one. So far, I have a basic sword that cannot break or be damaged on use. I've taken the implementation of the IEnergyStorage interface from the sample EnergyStorage class, but every ItemStack of my sword item retains the same amount of energy and depletes even if it wasn't the specific stack used to damage an entity. (I.E. If one stack starts with 320,000 FE and loses 400 FE when damaging an entity, every other stack will also lose 400 FE and contain 319,600 FE). The amount of energy stored in each stack isn't even saved when I close the game and launch it again. This behavior leads me to a few questions: How do I tell Forge that I'm trying to make use of the Energy capability? How do I properly store the amount of energy for any given ItemStack of the sword/axe/pickaxe etc.. (with NBT?), and access it or modify it with the implemented methods (I.E. extractEnergy, getEnergyStored) (I am aware that in order to display the stored energy in the tooltip, I'd have to @Override the addInformation method. I just need to know how to access the energy stored/max capacity to display it for each ItemStack) What else am I missing or not understanding? What should I do to improve my code? I'm sorry if this is a lot to ask, but my modding experience is limited and I've really never dealt with more complicated concepts like this. I tried looking around both these forums and Google to find answers, but as I expected I could find nothing for Minecraft 1.14.4 in particular, or anything in general dealing with both tools and the energy capability. I've also looked through Forge's documentation, but either it was too vague to help me figure out what to do, or I just didn't understand it well enough. I'm not sure where else I should look to start learning more, especially for newer Minecraft versions (1.13+). I would really appreciate any help. Here's my code: public class ChromiumSwordItem extends SwordItem implements IEnergyStorage { protected int energy; protected int capacity; protected int maxReceive; protected int maxExtract; public ChromiumSwordItem() { super( new ChromiumTier(), 0, 6.0f, new Item.Properties() .group(ChromiumArsenal.CREATIVE_TAB) .maxDamage(0) .defaultMaxDamage(0) .setNoRepair() .rarity(Rarity.UNCOMMON) ); setRegistryName("chromium_sword"); capacity = 320000; maxReceive = 400; maxExtract = 320; // energy = Math.max(0 , Math.min(capacity, energy)); // Start all Chromium Swords with maximum energy for testing energy = capacity; } @Override public int receiveEnergy(int maxReceive, boolean simulate) { if (!canReceive()) return 0; int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive)); if (!simulate) energy += energyReceived; return energyReceived; } @Override public int extractEnergy(int maxExtract, boolean simulate) { if (!canExtract()) return 0; int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract)); if (!simulate) energy -= energyExtracted; return energyExtracted; } @Override public int getEnergyStored() { return energy; } @Override public int getMaxEnergyStored() { return capacity; } @Override public boolean canExtract() { return this.maxExtract > 0; } @Override public boolean canReceive() { return this.maxReceive > 0; } // TODO: Override addInformation, hitEntity, durability bar stuff }
November 6, 20196 yr They're all the same because you're storing data in the (singleton) item class. You need to use capabilities. Override Item#initCapabilities and init a capability for the item stack given to you there. https://mcforge.readthedocs.io/en/1.13.x/datastorage/capabilities/ Edited November 6, 20196 yr by Draco18s Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
November 6, 20196 yr Author 2 hours ago, Draco18s said: They're all the same because you're storing data in the (singleton) item class. You need to use capabilities. Override Item#initCapabilities and init a capability for the item stack given to you there. https://mcforge.readthedocs.io/en/1.13.x/datastorage/capabilities/ Thank you for your response. After reading through the docs (again) I'm still not sure how to actually initialize a capability when overriding initCapabilities. This is what I tried: @Nullable @Override public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) { return new ICapabilityProvider() { @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { if (cap == CapabilityEnergy.ENERGY) { // If not new ChromiumSwordItem(), what should I put here? (If that's causing the issue...) return LazyOptional.of(() -> new ChromiumSwordItem()).cast(); } return LazyOptional.empty(); } }; } This is how I displayed the energy inside the tooltip: @Override public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) { super.addInformation(stack, worldIn, tooltip, flagIn); stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> tooltip.add(new TranslationTextComponent(String.valueOf(e.getEnergyStored())))); } This is how I extracted energy: @Override public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker) { // Instead of false, check if the player is in creative stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> e.extractEnergy(maxExtract, false)); return super.hitEntity(stack, target, attacker); } I'm not sure if I'm doing all of this wrong or only parts of it are incorrect, but now instead of extracting energy, when I hit an entity, no energy is extracted. I can't tell if that's because I extracted the energy wrong, I'm displaying it wrong in the tooltip, incorrectly initialized the energy capability, or any mixture of the three. Edited November 6, 20196 yr by Zorochase
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.