Skip to content

Commit

Permalink
add global listener system
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Jan 25, 2025
1 parent 7a15b67 commit 9a01139
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/main/java/com/gtnewhorizon/gtnhlib/keybind/SyncedKeybind.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public final class SyncedKeybind {
private boolean isKeyDown;

private final WeakHashMap<EntityPlayerMP, Boolean> mapping = new WeakHashMap<>();
private final WeakHashMap<EntityPlayerMP, Set<IKeyPressedListener>> listeners = new WeakHashMap<>();
private final WeakHashMap<EntityPlayerMP, Set<IKeyPressedListener>> playerListeners = new WeakHashMap<>();
private final Set<IKeyPressedListener> globalListeners = Collections.newSetFromMap(new WeakHashMap<>());

// Doubly-wrapped supplier for client-side only type
private SyncedKeybind(Supplier<Supplier<KeyBinding>> keybindingGetter) {
Expand Down Expand Up @@ -135,25 +136,44 @@ public boolean isKeyDown(EntityPlayer player) {
* @param player The player who owns this listener.
* @param listener The handler for the key clicked event.
*/
public void registerListener(EntityPlayerMP player, IKeyPressedListener listener) {
Set<IKeyPressedListener> listenerSet = listeners
public void registerPlayerListener(EntityPlayerMP player, IKeyPressedListener listener) {
Set<IKeyPressedListener> listenerSet = playerListeners
.computeIfAbsent(player, k -> Collections.newSetFromMap(new WeakHashMap<>()));
listenerSet.add(listener);
}

/**
* Remove a player's listener on this keybinding.
* Remove a player's listener on this keybinding for a provided player.
*
* @param player The player who owns this listener.
* @param listener The handler for the key clicked event.
*/
public void removeListener(EntityPlayerMP player, IKeyPressedListener listener) {
Set<IKeyPressedListener> listenerSet = listeners.get(player);
public void removePlayerListener(EntityPlayerMP player, IKeyPressedListener listener) {
Set<IKeyPressedListener> listenerSet = playerListeners.get(player);
if (listenerSet != null) {
listenerSet.remove(listener);
}
}

/**
* Registers an {@link IKeyPressedListener} to this key, which will have its {@link IKeyPressedListener#onKeyPressed
* onKeyPressed} method called when any player presses this key.
*
* @param listener The handler for the key clicked event.
*/
public void registerGlobalListener(IKeyPressedListener listener) {
globalListeners.add(listener);
}

/**
* Remove a global listener on this keybinding.
*
* @param listener The handler for the key clicked event.
*/
public void removeGlobalListener(IKeyPressedListener listener) {
globalListeners.remove(listener);
}

static SyncedKeybind getFromSyncId(int id) {
return KEYBINDS.get(id);
}
Expand Down Expand Up @@ -215,11 +235,16 @@ public static void onInputEvent(InputEvent.KeyInputEvent event) {

// Updated by the packet handler
void onKeyPressed(EntityPlayerMP player) {
Set<IKeyPressedListener> listenerSet = listeners.get(player);
// Player listeners
Set<IKeyPressedListener> listenerSet = playerListeners.get(player);
if (listenerSet != null && !listenerSet.isEmpty()) {
for (IKeyPressedListener listener : listenerSet) {
listener.onKeyPressed(player, this);
}
}
// Global listeners
for (IKeyPressedListener listener : globalListeners) {
listener.onKeyPressed(player, this);
}
}
}

0 comments on commit 9a01139

Please sign in to comment.