-
Notifications
You must be signed in to change notification settings - Fork 497
/
EntityLaser.java
192 lines (147 loc) · 5.43 KB
/
EntityLaser.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import buildcraft.api.core.Position;
public abstract class EntityLaser extends Entity {
public static final ResourceLocation[] LASER_TEXTURES = new ResourceLocation[]{
new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_ENTITIES + "/laser_1.png"),
new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_ENTITIES + "/laser_2.png"),
new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_ENTITIES + "/laser_3.png"),
new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_ENTITIES + "/laser_4.png"),
new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_ENTITIES + "/stripes.png"),
new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_ENTITIES + "/blue_stripes.png")};
public LaserData data = new LaserData();
protected boolean needsUpdate = true;
public EntityLaser(World world) {
super(world);
data.head = new Position(0, 0, 0);
data.tail = new Position(0, 0, 0);
}
public EntityLaser(World world, Position head, Position tail) {
super(world);
data.head = head;
data.tail = tail;
setPositionAndRotation(head.x, head.y, head.z, 0, 0);
setSize(10, 10);
}
@Override
protected void entityInit() {
preventEntitySpawning = false;
noClip = true;
isImmuneToFire = true;
dataWatcher.addObject(8, Integer.valueOf(0));
dataWatcher.addObject(9, Integer.valueOf(0));
dataWatcher.addObject(10, Integer.valueOf(0));
dataWatcher.addObject(11, Integer.valueOf(0));
dataWatcher.addObject(12, Integer.valueOf(0));
dataWatcher.addObject(13, Integer.valueOf(0));
dataWatcher.addObject(14, Byte.valueOf((byte) 0));
}
@Override
public void onUpdate() {
if (data.head == null || data.tail == null) {
return;
}
if (!worldObj.isRemote && needsUpdate) {
updateDataServer();
needsUpdate = false;
}
//if (worldObj.isRemote) {
// updateDataClient();
//}
boundingBox.minX = Math.min(data.head.x, data.tail.x);
boundingBox.minY = Math.min(data.head.y, data.tail.y);
boundingBox.minZ = Math.min(data.head.z, data.tail.z);
boundingBox.maxX = Math.max(data.head.x, data.tail.x);
boundingBox.maxY = Math.max(data.head.y, data.tail.y);
boundingBox.maxZ = Math.max(data.head.z, data.tail.z);
boundingBox.minX--;
boundingBox.minY--;
boundingBox.minZ--;
boundingBox.maxX++;
boundingBox.maxY++;
boundingBox.maxZ++;
data.update();
}
protected void updateDataClient() {
data.head.x = decodeDouble(dataWatcher.getWatchableObjectInt(8));
data.head.y = decodeDouble(dataWatcher.getWatchableObjectInt(9));
data.head.z = decodeDouble(dataWatcher.getWatchableObjectInt(10));
data.tail.x = decodeDouble(dataWatcher.getWatchableObjectInt(11));
data.tail.y = decodeDouble(dataWatcher.getWatchableObjectInt(12));
data.tail.z = decodeDouble(dataWatcher.getWatchableObjectInt(13));
data.isVisible = dataWatcher.getWatchableObjectByte(14) == 1;
}
protected void updateDataServer() {
dataWatcher.updateObject(8, Integer.valueOf(encodeDouble(data.head.x)));
dataWatcher.updateObject(9, Integer.valueOf(encodeDouble(data.head.y)));
dataWatcher.updateObject(10, Integer.valueOf(encodeDouble(data.head.z)));
dataWatcher.updateObject(11, Integer.valueOf(encodeDouble(data.tail.x)));
dataWatcher.updateObject(12, Integer.valueOf(encodeDouble(data.tail.y)));
dataWatcher.updateObject(13, Integer.valueOf(encodeDouble(data.tail.z)));
dataWatcher.updateObject(14, Byte.valueOf((byte) (data.isVisible ? 1 : 0)));
}
public void setPositions(Position head, Position tail) {
data.head = head;
data.tail = tail;
setPositionAndRotation(head.x, head.y, head.z, 0, 0);
needsUpdate = true;
}
public void show() {
data.isVisible = true;
needsUpdate = true;
}
public void hide() {
data.isVisible = false;
needsUpdate = true;
}
public boolean isVisible() {
return data.isVisible;
}
public abstract ResourceLocation getTexture();
protected int encodeDouble(double d) {
return (int) (d * 8192);
}
protected double decodeDouble(int i) {
return i / 8192D;
}
// The read/write to nbt seem to be useless
@Override
protected void readEntityFromNBT(NBTTagCompound nbt) {
double headX = nbt.getDouble("headX");
double headY = nbt.getDouble("headZ");
double headZ = nbt.getDouble("headY");
data.head = new Position(headX, headY, headZ);
double tailX = nbt.getDouble("tailX");
double tailY = nbt.getDouble("tailZ");
double tailZ = nbt.getDouble("tailY");
data.tail = new Position(tailX, tailY, tailZ);
}
@Override
protected void writeEntityToNBT(NBTTagCompound nbt) {
nbt.setDouble("headX", data.head.x);
nbt.setDouble("headY", data.head.y);
nbt.setDouble("headZ", data.head.z);
nbt.setDouble("tailX", data.tail.x);
nbt.setDouble("tailY", data.tail.y);
nbt.setDouble("tailZ", data.tail.z);
}
// Workaround for the laser's posY loosing it's precision e.g 103.5 becomes 104
public Position renderOffset() {
return new Position(data.head.x - posX, data.head.y - posY, data.head.z - posZ);
}
@Override
public int getBrightnessForRender(float par1) {
return 210;
}
}