Reorganize basemod ticking to use ITickHandler. Introduce "GAME" tick vs "WORLD" tick for

mods that want to tick per world and "globally"
This commit is contained in:
Christian Weeks committed 2012-06-07 13:53:16 -04:00
1 parent 5acab9e790
commit 856fa4f337
13 files changed
+197 -147

No files matched your search

@@ -25,6 +25,7 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
@@ -266,8 +267,8 @@ public class FMLClientHandler implements IFMLSidedHandler
public void onPreWorldTick()
{
if (client.field_6324_e != null) {
FMLCommonHandler.instance().worldTickStart(client.field_6324_e);
FMLCommonHandler.instance().tickStart(TickType.WORLDGUI, 0.0f, client.field_6313_p);
// For the client world ticks and game ticks are the same
FMLCommonHandler.instance().tickStart(EnumSet.of(TickType.WORLD,TickType.GAME,TickType.WORLDGUI), 0.0f, client.field_6324_e, client.field_6313_p);
}
}
@@ -277,8 +278,8 @@ public class FMLClientHandler implements IFMLSidedHandler
public void onPostWorldTick()
{
if (client.field_6324_e != null) {
FMLCommonHandler.instance().worldTickEnd(client.field_6324_e);
FMLCommonHandler.instance().tickEnd(TickType.WORLDGUI, 0.0f, client.field_6313_p);
// For the client world ticks and game ticks are the same
FMLCommonHandler.instance().tickEnd(EnumSet.of(TickType.WORLD,TickType.GAME,TickType.WORLDGUI), 0.0f, client.field_6324_e, client.field_6313_p);
}
for (IKeyHandler entry : keyHandlers)
{
@@ -294,21 +295,18 @@ public class FMLClientHandler implements IFMLSidedHandler
loadTextures(fallbackTexturePack);
firstTick = false;
}
FMLCommonHandler.instance().tickStart(TickType.WORLDLOADTICK);
FMLCommonHandler.instance().tickStart(TickType.GUILOADTICK);
FMLCommonHandler.instance().tickStart(EnumSet.of(TickType.WORLDLOAD,TickType.GUILOAD));
}
}
public void onRenderTickStart(float partialTickTime)
{
FMLCommonHandler.instance().tickStart(TickType.RENDER, partialTickTime);
FMLCommonHandler.instance().tickStart(TickType.GUI, partialTickTime, client.field_6313_p);
FMLCommonHandler.instance().tickStart(EnumSet.of(TickType.RENDER,TickType.GUI), partialTickTime, client.field_6313_p);
}
public void onRenderTickEnd(float partialTickTime)
{
FMLCommonHandler.instance().tickEnd(TickType.RENDER, partialTickTime);
FMLCommonHandler.instance().tickEnd(TickType.GUI, partialTickTime, client.field_6313_p);
FMLCommonHandler.instance().tickEnd(EnumSet.of(TickType.RENDER,TickType.GUI), partialTickTime, client.field_6313_p);
}
/**
* Get the server instance
+3 -3
View File
@@ -44,13 +44,13 @@ public abstract class BaseMod implements cpw.mods.fml.common.modloader.BaseMod
Minecraft mc = (Minecraft) minecraftInstance;
boolean hasWorld = mc.field_6324_e != null;
// World and render ticks
if (((tickEnd && tick==TickType.WORLD) || (tickEnd && tick==TickType.RENDER)) && hasWorld) {
if (((tickEnd && tick==TickType.GAME) || (tickEnd && tick==TickType.RENDER)) && hasWorld) {
return onTickInGame((Float) data[0], mc);
} else if (((tickEnd && tick==TickType.WORLDGUI) || (tickEnd && tick==TickType.GUI))) {
return onTickInGUI((Float) data[0], mc, (GuiScreen)data[1]);
} else if (tick == TickType.WORLDLOADTICK && hasWorld) {
} else if (tick == TickType.WORLDLOAD && hasWorld) {
return onTickInGame(0.0f, mc);
} else if (tick == TickType.GUILOADTICK) {
} else if (tick == TickType.GUILOAD) {
return onTickInGUI(0.0f, mc, mc.field_6313_p);
}
return true;
@@ -20,6 +20,7 @@ import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -89,78 +90,38 @@ public class FMLCommonHandler
sidedDelegate = handler;
}
/**
* Pre-tick the mods
*/
public void worldTickStart(Object world)
public void tickStart(EnumSet<TickType> ticks, Object ... data)
{
tickStart(TickType.WORLD,0.0f, world);
}
/**
* Post-tick the mods
*/
public void worldTickEnd(Object world)
{
tickEnd(TickType.WORLD,0.0f);
}
public void tickStart(TickType type, Object ... data)
{
sidedDelegate.profileStart("modTickStart");
sidedDelegate.profileStart(type.name());
for (ModContainer mod : Loader.getModList())
{
sidedDelegate.profileStart(mod.getName());
mod.tickStart(type, data);
sidedDelegate.profileEnd();
}
for (ModContainer mod : auxilliaryContainers)
{
sidedDelegate.profileStart(mod.getMod().getClass().getSimpleName());
mod.tickStart(type, data);
sidedDelegate.profileEnd();
}
sidedDelegate.profileStart("modTickStart$"+ticks);
for (ITickHandler ticker : tickHandlers)
{
if (ticker.ticks().contains(type))
EnumSet<TickType> ticksToRun = EnumSet.copyOf(ticker.ticks());
ticksToRun.removeAll(EnumSet.complementOf(ticks));
if (!ticksToRun.isEmpty())
{
sidedDelegate.profileStart(ticker.getClass().getSimpleName());
ticker.tickStart(type, data);
sidedDelegate.profileStart(ticker.getLabel());
ticker.tickStart(ticksToRun, data);
sidedDelegate.profileEnd();
}
}
sidedDelegate.profileEnd();
sidedDelegate.profileEnd();
}
public void tickEnd(TickType type, Object ... data)
public void tickEnd(EnumSet<TickType> ticks, Object ... data)
{
sidedDelegate.profileStart("modTickEnd");
sidedDelegate.profileStart(type.name());
for (ModContainer mod : Loader.getModList())
{
sidedDelegate.profileStart(mod.getName());
mod.tickEnd(type, data);
sidedDelegate.profileEnd();
}
for (ModContainer mod : auxilliaryContainers)
{
sidedDelegate.profileStart(mod.getMod().getClass().getSimpleName());
mod.tickEnd(type, data);
sidedDelegate.profileEnd();
}
sidedDelegate.profileStart("modTickEnd$"+ticks);
for (ITickHandler ticker : tickHandlers)
{
if (ticker.ticks().contains(type))
EnumSet<TickType> ticksToRun = EnumSet.copyOf(ticker.ticks());
ticksToRun.removeAll(EnumSet.complementOf(ticks));
if (!ticksToRun.isEmpty())
{
sidedDelegate.profileStart(ticker.getClass().getSimpleName());
ticker.tickEnd(type, data);
sidedDelegate.profileStart(ticker.getLabel());
ticker.tickEnd(ticksToRun, data);
sidedDelegate.profileEnd();
}
}
sidedDelegate.profileEnd();
sidedDelegate.profileEnd();
}
public List<IKeyHandler> gatherKeyBindings() {
@@ -112,17 +112,6 @@ public class FMLModContainer implements ModContainer
// TODO Auto-generated method stub
return null;
}
@Override
public void tickStart(TickType type, Object ... data)
{
// TODO Auto-generated method stub
}
@Override
public void tickEnd(TickType type, Object ... data)
{
// TODO Auto-generated method stub
}
@Override
public boolean matches(Object mod)
@@ -31,17 +31,23 @@ public interface ITickHandler
/**
* Called at the "start" phase of a tick
*
* Multiple ticks may fire simultaneously- you will only be called once with all the firing ticks
*
* @param type
* @param tickData
*/
public void tickStart(TickType type, Object... tickData);
public void tickStart(EnumSet<TickType> type, Object... tickData);
/**
* Called at the "end" phase of a tick
*
* Multiple ticks may fire simultaneously- you will only be called once with all the firing ticks
*
* @param type
* @param tickData
*/
public void tickEnd(TickType type, Object... tickData);
public void tickEnd(EnumSet<TickType> type, Object... tickData);
/**
* Returns the list of ticks this tick handler is interested in receiving at the minute
@@ -49,4 +55,10 @@ public interface ITickHandler
* @return
*/
public EnumSet<TickType> ticks();
/**
* A profiling label for this tick handler
* @return
*/
public String getLabel();
}
@@ -82,14 +82,6 @@ public interface ModContainer
* Move to the next mod state
*/
void nextState();
/**
* A tick has started
*/
void tickStart(TickType tick, Object ... data);
/**
* A tick has ended
*/
void tickEnd(TickType tick, Object ... data);
/**
* Does this mod match the supplied mod?
* @param mod
+15 -11
View File
@@ -19,10 +19,9 @@ import java.util.EnumSet;
public enum TickType {
/**
* Fired during the world evaluation loop
* server and client side
* server side
*
* arg 0 : a float zero (the "render time")
* arg 1 : The world that is ticking
* arg 0 : The world that is ticking
*/
WORLD,
/**
@@ -48,13 +47,18 @@ public enum TickType {
* client side
* Fired once as the world loads from disk
*/
WORLDLOADTICK,
WORLDLOAD,
/**
* client side
* Fired once as the world loads from disk
* arg 0 : the open gui
*/
GUILOADTICK;
GUILOAD,
/**
* client and server side
* Fired once per "global tick loop"
*/
GAME;
/**
* Partner ticks that are also cancelled by returning false from onTickInGame
@@ -63,12 +67,12 @@ public enum TickType {
*/
public EnumSet<TickType> partnerTicks()
{
if (this==WORLD) return EnumSet.of(RENDER,WORLDLOADTICK);
if (this==RENDER) return EnumSet.of(WORLD, WORLDLOADTICK);
if (this==GUI) return EnumSet.of(WORLDGUI, GUILOADTICK);
if (this==WORLDGUI) return EnumSet.of(GUI, GUILOADTICK);
if (this==WORLDLOADTICK) return EnumSet.of(WORLD, RENDER);
if (this==GUILOADTICK) return EnumSet.of(GUI, WORLDGUI);
if (this==WORLD) return EnumSet.of(RENDER,WORLDLOAD);
if (this==RENDER) return EnumSet.of(WORLD, WORLDLOAD);
if (this==GUI) return EnumSet.of(WORLDGUI, GUILOAD);
if (this==WORLDGUI) return EnumSet.of(GUI, GUILOAD);
if (this==WORLDLOAD) return EnumSet.of(WORLD, RENDER);
if (this==GUILOAD) return EnumSet.of(GUI, WORLDGUI);
return null;
}
}
@@ -0,0 +1,93 @@
/*
* The FML Forge Mod Loader suite.
* Copyright (C) 2012 cpw
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package cpw.mods.fml.common.modloader;
import java.util.EnumSet;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;
/**
* @author cpw
*
*/
public class BaseModTicker implements ITickHandler
{
private BaseMod mod;
private EnumSet<TickType> ticks;
BaseModTicker(BaseMod mod)
{
this.mod = mod;
this.ticks = EnumSet.noneOf(TickType.class);
}
BaseModTicker(EnumSet<TickType> ticks)
{
this.ticks = ticks;
}
@Override
public void tickStart(EnumSet<TickType> types, Object... tickData)
{
tickBaseMod(types, false, tickData);
}
@Override
public void tickEnd(EnumSet<TickType> types, Object... tickData)
{
tickBaseMod(ticks, true, tickData);
}
private void tickBaseMod(EnumSet<TickType> types, boolean end, Object... tickData)
{
for (TickType type : types)
{
if (!ticks.contains(type))
{
continue;
}
boolean keepTicking=mod.doTickInGame(type, end, FMLCommonHandler.instance().getMinecraftInstance(), tickData);
if (!keepTicking) {
ticks.remove(type);
ticks.removeAll(type.partnerTicks());
}
}
}
@Override
public EnumSet<TickType> ticks()
{
return ticks;
}
@Override
public String getLabel()
{
return mod.getClass().getSimpleName();
}
/**
* @param mod2
*/
public void setMod(BaseMod mod)
{
this.mod = mod;
}
}
@@ -32,7 +32,8 @@ public class ModLoaderHelper
public static void updateStandardTicks(BaseMod mod, boolean enable, boolean useClock)
{
ModLoaderModContainer mlmc = findOrBuildModContainer(mod);
EnumSet<TickType> ticks = mlmc.getTickTypes();
BaseModTicker ticker = mlmc.getTickHandler();
EnumSet<TickType> ticks = ticker.ticks();
// If we're enabled and we don't want clock ticks we get render ticks
if (enable && !useClock && FMLCommonHandler.instance().getSide().isClient()) {
ticks.add(TickType.RENDER);
@@ -47,14 +48,14 @@ public class ModLoaderHelper
}
if (enable) {
ticks.add(TickType.WORLDLOADTICK);
ticks.add(TickType.WORLDLOAD);
}
}
public static void updateGUITicks(BaseMod mod, boolean enable, boolean useClock)
{
ModLoaderModContainer mlmc = findOrBuildModContainer(mod);
EnumSet<TickType> ticks = mlmc.getTickTypes();
EnumSet<TickType> ticks = mlmc.getTickHandler().ticks();
// If we're enabled and we don't want clock ticks we get render ticks
if (enable && !useClock && FMLCommonHandler.instance().getSide().isClient()) {
ticks.add(TickType.GUI);
@@ -68,7 +69,7 @@ public class ModLoaderHelper
ticks.remove(TickType.WORLDGUI);
}
if (enable) {
ticks.add(TickType.GUILOADTICK);
ticks.add(TickType.GUILOAD);
}
}
@@ -54,7 +54,6 @@ public class ModLoaderModContainer implements ModContainer
private static final ProxyInjector NULLPROXY = new ProxyInjector("","","",null);
private Class <? extends BaseMod > modClazz;
private BaseMod mod;
private EnumSet<TickType> ticks;
private File modSource;
private ArrayList<String> dependencies;
private ArrayList<String> preDependencies;
@@ -64,12 +63,12 @@ public class ModLoaderModContainer implements ModContainer
private SourceType sourceType;
private ModMetadata metadata;
private ProxyInjector sidedProxy;
private BaseModTicker tickHandler;
public ModLoaderModContainer(Class <? extends BaseMod > modClazz, File modSource)
{
this.modClazz = modClazz;
this.modSource = modSource;
this.ticks = EnumSet.noneOf(TickType.class);
// We are unloaded
nextState();
}
@@ -81,7 +80,7 @@ public class ModLoaderModContainer implements ModContainer
ModLoaderModContainer(BaseMod instance) {
FMLCommonHandler.instance().addAuxilliaryModContainer(this);
this.mod=instance;
this.ticks = EnumSet.noneOf(TickType.class);
this.tickHandler = new BaseModTicker(instance);
}
@Override
@@ -101,8 +100,11 @@ public class ModLoaderModContainer implements ModContainer
{
try
{
EnumSet<TickType> ticks = EnumSet.noneOf(TickType.class);
this.tickHandler = new BaseModTicker(ticks);
configureMod();
mod = modClazz.newInstance();
this.tickHandler.setMod(mod);
}
catch (Exception e)
{
@@ -325,30 +327,6 @@ public class ModLoaderModContainer implements ModContainer
mod.modsLoaded();
}
@Override
public void tickStart(TickType tick, Object ... data)
{
if (ticks.contains(tick)) {
boolean keepTicking=mod.doTickInGame(tick, false, FMLCommonHandler.instance().getMinecraftInstance(), data);
if (!keepTicking) {
ticks.remove(tick);
ticks.removeAll(tick.partnerTicks());
}
}
}
@Override
public void tickEnd(TickType tick, Object ... data)
{
if (ticks.contains(tick)) {
boolean keepTicking=mod.doTickInGame(tick, true, FMLCommonHandler.instance().getMinecraftInstance(), data);
if (!keepTicking) {
ticks.remove(tick);
ticks.removeAll(tick.partnerTicks());
}
}
}
@Override
public String getName()
{
@@ -383,11 +361,6 @@ public class ModLoaderModContainer implements ModContainer
return modClazz.isInstance(mod);
}
public void setTickType(EnumSet<TickType> type)
{
this.ticks=EnumSet.copyOf(type);
}
/**
* Find all the BaseMods in the system
* @param <A>
@@ -601,14 +574,6 @@ public class ModLoaderModContainer implements ModContainer
return mod;
}
/**
* @return
*/
public EnumSet<TickType> getTickTypes()
{
return ticks;
}
/**
* @param keyHandler
* @param allowRepeat
@@ -709,4 +674,12 @@ public class ModLoaderModContainer implements ModContainer
{
mod.keyBindingEvent(keybinding);
}
/**
* @return
*/
public BaseModTicker getTickHandler()
{
return this.tickHandler;
}
}
@@ -26,7 +26,15 @@
this.field_6033_f = new ServerConfigurationManager(this);
this.field_6028_k[0] = new EntityTracker(this, 0);
this.field_6028_k[1] = new EntityTracker(this, -1);
@@ -487,7 +492,9 @@
@@ -447,6 +452,7 @@
long var1 = System.nanoTime();
ArrayList var3 = new ArrayList();
Iterator var4 = field_6037_b.keySet().iterator();
+ FMLServerHandler.instance().onPreServerTick();
while (var4.hasNext())
{
@@ -487,7 +493,9 @@
this.field_6033_f.func_28169_a(new Packet4UpdateTime(var7.func_22080_k()), var7.field_4272_q.field_6165_g);
}
@@ -36,3 +44,11 @@
while (true)
{
@@ -533,6 +541,7 @@
this.field_48076_G = Packet.field_48101_l;
this.field_48082_x[this.field_9014_h % 100] = Packet.field_48102_m - this.field_48077_H;
this.field_48077_H = Packet.field_48102_m;
+ FMLServerHandler.instance().onPostServerTick();
}
public void func_6010_a(String p_6010_1_, ICommandListener p_6010_2_)
@@ -18,6 +18,7 @@ import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Properties;
import java.util.Random;
@@ -54,6 +55,7 @@ import cpw.mods.fml.common.ModMetadata;
import cpw.mods.fml.common.ProxyInjector;
import cpw.mods.fml.common.ReflectionHelper;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.TickType;
import cpw.mods.fml.common.modloader.ModLoaderModContainer;
import cpw.mods.fml.common.modloader.ModProperty;
import cpw.mods.fml.common.registry.FMLRegistry;
@@ -145,12 +147,21 @@ public class FMLServerHandler implements IFMLSidedHandler
Loader.instance().initializeMods();
}
public void onPreServerTick()
{
FMLCommonHandler.instance().tickStart(EnumSet.of(TickType.GAME));
}
public void onPostServerTick()
{
FMLCommonHandler.instance().tickEnd(EnumSet.of(TickType.GAME));
}
/**
* Every tick just before world and other ticks occur
*/
public void onPreWorldTick(World world)
{
FMLCommonHandler.instance().worldTickStart(world);
FMLCommonHandler.instance().tickStart(EnumSet.of(TickType.WORLD), world);
}
/**
@@ -158,7 +169,7 @@ public class FMLServerHandler implements IFMLSidedHandler
*/
public void onPostWorldTick(World world)
{
FMLCommonHandler.instance().worldTickEnd(world);
FMLCommonHandler.instance().tickEnd(EnumSet.of(TickType.WORLD), world);
}
/**
+1 -1
View File
@@ -37,7 +37,7 @@ public abstract class BaseMod implements cpw.mods.fml.common.modloader.BaseMod
@Override
public final boolean doTickInGame(TickType tick, boolean tickEnd, Object minecraftInstance, Object... data)
{
if (tick==TickType.WORLD && tickEnd) {
if (tick==TickType.GAME && tickEnd) {
return onTickInGame((MinecraftServer)minecraftInstance);
} else {
return true;