/
NMSWorldImpl.java
386 lines (307 loc) · 15.8 KB
/
NMSWorldImpl.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package com.bgsoftware.superiorskyblock.nms.v1192;
import com.bgsoftware.common.reflection.ReflectField;
import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.formatting.Formatters;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.listener.SignsListener;
import com.bgsoftware.superiorskyblock.nms.ICachedBlock;
import com.bgsoftware.superiorskyblock.nms.NMSWorld;
import com.bgsoftware.superiorskyblock.nms.algorithms.NMSCachedBlock;
import com.bgsoftware.superiorskyblock.nms.bridge.PistonPushReaction;
import com.bgsoftware.superiorskyblock.nms.v1192.generator.IslandsGeneratorImpl;
import com.bgsoftware.superiorskyblock.nms.v1192.spawners.TickingSpawnerBlockEntityNotifier;
import com.bgsoftware.superiorskyblock.nms.v1192.world.KeyBlocksCache;
import com.bgsoftware.superiorskyblock.nms.v1192.world.PropertiesMapper;
import com.bgsoftware.superiorskyblock.nms.v1192.world.WorldEditSessionImpl;
import com.bgsoftware.superiorskyblock.nms.world.WorldEditSession;
import com.bgsoftware.superiorskyblock.tag.CompoundTag;
import com.destroystokyo.paper.antixray.ChunkPacketBlockController;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundInitializeBorderPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.entity.TickingBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.border.WorldBorder;
import net.minecraft.world.level.lighting.LevelLightEngine;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.craftbukkit.v1_19_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_19_R1.block.CraftBlock;
import org.bukkit.craftbukkit.v1_19_R1.block.CraftSign;
import org.bukkit.craftbukkit.v1_19_R1.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.generator.ChunkGenerator;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.IntFunction;
public class NMSWorldImpl implements NMSWorld {
private static final ReflectMethod<Object> LINES_SIGN_CHANGE_EVENT = new ReflectMethod<>(SignChangeEvent.class, "lines");
private static final ReflectField<Object> CHUNK_PACKET_BLOCK_CONTROLLER = new ReflectField<>(Level.class,
Object.class, "chunkPacketBlockController")
.removeFinal();
private static final ReflectField<List<TickingBlockEntity>> LEVEL_BLOCK_ENTITY_TICKERS = new ReflectField<>(
Level.class, List.class, Modifier.PROTECTED | Modifier.FINAL, 1);
private final SuperiorSkyblockPlugin plugin;
private final Singleton<SignsListener> signsListener;
public NMSWorldImpl(SuperiorSkyblockPlugin plugin) {
this.plugin = plugin;
this.signsListener = plugin.getListener(SignsListener.class);
}
@Override
public Key getBlockKey(ChunkSnapshot chunkSnapshot, int x, int y, int z) {
Block block = ((CraftBlockData) chunkSnapshot.getBlockData(x, y, z)).getState().getBlock();
Location location = new Location(
Bukkit.getWorld(chunkSnapshot.getWorldName()),
(chunkSnapshot.getX() << 4) + x,
y,
(chunkSnapshot.getZ() << 4) + z
);
return Keys.of(KeyBlocksCache.getBlockKey(block), location);
}
@Override
public void listenSpawner(Location location, IntFunction<Integer> delayChangeCallback) {
org.bukkit.World world = location.getWorld();
if (world == null)
return;
ServerLevel serverLevel = ((CraftWorld) world).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos);
if (!(blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity))
return;
List<TickingBlockEntity> blockEntityTickers = LEVEL_BLOCK_ENTITY_TICKERS.get(serverLevel);
Iterator<TickingBlockEntity> blockEntityTickersIterator = blockEntityTickers.iterator();
List<TickingBlockEntity> tickersToAdd = new ArrayList<>();
while (blockEntityTickersIterator.hasNext()) {
TickingBlockEntity tickingBlockEntity = blockEntityTickersIterator.next();
if (tickingBlockEntity.getPos().equals(blockPos) &&
!(tickingBlockEntity instanceof TickingSpawnerBlockEntityNotifier)) {
blockEntityTickersIterator.remove();
tickersToAdd.add(new TickingSpawnerBlockEntityNotifier(spawnerBlockEntity, tickingBlockEntity, delayChangeCallback));
}
}
if (!tickersToAdd.isEmpty())
blockEntityTickers.addAll(tickersToAdd);
}
@Override
public void setWorldBorder(SuperiorPlayer superiorPlayer, Island island) {
if (!plugin.getSettings().isWorldBorders())
return;
Player player = superiorPlayer.asPlayer();
org.bukkit.World world = superiorPlayer.getWorld();
if (world == null || player == null)
return;
int islandSize = island == null ? 0 : island.getIslandSize();
boolean disabled = !superiorPlayer.hasWorldBorderEnabled() || islandSize < 0;
ServerLevel serverLevel = ((CraftWorld) world).getHandle();
WorldBorder worldBorder;
if (disabled || island == null || (!plugin.getSettings().getSpawn().isWorldBorder() && island.isSpawn())) {
worldBorder = serverLevel.getWorldBorder();
} else {
worldBorder = new WorldBorder();
worldBorder.world = serverLevel;
org.bukkit.World.Environment environment = world.getEnvironment();
Location center = island.getCenter(environment);
worldBorder.setWarningBlocks(0);
worldBorder.setSize((islandSize * 2) + 1);
worldBorder.setCenter(center.getX(), center.getZ());
double worldBorderSize = worldBorder.getSize();
switch (superiorPlayer.getBorderColor()) {
case GREEN -> worldBorder.lerpSizeBetween(worldBorderSize - 0.1D, worldBorderSize, Long.MAX_VALUE);
case RED -> worldBorder.lerpSizeBetween(worldBorderSize, worldBorderSize - 1.0D, Long.MAX_VALUE);
}
}
ClientboundInitializeBorderPacket initializeBorderPacket = new ClientboundInitializeBorderPacket(worldBorder);
((CraftPlayer) player).getHandle().connection.send(initializeBorderPacket);
}
@Override
public Object getBlockData(org.bukkit.block.Block block) {
return block.getBlockData();
}
@Override
public void setBlock(Location location, int combinedId) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return;
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
NMSUtils.setBlock(serverLevel.getChunkAt(blockPos), blockPos, combinedId, null, null);
ClientboundBlockUpdatePacket blockUpdatePacket = new ClientboundBlockUpdatePacket(serverLevel, blockPos);
NMSUtils.sendPacketToRelevantPlayers(serverLevel, blockPos.getX() >> 4, blockPos.getZ() >> 4, blockUpdatePacket);
}
@Override
public ICachedBlock cacheBlock(org.bukkit.block.Block block) {
return new NMSCachedBlock(block);
}
@Override
public CompoundTag readBlockStates(Location location) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return null;
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getX(), location.getY(), location.getZ());
BlockState blockState = serverLevel.getBlockState(blockPos);
if (blockState.getValues().isEmpty())
return null;
CompoundTag compoundTag = new CompoundTag();
blockState.getValues().forEach((property, value) -> {
String name = property.getName();
if (property instanceof BooleanProperty) {
compoundTag.setByte(name, (Boolean) value ? (byte) 1 : 0);
} else if (property instanceof IntegerProperty integerProperty) {
compoundTag.setIntArray(name, new int[]{(Integer) value, integerProperty.min, integerProperty.max});
} else if (property instanceof EnumProperty<?>) {
compoundTag.setString(PropertiesMapper.getPropertyName(property), ((Enum<?>) value).name());
}
});
return compoundTag;
}
@Override
public byte[] getLightLevels(Location location) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return new byte[0];
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
LevelLightEngine lightEngine = serverLevel.getLightEngine();
return new byte[]{
location.getWorld().getEnvironment() != org.bukkit.World.Environment.NORMAL ? 0 :
(byte) lightEngine.getLayerListener(LightLayer.SKY).getLightValue(blockPos),
(byte) lightEngine.getLayerListener(LightLayer.BLOCK).getLightValue(blockPos)
};
}
@Override
public CompoundTag readTileEntity(Location location) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return null;
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos);
if (blockEntity == null)
return null;
net.minecraft.nbt.CompoundTag compoundTag = blockEntity.saveWithFullMetadata();
compoundTag.remove("x");
compoundTag.remove("y");
compoundTag.remove("z");
return CompoundTag.fromNBT(compoundTag);
}
@Override
public boolean isWaterLogged(org.bukkit.block.Block block) {
if (Materials.isWater(block.getType()))
return true;
BlockData blockData = block.getBlockData();
return blockData instanceof Waterlogged && ((Waterlogged) blockData).isWaterlogged();
}
@Override
public PistonPushReaction getPistonReaction(org.bukkit.block.Block block) {
BlockState blockState = ((CraftBlock) block).getNMS();
return PistonPushReaction.values()[blockState.getPistonPushReaction().ordinal()];
}
@Override
public int getDefaultAmount(org.bukkit.block.Block bukkitBlock) {
BlockState blockState = ((CraftBlock) bukkitBlock).getNMS();
Block block = blockState.getBlock();
return NMSUtils.isDoubleBlock(block, blockState) ? 2 : 1;
}
@Override
public void placeSign(Island island, Location location) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return;
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos);
if (!(blockEntity instanceof SignBlockEntity signBlockEntity))
return;
String[] lines = new String[4];
System.arraycopy(CraftSign.revertComponents(signBlockEntity.messages), 0, lines, 0, lines.length);
String[] strippedLines = new String[4];
for (int i = 0; i < 4; i++)
strippedLines[i] = Formatters.STRIP_COLOR_FORMATTER.format(lines[i]);
Component[] newLines;
if (signsListener.get().shouldReplaceSignLines(island.getOwner(), island, location, strippedLines, false))
newLines = CraftSign.sanitizeLines(strippedLines);
else
newLines = CraftSign.sanitizeLines(lines);
System.arraycopy(newLines, 0, signBlockEntity.messages, 0, 4);
}
@SuppressWarnings("deprecation")
@Override
public void setSignLines(SignChangeEvent signChangeEvent, String[] lines) {
if (LINES_SIGN_CHANGE_EVENT.isValid()) {
for (int i = 0; i < lines.length; i++)
signChangeEvent.setLine(i, lines[i]);
}
}
@Override
public void playGeneratorSound(Location location) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return;
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
serverLevel.levelEvent(1501, blockPos, 0);
}
@Override
public void playBreakAnimation(org.bukkit.block.Block block) {
ServerLevel serverLevel = ((CraftWorld) block.getWorld()).getHandle();
BlockPos blockPos = new BlockPos(block.getX(), block.getY(), block.getZ());
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
}
@Override
public void playPlaceSound(Location location) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return;
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
SoundType soundType = serverLevel.getBlockState(blockPos).getSoundType();
serverLevel.playSound(null, blockPos, soundType.getPlaceSound(), SoundSource.BLOCKS,
(soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F);
}
@Override
public int getMinHeight(org.bukkit.World world) {
return world.getMinHeight();
}
@Override
public void removeAntiXray(org.bukkit.World bukkitWorld) {
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
if (CHUNK_PACKET_BLOCK_CONTROLLER.isValid())
CHUNK_PACKET_BLOCK_CONTROLLER.set(serverLevel, ChunkPacketBlockController.NO_OPERATION_INSTANCE);
}
@Override
public ChunkGenerator createGenerator(SuperiorSkyblockPlugin plugin) {
return new IslandsGeneratorImpl(plugin);
}
@Override
public WorldEditSession createEditSession(World world) {
return new WorldEditSessionImpl(((CraftWorld) world).getHandle());
}
}