2424import org .bukkit .util .Vector ;
2525import org .bukkit .event .block .BlockPhysicsEvent ;
2626import org .bukkit .event .player .PlayerTeleportEvent ;
27+ import org .bukkit .event .player .PlayerChangedWorldEvent ;
2728
2829import java .io .*;
2930import java .nio .charset .StandardCharsets ;
@@ -45,6 +46,7 @@ public class TuffX extends JavaPlugin implements Listener, PluginMessageListener
4546
4647 private final Set <UUID > awaitingInitialBatch = ConcurrentHashMap .newKeySet ();
4748 private final Map <UUID , AtomicInteger > initialChunksToProcess = new ConcurrentHashMap <>();
49+ private Set <String > enabledWorlds ;
4850
4951 private BukkitTask processorTask ;
5052
@@ -59,6 +61,9 @@ public void onEnable() {
5961 saveDefaultConfig ();
6062 this .CHUNKS_PER_TICK = getConfig ().getInt ("chunks-per-tick" , 6 );
6163 this .debug = getConfig ().getBoolean ("debug-mode" , false );
64+ this .enabledWorlds = new HashSet <>(getConfig ().getStringList ("enabled-worlds" ));
65+
66+ logDebug ("TuffX will be active in the following worlds: " + String .join (", " , this .enabledWorlds ));
6267
6368 getServer ().getMessenger ().registerOutgoingPluginChannel (this , CHANNEL );
6469 getServer ().getMessenger ().registerIncomingPluginChannel (this , CHANNEL , this );
@@ -100,6 +105,12 @@ private void handleSingleChunkRequest(Player player, int chunkX, int chunkZ, UUI
100105
101106 private void handleIncomingPacket (Player player , Location loc , String action , int chunkX , int chunkZ , DataInputStream in ) throws IOException {
102107 UUID playerId = player .getUniqueId ();
108+
109+ if (!enabledWorlds .contains (player .getWorld ().getName ())) {
110+ if (awaitingInitialBatch .contains (playerId )) player .sendPluginMessage (this , CHANNEL , createLoadFinishedPayload ());
111+ return ;
112+ }
113+
103114 switch (action .toLowerCase ()) {
104115 case "request_chunk" :
105116 handleSingleChunkRequest (player ,chunkX ,chunkZ ,playerId );
@@ -183,6 +194,25 @@ public void run() {
183194 }.runTaskTimer (this , 0L , 1L );
184195 }
185196
197+ @ EventHandler (priority = EventPriority .MONITOR )
198+ public void onPlayerChangeWorld (PlayerChangedWorldEvent event ) {
199+ Player player = event .getPlayer ();
200+ UUID playerId = player .getUniqueId ();
201+
202+ Queue <Vector > playerQueue = requestQueue .get (playerId );
203+
204+ if (playerQueue != null && !playerQueue .isEmpty ()) {
205+ logDebug ("Player " + player .getName () + " changed worlds. Clearing " + playerQueue .size () + " pending chunk requests from their old world." );
206+ playerQueue .clear ();
207+ }
208+
209+ if (awaitingInitialBatch .remove (playerId )) {
210+ logDebug ("Player " + player .getName () + " changed worlds during initial load. Cancelling." );
211+ player .sendPluginMessage (this , CHANNEL , createLoadFinishedPayload ());
212+ initialChunksToProcess .remove (playerId );
213+ }
214+ }
215+
186216 private void processAndSendChunk (final Player player , final Chunk chunk ) {
187217 if (chunk == null || !player .isOnline ()) return ;
188218
0 commit comments