-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use HandlerListCollection for global event listener
- Loading branch information
Showing
3 changed files
with
116 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...t-platform/src/main/java/io/fairyproject/bukkit/events/handler/HandlerListCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package io.fairyproject.bukkit.events.handler; | ||
|
||
import io.fairyproject.bukkit.FairyBukkitPlatform; | ||
import io.fairyproject.bukkit.events.GlobalEventListener; | ||
import org.bukkit.event.*; | ||
import org.bukkit.plugin.EventExecutor; | ||
import org.bukkit.plugin.RegisteredListener; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.ListIterator; | ||
|
||
public class HandlerListCollection extends ArrayList<HandlerList> implements Listener, EventExecutor { | ||
|
||
private static final boolean HAS_HANDSHAKE_EVENT; | ||
|
||
private final ArrayList<HandlerList> forward; | ||
private final GlobalEventListener globalEventListener; | ||
|
||
static { | ||
boolean hasHandshakeEvent = false; | ||
try { | ||
Class.forName("com.destroystokyo.paper.event.player.PlayerHandshakeEvent"); | ||
hasHandshakeEvent = true; | ||
} catch (ClassNotFoundException ignored) { | ||
} | ||
HAS_HANDSHAKE_EVENT = hasHandshakeEvent; | ||
} | ||
|
||
public HandlerListCollection(ArrayList<HandlerList> forward, GlobalEventListener globalEventListener) { | ||
this.forward = forward; | ||
this.globalEventListener = globalEventListener; | ||
|
||
for (HandlerList handlerList : this.forward) { | ||
registerHandlerList(handlerList); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean add(HandlerList handlerList) { | ||
boolean result = forward.add(handlerList); | ||
if (result) { | ||
registerHandlerList(handlerList); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private void registerHandlerList(HandlerList handlerList) { | ||
handlerList.register(new RegisteredListener( | ||
this, | ||
this, | ||
EventPriority.NORMAL, | ||
FairyBukkitPlatform.PLUGIN, | ||
false | ||
)); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object o) { | ||
return forward.remove(o); | ||
} | ||
|
||
@Override | ||
public HandlerList remove(int index) { | ||
return forward.remove(index); | ||
} | ||
|
||
@Override | ||
public @NotNull Iterator<HandlerList> iterator() { | ||
return forward.iterator(); | ||
} | ||
|
||
@Override | ||
public @NotNull ListIterator<HandlerList> listIterator() { | ||
return forward.listIterator(); | ||
} | ||
|
||
@Override | ||
public @NotNull ListIterator<HandlerList> listIterator(int index) { | ||
return forward.listIterator(index); | ||
} | ||
|
||
@Override | ||
public void execute(@NotNull Listener listener, @NotNull Event event) { | ||
if (HAS_HANDSHAKE_EVENT) { | ||
if (event instanceof com.destroystokyo.paper.event.player.PlayerHandshakeEvent) { | ||
com.destroystokyo.paper.event.player.PlayerHandshakeEvent handshakeEvent = (com.destroystokyo.paper.event.player.PlayerHandshakeEvent) event; | ||
HandlerList handlers = handshakeEvent.getHandlers(); | ||
if (handlers.getRegisteredListeners().length == 1) { | ||
handshakeEvent.setCancelled(true); | ||
handlers.unregister(this); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
globalEventListener.onEventFired(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version = 0.7.8b5-SNAPSHOT | ||
version = 0.7.8b7-SNAPSHOT |