aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/tileentities/render/TileEntityBlackhole.java
blob: f15aa45eee68e789479ca97b9bece36c1f9fd37c (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
package gregtech.common.tileentities.render;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TileEntityBlackhole extends TileEntity {

    // Should run from 0 to 1, >.5 starts showing changes
    private float stability = 1;
    private float laserR = 0.318f, laserG = 0.157f, laserB = 0.533f;
    private boolean laserRender = false;

    private static final String NBT_TAG = "BLACKHOLE";

    private static final String STABILITY_NBT_TAG = NBT_TAG + "STABILITY";
    private static final String COLOR_RED_NBT_TAG = NBT_TAG + "COLOR_RED";
    private static final String COLOR_GREEN_NBT_TAG = NBT_TAG + "COLOR_GREEN";
    private static final String COLOR_BLUE_NBT_TAG = NBT_TAG + "COLOR_BLUE";
    private static final String RENDER_NBT_TAG = NBT_TAG + "LASER_RENDER";

    public void setLaserColor(float r, float g, float b) {
        if (!worldObj.isRemote) {
            laserR = r;
            laserG = g;
            laserB = b;
            updateToClient();
        }
    }

    public void toggleLaser(boolean toggle) {
        if (!worldObj.isRemote) {
            laserRender = toggle;
            updateToClient();
        }
    }

    public float getLaserR() {
        return laserR;
    }

    public float getLaserG() {
        return laserG;
    }

    public float getLaserB() {
        return laserB;
    }

    public boolean getLaserRender() {
        return laserRender;
    }

    public void setStability(float stability) {
        // Can probably be simplified, maps stability > .5 as 1, and stability <.5 from 0 to 1
        if (!worldObj.isRemote) {
            this.stability = ((float) Math.min(stability + .5, 1f) - .5f) * 2f;
            updateToClient();
        }
    }

    public float getStability() {
        return stability;
    }

    public void writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);
        compound.setFloat(STABILITY_NBT_TAG, stability);
        compound.setFloat(COLOR_RED_NBT_TAG, laserR);
        compound.setFloat(COLOR_GREEN_NBT_TAG, laserG);
        compound.setFloat(COLOR_BLUE_NBT_TAG, laserB);
        compound.setBoolean(RENDER_NBT_TAG, laserRender);
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        stability = compound.getFloat(STABILITY_NBT_TAG);
        laserR = compound.getFloat(COLOR_RED_NBT_TAG);
        laserG = compound.getFloat(COLOR_GREEN_NBT_TAG);
        laserB = compound.getFloat(COLOR_BLUE_NBT_TAG);
        laserRender = compound.getBoolean(RENDER_NBT_TAG);
        super.readFromNBT(compound);

    }

    @Override
    public Packet getDescriptionPacket() {
        NBTTagCompound nbttagcompound = new NBTTagCompound();
        writeToNBT(nbttagcompound);

        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, nbttagcompound);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
        readFromNBT(pkt.func_148857_g());
    }

    public void updateToClient() {
        worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    }

}