-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathLimboAPI.java
More file actions
723 lines (624 loc) · 27.3 KB
/
LimboAPI.java
File metadata and controls
723 lines (624 loc) · 27.3 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/*
* Copyright (C) 2021 - 2025 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.elytrium.limboapi;
import com.google.inject.Inject;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.KickedFromServerEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.event.VelocityEventManager;
import com.velocitypowered.proxy.network.Connections;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.netty.MinecraftCompressDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftCompressorAndLengthEncoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder;
import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import net.elytrium.commons.config.YamlConfig;
import net.elytrium.commons.kyori.serialization.Serializer;
import net.elytrium.commons.kyori.serialization.Serializers;
import net.elytrium.commons.utils.reflection.ReflectionException;
import net.elytrium.commons.utils.updates.UpdatesChecker;
import net.elytrium.fastprepare.PreparedPacketFactory;
import net.elytrium.fastprepare.handler.PreparedPacketEncoder;
import net.elytrium.limboapi.api.Limbo;
import net.elytrium.limboapi.api.LimboFactory;
import net.elytrium.limboapi.api.chunk.BuiltInBiome;
import net.elytrium.limboapi.api.chunk.Dimension;
import net.elytrium.limboapi.api.chunk.VirtualBiome;
import net.elytrium.limboapi.api.chunk.VirtualBlock;
import net.elytrium.limboapi.api.chunk.VirtualBlockEntity;
import net.elytrium.limboapi.api.chunk.VirtualChunk;
import net.elytrium.limboapi.api.chunk.VirtualWorld;
import net.elytrium.limboapi.api.file.BuiltInWorldFileType;
import net.elytrium.limboapi.api.file.WorldFile;
import net.elytrium.limboapi.api.material.Block;
import net.elytrium.limboapi.api.material.Item;
import net.elytrium.limboapi.api.material.VirtualItem;
import net.elytrium.limboapi.api.protocol.PreparedPacket;
import net.elytrium.limboapi.api.protocol.item.ItemComponentMap;
import net.elytrium.limboapi.api.protocol.packets.PacketFactory;
import net.elytrium.limboapi.file.WorldFileTypeRegistry;
import net.elytrium.limboapi.injection.disconnect.DisconnectListener;
import net.elytrium.limboapi.injection.event.EventManagerHook;
import net.elytrium.limboapi.injection.login.LoginListener;
import net.elytrium.limboapi.injection.login.LoginTasksQueue;
import net.elytrium.limboapi.injection.packet.LegacyPlayerListItemHook;
import net.elytrium.limboapi.injection.packet.LimboCompressDecoder;
import net.elytrium.limboapi.injection.packet.MinecraftDiscardCompressDecoder;
import net.elytrium.limboapi.injection.packet.MinecraftLimitedCompressDecoder;
import net.elytrium.limboapi.injection.packet.PreparedPacketImpl;
import net.elytrium.limboapi.injection.packet.RemovePlayerInfoHook;
import net.elytrium.limboapi.injection.packet.UpsertPlayerInfoHook;
import net.elytrium.limboapi.material.Biome;
import net.elytrium.limboapi.protocol.LimboProtocol;
import net.elytrium.limboapi.protocol.packets.PacketFactoryImpl;
import net.elytrium.limboapi.server.CachedPackets;
import net.elytrium.limboapi.server.LimboImpl;
import net.elytrium.limboapi.server.item.SimpleItemComponentManager;
import net.elytrium.limboapi.server.item.SimpleItemComponentMap;
import net.elytrium.limboapi.server.world.SimpleBlock;
import net.elytrium.limboapi.server.world.SimpleBlockEntity;
import net.elytrium.limboapi.server.world.SimpleItem;
import net.elytrium.limboapi.server.world.SimpleTagManager;
import net.elytrium.limboapi.server.world.SimpleWorld;
import net.elytrium.limboapi.server.world.chunk.SimpleChunk;
import net.elytrium.limboapi.utils.ReloadListener;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import org.bstats.velocity.Metrics;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.slf4j.Logger;
@Plugin(
id = "limboapi",
name = "LimboAPI",
version = BuildConstants.LIMBO_VERSION,
description = "Velocity plugin for making virtual servers.",
url = "https://elytrium.net/",
authors = {
"Elytrium (https://elytrium.net/)",
}
)
public class LimboAPI implements LimboFactory {
private static final int SUPPORTED_MAXIMUM_PROTOCOL_VERSION_NUMBER = 775;
@MonotonicNonNull
private static Logger LOGGER;
@MonotonicNonNull
private static Serializer SERIALIZER;
public static final ConcurrentHashMap<Player, UUID> INITIAL_ID = new ConcurrentHashMap<>();
private static final MethodHandle STATE_FIELD;
private final VelocityServer server;
private final Metrics.Factory metricsFactory;
private final File configFile;
private final Set<Player> players;
private final CachedPackets packets;
private final PacketFactory packetFactory;
private final SimpleItemComponentManager itemComponentManager = new SimpleItemComponentManager();
private final HashMap<Player, LoginTasksQueue> loginQueue;
private final HashMap<Player, Function<KickedFromServerEvent, Boolean>> kickCallback;
private final HashMap<Player, RegisteredServer> nextServer;
private PreparedPacketFactory preparedPacketFactory;
private PreparedPacketFactory configPreparedPacketFactory;
private PreparedPacketFactory loginUncompressedPreparedPacketFactory;
private PreparedPacketFactory loginPreparedPacketFactory;
private ProtocolVersion minVersion;
private ProtocolVersion maxVersion;
private LoginListener loginListener;
private boolean compressionEnabled;
private EventManagerHook eventManagerHook;
@Inject
public LimboAPI(Logger logger, ProxyServer server, Metrics.Factory metricsFactory, @DataDirectory Path dataDirectory) {
setLogger(logger);
this.server = (VelocityServer) server;
this.metricsFactory = metricsFactory;
this.configFile = dataDirectory.resolve("config.yml").toFile();
this.players = new HashSet<>();
this.packetFactory = new PacketFactoryImpl();
this.packets = new CachedPackets(this);
this.loginQueue = new HashMap<>();
this.kickCallback = new HashMap<>();
this.nextServer = new HashMap<>();
int maximumProtocolVersionNumber = ProtocolVersion.MAXIMUM_VERSION.getProtocol();
if (maximumProtocolVersionNumber < SUPPORTED_MAXIMUM_PROTOCOL_VERSION_NUMBER) {
LOGGER.error("Please update Velocity (https://papermc.io/downloads#Velocity). LimboAPI support: https://ely.su/discord");
this.server.shutdown();
return;
} else if (maximumProtocolVersionNumber != SUPPORTED_MAXIMUM_PROTOCOL_VERSION_NUMBER) {
LOGGER.warn("Current LimboAPI version doesn't support current Velocity version (protocol version numbers: supported - {}, velocity - {})",
SUPPORTED_MAXIMUM_PROTOCOL_VERSION_NUMBER, maximumProtocolVersionNumber);
LOGGER.warn("Please update LimboAPI (https://github.com/Elytrium/LimboAPI/releases). LimboAPI support: https://ely.su/discord");
}
LOGGER.info("Initializing Simple Virtual World system...");
SimpleBlock.init();
SimpleBlockEntity.init();
SimpleItem.init();
SimpleTagManager.init();
LOGGER.info("Hooking into PlayerList/UpsertPlayerInfo and StateRegistry...");
try {
LegacyPlayerListItemHook.init(this, LimboProtocol.PLAY_CLIENTBOUND_REGISTRY);
UpsertPlayerInfoHook.init(this, LimboProtocol.PLAY_CLIENTBOUND_REGISTRY);
RemovePlayerInfoHook.init(this, LimboProtocol.PLAY_CLIENTBOUND_REGISTRY);
LimboProtocol.init();
} catch (Throwable e) {
throw new ReflectionException(e);
}
}
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
Settings.IMP.setLogger(LOGGER);
if (Settings.IMP.reload(this.configFile, Settings.IMP.PREFIX) == YamlConfig.LoadResult.CONFIG_NOT_EXISTS) {
LOGGER.warn("************* FIRST LAUNCH *************");
LOGGER.warn("Thanks for installing LimboAPI!");
LOGGER.warn("(C) 2021 - 2024 Elytrium");
LOGGER.warn("");
LOGGER.warn("Check out our plugins here: https://ely.su/github <3");
LOGGER.warn("Discord: https://ely.su/discord");
LOGGER.warn("****************************************");
}
int level = this.server.getConfiguration().getCompressionLevel();
int threshold = this.server.getConfiguration().getCompressionThreshold();
this.preparedPacketFactory = new PreparedPacketFactory(
PreparedPacketImpl::new,
LimboProtocol.getLimboStateRegistry(),
this.compressionEnabled,
level,
threshold,
Settings.IMP.MAIN.SAVE_UNCOMPRESSED_PACKETS,
true,
Settings.IMP.MAIN.COMPATIBILITY_MODE
);
this.configPreparedPacketFactory = new PreparedPacketFactory(
PreparedPacketImpl::new,
StateRegistry.CONFIG,
this.compressionEnabled,
level,
threshold,
Settings.IMP.MAIN.SAVE_UNCOMPRESSED_PACKETS,
true,
Settings.IMP.MAIN.COMPATIBILITY_MODE
);
this.loginUncompressedPreparedPacketFactory = new PreparedPacketFactory(
PreparedPacketImpl::new,
StateRegistry.LOGIN,
false,
level,
threshold,
false,
true,
Settings.IMP.MAIN.COMPATIBILITY_MODE
);
this.loginPreparedPacketFactory = new PreparedPacketFactory(
PreparedPacketImpl::new,
StateRegistry.LOGIN,
this.compressionEnabled,
level,
threshold,
Settings.IMP.MAIN.SAVE_UNCOMPRESSED_PACKETS,
true,
Settings.IMP.MAIN.COMPATIBILITY_MODE
);
this.reloadPreparedPacketFactory();
this.reload();
this.metricsFactory.make(this, 12530);
if (Settings.IMP.MAIN.CHECK_FOR_UPDATES) {
if (!UpdatesChecker.checkVersionByURL("https://raw.githubusercontent.com/Elytrium/LimboAPI/master/VERSION", Settings.IMP.VERSION)) {
LOGGER.error("****************************************");
LOGGER.warn("The new LimboAPI update was found, please update.");
LOGGER.error("https://github.com/Elytrium/LimboAPI/releases/");
LOGGER.error("****************************************");
}
}
}
@Subscribe(order = PostOrder.LAST)
public void postProxyInitialization(ProxyInitializeEvent event) throws IllegalAccessException {
this.eventManagerHook.reloadHandlers();
}
public void reload() {
Settings.IMP.reload(this.configFile, Settings.IMP.PREFIX);
ComponentSerializer<Component, Component, String> serializer = Settings.IMP.SERIALIZER.getSerializer();
if (serializer == null) {
LOGGER.warn("The specified serializer could not be founded, using default. (LEGACY_AMPERSAND)");
setSerializer(new Serializer(Objects.requireNonNull(Serializers.LEGACY_AMPERSAND.getSerializer())));
} else {
setSerializer(new Serializer(serializer));
}
LOGGER.info("Creating and preparing packets...");
this.reloadVersion();
this.packets.createPackets();
this.loginListener = new LoginListener(this, this.server);
this.eventManagerHook = new EventManagerHook(this, this.server.getEventManager());
VelocityEventManager eventManager = this.server.getEventManager();
eventManager.unregisterListeners(this);
eventManager.register(this, this.loginListener);
eventManager.register(this, this.eventManagerHook);
eventManager.register(this, new DisconnectListener(this));
eventManager.register(this, new ReloadListener(this));
LOGGER.info("Loaded!");
}
private void reloadVersion() {
if (Settings.IMP.MAIN.PREPARE_MAX_VERSION.equals("LATEST")) {
this.maxVersion = ProtocolVersion.MAXIMUM_VERSION;
} else {
this.maxVersion = ProtocolVersion.valueOf("MINECRAFT_" + Settings.IMP.MAIN.PREPARE_MAX_VERSION);
}
this.minVersion = ProtocolVersion.valueOf("MINECRAFT_" + Settings.IMP.MAIN.PREPARE_MIN_VERSION);
if (ProtocolVersion.MAXIMUM_VERSION.compareTo(this.maxVersion) > 0 || ProtocolVersion.MINIMUM_VERSION.compareTo(this.minVersion) < 0) {
LOGGER.warn(
"Currently working only with "
+ this.minVersion.getVersionIntroducedIn() + " - " + this.maxVersion.getMostRecentSupportedVersion()
+ " versions, modify the plugins/limboapi/config.yml file if you want the plugin to work with other versions."
);
}
}
public void reloadPreparedPacketFactory() {
int level = this.server.getConfiguration().getCompressionLevel();
int threshold = this.server.getConfiguration().getCompressionThreshold();
this.compressionEnabled = threshold != -1;
this.preparedPacketFactory.updateCompressor(this.compressionEnabled, level, threshold,
Settings.IMP.MAIN.SAVE_UNCOMPRESSED_PACKETS, Settings.IMP.MAIN.COMPATIBILITY_MODE);
this.configPreparedPacketFactory.updateCompressor(this.compressionEnabled, level, threshold,
Settings.IMP.MAIN.SAVE_UNCOMPRESSED_PACKETS, Settings.IMP.MAIN.COMPATIBILITY_MODE);
this.loginPreparedPacketFactory.updateCompressor(this.compressionEnabled, level, threshold,
Settings.IMP.MAIN.SAVE_UNCOMPRESSED_PACKETS, Settings.IMP.MAIN.COMPATIBILITY_MODE);
}
@Override
public VirtualBlock createSimpleBlock(Block block) {
return SimpleBlock.fromLegacyID((short) block.getID());
}
@Override
public VirtualBlock createSimpleBlock(short legacyID) {
return SimpleBlock.fromLegacyID(legacyID);
}
@Override
public VirtualBlock createSimpleBlock(String modernID) {
return SimpleBlock.fromModernID(modernID);
}
@Override
public VirtualBlock createSimpleBlock(String modernID, Map<String, String> properties) {
return SimpleBlock.fromModernID(modernID, properties);
}
@Override
public VirtualBlock createSimpleBlock(short id, boolean modern) {
if (modern) {
return SimpleBlock.solid(id);
} else {
return SimpleBlock.fromLegacyID(id);
}
}
@Override
public VirtualBlock createSimpleBlock(boolean solid, boolean air, boolean motionBlocking, short id) {
return new SimpleBlock(solid, air, motionBlocking, id);
}
@Override
public VirtualBlock createSimpleBlock(boolean solid, boolean air, boolean motionBlocking, String modernID, Map<String, String> properties) {
return new SimpleBlock(solid, air, motionBlocking, modernID, properties);
}
@Override
public VirtualWorld createVirtualWorld(Dimension dimension, double posX, double posY, double posZ, float yaw, float pitch) {
return new SimpleWorld(dimension, posX, posY, posZ, yaw, pitch);
}
@Override
public VirtualChunk createVirtualChunk(int posX, int posZ) {
return new SimpleChunk(posX, posZ);
}
@Override
public VirtualChunk createVirtualChunk(int posX, int posZ, VirtualBiome defaultBiome) {
return new SimpleChunk(posX, posZ, defaultBiome);
}
@Override
public VirtualChunk createVirtualChunk(int posX, int posZ, BuiltInBiome defaultBiome) {
return new SimpleChunk(posX, posZ, Biome.of(defaultBiome));
}
@Override
public Limbo createLimbo(VirtualWorld world) {
return new LimboImpl(this, world);
}
@Override
public void releasePreparedPacketThread(Thread thread) {
this.preparedPacketFactory.releaseThread(thread);
}
@Override
public PreparedPacket createPreparedPacket() {
return (PreparedPacket) this.preparedPacketFactory.createPreparedPacket(this.minVersion, this.maxVersion);
}
@Override
public PreparedPacket createPreparedPacket(ProtocolVersion minVersion, ProtocolVersion maxVersion) {
return (PreparedPacket) this.preparedPacketFactory.createPreparedPacket(minVersion, maxVersion);
}
@Override
public PreparedPacket createConfigPreparedPacket() {
return (PreparedPacket) this.configPreparedPacketFactory.createPreparedPacket(this.minVersion, this.maxVersion);
}
@Override
public PreparedPacket createConfigPreparedPacket(ProtocolVersion minVersion, ProtocolVersion maxVersion) {
return (PreparedPacket) this.configPreparedPacketFactory.createPreparedPacket(minVersion, maxVersion);
}
public ByteBuf encodeSingleLogin(MinecraftPacket packet, ProtocolVersion version) {
return this.loginPreparedPacketFactory.encodeSingle(packet, version);
}
public ByteBuf encodeSingleLoginUncompressed(MinecraftPacket packet, ProtocolVersion version) {
return this.loginUncompressedPreparedPacketFactory.encodeSingle(packet, version);
}
public void inject3rdParty(Player player, MinecraftConnection connection, ChannelPipeline pipeline) {
StateRegistry state = connection.getState();
if (connection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0
|| (state != StateRegistry.CONFIG && state != StateRegistry.LOGIN)) {
this.preparedPacketFactory.inject(player, connection, pipeline);
} else {
this.configPreparedPacketFactory.inject(player, connection, pipeline);
}
}
public void setState(MinecraftConnection connection, StateRegistry stateRegistry) {
connection.setState(stateRegistry);
this.setEncoderState(connection, stateRegistry);
this.fixDecoderState(connection, stateRegistry);
}
public void setActiveSessionHandler(MinecraftConnection connection, StateRegistry stateRegistry,
MinecraftSessionHandler sessionHandler) {
connection.setActiveSessionHandler(stateRegistry, sessionHandler);
this.setEncoderState(connection, stateRegistry);
this.fixDecoderState(connection, stateRegistry);
}
public void setEncoderState(MinecraftConnection connection, StateRegistry state) {
// As CONFIG state was added in 1.20.2, no need to track it for lower versions
if (connection.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_20_2) < 0) {
return;
}
if (Settings.IMP.MAIN.COMPATIBILITY_MODE) {
MinecraftEncoder encoder = connection.getChannel().pipeline().get(MinecraftEncoder.class);
if (encoder != null) {
encoder.setState(state);
}
}
PreparedPacketEncoder encoder = connection.getChannel().pipeline().get(PreparedPacketEncoder.class);
if (encoder != null) {
if (state != StateRegistry.CONFIG && state != StateRegistry.LOGIN) {
encoder.setFactory(this.preparedPacketFactory);
} else {
encoder.setFactory(this.configPreparedPacketFactory);
}
}
}
public void fixDecoderState(MinecraftConnection connection, StateRegistry state) {
if (state.name() == null) { // custom state
MinecraftDecoder decoder = connection.getChannel().pipeline().get(MinecraftDecoder.class);
if (decoder != null) {
try {
// Let decoder know what we're in PLAY state, or it will kick the player.
STATE_FIELD.invokeExact(decoder, StateRegistry.PLAY);
} catch (Throwable throwable) {
LimboAPI.getLogger().error("Failed to fixup decoder", throwable);
}
}
}
}
public void deject3rdParty(ChannelPipeline pipeline) {
this.preparedPacketFactory.deject(pipeline);
}
public void fixDecompressor(ChannelPipeline pipeline, int threshold, boolean onLogin) {
ChannelHandler decoder;
if (onLogin && Settings.IMP.MAIN.DISCARD_COMPRESSION_ON_LOGIN) {
decoder = new MinecraftDiscardCompressDecoder();
} else if (!onLogin && Settings.IMP.MAIN.DISCARD_COMPRESSION_AFTER_LOGIN) {
decoder = new MinecraftDiscardCompressDecoder();
} else {
int level = this.server.getConfiguration().getCompressionLevel();
VelocityCompressor compressor = Natives.compress.get().create(level);
decoder = new MinecraftLimitedCompressDecoder(threshold, compressor);
}
if (Settings.IMP.MAIN.COMPATIBILITY_MODE && pipeline.context(Connections.COMPRESSION_DECODER) != null) {
pipeline.replace(Connections.COMPRESSION_DECODER, Connections.COMPRESSION_DECODER, decoder);
} else {
pipeline.addBefore(Connections.MINECRAFT_DECODER, Connections.COMPRESSION_DECODER, decoder);
}
}
public void fixCompressor(ChannelPipeline pipeline, ProtocolVersion version) {
ChannelHandler compressionHandler = pipeline.get(Connections.COMPRESSION_ENCODER);
if (compressionHandler == null) {
if (!Settings.IMP.MAIN.COMPATIBILITY_MODE) {
pipeline.addBefore(Connections.MINECRAFT_DECODER, Connections.FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE);
}
} else {
int level = this.server.getConfiguration().getCompressionLevel();
int compressionThreshold = this.server.getConfiguration().getCompressionThreshold();
VelocityCompressor compressor = Natives.compress.get().create(level);
if (!Settings.IMP.MAIN.COMPATIBILITY_MODE) {
MinecraftCompressorAndLengthEncoder encoder = new MinecraftCompressorAndLengthEncoder(compressionThreshold, compressor);
pipeline.remove(compressionHandler);
pipeline.addBefore(Connections.MINECRAFT_ENCODER, Connections.COMPRESSION_ENCODER, encoder);
}
if (pipeline.get(Connections.COMPRESSION_DECODER) instanceof LimboCompressDecoder) {
MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(compressionThreshold, compressor);
pipeline.replace(Connections.COMPRESSION_DECODER, Connections.COMPRESSION_DECODER, decoder);
} else if (Settings.IMP.MAIN.COMPATIBILITY_MODE) {
compressor.close();
}
}
}
@Override
public void passLoginLimbo(Player player) {
if (this.loginQueue.containsKey(player)) {
this.loginQueue.get(player).next();
}
}
@Override
public VirtualItem getItem(Item item) {
return SimpleItem.fromItem(item);
}
@Override
public VirtualItem getItem(String itemID) {
return SimpleItem.fromModernID(itemID);
}
@Override
public VirtualItem getLegacyItem(int itemLegacyID) {
return SimpleItem.fromLegacyID(itemLegacyID);
}
@Override
public ItemComponentMap createItemComponentMap() {
return new SimpleItemComponentMap(this.itemComponentManager);
}
@Override
public VirtualBlockEntity getBlockEntity(String entityID) {
return SimpleBlockEntity.fromModernID(entityID);
}
@Override
public PacketFactory getPacketFactory() {
return this.packetFactory;
}
public VelocityServer getServer() {
return this.server;
}
public void setLimboJoined(Player player) {
if (!this.isLimboJoined(player)) {
ConnectedPlayer connectedPlayer = (ConnectedPlayer) player;
connectedPlayer.getPhase().onFirstJoin(connectedPlayer);
this.players.add(player);
}
}
public void unsetLimboJoined(Player player) {
this.players.remove(player);
}
public boolean isLimboJoined(Player player) {
return this.players.contains(player);
}
public CachedPackets getPackets() {
return this.packets;
}
public void addLoginQueue(Player player, LoginTasksQueue queue) {
this.loginQueue.put(player, queue);
}
public void removeLoginQueue(Player player) {
this.loginQueue.remove(player);
}
public boolean hasLoginQueue(Player player) {
return this.loginQueue.containsKey(player);
}
public LoginTasksQueue getLoginQueue(Player player) {
return this.loginQueue.get(player);
}
public void setKickCallback(Player player, Function<KickedFromServerEvent, Boolean> queue) {
this.kickCallback.put(player, queue);
}
public void removeKickCallback(Player player) {
this.kickCallback.remove(player);
}
public Function<KickedFromServerEvent, Boolean> getKickCallback(Player player) {
return this.kickCallback.get(player);
}
public void setNextServer(Player player, RegisteredServer nextServer) {
this.nextServer.put(player, nextServer);
}
public void removeNextServer(Player player) {
this.nextServer.remove(player);
}
public boolean hasNextServer(Player player) {
return this.nextServer.containsKey(player);
}
public RegisteredServer getNextServer(Player player) {
return this.nextServer.get(player);
}
public void setInitialID(Player player, UUID nextServer) {
INITIAL_ID.put(player, nextServer);
}
public void removeInitialID(Player player) {
INITIAL_ID.remove(player);
}
public UUID getInitialID(Player player) {
return INITIAL_ID.get(player);
}
public LoginListener getLoginListener() {
return this.loginListener;
}
public boolean isCompressionEnabled() {
return this.compressionEnabled;
}
public PreparedPacketFactory getPreparedPacketFactory() {
return this.preparedPacketFactory;
}
public ProtocolVersion getPrepareMinVersion() {
return this.minVersion;
}
public ProtocolVersion getPrepareMaxVersion() {
return this.maxVersion;
}
public EventManagerHook getEventManagerHook() {
return this.eventManagerHook;
}
@Override
public WorldFile openWorldFile(BuiltInWorldFileType apiType, Path file) throws IOException {
return WorldFileTypeRegistry.fromApiType(apiType, file);
}
@Override
public WorldFile openWorldFile(BuiltInWorldFileType apiType, InputStream stream) throws IOException {
return WorldFileTypeRegistry.fromApiType(apiType, stream);
}
@Override
public WorldFile openWorldFile(BuiltInWorldFileType apiType, CompoundBinaryTag tag) {
return WorldFileTypeRegistry.fromApiType(apiType, tag);
}
private static void setLogger(Logger logger) {
LOGGER = logger;
}
public static Logger getLogger() {
return LOGGER;
}
private static void setSerializer(Serializer serializer) {
SERIALIZER = serializer;
}
public static Serializer getSerializer() {
return SERIALIZER;
}
static {
try {
STATE_FIELD = MethodHandles.privateLookupIn(MinecraftDecoder.class, MethodHandles.lookup())
.findSetter(MinecraftDecoder.class, "state", StateRegistry.class);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}