Jump to content

Recommended Posts

Posted

At first, let me explain why I want to do this.

 

I want to implement food spoilage mechanics in my mod. In theory, I could have used the Item#onUpdate method, but as far as I am aware, this method gets invoked only when the item is carried by some entity. After a while, I came up with an idea, where all ItemStacks having their item instance of ItemFood would be registered in some sort of registry which would be iterated on every world tick (using TickEvent.WorldTickEvent) and have those stacks updated (possibly calling onUpdate with entity parameter null, I don't know yet).

 

So, to my problem. To achieve this, I need to modify the bytecode of the ItemStack class (at very least the constructor) to broadcast a custom defined event across forge event bus (this event would be used to register the stack to the tick handler, but this is irrelevant now). I have studied some ASM and java bytecode for last few days, but I can't see how I would register that class loader. I have also read something about coremods, but all these posts were written for 1.6 versions and the moderators were never too much satisfied with the coremod approach.

 

So, to conclude; Is there any way Forge has those ASM injecting routines handled? If so, can you give me an exaple how to register such class visitor into the forge's framework? Or, if possible, is there a way better approach than that I am suggesting?

 

Many thanks for all your time in advance.

Posted

Its done through a Javascript loading system.

I have no idea how it works.

 

 

You should really try to get this working through events, capabilities, and other non-ASM methods first, though. The main problem you have is not ItemStacks not being ticked, but containers not applying update ticks to their inventory and there's no way to enforce that compliance.

 

But if you want to coremod, feel free to figure out how to do so yourself. Its supported only in the sense that a system is available for those that know how to use it because core modding is a last resort.

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.

Posted (edited)
1 hour ago, Draco18s said:

ItemStacks not being ticked

 

1 hour ago, Draco18s said:

containers not applying update ticks to their inventory

 

Well, after these clues you gave me, I did a quick search and this thread popped up, in which the guy is trying to solve pretty much the same issue, albeit without thinking about bytecode manipulation at all. I think I will have to think up a better way of doing this after all. Anyways, thanks for your time.

 

EDIT:

For whoever is interested in researching this further, I discovered wumple-util has already done this (using IContainerListener). You can look at it here.

Edited by Enginecrafter77
Posted (edited)

There are many things that can go wrong with listening to containers.

I.E. If a mod implements a backpack or a pedestal holding a single ItemStack and the author decided to not use capability, then the system will be exploited.

 

I've experimented with the food-decaying concept a bit in the past, and the approach I used is to store the world time when the food ItemStack is created in its NBT. Then, override onUpdate to check if currentWorldTime - storedWorldTime > decayTime. This way, even if the food is stored in a chest (or any other place that is not trackable by a listener), once the player takes it out, onUpdate will immediately be invoked and check whether the food should decay.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Posted
1 hour ago, DavidM said:

once the player takes it out, onUpdate will immediately be invoked and check whether the food should decay.

Even this can be exploited. I know there is a mod that adds a Lunch Box item from which you can eat from.

 

I like the idea of Attaching a Capability to each ItemStack and using that as a registry, using some sort of "WeakList" implementation to avoid Memory Leaks. Then just iterating over them every world tick. Or some other not performance crippling way. IE every 20 ticks or split up between the ticks.

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
13 minutes ago, diesieben07 said:

Updating every item stack every world tick sounds like a terrible idea.

I assume it would only be ItemStacks that contain Items that are food. And I agree but it is the brute force approach. Also I said you probably shouldn't do it every tick for large numbers of items. Maybe instead store ItemStacks in a WeakHashMap that maps them to their expiration time. Then only update the ItemStack when their expiration has happened.

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
Just now, diesieben07 said:

I am still not sure why storing the world time in the stack and not ticking them every tick won't work here.

8 minutes ago, Animefan8888 said:

Maybe instead store ItemStacks in a WeakHashMap that maps them to their expiration time. Then only update the ItemStack when their expiration has happened.

My new recommendation doesn't update them every tick.

My recommendation involves this.

  1. Capability to store creation time.
  2. When the Capability is Attached in AttachCapabilityEvent<ItemStack> also make sure to add it to a WeakHashMap which will map the Expiration Time to all of the ItemStacks that expire at that time, or ItemStacks to their Expiration Time which ever you want.
  3. Then when the expiration time happens expire all the ItemStacks. Which would likely include removing them from the WeakHashMap.

This way there is no way to get around Items expiring because you are always dealing with the ItemStack.

 

Mind you it is kinda hacky. But this way you can update every ItemStack as you wish and only when they need to. Think of it as a scheduling system.

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.

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.