Jump to content

Recommended Posts

Posted

Hi, I'm trying to set (reset) a blockstate after a certain amount of ticks. I can't seem to find any examples on how ticks are used in general.

The below method is for an item. If a block is clicked with the item, it changes the block to another one. I want to implement a timer that resets this block to its original state.

 

    @Override
    public InteractionResult useOn(UseOnContext pContext) {
        if (!pContext.getLevel().isClientSide()) {
            BlockPos positionClicked = pContext.getClickedPos();
            BlockState originalBlockState = pContext.getLevel().getBlockState(positionClicked);
            pContext.getLevel().setBlock(pContext.getClickedPos(), Blocks.PUMPKIN.defaultBlockState(), 3);
            // TODO reset to originalBlock after ticks (e.g. 10 seconds)
        }
        return super.useOn(pContext);
    }

 

A little code snippet or a pointer to a few examples would be very helfpful.

Posted (edited)

Normally there are 2 ways to get a block to tick.

Use a BlockEntity (like a furnace keeping track of burn time) or have the block implement random ticking (like crop growth).

You can also use the LevelTickEvent to implement more generic things, but for your usecase you will then have the problem of where to keep track of per block state.

You might for example attach a capability to the LevelChunk that contains the block to hold the data?

See DataStorage here: https://forge.gemwire.uk/wiki/Main_Page

Edited by warjort
  • Thanks 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted
3 hours ago, diesieben07 said:

In general "tick" means a game update, the game updates 20 times every second, so there are 20 ticks every second. If you wait 40 ticks, you have waited 2 seconds (assuming the server thread is not lagging).

You have to remember the position, most likely in a Level capability and then use LevelTickEvent (note that it fires twice every tick) to check if there is a position to reset this tick.

    @SubscribeEvent
    public static void countTicks(TickEvent event){
        if(!ticking) return;
        ticks++;
        if(ticks == 100){
          // do stuff and reset
        }
    }

Ok, thank you. I managed to get it to work with the above code. I could not find `LevelTickEvent` in the codebase. Not sure if its the same as `TickEvent`

Posted (edited)

In 1.18.2 it is called WorldTickEvent

I think if you subscribe to TickEvent you will get all tick events; server, level, player, etc. ? So you will get a lot more events than you want.

 

As described above, you need to look at event.phase (either START or END) otherwise you will be doubling counting.

Similarly, you might want to check event.side if you don't want your code to run on both the logical client and server, usually ticking is only done on the client to support things like animations.

 

Finally, you can't store the ticking/ticks like you are probably doing.

What if there are 2 blocks that you need to tick?

What if somebody restarts the game while it is ticking. What is going to save/reload the ticking/ticks state?

See the comments above about capabilities for how to solve this.

Edited by warjort
  • Thanks 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted
14 minutes ago, warjort said:

In 1.18.2 it is called WorldTickEvent

I think if you subscribe to TickEvent you will get all tick events; server, level, player, etc. ? So you will get a lot more events than you want.

 

As described above, you need to look at event.phase (either START or END) otherwise you will be doubling counting.

Similarly, you might want to check event.side if you don't want your code to run on both the logical client and server, usually ticking is only done on the client to support things like animations.

 

Finally, you can't store the ticking/ticks like you are probably doing.

What if there are 2 blocks that you need to tick?

What if somebody restarts the game while it is ticking. What is going to save/reload the ticking/ticks state?

See the comments above about capabilities for how to solve this.

Hi, I was just responding to your other comment.

Ok, I changed it to this. Is this what you mean?

@SubscribeEvent
public static void countTicks(TickEvent.WorldTickEvent event) {...

On saving the ticks: I know its not the right way and only works for one block. Its meant for testing only at the moment. I will have a read thru the capabilties in the link you provided. I also had a look at the Furnace and BlockEntity, but find it hard to understand how the FurnaceTicker is working. (I'm fairly new to the minecraft codebase).  Anyways thanks for the help.

Posted

Yes for the event name.

 

For the furnace ticker look at BlastFurnaceBlock.getTicker() and how its helper method creates a server side ticker for AbstractFurnaceBlockEntity.serverTick()

The "litTime" in the BlockEntity would be equivalent to your ticks.

  • Thanks 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted
2 hours ago, warjort said:

Yes for the event name.

 

For the furnace ticker look at BlastFurnaceBlock.getTicker() and how its helper method creates a server side ticker for AbstractFurnaceBlockEntity.serverTick()

The "litTime" in the BlockEntity would be equivalent to your ticks.

Ah ok, so in my case, litTime could be hardcoded becuase it is not depending on a fuel type. 

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

    • I've been using this modpack for a few days now with no issues, it was working properly just an hour or two ago and now all of a sudden when I try to load it I get this error: 'The game crashed: exception in server tick loop Error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $' debug log: https://pastebin.com/1htBuhsP crash report: https://pastebin.com/QggHKYDg
    • Everything worked fine until I decided to install supplementaries squared, amendments and a couple other mods like artifacts, and compats for it. I'm having a hard time fixing this, and I get this crash whenever I try to enter a world. Not even a new world is feasible. Any help would be highly appreciated!! https://pastebin.com/7KXYZ6fM
    • Internal Exception: com.google.gson.JsonParseException: Error loading registry data: Unknown element: ResourceKey[minecraft:worldgen/configured_feature / minecraft:crimson_forest_vegetation]   This error is there when i try to join my friends server. We had some mods and it was okay but after 3 days i cant join.
    • It just says the mod i had but i dont have now.. and i didnt even use these mods and it still was crashing with the mods  
    • Hello,   I've been back and forth in emails with some CurseForge support staff on the issue but there's been no progress. In the CurseForge app, I have custom profiles and some public modpacks that will not load no matter what. I've goen through every link sent to me, I've reinstalled Minecraft, reinstalled the launcher, repaired the installations individually, uninstalled and reinstalled the public modpacks, downloaded the latest Java--all of it. But each time, I'm always met with either "java runtime exception", "exit code 1" on the rare occasions Minecraft does launch, or "java is out of date" (which literally cannot be possible since I reinstalled the latest version a few different times.) Making a new profile works, but I want to be able to play the ones I had before.  I had gotten one of the old public Minecraft modpacks to work, the prehistoric one about dinosaurs being featured right now, but then it only worked once. I've tried to retrace my steps and it just would not work. I have genuinely no clue what's going on.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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