    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.common.modloader;
    014
    015import java.util.EnumSet;
    016
    017import cpw.mods.fml.common.FMLCommonHandler;
    018import cpw.mods.fml.common.ITickHandler;
    019import cpw.mods.fml.common.TickType;
    020
    021/**
    022 * @author cpw
    023 *
    024 */
    025public class BaseModTicker implements ITickHandler
    026{
    027
    028    private BaseModProxy mod;
    029    private EnumSet<TickType> ticks;
    030    private boolean clockTickTrigger;
    031    private boolean sendGuiTicks;
    032
    033
    034    BaseModTicker(BaseModProxy mod, boolean guiTicker)
    035    {
    036        this.mod = mod;
    037        this.ticks = EnumSet.of(TickType.WORLDLOAD);
    038        this.sendGuiTicks = guiTicker;
    039    }
    040
    041    BaseModTicker(EnumSet<TickType> ticks, boolean guiTicker)
    042    {
    043        this.ticks = ticks;
    044        this.sendGuiTicks = guiTicker;
    045    }
    046
    047    @Override
    048    public void tickStart(EnumSet<TickType> types, Object... tickData)
    049    {
    050        tickBaseMod(types, false, tickData);
    051    }
    052
    053    @Override
    054    public void tickEnd(EnumSet<TickType> types, Object... tickData)
    055    {
    056        tickBaseMod(types, true, tickData);
    057    }
    058
    059    private void tickBaseMod(EnumSet<TickType> types, boolean end, Object... tickData)
    060    {
    061        if (FMLCommonHandler.instance().getSide().isClient() && ( ticks.contains(TickType.CLIENT) || ticks.contains(TickType.WORLDLOAD)))
    062        {
    063            EnumSet cTypes=EnumSet.copyOf(types);
    064            if ( ( end && types.contains(TickType.CLIENT)) || types.contains(TickType.WORLDLOAD))
    065            {
    066                clockTickTrigger =  true;
    067                cTypes.remove(TickType.CLIENT);
    068                cTypes.remove(TickType.WORLDLOAD);
    069            }
    070
    071            if (end && clockTickTrigger && types.contains(TickType.RENDER))
    072            {
    073                clockTickTrigger = false;
    074                cTypes.remove(TickType.RENDER);
    075                cTypes.add(TickType.CLIENT);
    076            }
    077
    078            sendTick(cTypes, end, tickData);
    079        }
    080        else
    081        {
    082            sendTick(types, end, tickData);
    083        }
    084    }
    085
    086    private void sendTick(EnumSet<TickType> types, boolean end, Object... tickData)
    087    {
    088        for (TickType type : types)
    089        {
    090            if (!ticks.contains(type))
    091            {
    092                continue;
    093            }
    094
    095            boolean keepTicking=true;
    096            if (sendGuiTicks)
    097            {
    098                keepTicking = mod.doTickInGUI(type, end, tickData);
    099            }
    100            else
    101            {
    102                keepTicking = mod.doTickInGame(type, end, tickData);
    103            }
    104            if (!keepTicking) {
    105                ticks.remove(type);
    106                ticks.removeAll(type.partnerTicks());
    107            }
    108        }
    109    }
    110
    111    @Override
    112    public EnumSet<TickType> ticks()
    113    {
    114        return (clockTickTrigger ? EnumSet.of(TickType.RENDER) : ticks);
    115    }
    116
    117    @Override
    118    public String getLabel()
    119    {
    120        return mod.getClass().getSimpleName();
    121    }
    122
    123    public void setMod(BaseModProxy mod)
    124    {
    125        this.mod = mod;
    126    }
    127}