Jump to content

[1.15.2] How do I create an item with power?


TallYate

Recommended Posts

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.

Link to comment
Share on other sites

@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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"));
		}
	}
}


 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

@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>)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements




×
×
  • Create New...

Important Information

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