aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/multitileentity/base/BaseTickableMultiTileEntity.java
blob: c8bc04557f068bbefbd066fda51caab4b593fb53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package gregtech.api.multitileentity.base;

import gregtech.api.multitileentity.interfaces.IMultiTileEntity.IMTE_OnNeighborBlockChange;
import gregtech.api.util.GT_Log;
import gregtech.api.util.GT_Util;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.World;

import static gregtech.GT_Mod.GT_FML_LOGGER;

public abstract class BaseTickableMultiTileEntity extends BaseMultiTileEntity implements IMTE_OnNeighborBlockChange {
    /** Variable for seeing if the Tick Function is called right now. */
    public boolean mIsRunningTick = false;
    /** Gets set to true when the Block received a Block Update. */
    public boolean mBlockUpdated = false;
    /** Timer Value */
    protected long mTimer = 0;
    /** Variable for updating Data to the Client */
    private boolean mSendClientData = false;


    public BaseTickableMultiTileEntity() {
        super(true);
    }

    @Override
    public final void updateEntity() {
        mIsRunningTick = true;
        final boolean isServerSide = isServerSide();
        try {
            if (mTimer++ == 0) {
                markDirty();
                GT_Util.markChunkDirty(this);
                onFirstTick(isServerSide);
            }
            if (!isDead()) onPreTick(mTimer, isServerSide);
            if (!isDead()) {
                mTimer++;
                super.updateEntity();
            }
            if (!isServerSide) {
                if (mNeedsUpdate) {
                    worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
                    //worldObj.func_147479_m(xCoord, yCoord, zCoord);
                    mNeedsUpdate = false;
                }
            }
            if (!isDead()) onTick(mTimer, isServerSide);
            if (!isDead() && isServerSide && mTimer > 2 && mSendClientData) {
                sendClientData(null);
            }
            if (!isDead()) onPostTick(mTimer, isServerSide);

        } catch (Throwable e) {
            GT_FML_LOGGER.error("UpdateEntity Failed", e);
            e.printStackTrace(GT_Log.err);
            try {
                onTickFailed(mTimer, isServerSide);
            } catch (Throwable e2) {
                GT_FML_LOGGER.error("UpdateEntity:onTickFailed Failed", e);
            }
        }

        mIsRunningTick = false;
    }

    @Override
    public void sendClientData(EntityPlayerMP aPlayer) {
        if (mSendClientData) {
            GT_FML_LOGGER.info("Sending client data");
            super.sendClientData(aPlayer);
            mSendClientData = false;
        }
    }

    /** The very first Tick happening to this TileEntity */
    public void onFirstTick(boolean isServerSide) {
        if (isServerSide) {
            checkDropCover();
        } else {
            requestCoverDataIfNeeded();
        }
    }

    /** The first part of the Tick. */
    public void onPreTick(long aTick, boolean isServerSide) { /*Do nothing*/ }

    /** The regular Tick. */
    public void onTick(long aTimer, boolean isServerSide) { /*Do nothing*/ }

    /** The absolute last part of the Tick. */
    public void onPostTick(long aTick, boolean isServerSide) { /*Do nothing*/ }

    /** Gets called when there is an Exception happening during one of the Tick Functions. */
    public void onTickFailed(long aTimer, boolean isServerSide) { /*Do nothing*/ }

    @Override
    public void onNeighborBlockChange(World aWorld, Block aBlock) {
        mBlockUpdated = true;
    }

    @Override
    public void issueClientUpdate() {
        mSendClientData = true;
    }

    @Override
    public byte getComparatorValue(byte aSide) {
        return 0;
    }

}