Jump to content

[new method] GameData should register an item with your own modcontainer?


AXELTOPOLINO

Recommended Posts

Hello everyone,

I'm working on a difficult mod and I'm adding some items in game during the game session.

I know this is highly discouraged, but there's an important reason if I'm doing it.

The problem is that when you register a new item during the game, GameData.newItemAdded() registers it with "Minecraft" as modContainer. Differently I'd like to use my mod's modContainer that I can get via "FMLCommonHandler.instance().findContainerFor(MyModCommon.instance)".

 

For this reason I need a change of the method GameData.newItemAdded(Item item) into GameData.newItemAdded(Item item, ModContainer container), so I can specify the ModContainer.

 

Could you add this feature please?

 

Thanks,

Axel

Link to comment
Share on other sites

  • 4 weeks later...

First of all, thank you diesieben07 for your helpful reply (I have also clicked the "thanks" button :) ),

I have understood everything of your idea and it seems the very best solution, but even if I have coded many mods, I have never used reflection.

Could you link me some documentation about it? As you are right, I need to have access to the private fields.

 

Again thank you!

Axel

 

ps. creating Items mid-game? Look is 90% ready: http://www.minecraftforum.net/topic/1965419-wip162-axels-item-printer-create-your-custom-items-in-game/  ;D

Link to comment
Share on other sites

Could you link me some documentation about it? As you are right, I need to have access to the private fields.

Long answer: http://docs.oracle.com/javase/tutorial/reflect/

Short answer: http://stackoverflow.com/questions/1196192/how-do-i-read-a-private-field-in-java

You don't need to create a new Item instance for stuff like that. Make one Item which uses NBT to store the data.

 

I loved the long answer, I know how to use reflection now :)

 

Anyway you can't use an unique item instance for making more items with different stats. For example, even if there's only one class ItemSword.java, minecraft calls one instance of the wood sword, one for the iron sword, one for the diamond sword, etc. So I think we cannot use only one Item instance for all my custom items...or am I wrong (and I'd really like to be wrong)? :D

 

Thanks dude for your helpful support!

Axel

Link to comment
Share on other sites

Anyway you can't use an unique item instance for making more items with different stats. For example, even if there's only one class ItemSword.java, minecraft calls one instance of the wood sword, one for the iron sword, one for the diamond sword, etc. So I think we cannot use only one Item instance for all my custom items...or am I wrong (and I'd really like to be wrong)? :D

Yes, Mojang does that because vanilla has all the Item ids they want. They can create as many as they want because they don't have to worry about any restrictions. It's their game after all, they can change it if they need to. Modders can't.

Every ItemStack has an NBTTagCompound attached to it which allows you to store basically infinite amount of data into one ItemStack. Vanilla uses this feature for Enchantments, Written Books, etc.

Forge provides various hooks in the Item class to make the Item act dependent on the actual ItemStack being used. For example there is a new version of canHarvestBlock which gets the ItemStack being used as a parameter. Then this method can act differently depending on the Data on the NBTTagCompound of the ItemStack.

 

Anyway you can't use an unique item instance for making more items with different stats. For example, even if there's only one class ItemSword.java, minecraft calls one instance of the wood sword, one for the iron sword, one for the diamond sword, etc. So I think we cannot use only one Item instance for all my custom items...or am I wrong (and I'd really like to be wrong)? :D

Yes, Mojang does that because vanilla has all the Item ids they want. They can create as many as they want because they don't have to worry about any restrictions. It's their game after all, they can change it if they need to. Modders can't.

Every ItemStack has an NBTTagCompound attached to it which allows you to store basically infinite amount of data into one ItemStack. Vanilla uses this feature for Enchantments, Written Books, etc.

Forge provides various hooks in the Item class to make the Item act dependent on the actual ItemStack being used. For example there is a new version of canHarvestBlock which gets the ItemStack being used as a parameter. Then this method can act differently depending on the Data on the NBTTagCompound of the ItemStack.

That's quite interesting, but you cannot see in the creative menu the itemstacks, but only the items, then a new itemstack is created when you pick up an item. So your idea (which is a really smart one) can't be used for making completely different items in inventory. If I'm wrong, please tell me as it would be the final solution for the 32000 items limit :D
Link to comment
Share on other sites

That's quite interesting, but you cannot see in the creative menu the itemstacks, but only the items, then a new itemstack is created when you pick up an item. So your idea (which is a really smart one) can't be used for making completely different items in inventory. If I'm wrong, please tell me as it would be the final solution for the 32000 items limit :D

There are enchanted books in the creative inventory, right? And you can pick them up and they are still enchanted, right?

Now let me tell you that they all have the same ItemId and the same Damage value. The only difference between is their enchantments which are stored in the ItemStack's NBTTagCompound. When the ItemStack is cloned (on pickup from the creative inventory for example) the NBTTagCompound get's cloned, too.

 

You are literally doing a revolution in my brain :D

So, how can I actually register different itemstacks when minecraft loads (I mean as Minecraft actually does), because I'd like to see them in the creative menu like the enchanted books.

 

Are there any troubles using Java reflection also after recompiling? I'm worried about fields name..

 

As always thanks for your time and patience!

Axel

Link to comment
Share on other sites

So, how can I actually register different itemstacks when minecraft loads (I mean as Minecraft actually does), because I'd like to see them in the creative menu like the enchanted books.

Override
getSubItems

in your Item class and add all the ItemStacks you want in the Creative menu to the list you get as the parameter.

Are there any troubles using Java reflection also after recompiling? I'm worried about fields name..

If you are accessing Minecraft fields/methods, yes there are issues because the fields and methods are obfuscated outside of MCP. But if you access FML or Forge stuff you'll be fine.

 

For ItemStacks: wonderful, I'll let you know if it works (as I must have different itemstacks with 2 big arrays, some String values and some Int and Short values. This is why I thought itemStack weren't the right choice for doing something like this mod)

 

For reflection: I have reflected only FML fields now. But for curiosity: how can I know obfuscated names of fields and methods of MC? Should I check .class files and finding the right name or is there a public available list? Reflection would give us the possibility of avoiding base classes edits for modding :D

Link to comment
Share on other sites

I know I'm a bit offtopic, but I'm trying to do what you suggested: using item subtypes instead of several different items.

I just have a question: I must create several types of swords, but if I use subtypes I cannot set different itemMaxDamage values.

 

Is there a way for doing it using subtypes?

 

Thanks in advance,

Axel

Link to comment
Share on other sites

All the weaponDamage does is create a new AttributeModifier on the Item. You can do that via NBT, too. Have a look at ItemStack#getAttributeModifiers where the per-item modifiers get computed. That should give you an idea of the NBT layout for AttributeModifiers.

 

Still working on it, trying to understand the idea correctly. First of all I haven't found the method ItemStack#getAttributeModifiers, is it probably func_111283_C()?

Anyway:

- I put N itemsubtypes using Item#getSubItems increasing the damage value for each subtype

- I can show subtypes in creative inventory thanks to Item#getCreativeTabs

 

But:

1) Which is the itemStack method that let me change the attribute modifiers of that itemstack only?

2) How can I damage an itemstack which uses an item subtype (for example it's a subtype of axe which should have a custom "MaxDamage", different from the other subtypes)

3) How can I add a new custom attribute modifier to the itemstack (like "acid-resistence")?

4) Can the nbtcompound be used for item subtypes that can be stackable?

 

I feel so newbie about this part of Minecraft code, but luckily you are helping us :)

Thank you,

Axel

Link to comment
Share on other sites

Still working on it, trying to understand the idea correctly. First of all I haven't found the method ItemStack#getAttributeModifiers, is it probably func_111283_C()?

Update forge, they updated the MCP mappings lately, it makes things a lot easier.

1) Which is the itemStack method that let me change the attribute modifiers of that itemstack only?

Use ItemStack.stackTagCompound to get the Stack NBT. Then look here: http://www.minecraftwiki.net/wiki/Player.dat_format#Attribute_Modifiers to find out about the format to use.

2) How can I damage an itemstack which uses an item subtype (for example it's a subtype of axe which should have a custom "MaxDamage", different from the other subtypes)

Override getMaxDamage(ItemStack), getDamage(ItemStack) and getDisplayDamage(ItemStack) and return the values you need depending on the NBT of the stack passed in.

3) How can I add a new custom attribute modifier to the itemstack (like "acid-resistence")?
Whenever you would apply acid damage to the player, check if he has your Item and if that item's NBT contains the "acid-resistant" flag.

4) Can the nbtcompound be used for item subtypes that can be stackable?

Items can be stacked if the NBT is equal.

 

Perfect, so I will just edit the Attribute modifier value for having a custom "damage" of the item (decreasing its value for consuming the itemstack), am I right?

 

The thing I wanted to ask in 4) is: Can the subtypes have different MaxStackSize? I really need this feature..

 

I hope my clicks on "thank you" button will be helpful for you, as you really deserve them :)

Axel

 

Edit: I'm stupid, I didn't see ItemStack#getMaxStackSize :D

 

Edit2: ItemStack#getMaxStackSize isn't subtype-sensitive as it calls ItemStack#getItem which is not subtype-sensitive. Is there any solution for that? Thanks :)

Link to comment
Share on other sites

I don't think there is, at least not at the moment. Maybe make a PullRequest, as it's quite a simple change: add a new Method in Item

getMaxStackSize 

but which takes an Itemstack parameter and make the method in ItemStack call that one instead.

 

Yes, I just hoped there was a forge edit already :)

Where should I pull the request? Here in this forge section or on github? Thanks

Link to comment
Share on other sites

Ok, not everything is NBT sensitive and the subitems are all loaded correctly.

 

I think this is almost obvious for you, but I still have one problem: how can I modify the ItemStack damage against EntityLivings if it is stored in the NBT of the itemstack (each subitem has got his own value for this field) ?

 

Thanks!

Axel

Link to comment
Share on other sites

I think this is almost obvious for you, but I still have one problem: how can I modify the ItemStack damage against EntityLivings if it is stored in the NBT of the itemstack (each subitem has got his own value for this field) ?

The same as with the Swords?

 

Swords are different items, they don't have subitems and they use the following method which doesn't seem to be NBT sensitive:

    public Multimap getItemAttributeModifiers()
    {
        Multimap multimap = super.getItemAttributeModifiers();
        multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(field_111210_e, "Weapon modifier", (double)this.weaponDamage, 0));
        return multimap;
    }

 

I don't think there's another method  :-\

Thank you,

Axel

Link to comment
Share on other sites

I have read the section you linked and all related pages, but I still don't get it: what is the NBT sensitive method where I can put an AttributeModifier?

 

As if I have understood correctly, I should put something like:

SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(field_111210_e, "Weapon modifier", itemstack.stackNBTCompound.getDouble("DamageVsEntity)", 0);

 

Did I misunderstand something? Thanks for the patience :)

Link to comment
Share on other sites

I'm going to have to request you people either take it to PMs or make a new topic in Modder Support. You're going off topic.

Read the EAQ before posting! OR ELSE!

 

This isn't building better software, its trying to grab a place in the commit list of a highly visible github project.

 

www.forgeessentials.com

 

Don't PM me, I don't check this account unless I have to.

Link to comment
Share on other sites

I'm going to have to request you people either take it to PMs or make a new topic in Modder Support. You're going off topic.

 

Yes, you're right. I just hoped this was the very last answer as this is my very last problem. If diesieben07 will tell me the answer, a new topic won't be necessary :)

Sorry for messing on the forum,

Axel

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • version used : minecraft forge 1.20.1 Logs : https://bytebin.lucko.me/rtG4EA0vfb hi! so when i try to create a new minecraft world in single player, it just crashes?? does anyone has an idea what could be causing this ?
    • Hey today I was trying to play in 1.21 modpack that I made on fabric (my version of fabric is in 0.16.0) with this list of mods : - Entity Model Features (by traben_0) - Entity Texture Features (by traben_0) - Advanced Chat (by Wesley8081) - Ambient Environment (by Jaredlll08) - Ambient Souds 6 (by CreativeMD) - Animatica (by FoundationGames) - Armor Statues (by Fuzs) - Auto HUD (by Crendgrim) - Bad Packets (by badasintended) - Balm (by BlayTheNinth) - Better Beds (by Motschen) - Better Chunk Loading (by someaddon) - Better Compatibility Checker (by Gaz-) - Better FPS [Render Distance] (by someaddon) - Better Ping Display (by Quintinity) - Better Statistics Screen (by TheCSDev) - Better Armor HUD (by efekos) - Better F3 ( by cominixo) - Blur + (by Motschen) - Bobby (by Johni0702) - Cat Jam (by shmove) - Catalogue (by MrCrayfish) - Chat Heads (by dzwdz) - Cherished Worlds (by TheIllusiveC4) - Chunk Sending (by someaddon) - Chunks Fade In (by kerudion) - Chunky (by pop4959) - Cloth Config (by shedaniel) - Clumps (by Jaredlll08) - Cobweb (CrystalSpider) - Collective (by Serilum) - Configured (by MrCrayfish) - Connectivity (by someaddon) - Continuity (by Pepper_Bell) - Creative Core (by CreativeMD) - Cupboard (by someaddon) - Cut Through (by Fuzs) - Dark Loading Screen (by Neecko5b84) - Debugify (by XanderIsDev) - Double Doors (by Serilum) - Dynamic FPS (by juliand665) - Dynamic Lights (by Tschipcraft) - Easy Anvils (by Fuzs) - Easy Magic (by Fuzs) - Easy Shulker Boxes (by Fuzs) - Eating Animation (by theone-ss) - Enhanced Attack Indicator (by Mineash) - Enhanced Block Entities (by FoundationGames) - Enhanced Visuals (by CreativeMD) - Entity Culling (by tr7zw) - Environmental Water (by AgentMindStorm) - Essential (by SparkUniverse_) - Extra Thicc Packets (by JaselUmena) - Fabric (by modmuss50) - Fabric Language Kotlin (by modmuss50) - Falling Leaves (by RandomMcSomethin) - Fancy Block Particles (by Hantonik) - Fancy Health Bar (by efekos) - Farsight (by someaddon) - Fast IP Ping (by Fallen_Breath) - Ferrite Core (by  malte0811) - Fish On The Line (by Serilum) - Fix GPU Memory Leak  (by someaddon) - Forge Config API Port (by Fuzs) - FPS Reducer (by bre2el) - Freecam (by hashalite) - Fzzy Config (by fzzyhmstrs) - Get It Together (by bl4ckscor3) - Global Packs (by JTK222) - Harvest With Ease (by CrystalSpider) - Ichuntil Util (by iChun) - Immediately Fast (by RaphilMC) - Immersive UI (by SSKirillSS) - Indium (by comp500) - Inventory HUD+ (by dmitrylovin) - Iris Shaders (by coderbot) - Item Physic Full (by CreativeMD) - Jade (by Snownee) - Journey Map (by techbrew) - Just Enough Breeding (by Christofmeg) - Just Enough Items (by mezz) - Just Enough Professions (by Mrbysco) - Language Reload (by Jerozgen) - Leaves Be Gone (by Fuzs) - Let Me Despawn (by frikinjay) - Let Sleeping Dogs Lie (by iChun) - Lithium (by JellySquid) - Loading Backgrounds (by Lupin) - Mineblock's Repeated Utilities (by itsmineblock11) - Menulogue (by MrCrayfish) - Mob Plaques (by Fuzs) - Mod Menu (by Prospector) - Model Gap Fix (by MehVahdJukaar) - Mouse Tweaks (by YaLTeR) - Nether Portal Fix (by BlayTheNinth) - Nicer Skies (by ztereohype) - Noisium (by Steveplays28) - Not Enough Animations (by tr7zw) - Opti GUI (by opekope2) - Packet Fixer (by TonimatasDEV) - Particle Core (by fzzyhmstrs) - Passive Shields (by Serilum) - Pick Up Notifier (by Fuzs) - Ping Wheel (by LukenSkyne) - Puzzle (by Motschen) - Puzzles LIB (by Fuzs) - Reese's Sodium Options (by FlashyReese) - Resource Pack Overrides (by Fuzs) - Server Country Flags (by khajiitos) - Server Performance (by someaddon) - Skin Layer 3D (by tr7zw) - Sodium (by JellySquid) - Sodium Extra (by FlashyReese) - Sodium Shadowy Path Blocks (by Rynnavinx) - Sound Physics Remastered (by henkelmax) - Sounds (by itsmineblock11) - Spark (by lucko) - Stack Refill (by Serilum) - Structure Essentials (by someaddon) - TCD Commons API (by TheCSDev) - Text Placeholder API (by Patbox) - Villager Names (by Serilum) - Visual Overhaul (by Motschen) - Visual Workbench (by Fuzs) - Visuality (by PinkGoosik) - Wakes (by Goby56) - Wavey Capes (by tr7zw) - YDM'S Weapon Master  (by YourDailyModderx) - Yet Another Config LIB (by XanderIsDev) - Zoomify (by XanderIsDev) And When I load it it work, I can create a world and even let him open forever at least I didn't move cause if so the game just crash and tell me this message :   Encountered an unexpected exception net.minecraft.class_148: Ticking entity at net.minecraft.server.MinecraftServer.method_3813(MinecraftServer.java:1025) at net.minecraft.server.MinecraftServer.method_3748(MinecraftServer.java:912) at net.minecraft.class_1132.method_3748(class_1132.java:114) at net.minecraft.server.MinecraftServer.method_29741(MinecraftServer.java:697) at net.minecraft.server.MinecraftServer.method_29739(MinecraftServer.java:281) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.class_4706.isEmpty()" because "tickets" is null at net.minecraft.class_3204.canNoneExpire(class_3204.java:1034) at net.minecraft.class_3204.handler$dln000$lithium$unregisterExpiringTicket(class_3204.java:1075) at net.minecraft.class_3204.method_17645(class_3204.java:172) at net.minecraft.class_3204.method_17292(class_3204.java:194) at net.minecraft.class_3215.method_17300(class_3215.java:443) at com.betterchunkloading.chunk.PlayerChunkData.checkPrediction(PlayerChunkData.java:406) at com.betterchunkloading.chunk.PlayerChunkData.checkDirection(PlayerChunkData.java:353) at com.betterchunkloading.chunk.PlayerChunkData.trackPlayerMovement(PlayerChunkData.java:177) at com.betterchunkloading.chunk.PlayerChunkData.onChunkChanged(PlayerChunkData.java:130) at com.betterchunkloading.event.EventHandler.onPlayerTick(EventHandler.java:200) at net.minecraft.class_3222.handler$zed000$betterchunkloading$onPlayerTick(class_3222.java:2618) at net.minecraft.class_3222.method_5773(class_3222.java:564) at net.minecraft.class_3218.mixinextras$bridge$method_5773$202(class_3218.java) at net.minecraft.class_3218.wrapOperation$eei000$puzzleslib$tickNonPassenger(class_3218.java:7797) at net.minecraft.class_3218.method_18762(class_3218.java:770) at net.minecraft.class_1937.method_18472(class_1937.java:498) at net.minecraft.class_3218.method_31420(class_3218.java:408) at net.minecraft.class_5574.method_31791(class_5574.java:54) at net.minecraft.class_3218.method_18765(class_3218.java:372) at net.minecraft.server.MinecraftServer.method_3813(MinecraftServer.java:1021) ... 5 more Sorry guys if it obvious but actually I'm an old player of Minecraft but I'm new in modding so yup. But Thanks everyone by advance to help me with this.
    • These forums are for Minecraft Forge, not Fabric. You would probably be better served posting this question wherever fabric support is given, most likely their discord server.
    • Add the new latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here
    • Update: I tried to reproduce the bug and found out that if I place a flare on a block and then dismantle it with the pickarang (Quark), the flare gets no NBT Tag after getting assigned an empty one through the offhand.
  • Topics

×
×
  • Create New...

Important Information

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