-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
NMSUtils.java
182 lines (147 loc) · 7.39 KB
/
NMSUtils.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
package com.bgsoftware.superiorskyblock.nms.v1_12_R1;
import com.bgsoftware.common.reflection.ReflectField;
import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.tag.CompoundTag;
import com.bgsoftware.superiorskyblock.threads.Executor;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;
import com.google.common.collect.Maps;
import net.minecraft.server.v1_12_R1.Block;
import net.minecraft.server.v1_12_R1.BlockPosition;
import net.minecraft.server.v1_12_R1.Chunk;
import net.minecraft.server.v1_12_R1.ChunkCoordIntPair;
import net.minecraft.server.v1_12_R1.ChunkProviderServer;
import net.minecraft.server.v1_12_R1.ChunkSection;
import net.minecraft.server.v1_12_R1.EntityHuman;
import net.minecraft.server.v1_12_R1.EntityPlayer;
import net.minecraft.server.v1_12_R1.IBlockData;
import net.minecraft.server.v1_12_R1.IChunkLoader;
import net.minecraft.server.v1_12_R1.NBTTagCompound;
import net.minecraft.server.v1_12_R1.Packet;
import net.minecraft.server.v1_12_R1.PlayerChunkMap;
import net.minecraft.server.v1_12_R1.TileEntity;
import net.minecraft.server.v1_12_R1.World;
import net.minecraft.server.v1_12_R1.WorldServer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
public final class NMSUtils {
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
private static final ReflectField<IChunkLoader> CHUNK_LOADER = new ReflectField<>(
ChunkProviderServer.class, IChunkLoader.class, "chunkLoader");
private static final ReflectMethod<Void> SAVE_CHUNK = new ReflectMethod<>(
IChunkLoader.class, "a", World.class, Chunk.class);
private static final ReflectMethod<Void> TILE_ENTITY_LOAD = new ReflectMethod<>(
TileEntity.class, "a", NBTTagCompound.class);
private static final Map<UUID, IChunkLoader> chunkLoadersMap = Maps.newHashMap();
private NMSUtils() {
}
public static void runActionOnChunks(WorldServer worldServer, Collection<ChunkCoordIntPair> chunksCoords,
boolean saveChunks, Runnable onFinish, Consumer<Chunk> chunkConsumer,
Consumer<Chunk> updateChunk) {
List<ChunkCoordIntPair> unloadedChunks = new ArrayList<>();
List<Chunk> loadedChunks = new ArrayList<>();
chunksCoords.forEach(chunkCoords -> {
Chunk chunk = worldServer.getChunkIfLoaded(chunkCoords.x, chunkCoords.z);
if (chunk != null) {
loadedChunks.add(chunk);
} else {
unloadedChunks.add(chunkCoords);
}
});
boolean hasUnloadedChunks = !unloadedChunks.isEmpty();
loadedChunks.forEach(chunkConsumer);
if (updateChunk != null)
loadedChunks.forEach(updateChunk);
if (hasUnloadedChunks) {
runActionOnUnloadedChunks(worldServer, unloadedChunks, saveChunks, chunkConsumer, onFinish);
} else if (onFinish != null) {
onFinish.run();
}
}
public static void runActionOnUnloadedChunks(WorldServer worldServer, Collection<ChunkCoordIntPair> chunks,
boolean saveChunks, Consumer<Chunk> chunkConsumer,
Runnable onFinish) {
IChunkLoader chunkLoader = chunkLoadersMap.computeIfAbsent(worldServer.getDataManager().getUUID(),
uuid -> CHUNK_LOADER.get(worldServer.getChunkProvider()));
Executor.createTask().runAsync(v -> {
chunks.forEach(chunkCoords -> {
if (!chunkLoader.chunkExists(chunkCoords.x, chunkCoords.z))
return;
try {
Chunk loadedChunk = chunkLoader.a(worldServer, chunkCoords.x, chunkCoords.z);
if (loadedChunk != null) {
chunkConsumer.accept(loadedChunk);
if (saveChunks) {
if (SAVE_CHUNK.isValid())
SAVE_CHUNK.invoke(chunkLoader, worldServer, loadedChunk);
else {
chunkLoader.saveChunk(worldServer, loadedChunk, false);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
PluginDebugger.debug(ex);
}
});
}).runSync(v -> {
if (onFinish != null)
onFinish.run();
});
}
public static void sendPacketToRelevantPlayers(WorldServer worldServer, int chunkX, int chunkZ, Packet<?> packet) {
PlayerChunkMap playerChunkMap = worldServer.getPlayerChunkMap();
for (EntityHuman entityHuman : worldServer.players) {
if (entityHuman instanceof EntityPlayer && playerChunkMap.a((EntityPlayer) entityHuman, chunkX, chunkZ))
((EntityPlayer) entityHuman).playerConnection.sendPacket(packet);
}
}
public static void setBlock(Chunk chunk, BlockPosition blockPosition, int combinedId, CompoundTag tileEntity) {
if (!isValidPosition(chunk.world, blockPosition))
return;
IBlockData blockData = Block.getByCombinedId(combinedId);
if (blockData.getMaterial().isLiquid() && plugin.getSettings().isLiquidUpdate()) {
chunk.world.setTypeAndData(blockPosition, blockData, 3);
return;
}
int blockX = blockPosition.getX() & 15;
int blockY = blockPosition.getY();
int blockZ = blockPosition.getZ() & 15;
int highestBlockLight = chunk.b(blockX, blockZ);
boolean initLight = false;
int indexY = blockY >> 4;
ChunkSection chunkSection = chunk.getSections()[indexY];
if (chunkSection == null) {
chunkSection = chunk.getSections()[indexY] = new ChunkSection(indexY << 4, chunk.world.worldProvider.m());
initLight = blockY > highestBlockLight;
}
chunkSection.setType(blockX, blockY & 15, blockZ, blockData);
chunk.markDirty();
if (initLight)
chunk.initLighting();
if (tileEntity != null) {
NBTTagCompound tileEntityCompound = (NBTTagCompound) tileEntity.toNBT();
if (tileEntityCompound != null) {
tileEntityCompound.setInt("x", blockPosition.getX());
tileEntityCompound.setInt("y", blockPosition.getY());
tileEntityCompound.setInt("z", blockPosition.getZ());
TileEntity worldTileEntity = chunk.world.getTileEntity(blockPosition);
if (worldTileEntity != null) {
if (TILE_ENTITY_LOAD.isValid())
TILE_ENTITY_LOAD.invoke(worldTileEntity, tileEntityCompound);
else
worldTileEntity.load(tileEntityCompound);
}
}
}
}
private static boolean isValidPosition(World world, BlockPosition blockPosition) {
return blockPosition.getX() >= -30000000 && blockPosition.getZ() >= -30000000 &&
blockPosition.getX() < 30000000 && blockPosition.getZ() < 30000000 &&
blockPosition.getY() >= 0 && blockPosition.getY() < world.getHeight();
}
}