Jump to content

[1.12.2] bukkit like scheduler for forge?


JoelGodOfWar

Recommended Posts

Is there a scheduler for the server to run a runnable after a delay? Like the one in bukkit/spigot?

I'm trying to port my plugin to a Forge Serverside Mod. The mod will only do anything on a server.

Any help will be greatly appreciated.

 

For bukkit/spigot I use the following code.

 

Spoiler

		if(player.hasPermission("sps.hermits")||player.hasPermission("sps.op")){
			if(!isCanceled){
				// Below this schedules the runnable to run after a delay
				transitionTask = this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {

					public void run() {
						setDatime(player, world);
					}
				}, 10 * 20); // This waits 10 seconds before running the Runnable.
			}else{
				isCanceled = false;
			}
		}else if(!player.hasPermission("sps.hermits")||player.hasPermission("sps.op")){
			player.sendMessage("" + lang.get("noperm." + daLang + ""));
		}

 

 

I've been out of Forge Modding for nearly 3 years, and decided to try to bring a plugin from bukkit, into forge.

Link to comment
Share on other sites

1 hour ago, JoelGodOfWar said:

Is there a scheduler for the server to run a runnable after a delay?

Not that I know of but you can subscribe to the ServerTickEvent and count the ticks yourself.

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.

Link to comment
Share on other sites

So I may have found a way to work around this.

Is there anything I should change? Several variables are set in the event that would schedule the runnable in bukkit, daTimer2, playerisinbed, etc.

Spoiler

	@SubscribeEvent
	public void servertick(ServerTickEvent event){
		if((daTimer - daTimer2) == 200){
			if(playerisinbed==true){
				if(!isCanceled){
					ITextComponent damsg = new TextComponentString("10s, !isCanceled");
					server.sendMessage(damsg); // Sends message to server terminal not chat.
					broadcast("" + damsg); //Sends message to chat.
				}
			}else{
				daTimer = 0;
			}
		}else if(daTimer > 200){

		}
		
		daTimer++;
	}
	
	public void broadcast(String string){
		
		ITextComponent textComponent = ITextComponent.Serializer.jsonToComponent(string);
		for(EntityPlayerMP playerMP : server.getPlayerList().getPlayers()) {
            try {
				playerMP.sendMessage(TextComponentUtils.processComponent(server, textComponent, playerMP));
			} catch (CommandException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	}

 

 

Link to comment
Share on other sites

2 hours ago, JoelGodOfWar said:

if(playerisinbed==true){

Which player?

 

2 hours ago, JoelGodOfWar said:

}else if(daTimer > 200){ }

By doing this, you're making it so that the timer counts up forever, because unless "the" player is in a bed, on exactly tick #200, you never reset this counter. And oh yeah, daTimer2 never gets set to anything, so you might have to wait a long time for no apparent reason (because if daTimer2 is something huge, and daTimer got reset to 0, you have to wait a while).

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

8 minutes ago, Draco18s said:

Which player?

 

By doing this, you're making it so that the timer counts up forever, because unless "the" player is in a bed, on exactly tick #200, you never reset this counter. And oh yeah, daTimer2 never gets set to anything, so you might have to wait a long time for no apparent reason (because if daTimer2 is something huge, and daTimer got reset to 0, you have to wait a while).

Which player doesn't matter for my reasons, a player is in bed, the variable is set in another function to start the look for the 200 tick delay.

daTimer2 is set to daTimer by the same function as playerisinbed. Should I reset daTimer every now and then?

I just need to know when 200 ticks(10 seconds) have elapsed. And since the servertickevent runs every tick, either I need to reset daTimer constantly, or just let it count up. I've been looking at bukkit's source to try to see if there was a way to make my own scheduler. I'm surprised forge has never added a scheduler, to run code at a certain delay.

I'm trying to port my SinglePlayerSleep plugin over to forge, it will run serverside only, adding nothing if used on clientside.

Link to comment
Share on other sites

10 hours ago, JoelGodOfWar said:

Should I reset daTimer every now and then?

...every time you change daTimer or your timer won't be 200 ticks long.

Otherwise it's going to look like this:

"Hey! Start a timer!"
"How long?"

"I dunno, wait until whatever time makes the condition true."

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:

...every time you change daTimer or your timer won't be 200 ticks long.

Otherwise it's going to look like this:

"Hey! Start a timer!"
"How long?"

"I dunno, wait until whatever time makes the condition true."

What if I start the timer with daTimer being 200, and count down to 0, then until I set it to 200 again, it does nothing?

Do you think that would be more efficient?

I could also add checks so that when daTimer gets higher than 200, or lower than 0, the counter stops adding/subtracting until it is called.

Link to comment
Share on other sites

2 hours ago, JoelGodOfWar said:

Do you think that would be more efficient?

Yes

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

44 minutes ago, Draco18s said:

Yes

How about this? If daTimer is set to -1 do nothing, if daTimer gets below -1 then set it to -1 so it does nothing, if daTimer is set higher than -1 then count down to 0 to run code.

Spoiler

		if(daTimer <= -2){
			daTimer = -1;
		}else if(!(daTimer == -1)){
			daTimer--;
		}else if(daTimer == 0){
			// Timer done run code.
		}

 

 

Link to comment
Share on other sites

Two statements

		if(daTimer == 0){
			// Timer done run code.
			daTimer = -1;
		}else if(daTimer > 0){
			daTimer--;
		}

There's literally no need to set it to -1 if its less than -1, as if it is less than -1, you don't do anything anyway.

  • 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

32 minutes ago, Draco18s said:

Two statements


		if(daTimer == 0){
			// Timer done run code.
			daTimer = -1;
		}else if(daTimer > 0){
			daTimer--;
		}

There's literally no need to set it to -1 if its less than -1, as if it is less than -1, you don't do anything anyway.

That does seem more efficient than my code. Hope my next bit of code goes this smoothly. Thank you.

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



×
×
  • Create New...

Important Information

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