下面列出了怎么用net.minecraftforge.fml.common.gameevent.TickEvent.ServerTickEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@SubscribeEvent
public void onServerTick(ServerTickEvent event)
{
if (event.phase == TickEvent.Phase.END)
{
return;
}
// Once every second
if (++this.serverTickCounter >= 20)
{
this.serverTickCounter = 0;
ChunkLoading.getInstance().tickChunkTimeouts();
}
// This is currently only used for debug tasks, so let's disable it normally
//TaskScheduler.getInstance().runTasks();
this.teleportPlayers();
++this.playerTickCounter;
}
@Override
protected void onServerTick(ServerTickEvent ev)
{
if (this.worldCreated && !this.serverStarted)
{
// The server has started ticking - we can set up its state machine,
// and move on to the next state in our own machine.
this.serverStarted = true;
MalmoMod.instance.initIntegratedServer(currentMissionInit()); // Needs to be done from the server thread.
episodeHasCompleted(ClientState.WAITING_FOR_SERVER_READY);
}
}
@Override
protected void onServerTick(TickEvent.ServerTickEvent ev)
{
try
{
checkForMissionCommand();
}
catch (Exception e)
{
// TODO: What now?
e.printStackTrace();
}
}
@Override
protected void onServerTick(ServerTickEvent ev)
{
if (!ServerStateMachine.this.checkWatchList())
{
// Something has gone wrong - we've lost a connection.
// Need to respond to this, otherwise we'll sit here forever waiting for a client that no longer exists
// to tell us it's finished its mission.
MalmoMod.safeSendToAll(MalmoMessageType.SERVER_ABORT);
episodeHasCompleted(ServerState.ERROR);
}
}
@SubscribeEvent
public void onPostServerTick(ServerTickEvent event){
if(event.phase == Phase.END) {
RailNetworkManager.getServerInstance().onPostServerTick();
ChunkLoadManager.INSTANCE.update();
}
}
/** Subclass should overrride this to act on server ticks.*/
protected void onServerTick(ServerTickEvent ev) {}
@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent ev)
{
// Use the server tick to ensure we regularly update our state (from the server thread)
updateState();
}
@Override
protected void onServerTick(ServerTickEvent ev)
{
// We wait until we start to get server ticks, at which point we assume Minecraft has finished starting up.
episodeHasCompleted(ServerState.DORMANT);
}
@Override
protected void onServerTick(ServerTickEvent ev)
{
if (!ServerStateMachine.this.checkWatchList())
onError(null); // We've lost a connection - abort the mission.
}
@Override
protected void onServerTick(ServerTickEvent ev)
{
if (this.missionHasEnded)
return; // In case we get in here after deciding the mission is over.
if (!ServerStateMachine.this.checkWatchList())
onError(null); // We've lost a connection - abort the mission.
if (ev.phase == Phase.START)
{
// Measure our performance - especially useful if we've been overclocked.
if (this.secondStartTimeMs == 0)
this.secondStartTimeMs = System.currentTimeMillis();
long timeNow = System.currentTimeMillis();
if (timeNow - this.secondStartTimeMs > 1000)
{
long targetTicks = 1000 / TimeHelper.serverTickLength;
if (this.tickCount < targetTicks)
System.out.println("Warning: managed " + this.tickCount + "/" + targetTicks + " ticks this second.");
this.secondStartTimeMs = timeNow;
this.tickCount = 0;
}
this.tickCount++;
}
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
if (ev.phase == Phase.END && getHandlers() != null && getHandlers().worldDecorator != null)
{
World world = server.getEntityWorld();
getHandlers().worldDecorator.update(world);
}
if (ev.phase == Phase.END)
{
if (getHandlers() != null && getHandlers().quitProducer != null && getHandlers().quitProducer.doIWantToQuit(currentMissionInit()))
{
ServerStateMachine.this.quitCode = getHandlers().quitProducer.getOutcome();
onMissionEnded(true);
}
else if (this.runningAgents.isEmpty())
{
ServerStateMachine.this.quitCode = "All agents finished";
onMissionEnded(true);
}
// We need to make sure we keep the weather within mission parameters.
// We set the weather just after building the world, but it's not a permanent setting,
// and apparently there is a known bug in Minecraft that means the weather sometimes changes early.
// To get around this, we reset it periodically.
if (server.getTickCounter() % 500 == 0)
{
EnvironmentHelper.setMissionWeather(currentMissionInit(), server.getEntityWorld().getWorldInfo());
}
}
}
@SubscribeEvent
public void onPreServerTick(ServerTickEvent event){
if(event.phase == Phase.START) {
RailNetworkManager.getServerInstance().onPreServerTick();
}
}