Jump to content

Recommended Posts

Posted

Hey guys!

 

So i've written a server tick counter and I'm sure its all correct, but it doesn't seem to be running on the server. Can anyone help me as to figure out why?

 

Here is how I register my code:

@EventHandler
public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(new ServerEvents());
}

 

And here is my code:

import java.util.List;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.ServerTickEvent;

public class ServerEvents
{
int intervalGold = 30;
int intervalCounter = 0;
//*20 for ticks, *60 for minutes
int finalDuration = 1 * 20 * 60;

@SubscribeEvent
public void onServerTick(ServerTickEvent event) {
	if(event.phase == Phase.END) {
		//20 ticks = 1 second - check the counter is finished.
		if(intervalCounter % finalDuration == 0) {
			intervalCounter = 1; //Reset counter
			List<EntityPlayerMP> playerList = MinecraftServer.getServer().getConfigurationManager().playerEntityList;

			for(EntityPlayerMP player : playerList) {
				player.addChatMessage(new ChatComponentText("Hello"));
			}
		} else {
			intervalCounter++;
		}
	}
}
}

Posted

As mentioned, do some basic debugging: put 'System.out.println("Server tick called")' as the first line in your event listener to see if it is actually running, print out the tick counter to see what values it has, etc.

 

Also, if you are going to reset the counter each time it reaches the final duration, use '>=' instead of the modulo '%' operator. There is no point in doing division if the counter simply loops from 1-max over and over again.

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.

×
×
  • Create New...

Important Information

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