Jump to content

Recommended Posts

Posted

I'm trying to add some extra data to items and I have been using NBT data for that in the past, though I was wondering if I should be using capabilities now since I'm using 1.12. However, I have a few questions about it. The capability stores all of the information within itself, and not on the itemstack's NBT, right? If that's the case, that would seem to mess up how I'm overwriting the ItemStack's Attributes. Right now, I am creating an AttributeModifier's NBT taglist which when I apply it to a weapon for instance, it uses my AttributeModifiers instead of the vanilla default one. Though if I'm storing this tag inside the capability, the ItemStack won't know it's there (if I'm thinking correctly).

 

Other than that, it seems like I can store the other information fairly easily. The other question though is actually adding this information to the capability. I can't add the information within the AttachCapabilityEvent because one of what I've read up on, and two because I need an EntityPlayer to be passed in somehow. That leaves me to use events again and I've already had several issues with those which is why I'm looking into setting this stuff up in a capability.

 

In the end, if anyone could provide some more information on how the item capability works and if it will even be a solution to my problem. Thanks!

Developer of Levels and Lost Eclipse

Posted
5 minutes ago, TheXFactor117 said:

I'm trying to add some extra data to items and I have been using NBT data for that in the past, though I was wondering if I should be using capabilities now since I'm using 1.12. However, I have a few questions about it. The capability stores all of the information within itself, and not on the itemstack's NBT, right? If that's the case, that would seem to mess up how I'm overwriting the ItemStack's Attributes. Right now, I am creating an AttributeModifier's NBT taglist which when I apply it to a weapon for instance, it uses my AttributeModifiers instead of the vanilla default one. Though if I'm storing this tag inside the capability, the ItemStack won't know it's there (if I'm thinking correctly).

 

Other than that, it seems like I can store the other information fairly easily. The other question though is actually adding this information to the capability. I can't add the information within the AttachCapabilityEvent because one of what I've read up on, and two because I need an EntityPlayer to be passed in somehow. That leaves me to use events again and I've already had several issues with those which is why I'm looking into setting this stuff up in a capability.

 

In the end, if anyone could provide some more information on how the item capability works and if it will even be a solution to my problem. Thanks!

If you use the Forge provided ItemStack sensitive version of getAttributeModifiers you get an ItemStack parameter. With an ItemStack parameter you can use ItemStack#getCapability in which you could get the data. And in what way would you need the player? The AttachCapabilityEvent is indeed the way to register it.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted (edited)

Yes I am using the ItemStack sensitive, so I think that solves that issue.

 

I need the player for some of the information I'm setting. For example, the player has a level and whenever the player creates/gets a new weapon, that weapon should have the same level as the player. I know I need to use AttachCapabilityEvent to register the capability, but could I use that to setup the data for the item?

Edited by TheXFactor117

Developer of Levels and Lost Eclipse

Posted

You could get the player in many ways, you could have it linked to a certain character upon creation (Item#onCreated) which could be reset by placing it back into a crafting table (Custom IRecipe implementation) or on right click (Item#onItemRightClick) you could bind it, or even as soon as it is held in the players hand via Item#onUpdate.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Yes, but what about for vanilla items? I don't have access to those methods. I'm trying to add information to my own weapons + vanilla's weapons (and other mods weapons should everything be compatible). The problem is I don't know where I would setup all this information - I read something where you couldn't add information to the capability in the AttachCapabilityEvent. If that is the case, where would I do it for vanilla items? Events don't seem to be the place because half of them don't work as intended (e.g. you can't use ItemCraftedEvent to update NBT because that's not what it's designed for).

Developer of Levels and Lost Eclipse

Posted

You can most definitely attach NBT data or Capability Data to ItemStack in the PlayerEvent.ItemCraftedEvent. Then there is the PlayerInteractEvent if you want to do a Right Click bind, ItemCraftedEvent for when it is crafted, or the TickEvent.PlayerTickEvent for when the item is held.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

I don't understand how you can use ItemCraftedEvent to add NBT based on this. Whenever I try the NBT is never saved/added correctly. I was hoping capabilities would be able to solve this but it doesn't look like. Just so I'm understanding things right, you can't add capability data when you attach the capability?

Developer of Levels and Lost Eclipse

Posted
6 minutes ago, TheXFactor117 said:

I don't understand how you can use ItemCraftedEvent to add NBT based on this. Whenever I try the NBT is never saved/added correctly. I was hoping capabilities would be able to solve this but it doesn't look like. Just so I'm understanding things right, you can't add capability data when you attach the capability?

Ahhh, shift clicking the item out of the crafting table doesn't work while editing the result through the event. This is a problem with the Container#mergeItemStack because it uses ItemStack#copy and places that into the new slot or just modifies the current slot with a matching item. This can be 'fixed' by overriding the Items using substitution, using the Right Click/Player Tick, or whenever the vanilla ContainerWorkbench opens replace it with your own version that overrides the mergeItemStack method and applies the data to the stack when it comes out of the result slot or even using a custom Slot for the result instead of the vanilla one which will allow you to modify the Stack any way you want.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Attaching Capabilities is just like registering an Item pretty much. You are telling the game that the Entity, ItemStack, etc will have it. When this is called the object does't exist. So data cannot be saved to it. The data needs to be added specifically to each ItemStack in this case and is different between all of them. The Capability is not saved to the Item for this reason as they as well as Blocks are singletons.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Thanks, that's what I was thinking. So I'll still need events to set everything up...which makes me wonder whether or not it is actually worth converting everything to a capability.

Developer of Levels and Lost Eclipse

Posted

Ok - I'll look into implementing it anyways. Though a quick question regarding setting the values: what's the best way to set these values up? I don't want to use the PlayerTickEvent because I don't feel comfortable iterating through every player's inventory every tick to update information. ItemCraftedEvent doesn't really work (though I might not even need to set stuff up by crafting), I think there's an OpenContainer event which would handle things spawned in chests, maybe there are other events that would do this in a better way?

Developer of Levels and Lost Eclipse

Posted

You could use the PlayerTickEvent and only check the held item(s offhand maybe?) and armor slots if necessary, you do not need to iterate through the whole inventory. You could use the PlayerContainerEvent.Open/Close, but that will not do anything unless it opens or closes. You could use the ItemTooltipEvent if you want it to be displayed there and do some packet stuff accompanied with the afformentioned PlayerTickEvent.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Well PlayerTickEvent + specific slots might not work the best, it would require the player to put the item in the right slot before it was setup. Maybe the best option would be to set everything client side through ItemTooltipEvent and just send a packet to the server and set the values that way?

Developer of Levels and Lost Eclipse

Posted

If you do EntityPlayer#inventory.currentItem you get the currently selected slot. Plus ItemTooltipEvent is only fired when the item is hovered over in an inventory.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Yes I know, but that would be slightly annoying from a player's point of view. They get an item but it has the default stuff until they put it in their currently equipped item slot.

Developer of Levels and Lost Eclipse

Posted

That is why you would do both.

11 minutes ago, Animefan8888 said:

You could use the ItemTooltipEvent if you want it to be displayed there and do some packet stuff accompanied with the afformentioned PlayerTickEvent.

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Ah I see now - there is a downside to just using the TooltipEvent assuming the player doesn't actually look at the weapon...

 

There honestly just needs to be an event for when an item is added into a player inventory. Make my life a hell of a lot easier. Though would looping through the current item on PlayerTick cause any noticeable effects? I'm assuming the entire inventory would cause issues potentially or is the scale of that just not even noticeable nowadays with the computers we have?

Developer of Levels and Lost Eclipse

Posted

I don't think it would be to noticeable in SinglePlayer, but on a server with several people it could cause some problems depending on the server. Considering it is (36 + 4(if armor)) * numPlayers * 20 iterations every second. Then possibly multiple conditional checks. If done right it wouldn't make much of an impact, though doing the above would be significantly better.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

I'll do the above, though I've been having issues with my PlayerTickEvent being run twice on the server thread during Phase.START and it's kinda screwing the generation process up.

Developer of Levels and Lost Eclipse

Posted (edited)
@SubscribeEvent
	public void onPlayerTick(PlayerTickEvent event)
	{
		if (event.phase == Phase.START)
		{
			if (!event.player.getEntityWorld().isRemote)
			{	
				if (event.player.inventory.getCurrentItem().getItem() instanceof ItemSword && !NBTHelper.loadStackNBT(event.player.inventory.getCurrentItem()).hasKey("Level"))
				{
					ItemGenerator.createWeapon(event.player.inventory.getCurrentItem(), event.player);
				}
			}
		}
	}

 

and the createWeapon method...

 

public static void createWeapon(ItemStack stack, EntityPlayer player)
	{
		NBTTagCompound nbt = NBTHelper.loadStackNBT(stack);
		IPlayerInformation playerInfo = player.getCapability(CapabilityPlayerInformation.PLAYER_INFORMATION, null);

		if (playerInfo != null)
		{
			if (Rarity.getRarity(nbt) == Rarity.DEFAULT)
			{
				Rarity.setRarity(nbt, Rarity.getRandomRarity(nbt, player.getEntityWorld().rand)); // sets a random rarity
				
				MineHackSlash.LOGGER.info("Rarity: " + Rarity.getRarity(nbt));
				nbt.setInteger("HideFlags", 6); // hides Attribute Modifier and Unbreakable tags
				nbt.setInteger("Level", 1); // set level to current player level
				setRandomAttributes(stack, nbt, Rarity.getRarity(nbt), player);
				setAttributeModifiers(nbt, stack, player);
				stack.setStackDisplayName(Rarity.getRarity(nbt).getColor() + stack.getDisplayName());
				NBTHelper.saveStackNBT(stack, nbt);
			}
		}
	}

 

My output gets run twice on the server thread. I'm checking for Phase.Start and making sure we're on the server side. However, if I call createWeapon from anything other than PlayerTickEvent, that outputs only one time.

 

Edit: maybe the code I'm trying to run within a tick is taking too long to process? Because if you output world time the time is increasing so it's two separate ticks. It's almost like the event calls my method and calls it again before it can set anything. The Rarity is always set to the first call but later on the method setRandomAttributes uses the rarity to determine how many attributes to apply - this number always uses the second rarity created instead of the first. I'm just confused as to what is happening.

Edited by TheXFactor117

Developer of Levels and Lost Eclipse

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
    • Instructions on how to install newer Java can be found in the FAQ
    • That's just plain wrong... newer versions are much better optimised and start a lot faster than 1.8.9, both Forge and Minecraft itself. Comparing Fabric 1.21 with Forge 1.8 is like comparing apples and oranges... one's brand new and the other's over a decade old.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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