Jump to content

[1.11.2] Conversion Question for Ticks and Misc.


HalestormXV

Recommended Posts

Hello everyone. It has been quite some time since I was in the modding field. I was in it back with 1.7.10 and then stepped out for quite some time and am back in it with 1.11.2 (for now at least, soon I will bump to 1.12). I've gone through all the changes, updated my code and familizared myself with the jump from 1.7.10 to 1.11.2. But let me not divert from my inquiry.

 

I have an item class that can teleport a player on use (the teleporting is still being worked on). The item displays its NBT on the tooltip. Tooltip, Stored Location and Cooldown timer, etc. The cooldown timer is 5 minutes or 6000 ticks. I have a couple of questions about this item.

 

First the method I am using the grab the cooldown and change it to say 5 Min. or 4 Min or 3 Min. etc. is working fine and on the tooltip rather than count down every tick it properly updated to Cooldown: 5 Min. , Cooldown 4 Min. etc. However I noticed that once it reachs 0 it stays at 0 for a bit. I imagine this is because I am getting a remainder in my method. Perhaps a fresh set of eyes can view it and show how I can prevent that from happening. To be quite honest it only stays on 0 Min for a second or 2 and then properly updates the Tooltip to say it is ready for use, so this isn't a major concern if it can't be helepd. 

 

My second and probably more pressing question is how do I make it so that if the player is holding the item in there hand while it is on cooldown it is not constantly "refreshing" in their hand as if they are using it. I know this likely has to do with the onUpdate and with the NBT data changing so perhaps I can cancel the animation somehow? It is quite annoying to see the item constantly changing as the item's cooldown is ticking. 

 

Third and last question. As I indicated, it has been quite some time since I picked up my coder gloves and I only just starting modding with 1.11.2 about two days ago from this post so my optimizations are probably very rusty. Perhaps a brief indication of a better way to optimize the code would be helpful. Perhaps forge has new functions that I am not yet aware of etc. etc. Regardless I like efficient code, so if there are ways to optimize this class I would greatly appreciate some tips. 

 

Now after my long winded paragraph here is the code:

https://pastebin.com/H6A160FQ

Edited by HalestormXV
Link to comment
Share on other sites

I cannot help you with questions 2 or 3 but for 1 possibly try using 

Math.ceil(value)

instead of casting to an integer.  For example this should return one if the value is greater than 0 and less than or equal to 1.

Edited by drok0920
Spelling Mistake
Link to comment
Share on other sites

46 minutes ago, diesieben07 said:

A solution that will solve most of your problems: Don't have an active countdown, instead store the last world time (World::getTotalWorldTime) in the stack and then you can just calculate the difference between that and the current time without ever having to update the stack.

Okay, I can see how that would be better. But on a server it will calulcate the server time and I imagine if a player is logged off and the timer expires once they log back in the tooltip will refresh itself right? This would efectively bind the cooldown to the server's world time, which is fine (my original plan was to make it configurable but configurations arent implemented yet). Effectively the start cooldown would be something like:

this.startCoolDown(stack, world);

 

protected void startCoolDown(ItemStack stack, World world)
{
    if (stack.getTagCompound() != null)
    {
        //stack.getTagCompound().setInteger("coolDown", 6000);
        stack.getTagCompound().setLong("coolDown", world.getTotalWorldTime());
    }
}

 

And then when you use it you would do something along the lines of

long cooldown = this.getCoolDown(stack);
protected int getCoolDown(ItemStack stack) {
    if ( (stack.getTagCompound() != null) && (stack.getTagCompound().hasKey("coolDown")) )
    {
        return stack.getTagCompound().getLong("coolDown");
    }
    return 0;
}

 

Then you would calculate a 5 minute difference between the two and would it be if you reach 0 you can use the item otherwise "It's on cooldown" or if 5 minutes has passed "It's on cooldown"?

 

But what about displaying the remaining time left on the cooldown or does the addTooltip auto-refresh itself on its own, so simply having it display the coolDown tag is enough?

 

Edited by HalestormXV
Link to comment
Share on other sites

The tooltip gets updated every render frame. You can query the current time from the world (getInformation is Side-Only so you can safely use Minecraft.getMinecraft() if necessary) and calculate the duration. 

  • Like 1

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

2 hours ago, Draco18s said:

The tooltip gets updated every render frame. You can query the current time from the world (getInformation is Side-Only so you can safely use Minecraft.getMinecraft() if necessary) and calculate the duration. 

 

I see. Then I could calculate if 6000 ticks have passed (5 minutes) which I can still set to be configurable and if 6000 ticks have passed set offCooldown to true or something like that so you can use the item? Thus cutting out onUpdate completly.

 

EDIT: I am screwing something up roally and it is probably so simple becasue I have been trying to work with this stupid orb forever. Here is the new code: https://pastebin.com/CstGLimw

 

I only have it set to 200 ticks right now for testing purposes. But the issue is that once it calulates using + or - it goes negative.

The debug line reads this: [16:19:44] [Server thread/INFO]: [STDOUT]: The cooldown variable is currently: 8928

Then after you store the information and use the orb once it reads this: [16:19:46] [Server thread/INFO]: [STDOUT]: The cooldown variable is currently: -54521

 

All i am trying to do is check if 200 ticks have passed from intitally using it which is stored in the nbt as the TotalWorldTime, So if we are 200 ticks greater than the intital used time (since a server doesn't go backwards lol) you get to use it again. Like i said, i am overthinking and probably missing the simplest detail.

 

EDIT 2:

Scratch that whole first edit. Looks like I fixed it. Not sure what changed really but it looks like part of my issue was that I wasn't clearing out the tag when trying to append data to it. I thought it would just overwrite it on its own but perhaps I am wrong. Anyway here is the new code that works with a 200 tick buffer.

https://pastebin.com/43PvUJ8A

Debugger

[17:19:19] [Server thread/INFO]: [STDOUT]: Current World Time: 5973
[17:19:19] [Server thread/INFO]: [STDOUT]: Current Stored Time: 5910
[17:19:19] [Client thread/INFO]: [CHAT] §4The Scrying Orb is on cooldown.
	

Last question I have is how I can make this display in "minutes" on the actual tooltip so players know how long of a cooldown remains. Rembmer that 200 tick buffer is going to be 6000 (configurable) which is 5 minutes in real time. So I would need a way to display the 6000 ticks remaining in minutes as a tooltip without using onUpdate since i removed it. I imagining this is where what you said Draco18s comes into play right. Do you have an example of what it might look like?

Edited by HalestormXV
Link to comment
Share on other sites

Math.

Time Left In Ticks = Duration - (Current Time - Start Time)

Time Left in Minutes = Time Left in Ticks / (20 * 60)

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

5 hours ago, Draco18s said:

Math.

Time Left In Ticks = Duration - (Current Time - Start Time)

Time Left in Minutes = Time Left in Ticks / (20 * 60)

Lol I figured that much but thank you. What I should have said was would it be wiser to create a method that calulates this and then call that method in the addToolTip since it updates every render frame, or would it be better to calculate that within the addTooltip segment of the code on its own.

Link to comment
Share on other sites

Function overhead is almost zero.

  • Like 1

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

Got it all squared away. Had to do a little hackery on the cooldown display porton but it now works. By hackery I mean minor adjustments that I am sure have much better ways to accomplish. But everything seems to work as intended. onUpdate is gone completly and the item strictly uses the world timer and the cooldown converts properly to minutes and the tooltip updates effectively and when the item becomes usable the cooldown message changes entirely.

 

Here is the working code: https://pastebin.com/WcEXYnyR

 

I am open to some suggestions but as it stands this seems to work flawlessly, unless someone can spot an error I missed. And ty Draco for the conversion formula and diesieben07 for suggesting the world time.The 3600 timer is hard-coded atm but will eventually be configuarable with a minimum of at least a minute to avoid breaking anything.

Edited by HalestormXV
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

    • okay.. ';D i did my good old technique of troubleshooting modpack (disabling ALL the mods and gradually in parts enabbling them and lauching game, enabling more mods, launching e.t.c) and managed to narrow it down to one mod.... it was "better smithing table" causing all the errors and making several mods to spit errors.. Same way by FIRSTLY turning on "better smithing tables" and seeing which mods it clashed with was: "Doggy Tallents Next" "bartering station" "Curios API" And further i wasn't able to check what mods conflicted b'cuz it was like: it was working with X mods, enabled 10 more mods, crashed. procedurally disabled all of them and still was crashing, disabled some more mods, worked. enabled back mods with which it was crashing and now it wasn't  i can't understand why and what mods are bad but i'm happy i somehow got the main error causer which again was "better smithing table". Without that mod everything works just fine. SOLVED (?)
    • So I'm creating yet another minecraft modpack and stumbled upon error I've never encoutered.. I tried to troubleshoot it myself and it always worked but this time I didn't manage.. Here is minecraft crash report: https://pastebin.com/EVqzdDKg I can't find how or from where to post debug.log  I'm sorry, can someone help me? (as a disclaimer - i've tried already reinstalling minecraft and java)
    • It works without mods, I've ran it through the launcher by itself and runs perfectly fine, when I open it through Forge I can get through to the launcher but when I go to open the world it loads then gives me the error code 1. Is there anymore info that could help diagnose it?
    • Also had the issue. GLAD TO TELL YOU I HAVE THE FIX! Create: Applied Kinetic literally says "Replace all inscriber recipes with Create's sequenced assembly recipe". When I turned off this mod it worked fine. I also didn't use that mod of the pack i played so it didn't matter for me.
    • Right now im trying to make an own mod for minecraft for the version 1.16.5 with forge but whatever i do it still doesnt fix the error this is my build.gradle : buildscript { repositories { maven { url = "https://maven.minecraftforge.net" } mavenCentral() } dependencies { classpath 'net.minecraftforge.gradle:ForgeGradle:5.1.+' } } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'java' group = 'com.example' // Modify to your package name version = '1.0' archivesBaseName = 'flippermod' java { toolchain { languageVersion = JavaLanguageVersion.of(8) } } minecraft { version = "1.16.5-36.2.42" // Ensure this matches your Forge version mappings channel: 'official', version: '1.16.5' runs { client { workingDirectory project.file('run') property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' property 'forge.logging.console.level', 'debug' mods { flipper_mod { sourceSets.main.output } } } } } repositories { maven { url = "https://maven.minecraftforge.net/" } mavenCentral() } dependencies { minecraft "net.minecraftforge:forge:1.16.5-36.2.42" } and this one is my settings.gradle:  pluginManagement { repositories { gradlePluginPortal() maven { name = 'MinecraftForge' url = 'https://maven.minecraftforge.net/' } } } plugins { id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0' } rootProject.name = 'flippermod' this one is the mods.tml    modLoader="javafml" loaderVersion="[36,)" modId="flippermod" version="1.0.0" displayName="Flippermod" and the last one is the gradle-wrapper.properties distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip dc :"code_slivki"
  • Topics

×
×
  • Create New...

Important Information

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