001    /*
002     * The FML Forge Mod Loader suite.
003     * Copyright (C) 2012 cpw
004     *
005     * 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
006     * Software Foundation; either version 2.1 of the License, or any later version.
007     *
008     * 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
009     * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
010     *
011     * 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
012     * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
013     */
014    
015    package cpw.mods.fml.common.modloader;
016    
017    import java.util.EnumSet;
018    
019    import cpw.mods.fml.common.FMLCommonHandler;
020    import cpw.mods.fml.common.ITickHandler;
021    import cpw.mods.fml.common.TickType;
022    
023    /**
024     * @author cpw
025     *
026     */
027    public class BaseModTicker implements ITickHandler
028    {
029    
030        private BaseModProxy mod;
031        private EnumSet<TickType> ticks;
032        private boolean clockTickTrigger;
033        private boolean sendGuiTicks;
034    
035    
036        BaseModTicker(BaseModProxy mod, boolean guiTicker)
037        {
038            this.mod = mod;
039            this.ticks = EnumSet.of(TickType.WORLDLOAD);
040            this.sendGuiTicks = guiTicker;
041        }
042    
043        BaseModTicker(EnumSet<TickType> ticks, boolean guiTicker)
044        {
045            this.ticks = ticks;
046            this.sendGuiTicks = guiTicker;
047        }
048    
049        @Override
050        public void tickStart(EnumSet<TickType> types, Object... tickData)
051        {
052            tickBaseMod(types, false, tickData);
053        }
054    
055        @Override
056        public void tickEnd(EnumSet<TickType> types, Object... tickData)
057        {
058            tickBaseMod(types, true, tickData);
059        }
060    
061        private void tickBaseMod(EnumSet<TickType> types, boolean end, Object... tickData)
062        {
063            if (FMLCommonHandler.instance().getSide().isClient() && ( ticks.contains(TickType.CLIENT) || ticks.contains(TickType.WORLDLOAD)))
064            {
065                EnumSet cTypes=EnumSet.copyOf(types);
066                if ( ( end && types.contains(TickType.CLIENT)) || types.contains(TickType.WORLDLOAD))
067                {
068                    clockTickTrigger =  true;
069                    cTypes.remove(TickType.CLIENT);
070                    cTypes.remove(TickType.WORLDLOAD);
071                }
072    
073                if (end && clockTickTrigger && types.contains(TickType.RENDER))
074                {
075                    clockTickTrigger = false;
076                    cTypes.remove(TickType.RENDER);
077                    cTypes.add(TickType.CLIENT);
078                }
079    
080                sendTick(cTypes, end, tickData);
081            }
082            else
083            {
084                sendTick(types, end, tickData);
085            }
086        }
087    
088        private void sendTick(EnumSet<TickType> types, boolean end, Object... tickData)
089        {
090            for (TickType type : types)
091            {
092                if (!ticks.contains(type))
093                {
094                    continue;
095                }
096    
097                boolean keepTicking=true;
098                if (sendGuiTicks)
099                {
100                    keepTicking = mod.doTickInGUI(type, end, tickData);
101                }
102                else
103                {
104                    keepTicking = mod.doTickInGame(type, end, tickData);
105                }
106                if (!keepTicking) {
107                    ticks.remove(type);
108                    ticks.removeAll(type.partnerTicks());
109                }
110            }
111        }
112    
113        @Override
114        public EnumSet<TickType> ticks()
115        {
116            return (clockTickTrigger ? EnumSet.of(TickType.RENDER) : ticks);
117        }
118    
119        @Override
120        public String getLabel()
121        {
122            return mod.getClass().getSimpleName();
123        }
124    
125        public void setMod(BaseModProxy mod)
126        {
127            this.mod = mod;
128        }
129    }