Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Dealing with single item instances


MistaOmega
 Share

Recommended Posts

Excuse the vague title, I couldn't really describe it short and sweet

spacer.png

Right, basically I'm trying to make an item that can recieve power, for now I'm just adding the power directly every tick here: https://github.com/MistaOmega/Opes/blob/master/src/main/java/mistaomega/opes/items/StarlightInfusedNetherStar.java#L68-L75

 

Problem is, when I remove the item and get a new one, the power remains the same, so the basic question is, is there a way to only handle instances of items rather than every item from within this class, so that if I was to replace this item, the power would be different and not the same as when I removed it.

 

Thank you for any help ❤️

 

Link to comment
Share on other sites

Override Item#initCapabilities. In there, return a new ICapabilityProvider, essentially a wrapper for the getCapability method. Then, perform ItemStack#getCapability to query the ICapabilityProvider from the Item class.

Here's an example from my code.

 

@Override
	public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) {
		return new ICapabilityProvider() {
			
			protected IEnergyStorage energy = new IEnergyStorage() {

				@Override
				public int receiveEnergy(int maxReceive, boolean simulate) {

					if (getMaxPower() < maxReceive + getCurrentPower(stack)) {
						maxReceive = getMaxPower() - getCurrentPower(stack);
					}

					if (simulate == false) {
						setCurrentPower(stack, getCurrentPower(stack) + maxReceive);
					}

					return maxReceive;
				}

				@Override
				public int getMaxEnergyStored() {
					return getMaxPower();
				}

				@Override
				public int getEnergyStored() {
					return getCurrentPower(stack);
				}

				@Override
				public int extractEnergy(int maxExtract, boolean simulate) {
					return 0;
				}

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

				@Override
				public boolean canExtract() {
					return false;
				}
			};
			protected LazyOptional<IEnergyStorage> energyHandler = LazyOptional.of(() -> energy);
			
			@Override
			public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
				return this.getCapability(cap);
			}

			@Override
			public <T> LazyOptional<T> getCapability(Capability<T> cap) {
				if (cap == CapabilityEnergy.ENERGY) {
					return energyHandler.cast();
				}

				return  LazyOptional.empty();
			}
		};
	}

 

  • Thanks 1

...

Link to comment
Share on other sites

57 minutes ago, Haydenman2 said:

return LazyOptional.empty();

Don't do this. Return super if the capability isn't the one you care about, you never know who else might attach a capability to a stack.

Scratch that, I was thinking about how things work for TileEntities.

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

Howdy 

You need to store the information inside an ItemStack.  A unique ItemStack is created every time your Item is spawned:  ItemStack is the class used when an Item is actually "created" - in addition to the Item type, it also stores the item count (eg a stack of 46 planks), "damage" (eg pickaxe "wear-and-tear" indicator), and several other pieces of information

 

The easiest way to do that is NBT, for  a working example see here

https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe11_item_variants

or here

https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe12_item_nbt_animate

 

Like the other folks said, you can use a Capability but that is considerably more complicated / difficult to understand and isn't necessary unless you want other mods to be able to read/control the power of your item without knowing what kind of item it is.

 

-TGG

 

  • Thanks 1
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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.