Skip to content

Commit af21eab

Browse files
committedJul 28, 2023
Custom event bus
1 parent f23d745 commit af21eab

29 files changed

+268
-241
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package me.illusion.skyblockcore.common.event;
2+
3+
public abstract class SkyblockEvent {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.illusion.skyblockcore.common.event.impl;
2+
3+
import me.illusion.skyblockcore.common.event.SkyblockEvent;
4+
import me.illusion.skyblockcore.common.platform.SkyblockPlatform;
5+
6+
public class SkyblockPlatformEnabledEvent extends SkyblockEvent {
7+
8+
private final SkyblockPlatform platform;
9+
10+
public SkyblockPlatformEnabledEvent(SkyblockPlatform plugin) {
11+
this.platform = plugin;
12+
}
13+
14+
public SkyblockPlatform getPlatform() {
15+
return platform;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package me.illusion.skyblockcore.common.event.listener;
2+
3+
import me.illusion.skyblockcore.common.event.SkyblockEvent;
4+
5+
public interface SkyblockEventListener<T extends SkyblockEvent> {
6+
7+
void onEvent(T event);
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.illusion.skyblockcore.common.event.manager;
2+
3+
import me.illusion.skyblockcore.common.event.SkyblockEvent;
4+
import me.illusion.skyblockcore.common.event.listener.SkyblockEventListener;
5+
6+
public interface SkyblockEventManager {
7+
8+
<T extends SkyblockEvent> void subscribe(Class<T> eventClass, SkyblockEventListener<T> listener);
9+
10+
<T extends SkyblockEvent> void callEvent(T event);
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.illusion.skyblockcore.common.event.manager;
2+
3+
import java.util.Set;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
import me.illusion.skyblockcore.common.event.SkyblockEvent;
6+
import me.illusion.skyblockcore.common.event.listener.SkyblockEventListener;
7+
8+
public class SkyblockEventManagerImpl implements SkyblockEventManager {
9+
10+
private final Set<SkyblockEventHandler<?>> handlers = ConcurrentHashMap.newKeySet();
11+
12+
@Override
13+
public <T extends SkyblockEvent> void subscribe(Class<T> eventClass, SkyblockEventListener<T> listener) {
14+
handlers.add(new SkyblockEventHandler<>(eventClass, listener));
15+
}
16+
17+
@Override
18+
public <T extends SkyblockEvent> void callEvent(T event) {
19+
for (SkyblockEventHandler<?> handler : handlers) {
20+
Class<?> eventClass = handler.getEventClass();
21+
22+
if (!(eventClass.isAssignableFrom(event.getClass()))) {
23+
continue;
24+
}
25+
26+
SkyblockEventHandler<T> castedHandler = (SkyblockEventHandler<T>) handler;
27+
castedHandler.accept(event);
28+
}
29+
}
30+
31+
private static class SkyblockEventHandler<T extends SkyblockEvent> {
32+
33+
private final Class<T> eventClass;
34+
private final SkyblockEventListener<T> listener;
35+
36+
public SkyblockEventHandler(Class<T> eventClass, SkyblockEventListener<T> listener) {
37+
this.eventClass = eventClass;
38+
this.listener = listener;
39+
}
40+
41+
public Class<T> getEventClass() {
42+
return eventClass;
43+
}
44+
45+
public void accept(T event) {
46+
listener.onEvent(event);
47+
}
48+
}
49+
}

‎SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/platform/SkyblockPlatform.java

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.util.logging.Logger;
55
import me.illusion.skyblockcore.common.database.SkyblockDatabaseRegistry;
6+
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManager;
67
import me.illusion.skyblockcore.common.profile.SkyblockProfileCache;
78

89
/**
@@ -39,5 +40,12 @@ public interface SkyblockPlatform {
3940
*/
4041
SkyblockProfileCache getProfileCache();
4142

43+
/**
44+
* Gets the event manager for the platform
45+
*
46+
* @return The event manager
47+
*/
48+
SkyblockEventManager getEventManager();
49+
4250

4351
}

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/island/SkyblockIslandEvent.java ‎SkyblockCore-Server/src/main/java/me/illusion/skyblockcore/server/event/island/SkyblockIslandEvent.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package me.illusion.skyblockcore.spigot.event.island;
1+
package me.illusion.skyblockcore.server.event.island;
22

3+
import me.illusion.skyblockcore.common.event.SkyblockEvent;
34
import me.illusion.skyblockcore.server.island.SkyblockIsland;
4-
import me.illusion.skyblockcore.spigot.event.SkyblockEvent;
55

66
public abstract class SkyblockIslandEvent extends SkyblockEvent {
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package me.illusion.skyblockcore.server.event.island;
2+
3+
import me.illusion.skyblockcore.server.island.SkyblockIsland;
4+
5+
public class SkyblockIslandLoadEvent extends SkyblockIslandEvent {
6+
7+
public SkyblockIslandLoadEvent(SkyblockIsland island) {
8+
super(island);
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package me.illusion.skyblockcore.server.event.island;
2+
3+
import me.illusion.skyblockcore.server.island.SkyblockIsland;
4+
5+
public class SkyblockIslandUnloadEvent extends SkyblockIslandEvent {
6+
7+
public SkyblockIslandUnloadEvent(SkyblockIsland island) {
8+
super(island);
9+
}
10+
11+
}

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/player/SkyblockPlayerEvent.java ‎SkyblockCore-Server/src/main/java/me/illusion/skyblockcore/server/event/player/SkyblockPlayerEvent.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
package me.illusion.skyblockcore.spigot.event.player;
1+
package me.illusion.skyblockcore.server.event.player;
22

33
import java.util.UUID;
4-
import me.illusion.skyblockcore.spigot.event.SkyblockEvent;
5-
import org.bukkit.entity.Player;
4+
import me.illusion.skyblockcore.common.event.SkyblockEvent;
65

76
/**
87
* Generic Skyblock player event. Contains the player and their chosen profile ID.
98
*/
109
public abstract class SkyblockPlayerEvent extends SkyblockEvent {
1110

12-
private final Player player;
11+
private final UUID playerId;
1312
private final UUID profileId;
1413

15-
public SkyblockPlayerEvent(Player player, UUID profileId) {
16-
this.player = player;
14+
public SkyblockPlayerEvent(UUID playerId, UUID profileId) {
15+
this.playerId = playerId;
1716
this.profileId = profileId;
1817
}
1918

20-
public Player getPlayer() {
21-
return player;
19+
public UUID getPlayerId() {
20+
return playerId;
2221
}
2322

2423
public UUID getProfileId() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package me.illusion.skyblockcore.server.event.player;
2+
3+
import java.util.UUID;
4+
5+
/**
6+
* This event is called when a player joins the server. Contains the player and their chosen profile ID. This event may be called multiple times for the same
7+
* player if they switch profiles.
8+
*/
9+
public class SkyblockPlayerJoinEvent extends SkyblockPlayerEvent {
10+
11+
public SkyblockPlayerJoinEvent(UUID playerId, UUID profileId) {
12+
super(playerId, profileId);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package me.illusion.skyblockcore.server.event.player;
2+
3+
import java.util.UUID;
4+
5+
/**
6+
* This event is called when a player quits the server. Contains the player and their chosen profile ID. This event may be called multiple times for the same
7+
* player if they switch profiles.
8+
*/
9+
public class SkyblockPlayerQuitEvent extends SkyblockPlayerEvent {
10+
11+
public SkyblockPlayerQuitEvent(UUID playerId, UUID profileId) {
12+
super(playerId, profileId);
13+
}
14+
15+
}

‎SkyblockCore-Server/src/main/java/me/illusion/skyblockcore/server/island/AbstractIslandManager.java

+3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ public abstract class AbstractIslandManager implements SkyblockIslandManager {
1919

2020
protected final SkyblockFetchingDatabase database;
2121
protected final SkyblockProfileCache profileCache;
22+
protected final SkyblockPlatform platform;
2223

2324
public AbstractIslandManager(SkyblockPlatform platform) {
25+
this.platform = platform;
26+
2427
this.database = platform.getDatabaseRegistry().getChosenDatabase();
2528
this.profileCache = platform.getProfileCache();
2629
}

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/SkyblockSpigotPlugin.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import me.illusion.cosmos.utilities.command.command.CommandManager;
66
import me.illusion.cosmos.utilities.storage.MessagesFile;
77
import me.illusion.skyblockcore.common.database.SkyblockDatabaseRegistry;
8+
import me.illusion.skyblockcore.common.event.impl.SkyblockPlatformEnabledEvent;
9+
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManager;
10+
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManagerImpl;
811
import me.illusion.skyblockcore.common.profile.SkyblockProfileCache;
912
import me.illusion.skyblockcore.server.SkyblockServerPlatform;
1013
import me.illusion.skyblockcore.server.island.SkyblockIslandManager;
@@ -14,7 +17,6 @@
1417
import me.illusion.skyblockcore.spigot.config.SkyblockDatabasesFile;
1518
import me.illusion.skyblockcore.spigot.config.cosmos.SkyblockCosmosSetupFile;
1619
import me.illusion.skyblockcore.spigot.cosmos.SkyblockCosmosSetup;
17-
import me.illusion.skyblockcore.spigot.event.startup.SkyblockEnabledEvent;
1820
import me.illusion.skyblockcore.spigot.grid.SkyblockGridRegistry;
1921
import me.illusion.skyblockcore.spigot.island.IslandManagerImpl;
2022
import me.illusion.skyblockcore.spigot.network.SkyblockNetworkRegistryImpl;
@@ -45,6 +47,7 @@ public class SkyblockSpigotPlugin extends JavaPlugin implements SkyblockServerPl
4547
private SkyblockIslandManager islandManager;
4648
private SkyblockNetworkRegistry networkRegistry;
4749
private SkyblockProfileCache profileCache;
50+
private SkyblockEventManager eventManager;
4851

4952
@Override
5053
public void onEnable() {
@@ -60,6 +63,7 @@ public void onEnable() {
6063
gridRegistry = new SkyblockGridRegistry();
6164

6265
islandManager = new IslandManagerImpl(this);
66+
eventManager = new SkyblockEventManagerImpl();
6367

6468
registerNetworks();
6569

@@ -92,7 +96,7 @@ private void finishLoading() {
9296
}
9397

9498
networkRegistry.enable();
95-
Bukkit.getPluginManager().callEvent(new SkyblockEnabledEvent(this));
99+
eventManager.callEvent(new SkyblockPlatformEnabledEvent(this));
96100
});
97101

98102
}
@@ -117,4 +121,5 @@ private void initCosmos() {
117121
public void setProfileCache(SkyblockProfileCache profileCache) {
118122
this.profileCache = profileCache;
119123
}
124+
120125
}

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/SkyblockEvent.java

-25
This file was deleted.

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/island/SkyblockIslandLoadEvent.java

-24
This file was deleted.

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/island/SkyblockIslandUnloadEvent.java

-24
This file was deleted.

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/player/SkyblockPlayerJoinEvent.java

-29
This file was deleted.

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/player/SkyblockPlayerQuitEvent.java

-29
This file was deleted.

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/event/startup/SkyblockEnabledEvent.java

-32
This file was deleted.

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/island/IslandManagerImpl.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import me.illusion.cosmos.template.TemplatedArea;
77
import me.illusion.skyblockcore.common.data.IslandData;
88
import me.illusion.skyblockcore.common.utilities.time.Time;
9+
import me.illusion.skyblockcore.server.event.island.SkyblockIslandLoadEvent;
10+
import me.illusion.skyblockcore.server.event.island.SkyblockIslandUnloadEvent;
911
import me.illusion.skyblockcore.server.island.AbstractIslandManager;
1012
import me.illusion.skyblockcore.server.island.SkyblockIsland;
1113
import me.illusion.skyblockcore.spigot.SkyblockSpigotPlugin;
1214
import me.illusion.skyblockcore.spigot.cosmos.SkyblockCosmosSetup;
13-
import me.illusion.skyblockcore.spigot.event.island.SkyblockIslandLoadEvent;
14-
import me.illusion.skyblockcore.spigot.event.island.SkyblockIslandUnloadEvent;
1515
import me.illusion.skyblockcore.spigot.utilities.adapter.SkyblockBukkitAdapter;
16-
import org.bukkit.Bukkit;
1716

1817
/**
1918
* Manages islands. The lifecycle of an island is tied to a CosmosSession, which means that if the session is destroyed, the island is destroyed.
@@ -147,7 +146,7 @@ private CompletableFuture<SkyblockIsland> loadFromTemplate(UUID islandId, Island
147146
SkyblockIsland island = new SkyblockIsland(data, SkyblockBukkitAdapter.toSkyblockLocation(session.getPastedArea().getPasteLocation()));
148147
loadedIslands.put(islandId, island);
149148

150-
Bukkit.getPluginManager().callEvent(new SkyblockIslandLoadEvent(island));
149+
platform.getEventManager().callEvent(new SkyblockIslandLoadEvent(island));
151150

152151
return island;
153152
}));
@@ -163,7 +162,7 @@ private void removeInternal(UUID islandId) {
163162
SkyblockIsland island = loadedIslands.remove(islandId);
164163

165164
if (island != null) {
166-
Bukkit.getPluginManager().callEvent(new SkyblockIslandUnloadEvent(island));
165+
platform.getEventManager().callEvent(new SkyblockIslandUnloadEvent(island));
167166
}
168167

169168
unloadingIslands.remove(islandId);

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/network/complex/ComplexSkyblockNetwork.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.illusion.skyblockcore.common.communication.packet.PacketManager;
66
import me.illusion.skyblockcore.common.database.cache.SkyblockCacheDatabase;
77
import me.illusion.skyblockcore.common.database.fetching.SkyblockFetchingDatabase;
8+
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManager;
89
import me.illusion.skyblockcore.server.island.SkyblockIslandManager;
910
import me.illusion.skyblockcore.server.network.SkyblockNetworkStructure;
1011
import me.illusion.skyblockcore.spigot.SkyblockSpigotPlugin;
@@ -66,9 +67,9 @@ public String getName() {
6667
// Main startup logic
6768

6869
private void registerListeners() {
69-
registerListener(new ComplexPlayerJoinListener(this));
70-
registerListener(new ComplexIslandLoadListener(this));
71-
registerListener(new ComplexIslandUnloadListener(this));
70+
new ComplexPlayerJoinListener(this);
71+
new ComplexIslandLoadListener(this);
72+
new ComplexIslandUnloadListener(this);
7273
}
7374

7475
private void registerPacketHandlers() {
@@ -125,4 +126,8 @@ public SkyblockCacheDatabase getCacheDatabase() {
125126
public ComplexNetworkConfiguration getConfiguration() {
126127
return configuration;
127128
}
129+
130+
public SkyblockEventManager getEventManager() {
131+
return plugin.getEventManager();
132+
}
128133
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package me.illusion.skyblockcore.spigot.network.complex.listener;
22

3-
import me.illusion.skyblockcore.spigot.event.island.SkyblockIslandLoadEvent;
3+
import me.illusion.skyblockcore.server.event.island.SkyblockIslandLoadEvent;
44
import me.illusion.skyblockcore.spigot.network.complex.ComplexSkyblockNetwork;
5-
import org.bukkit.event.EventHandler;
6-
import org.bukkit.event.Listener;
75

86
/**
97
* This class is responsible for letting the communications handler know when an island has been loaded, so all other servers are aware of this island's
108
* existence and current server id.
119
*/
12-
public class ComplexIslandLoadListener implements Listener {
10+
public class ComplexIslandLoadListener {
1311

1412
private final ComplexSkyblockNetwork network;
1513

1614
public ComplexIslandLoadListener(ComplexSkyblockNetwork network) {
1715
this.network = network;
16+
17+
network.getEventManager().subscribe(SkyblockIslandLoadEvent.class, this::handle);
1818
}
1919

20-
@EventHandler
21-
private void onIslandLoad(SkyblockIslandLoadEvent event) {
20+
private void handle(SkyblockIslandLoadEvent event) {
2221
network.getCommunicationsHandler().updateIslandServer(event.getIsland());
2322
}
2423
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package me.illusion.skyblockcore.spigot.network.complex.listener;
22

3-
import me.illusion.skyblockcore.spigot.event.island.SkyblockIslandLoadEvent;
3+
import me.illusion.skyblockcore.server.event.island.SkyblockIslandLoadEvent;
44
import me.illusion.skyblockcore.spigot.network.complex.ComplexSkyblockNetwork;
5-
import org.bukkit.event.EventHandler;
6-
import org.bukkit.event.Listener;
75

86
/**
97
* This class is responsible for letting the communications handler know when an island has been unloaded, so all other servers are aware this island is no
108
* longer loaded.
119
*/
12-
public class ComplexIslandUnloadListener implements Listener {
10+
public class ComplexIslandUnloadListener {
1311

1412
private final ComplexSkyblockNetwork network;
1513

1614
public ComplexIslandUnloadListener(ComplexSkyblockNetwork network) {
1715
this.network = network;
16+
17+
network.getEventManager().subscribe(SkyblockIslandLoadEvent.class, this::handle);
1818
}
1919

20-
@EventHandler
21-
private void onIslandLoad(SkyblockIslandLoadEvent event) {
20+
private void handle(SkyblockIslandLoadEvent event) {
2221
network.getCommunicationsHandler().removeIsland(event.getIsland().getIslandId());
2322
}
2423
}

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/network/complex/listener/ComplexPlayerJoinListener.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,39 @@
22

33
import java.util.UUID;
44
import me.illusion.cosmos.utilities.concurrency.MainThreadExecutor;
5+
import me.illusion.skyblockcore.server.event.player.SkyblockPlayerJoinEvent;
56
import me.illusion.skyblockcore.server.island.SkyblockIsland;
6-
import me.illusion.skyblockcore.spigot.event.player.SkyblockPlayerJoinEvent;
7+
import me.illusion.skyblockcore.server.island.SkyblockIslandManager;
78
import me.illusion.skyblockcore.spigot.network.complex.ComplexSkyblockNetwork;
89
import me.illusion.skyblockcore.spigot.utilities.adapter.SkyblockBukkitAdapter;
10+
import org.bukkit.Bukkit;
911
import org.bukkit.entity.Player;
10-
import org.bukkit.event.EventHandler;
11-
import org.bukkit.event.Listener;
1212

1313
/**
1414
* This class is responsible for loading the player's island when they join the server. If another instance is responsible for the island, we do not load it.
1515
* Feel free to fork or modify this code so you can handle rejection, if island visitation is not a feature.
1616
*/
17-
public class ComplexPlayerJoinListener implements Listener {
17+
public class ComplexPlayerJoinListener {
1818

1919
private final ComplexSkyblockNetwork network;
20+
private final SkyblockIslandManager islandManager;
2021

2122
public ComplexPlayerJoinListener(ComplexSkyblockNetwork network) {
2223
this.network = network;
24+
this.islandManager = network.getIslandManager();
25+
26+
network.getEventManager().subscribe(SkyblockPlayerJoinEvent.class, this::handle);
2327
}
2428

25-
@EventHandler
26-
private void onJoin(SkyblockPlayerJoinEvent event) {
27-
Player player = event.getPlayer();
29+
private void handle(SkyblockPlayerJoinEvent event) {
30+
Player player = Bukkit.getPlayer(event.getPlayerId());
31+
UUID profileId = event.getProfileId();
32+
33+
if (player == null) {
34+
return;
35+
}
2836

29-
SkyblockIsland cached = network.getIslandManager().getProfileIsland(event.getProfileId());
37+
SkyblockIsland cached = islandManager.getProfileIsland(profileId);
3038

3139
if (cached != null) {
3240
player.teleport(SkyblockBukkitAdapter.toBukkitLocation(cached.getCenter()));
@@ -35,17 +43,17 @@ private void onJoin(SkyblockPlayerJoinEvent event) {
3543

3644
// We try to fetch the island id, and see if we can load it. If we can, we load it.
3745
network.getDatabase()
38-
.fetchIslandId(event.getProfileId()) // Fetch the island id
46+
.fetchIslandId(profileId) // Fetch the island id
3947
.thenCompose(islandId -> network.getCommunicationsHandler().canLoad(islandId)) // Check if we can load the island
4048
.thenAccept(allowed -> { // If we can load the island, we load it.
4149
if (allowed) {
42-
tryLoadDefault(event.getProfileId(), player);
50+
tryLoadDefault(profileId, player);
4351
}
4452
});
4553
}
4654

4755
private void tryLoadDefault(UUID profileId, Player player) {
48-
network.getIslandManager().loadPlayerIsland(profileId, "default").thenAcceptAsync(island -> {
56+
islandManager.loadPlayerIsland(profileId, "default").thenAcceptAsync(island -> {
4957

5058
player.teleport(SkyblockBukkitAdapter.toBukkitLocation(island.getCenter()));
5159

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/network/simple/SimpleSkyblockNetwork.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.illusion.cosmos.utilities.command.command.impl.AdvancedCommand;
44
import me.illusion.cosmos.utilities.storage.MessagesFile;
5+
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManager;
56
import me.illusion.skyblockcore.server.island.SkyblockIslandManager;
67
import me.illusion.skyblockcore.server.network.SkyblockNetworkStructure;
78
import me.illusion.skyblockcore.spigot.SkyblockSpigotPlugin;
@@ -56,8 +57,8 @@ private void registerProfileCache() {
5657
}
5758

5859
private void registerListeners() {
59-
registerListener(new SimplePlayerJoinListener(this));
60-
registerListener(new SimplePlayerQuitListener(this));
60+
new SimplePlayerJoinListener(this);
61+
new SimplePlayerQuitListener(this);
6162
}
6263

6364
private void registerCommands() {
@@ -89,4 +90,8 @@ public MessagesFile getMessages() {
8990
public SimpleNetworkConfiguration getConfiguration() {
9091
return configuration;
9192
}
93+
94+
public SkyblockEventManager getEventManager() {
95+
return plugin.getEventManager();
96+
}
9297
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
package me.illusion.skyblockcore.spigot.network.simple.listener;
22

3+
import java.util.UUID;
34
import me.illusion.cosmos.utilities.concurrency.MainThreadExecutor;
4-
import me.illusion.skyblockcore.spigot.event.player.SkyblockPlayerJoinEvent;
5+
import me.illusion.skyblockcore.server.event.player.SkyblockPlayerJoinEvent;
6+
import me.illusion.skyblockcore.server.island.SkyblockIslandManager;
57
import me.illusion.skyblockcore.spigot.network.simple.SimpleSkyblockNetwork;
68
import me.illusion.skyblockcore.spigot.utilities.adapter.SkyblockBukkitAdapter;
9+
import org.bukkit.Bukkit;
710
import org.bukkit.entity.Player;
8-
import org.bukkit.event.EventHandler;
9-
import org.bukkit.event.Listener;
1011

1112
/**
1213
* This is the simple player join listener, which loads the island when the player joins.
1314
*/
14-
public class SimplePlayerJoinListener implements Listener {
15+
public class SimplePlayerJoinListener {
1516

1617
private final SimpleSkyblockNetwork network;
18+
private final SkyblockIslandManager islandManager;
1719

1820
public SimplePlayerJoinListener(SimpleSkyblockNetwork network) {
1921
this.network = network;
22+
this.islandManager = network.getPlugin().getIslandManager();
23+
24+
network.getEventManager().subscribe(SkyblockPlayerJoinEvent.class, this::handle);
2025
}
2126

22-
@EventHandler
23-
private void onJoin(SkyblockPlayerJoinEvent event) {
24-
Player player = event.getPlayer();
27+
private void handle(SkyblockPlayerJoinEvent event) {
28+
Player player = Bukkit.getPlayer(event.getPlayerId());
29+
30+
if (player == null) {
31+
return;
32+
}
33+
34+
UUID profileId = event.getProfileId();
35+
String defaultIslandName = network.getConfiguration().getDefaultIslandName();
2536

26-
network.getPlugin().getIslandManager().loadPlayerIsland(event.getProfileId(), network.getConfiguration().getDefaultIslandName())
27-
.thenAcceptAsync(island -> {
37+
islandManager.loadPlayerIsland(profileId, defaultIslandName).thenAcceptAsync(island -> {
2838

29-
player.teleport(SkyblockBukkitAdapter.toBukkitLocation(island.getCenter()));
39+
player.teleport(SkyblockBukkitAdapter.toBukkitLocation(island.getCenter()));
3040

31-
}, MainThreadExecutor.INSTANCE); // Need to use main thread executor due to async teleportation not being allowed
41+
}, MainThreadExecutor.INSTANCE); // Need to use main thread executor due to async teleportation not being allowed
3242
}
3343

3444
}

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/network/simple/listener/SimplePlayerQuitListener.java

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
package me.illusion.skyblockcore.spigot.network.simple.listener;
22

33
import java.util.UUID;
4+
import me.illusion.skyblockcore.server.event.player.SkyblockPlayerQuitEvent;
45
import me.illusion.skyblockcore.server.island.SkyblockIsland;
56
import me.illusion.skyblockcore.server.island.SkyblockIslandManager;
6-
import me.illusion.skyblockcore.spigot.event.player.SkyblockPlayerQuitEvent;
77
import me.illusion.skyblockcore.spigot.network.simple.SimpleSkyblockNetwork;
88
import org.bukkit.Bukkit;
99
import org.bukkit.entity.Player;
10-
import org.bukkit.event.EventHandler;
11-
import org.bukkit.event.Listener;
1210

1311
/**
1412
* This is the simple player quit listener, which unloads the island when the player quits.
1513
*/
16-
public class SimplePlayerQuitListener implements Listener {
14+
public class SimplePlayerQuitListener {
1715

1816
private final SimpleSkyblockNetwork network;
17+
private final SkyblockIslandManager islandManager;
1918

2019
public SimplePlayerQuitListener(SimpleSkyblockNetwork network) {
2120
this.network = network;
21+
this.islandManager = network.getPlugin().getIslandManager();
22+
23+
network.getEventManager().subscribe(SkyblockPlayerQuitEvent.class, this::handle);
2224
}
2325

24-
@EventHandler
25-
private void onQuit(SkyblockPlayerQuitEvent event) {
26-
Player player = event.getPlayer();
26+
private void handle(SkyblockPlayerQuitEvent event) {
27+
Player player = Bukkit.getPlayer(event.getPlayerId());
2728
UUID profileId = event.getProfileId();
2829

29-
if (profileId == null) {
30+
if (player == null) {
3031
return;
3132
}
3233

33-
SkyblockIslandManager skyblockIslandManager = network.getPlugin().getIslandManager();
34+
if (profileId == null) {
35+
return;
36+
}
3437

35-
SkyblockIsland island = skyblockIslandManager.getProfileIsland(profileId);
38+
SkyblockIsland island = islandManager.getProfileIsland(profileId);
3639

3740
if (island == null) {
3841
return;
3942
}
4043

4144
// Unload the island after a while, this request will be cancelled if we attempt to load the island again, so no worries.
42-
skyblockIslandManager.requestUnloadIsland(island.getIslandId(), true, network.getConfiguration().getUnloadDelay());
43-
45+
islandManager.requestUnloadIsland(island.getIslandId(), true, network.getConfiguration().getUnloadDelay());
4446
player.teleport(Bukkit.getWorlds().get(0).getSpawnLocation()); // Just to be sure
4547
}
4648

‎SkyblockCore-Spigot/src/main/java/me/illusion/skyblockcore/spigot/network/simple/profile/SimpleProfileCache.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import java.util.UUID;
44
import java.util.concurrent.CompletableFuture;
5+
import me.illusion.skyblockcore.common.platform.SkyblockPlatform;
56
import me.illusion.skyblockcore.common.profile.AbstractSkyblockProfileCache;
7+
import me.illusion.skyblockcore.server.event.player.SkyblockPlayerJoinEvent;
8+
import me.illusion.skyblockcore.server.event.player.SkyblockPlayerQuitEvent;
69
import me.illusion.skyblockcore.spigot.SkyblockSpigotPlugin;
7-
import me.illusion.skyblockcore.spigot.event.player.SkyblockPlayerJoinEvent;
8-
import me.illusion.skyblockcore.spigot.event.player.SkyblockPlayerQuitEvent;
910
import org.bukkit.Bukkit;
1011
import org.bukkit.entity.Player;
1112
import org.bukkit.event.EventHandler;
@@ -19,8 +20,11 @@
1920
*/
2021
public class SimpleProfileCache extends AbstractSkyblockProfileCache implements Listener {
2122

23+
private final SkyblockPlatform platform;
24+
2225
public SimpleProfileCache(SkyblockSpigotPlugin plugin) {
2326
super(plugin.getDatabaseRegistry().getChosenDatabase());
27+
this.platform = plugin;
2428
}
2529

2630
@Override
@@ -34,11 +38,11 @@ public CompletableFuture<Void> saveProfileId(UUID playerId, UUID newProfileId) {
3438
return;
3539
}
3640

37-
SkyblockPlayerQuitEvent event = new SkyblockPlayerQuitEvent(player, oldProfileId);
38-
Bukkit.getPluginManager().callEvent(event);
41+
SkyblockPlayerQuitEvent event = new SkyblockPlayerQuitEvent(playerId, oldProfileId);
42+
platform.getEventManager().callEvent(event);
3943

40-
SkyblockPlayerJoinEvent joinEvent = new SkyblockPlayerJoinEvent(player, newProfileId);
41-
Bukkit.getPluginManager().callEvent(joinEvent);
44+
SkyblockPlayerJoinEvent joinEvent = new SkyblockPlayerJoinEvent(playerId, newProfileId);
45+
platform.getEventManager().callEvent(joinEvent);
4246
});
4347
}
4448

@@ -48,8 +52,8 @@ private void onJoin(PlayerJoinEvent event) {
4852
UUID playerId = player.getUniqueId();
4953

5054
cacheProfileId(playerId).thenAccept(profileId -> {
51-
SkyblockPlayerJoinEvent joinEvent = new SkyblockPlayerJoinEvent(player, profileId);
52-
Bukkit.getPluginManager().callEvent(joinEvent);
55+
SkyblockPlayerJoinEvent joinEvent = new SkyblockPlayerJoinEvent(playerId, profileId);
56+
platform.getEventManager().callEvent(joinEvent);
5357
});
5458
}
5559

@@ -64,8 +68,8 @@ private void onQuit(PlayerQuitEvent event) {
6468
return;
6569
}
6670

67-
SkyblockPlayerQuitEvent quitEvent = new SkyblockPlayerQuitEvent(player, cachedProfileId);
68-
Bukkit.getPluginManager().callEvent(quitEvent);
71+
SkyblockPlayerQuitEvent quitEvent = new SkyblockPlayerQuitEvent(playerId, cachedProfileId);
72+
platform.getEventManager().callEvent(quitEvent);
6973

7074
deleteFromCache(playerId);
7175
}

0 commit comments

Comments
 (0)
Please sign in to comment.