Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

2 hours ago, TallYate said:

ifPresent(handler

handler is your capability instance, you don't need to call getCapability inside here.

2 hours ago, TallYate said:

stack.setTag(nbt);

I don't know why you're doing this.

 

2 hours ago, TallYate said:

stack.getCapability

Use ifPresent

  

2 hours ago, TallYate said:

int energy = ...

energy=nbt.getInt("Energy");

Yes, that's it, just throw the value into a local variable, I'm sure nothing could go wrong.

Edited 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.

  • Replies 71
  • Views 13.2k
  • Created
  • Last Reply

Top Posters In This Topic

Most Popular Posts

  • Instead of asking, try it. We'll be here if it doesn't work

  • The forums have been acting oddly, but that's quite an odd result indeed.

  • Author
@Override
	@Nullable
	public CompoundNBT getShareTag(ItemStack stack) {
		CompoundNBT tag = stack.getOrCreateTag();
		stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(handler -> {tag.putInt("Energy", handler.getEnergyStored());});
		return tag;
	}
	
	@Override
	public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt)
    {
        stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(handler -> {nbt.putInt("Energy", handler.getEnergyStored());});
    }

ok I've done this.
I did the local variable thing because I thought it was a reference that I could change.
What am I actually supposed to do in readShareTag?

I'm sure that when you're READING its a great idea to WRITE.

Try READING instead.

Hint, the methods are supposed to be mirrors. In write you get the value FROM the capability, therefor in read you should...

Edited 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.

  • Author

I think I have to do something like
handler.getEnergyStored()=nbt.getInt("Energy");

but getEnergyStored() returns a value and not a variable/reference, and I don't see any other methods to write to the capability.

Edited by TallYate
changed last sentence

Gosh if there was only some other method that accepted a value instead of returning it.

Hint: You can add one to your implementation, even if it is not specified by the interface

Edited 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.

  • Author

stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(handler -> {
handler is an IEnergyStorage, how would I add my own method to that?

You implemented the interface somewhere, yes?

Use polymorphism to your advantage.

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.

  • Author

My Armor Item doesn’t implement IEnergyStorage, I think that came from CapabilityEnergy.ENERGY

23 minutes ago, TallYate said:

My Armor Item doesn’t implement IEnergyStorage, I think that came from CapabilityEnergy.ENERGY

Those are two different things. IEnergyStorage is the interface that defines the storage of the data, CapabilityEnergy.ENERGY is the type of capability.

Anyway, the class I was referring to is one that Forge created (EnergyStorage note the lack of an I on the front), I thought they left it for modders to implement, I was wrong.

 

What I'd do, then, is, extract all the energy that the client side capability thinks it has (just dump it on the ground, all we care about is getting the internal value to zero) then inserting the value received from the nbt data.

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.

  • Author

I put loggers in them, but nothing is logged. Is this normal?

Edited by TallYate
typo

Considering that I don't know what you actually did, I have no idea.

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.

  • Author

Ok so I saw that <T> is based on the capability so I made a class that is basically the same as CapabilityEnergy
 

public class ModEnergy
{
    @CapabilityInject(ModEnergyStorage.class)
    public static Capability<ModEnergyStorage> ENERGY = null;

    public static void register()
    {
        CapabilityManager.INSTANCE.register(ModEnergyStorage.class, new IStorage<ModEnergyStorage>()
        {
            @Override
            public INBT writeNBT(Capability<ModEnergyStorage> capability, ModEnergyStorage instance, Direction side)
            {
                return IntNBT.valueOf(instance.getEnergyStored());
            }

            @Override
            public void readNBT(Capability<ModEnergyStorage> capability, ModEnergyStorage instance, Direction side, INBT nbt)
            {
                if (!(instance instanceof EnergyStorage))
                    throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation");
                instance.setEnergyStored(((IntNBT)nbt).getInt());
            }
        },
        () -> new ModEnergyStorage(10000, 1000, 1000));
    }
}

but it uses ModEnergyStorage
 

public class ModEnergyStorage extends EnergyStorage{

	public ModEnergyStorage(int capacity, int maxReceive, int maxExtract) {
		super(capacity, maxReceive, maxExtract);
	}
	
    public void setEnergyStored(int x)
    {
        this.energy=x;
    }
	
}

for the extra method
and this is the Item Class

public class PowerArmor extends ArmorItem {
	public PowerArmor(IArmorMaterial materialIn, EquipmentSlotType slot, Properties builder) {
		super(materialIn, slot, builder);
	}

	private ModEnergyStorage newStorage() {
		return new ModEnergyStorage(10000, 1000, 1000);
	}

	@Override
	public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
		return new ICapabilityProvider() {
			protected ModEnergyStorage storage = newStorage();
			protected LazyOptional<ModEnergyStorage> storageHolder = LazyOptional.of(() -> storage);

			public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
				if (cap == ModEnergy.ENERGY) {
					return storageHolder.cast();
				}
				return LazyOptional.empty();
			}
		};
	}

	@Override
	public boolean shouldSyncTag() {
		return true;
	}

	

	@Override
	@Nullable
	public CompoundNBT getShareTag(ItemStack stack) {
		CompoundNBT tag = stack.getOrCreateTag();
		stack.getCapability(ModEnergy.ENERGY, null).ifPresent(handler -> {
			tag.putInt("Energy", handler.getEnergyStored());
			CrudeTechMod.log("getEnergy: " + handler.getEnergyStored());
		});
		return tag;
	}

	@Override
	public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) {
		stack.getCapability(ModEnergy.ENERGY, null).ifPresent(handler -> {
			handler.setEnergyStored(nbt.getInt("Energy"));
			CrudeTechMod.log("readEnergy: " + handler.getEnergyStored());
		});

	}

	@Override
	public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip,
			ITooltipFlag flagIn) {
		IEnergyStorage cap = stack.getCapability(ModEnergy.ENERGY, null).orElseGet(null);
		if (cap != null) {
			String charge = Integer.toString(cap.getEnergyStored());
			String capacity = Integer.toString(cap.getMaxEnergyStored());
			tooltip.add(new TranslationTextComponent("Charge: " + charge + "/" + capacity));
		} else {
			tooltip.add(new TranslationTextComponent("missing capability"));
		}
	}
}


 

10 minutes ago, TallYate said:

if (!(instance instanceof EnergyStorage))

This basically can't happen. ModEnergyStorage extends EnergyStorage, so getting a class that is the former and not the latter is impossible.

13 minutes ago, TallYate said:

CrudeTechMod.log("getEnergy: " + handler.getEnergyStored());

Is this getting called? No?
Have you tried figuring out if the parent method is getting called, but skipping over the lambda?

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.

  • Author
@Override
	@Nullable
	public CompoundNBT getShareTag(ItemStack stack) {
		CompoundNBT tag = stack.getOrCreateTag();
		stack.getCapability(ModCapabilityEnergy.ENERGY, null).ifPresent(handler -> {
			tag.putInt("Energy", handler.getEnergyStored());
			CrudeTechMod.log("getEnergy: " + handler.getEnergyStored());
		});
		CrudeTechMod.log("getShareTag end");
		return tag;
	}

Ok I made the capability more like forges and made a ModIEnergyStorage
now getShareTag is being run, but not readShareTag

Also, getShareTag is getting 0 energy
 

Edited by TallYate

  • Author

Oh I've been trying to test the energy using /give ...{Energy:500}
 

public class ModCapabilityEnergy implements INBTSerializable
{
    @CapabilityInject(ModIEnergyStorage.class)
    public static Capability<ModIEnergyStorage> ENERGY = null;

    public static void register()
    {
        CapabilityManager.INSTANCE.register(ModIEnergyStorage.class, new IStorage<ModIEnergyStorage>()
        {
            @Override
            public INBT writeNBT(Capability<ModIEnergyStorage> capability, ModIEnergyStorage instance, Direction side)
            {
            	
            	CrudeTechMod.log("writeNBT " + IntNBT.valueOf(instance.getEnergyStored()));
                return IntNBT.valueOf(instance.getEnergyStored());
            }

            @Override
            public void readNBT(Capability<ModIEnergyStorage> capability, ModIEnergyStorage instance, Direction side, INBT nbt)
            {
                if (!(instance instanceof ModEnergyStorage))
                    throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation");
                ((ModEnergyStorage)instance).energy = ((IntNBT)nbt).getInt();
                CrudeTechMod.log("readNBT " + ((ModEnergyStorage)instance).energy);
            }
        },
        () -> new ModEnergyStorage(1000));
    }

	@Override
	public INBT serializeNBT() {
		
		return null;
	}

	@Override
	public void deserializeNBT(INBT nbt) {
		
	}

}

how would I do serializeNBT since there is no input

Also I see that I'm supposed to do something like this

CapabilityManager.INSTANCE.register(ModIEnergyStorage.class, ModCapabilityEnergy.ENERGY.getStorage(), ModEnergyStorage.class);

but:
The method register(Class<T>, Capability.IStorage<T>, Callable<? extends T>) in the type CapabilityManager is not applicable for the arguments (Class<ModIEnergyStorage>, Capability.IStorage<ModIEnergyStorage>, Class<ModEnergyStorage>)

How do I write the capability to the ItemStack?

  • Author
@Override
	public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
		return new ICapabilityProvider()  {
			protected ModEnergyStorage storage = newStorage();
			protected LazyOptional<ModEnergyStorage> storageHolder = LazyOptional.of(() -> storage);

			public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
				if (cap == ModCapabilityEnergy.ENERGY) {
					return storageHolder.cast();
				}
				return LazyOptional.empty();
			}
		};
	}

How would I make this method in PowerArmor.java implement INBTSerializable, and attach it to the ItemStack?
Or should PowerArmor.java implement INBTSerializable
 

Also, do you know what I am supposed to put in here?

CapabilityManager.INSTANCE.register(ModIEnergyStorage.class, ModCapabilityEnergy.ENERGY.getStorage(), ModEnergyStorage.class);

The method register(Class<T>, Capability.IStorage<T>, Callable<? extends T>) in the type CapabilityManager is not applicable for the arguments (Class<ModIEnergyStorage>, Capability.IStorage<ModIEnergyStorage>, Class<ModEnergyStorage>)

12 minutes ago, TallYate said:

How would I make this method

The method doesn't implement anything. Methods return things. You want the thing the method returns to implement that interface.

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.

  • Author
@SuppressWarnings("null")
	@Override
	public INBT serializeNBT() {
		
		
		DataOutput d = null;
		try {
			d.writeInt(this.energy);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		IntNBT a = null;
		try {
			a.write(d);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return a;
	}

	@Override
	public void deserializeNBT(INBT nbt) {
		this.energy = ((IntNBT)nbt).getInt();
	}

I did serializeNBT and deserializeNBT, and added a thing so when I jump, I get 100 power.
It works, but when I open a chest, the power resets

https://github.com/TallYate/CrudeTechMod/tree/master/src/main/java/me/joshua/crudetechmod

Edited by TallYate
added github

please note if you want to get power from other mods this will not work.

 

no retrun in initCapabilities ICapabilitySerializable

 

  • Author
1 hour ago, loordgek said:

WHAT ??? Suppressing this will not work

ok I made it this:

@Override
	public INBT serializeNBT() {
		return new ModIntNBT(this.energy);
	}

(ModIntNBT is IntNBT but with a public constructor)

 

 

1 hour ago, loordgek said:

no retrun in initCapabilities ICapabilitySerializable

 

What do you mean. It returns storageHolder, which holds a ModEnergyStorage
 

public class ModEnergyStorage implements ModIEnergyStorage, INBTSerializable

 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.