/
NMSHandler.java
232 lines (194 loc) · 7.99 KB
/
NMSHandler.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package me.desht.dhutils.nms.v1_7_R2;
import me.desht.dhutils.nms.api.NMSAbstraction;
import net.minecraft.server.v1_7_R2.*;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_7_R2.CraftChunk;
import org.bukkit.craftbukkit.v1_7_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.lang.reflect.Method;
public class NMSHandler implements NMSAbstraction {
@Override
public boolean setBlockFast(World world, int x, int y, int z, int blockId, byte data) {
net.minecraft.server.v1_7_R2.World w = ((CraftWorld) world).getHandle();
Chunk chunk = w.getChunkAt(x >> 4, z >> 4);
return a(chunk, x & 0x0f, y, z & 0x0f, Block.e(blockId), data);
}
private boolean a(Chunk that, int i, int j, int k, Block block, int l) {
int i1 = k << 4 | i;
if (j >= that.b[i1] - 1) {
that.b[i1] = -999;
}
int j1 = that.heightMap[i1];
Block block1 = that.getType(i, j, k);
int k1 = that.getData(i, j, k);
if (block1 == block && k1 == l) {
return false;
} else {
boolean flag = false;
ChunkSection chunksection = that.i()[j >> 4];
if (chunksection == null) {
if (block == Blocks.AIR) {
return false;
}
chunksection = that.i()[j >> 4] = new ChunkSection(j >> 4 << 4, !that.world.worldProvider.g);
flag = j >= j1;
}
int l1 = that.locX * 16 + i;
int i2 = that.locZ * 16 + k;
if (!that.world.isStatic) {
block1.f(that.world, l1, j, i2, k1);
}
// CraftBukkit start - Delay removing containers until after they're cleaned up
if (!(block1 instanceof IContainer)) {
chunksection.setTypeId(i, j & 15, k, block);
}
// CraftBukkit end
if (!that.world.isStatic) {
block1.remove(that.world, l1, j, i2, block1, k1);
} else if (block1 instanceof IContainer && block1 != block) {
that.world.p(l1, j, i2);
}
// CraftBukkit start - Remove containers now after cleanup
if (block1 instanceof IContainer) {
chunksection.setTypeId(i, j & 15, k, block);
}
// CraftBukkit end
if (chunksection.getTypeId(i, j & 15, k) != block) {
return false;
} else {
chunksection.setData(i, j & 15, k, l);
if (flag) {
that.initLighting();
}
TileEntity tileentity;
if (block1 instanceof IContainer) {
tileentity = that.e(i, j, k);
if (tileentity != null) {
tileentity.u();
}
}
// CraftBukkit - Don't place while processing the BlockPlaceEvent, unless it's a BlockContainer
if (!that.world.isStatic && (!that.world.callingPlaceEvent || (block instanceof BlockContainer))) {
block.onPlace(that.world, l1, j, i2);
}
if (block instanceof IContainer) {
// CraftBukkit start - Don't create tile entity if placement failed
if (that.getType(i, j, k) != block) {
return false;
}
// CraftBukkit end
tileentity = that.e(i, j, k);
if (tileentity == null) {
tileentity = ((IContainer) block).a(that.world, l);
that.world.setTileEntity(l1, j, i2, tileentity);
}
if (tileentity != null) {
tileentity.u();
}
}
that.n = true;
return true;
}
}
}
@Override
public void forceBlockLightLevel(World world, int x, int y, int z, int level) {
net.minecraft.server.v1_7_R2.World w = ((CraftWorld) world).getHandle();
w.b(EnumSkyBlock.BLOCK, x, y, z, level);
}
@Override
public int getBlockLightEmission(int blockId) {
return Block.e(blockId).m();
}
@Override
public int getBlockLightBlocking(int blockId) {
return Block.e(blockId).k();
}
@SuppressWarnings("unchecked")
@Override
public void queueChunkForUpdate(Player player, int cx, int cz) {
((CraftPlayer) player).getHandle().chunkCoordIntPairQueue.add(new ChunkCoordIntPair(cx, cz));
}
@Override
public Vector[] getBlockHitbox(org.bukkit.block.Block block) {
net.minecraft.server.v1_7_R2.World w = ((CraftWorld)block.getWorld()).getHandle();
net.minecraft.server.v1_7_R2.Block b = w.getType(block.getX(), block.getY(), block.getZ());
b.updateShape(w, block.getX(), block.getY(), block.getZ());
return new Vector[] {
new Vector(block.getX() + b.x(), block.getY() + b.z(), block.getZ() + b.B()),
new Vector(block.getX() + b.y(), block.getY() + b.A(), block.getZ() + b.C())
};
}
@Override
public void recalculateBlockLighting(World world, int x, int y, int z) {
// Don't consider blocks that are completely surrounded by other non-transparent blocks
if (!canAffectLighting(world, x, y, z)) {
return;
}
int i = x & 0x0F;
int j = y & 0xFF;
int k = z & 0x0F;
CraftChunk craftChunk = (CraftChunk)world.getChunkAt(x >> 4, z >> 4);
Chunk nmsChunk = craftChunk.getHandle();
int i1 = k << 4 | i;
int maxY = nmsChunk.heightMap[i1];
Block block = nmsChunk.getType(i, j, k);
int j2 = block.k();
if (j2 > 0) {
if (j >= maxY) {
invokeNmsH(nmsChunk, i, j + 1, k);
}
} else if (j == maxY - 1) {
invokeNmsH(nmsChunk,i, j, k);
}
if (nmsChunk.getBrightness(EnumSkyBlock.SKY, i, j, k) > 0 || nmsChunk.getBrightness(EnumSkyBlock.BLOCK, i, j, k) > 0) {
invokeNmsE(nmsChunk, i, k);
}
net.minecraft.server.v1_7_R2.World w = ((CraftWorld) world).getHandle();
w.c(EnumSkyBlock.BLOCK, i, j, k);
}
private Method h;
private void invokeNmsH(Chunk nmsChunk, int i, int j, int k) {
try {
if (h == null) {
Class[] classes = {int.class, int.class, int.class};
h = Chunk.class.getDeclaredMethod("h", classes);
h.setAccessible(true);
}
h.invoke(nmsChunk, i, j, k);
} catch (Exception e) {
System.out.println("Reflection exception: " + e);
}
}
private Method e;
private void invokeNmsE(Chunk nmsChunk, int i, int j) {
try {
if (e == null) {
Class[] classes = {int.class, int.class};
e = Chunk.class.getDeclaredMethod("e", classes);
e.setAccessible(true);
}
e.invoke(nmsChunk, i, j);
} catch (Exception e) {
System.out.println("Reflection exception: " + e);
}
}
private boolean canAffectLighting(World world, int x, int y, int z) {
org.bukkit.block.Block base = world.getBlockAt(x, y, z);
org.bukkit.block.Block east = base.getRelative(BlockFace.EAST);
org.bukkit.block.Block west = base.getRelative(BlockFace.WEST);
org.bukkit.block.Block up = base.getRelative(BlockFace.UP);
org.bukkit.block.Block down = base.getRelative(BlockFace.DOWN);
org.bukkit.block.Block south = base.getRelative(BlockFace.SOUTH);
org.bukkit.block.Block north = base.getRelative(BlockFace.NORTH);
return east.getType().isTransparent() ||
west.getType().isTransparent() ||
up.getType().isTransparent() ||
down.getType().isTransparent() ||
south.getType().isTransparent() ||
north.getType().isTransparent();
}
}