Jump to content

[Solved] Item Lifespan


Rohzek

Recommended Posts

I don't know if this is easily possible, but... Is there anyway to check an item's lifespan while it's in a container, and or get the container it's in assuming the container is in a loaded chunk?

 

The idea would be to make food items that go stale and moldy over a period of time.. and not "age" while in a safe container (like a custom chest... fridge) or in a container with another item (such as ice).

 

Basically: I know I can get the age based on amount of time it spends in a player's inventory and save it to the item's NBT, but is there something similar for general time or time spent in container?

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

So I can get the world time into the NBT on creation with getWorldTime()... Then later get the world time again and say if the new world time is x amount larger than the original... then decay.. That would work for overall decay but what if I wanted to expand/delay the amount of time it takes to decay if it's in a particular container.. or is that just beyond what I can do with Minecraft?

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

There are no such things as limits - you can do whatever you please.

It is matter of design.

 

Using provided tools (NBT) you could save time of creation, then also time of transition to container. Only thing to do then is to update all Items whenever container is opened - update ofc based on stored data. But doing complicated things require you to complicate your code - what if you put in and out your item few times (say freeze it for some time, then unfreeze and again freeze for some time) - at thins point you have to redesign.

 

What I would store:

int decayFactor - say it starts from 0 and goes to x number. Say max will be 48000 tick = two days.

When player has food in inventory you can easily increment this factor.

When item is in world - you can also operate on EntityItem and change NBT.

When item is put into container you save timestamp and leave decayFactor.

int storeTimestamp - moment of transition to container.

When you pull out food from container you calculate based on time passed (currentTime - storeTimestamp) and container type how much should be added to decayFactor.

 

Viola - cut and dried fridge system. :P

 

Note: Keep in mind: 1st - it is a game, moreover Minecraft, it is not supposed to be real; 2nd - don't overcomplicate systems - makes harder to track things down, uses more resources and memory. Better to make "imitation" such as one above.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Okay so right now... I have it so that 100 ticks after creation the item goes stale.. and 100 ticks after that it goes moldy.. It does this by storing time and data to the ItemStack's TagCompound. I'm not sure where to go from here though, how do I transfer/read that from the entityitem and or detect when items are placed into a container?

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

Okay, using a PlayerTickEvent items now degrade correctly when in chests. I can either lower the amount of change or just not check at all I guess, if they are in a "fridge". Cool. Now what I have left is the entity item. I see you can just get the itemstack from the entity item so that shouldn't be too hard to just call on onEntityItemUpdate.  I'm just not quite sure about one thing.. the method I'm using to get the time (world.getWorldTime()) always seems to return a big number. Does it reset to 0.. like when the next day starts? If not how long before it's too big to be cast as int/long/double or whatever I store it in? If so how does that work with the check.. or am I using the wrong type of time?

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

World#getWorldTime

returns the current time of day (so it resets to 0 at the start of the in-game day),

World#getTotalWorldTime

returns the total number of ticks that the world has been active (so it doesn't reset).

 

If you store the time as a long (what it's given to you as), that will last for

Long.MAX_VALUE

(

2^63-1

) ticks; which at 20 ticks per second is equivalent to

1.2810238940076 x 10^14

hours,

5337599558365

days or roughly

14623560433

years. I don't think you need to worry too much about your code running for that long.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

EDIT: I still don't know how to deal with Item Frames.. but my problem my last problem was one number out of place messing the numbers up.

Anyone have any ideas about how to work with item frames? Do I still have access to the entityItem / the itemstack's nbt or something somehow while it's on an item frame?

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

EDIT: I still don't know how to deal with Item Frames.. but my problem my last problem was one number out of place messing the numbers up.

Anyone have any ideas about how to work with item frames? Do I still have access to the entityItem / the itemstack's nbt or something somehow while it's on an item frame?

 

Items in ItemFrames basically don't exist as far as Minecraft is concerned.  There's no code that triggers when an item is placed into the frame (seriously, the item stack isn't told, the item isn't told, and there's no Forge event, and no update methods are called) or when the item frame is broken (although that causes it to drop, and now that the item is an ItemEntity, there is the

onEntityItemUpdate

method, which runs every tick, so you could check there).

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

Hmm. Interesting. I COULD compound things by adding checks to see how long it's been since the update function of the entityitem has ran similar to how I handled the chest..but I'm not sure I care THAT much, at least right now... I guess item frames are free safe storage for my foods then. Thanks. :)

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

I guess item frames are free safe storage for my foods then. Thanks. :)

 

Only sort of.  If you're checking onUpdate how long its been since creation (rather than incrementing a counter every tick) then it's not free.  As soon as the player breaks the item frame and picks up the food, it'll go rotten.

 

But I know what you're thinking, it turned into a kind of "phylactery" effect for my Unique Artifacts mod.  If a player found an item that increased their health (or speed, etc.) they could shove it into an item frame and get the bonus permanently.  Wasn't something I could prevent so I called it a feature.

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

Wasn't something I could prevent so I called it a feature.

Single simple non-disruptive ASM hook in EntityItemFrame ;)

 

I suppose I could, but when it was reported, I looked into existing hooks and didn't find anything and said, "Actually, that effect is really neat, I like it."

 

So yes, with some ASM I could "fix" the bug, but I thought it was a neat trick.  It adds a little something to PvP servers.  If someone finds your base where you've stashed all your stat boost items and breaks them, you're no longer so uber.

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

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

    • Another Minecraft Launcher https://multimc.org/#Download or https://atlauncher.com/downloads
    • Remove these mods and combine OptiFine HD U I6 with Forge 47.2.18 Start with Optifine - without any other mods - if this works, add the mods one by one or in groups until you find the mod, conflicting with Optifine
    • Ok yeah I think this might be where the problem starts. For starters, I think you can get rid of your RecipeType impl and make it a static constant instead. I'm not 100% sure this will fix it but it's posssible. RuneInscribingRecipe public class RuneInscribingRecipe implements Recipe<SimpleContainer> { public static final RecipeType<RuneInscribingRecipe> RECIPE_TYPE = new RecipeType<>(){}; // .... @Override public RecipeType<?> getType() { return RECIPE_TYPE; } // .... } RuneInscriberMenu private Optional<RecipeHolder<RuneInscribingRecipe>> getCurrentRecipe() { SimpleContainer inventory = new SimpleContainer(2); inventory.setItem(BASE_INPUT_SLOT, this.inventory.getStackInSlot(BASE_INPUT_SLOT)); inventory.setItem(TEMPLATE_INPUT_SLOT, this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); List<RecipeHolder<RuneInscribingRecipe>> list = this.blockEntity.getLevel().getRecipeManager().getRecipesFor(RuneInscribingRecipe.RECIPE_TYPE, inventory, this.blockEntity.getLevel()); if (list.isEmpty()) { return Optional.empty(); } return Optional.of(list.get(0)); }   Also, this here is a little suspicious where you're getting the `this.inventory.getStackInSlot` calls. I'd try and make sure those are what you expect they are with either a Debug line in IDE or print statement SimpleContainer inventory = new SimpleContainer(2); inventory.setItem(BASE_INPUT_SLOT, this.inventory.getStackInSlot(BASE_INPUT_SLOT)); System.out.printf("CHECKING BASE INPUT OF RECIPE: %s\n", this.inventory.getStackInSlot(BASE_INPUT_SLOT)); inventory.setItem(TEMPLATE_INPUT_SLOT, this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); System.out.printf("CHECKING TEMPLATE INPUT OF RECIPE: %s\n", this.inventory.getStackInSlot(TEMPLATE_INPUT_SLOT)); ^ See what the result of those print statements are, make sure they're right
    • Upon testing again after adding them, the game still doesn't crash however the resource packs are still unfortunately broken.
    • Hi! Can someone please point me to the correct FAQ or place? I'm not able do DuckDuckGo myself out of this one. As you can hopefully see, chunks are not rendered. I run a forge server, fresh install on a linux VM. Curseforge on the Win11 client with Embeddium, Oculus and Unbound shader. No mods. Forge 47.2.23 on server & client.  No errors, no crashes, no apparent CPU or RAM issues with the VM. This screenshot was captured while creative, but the same happens when flying in survival. Waiting (floating) for a few minutes sooner or (often) later, lets the missing chunks appear. Usually takes 5-10 minutes.  When on a local game, the chunks appear slowly, but they do appear without pause.   This must be something simple, obvious, but I can't find the correct "search words" to find my way out. "Chunks not loading / rendering" didn't help. Browsing several forums neither. 
  • Topics

×
×
  • Create New...

Important Information

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