Skip to content

Commit

Permalink
Implements option to change eventPriority of AsyncPlayerChatEvent via…
Browse files Browse the repository at this point in the history
… config
  • Loading branch information
wellnesscookie committed Jan 9, 2021
1 parent 66cd6c0 commit 78e6fc7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/main/java/world/bentobox/chat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.HashSet;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.World;

import org.bukkit.event.player.AsyncPlayerChatEvent;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.Config;
Expand All @@ -22,13 +24,17 @@
public class Chat extends Addon {

// Settings and configuration
public static Chat addon;
private Settings settings;
private Config<Settings> configObject = new Config<>(this, Settings.class);
private Set<GameModeAddon> registeredGameModes;
private ChatListener listener;

@Override
public void onEnable() {

addon = this;

/* Config */
// Save default config from config.yml
saveDefaultConfig();
Expand All @@ -42,6 +48,9 @@ public void onEnable() {

// Register listener
listener = new ChatListener(this);
// This manually registers the AsyncPlayerChatEvent with the priority from configuration
Bukkit.getPluginManager().registerEvent(AsyncPlayerChatEvent.class, listener, getSettings().getEventPriority(), listener, getPlugin());
// This will register the remaining events with @EventHandler annotation inside the listener
this.registerListener(listener);

// Register request handlers
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/world/bentobox/chat/Settings.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package world.bentobox.chat;

import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import world.bentobox.bentobox.api.configuration.ConfigComment;
import world.bentobox.bentobox.api.configuration.ConfigEntry;
import world.bentobox.bentobox.api.configuration.ConfigObject;
import world.bentobox.bentobox.api.configuration.StoreAt;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
* Contains the config settings for this addon.
Expand All @@ -31,7 +34,11 @@ public class Settings implements ConfigObject {
@ConfigEntry(path = "island-chat.log")
private boolean logIslandChats;


@ConfigComment("Sets priority of AsyncPlayerChatEvent. Change this if Chat addon")
@ConfigComment("is conflicting with other plugins which listen to the same event")
@ConfigComment("Acceptable values: lowest, low, normal, high, highest, monitor")
@ConfigEntry(path = "chat-listener.priority")
private String eventPriority = "normal";

public List<String> getTeamChatGamemodes() {
return teamChatGamemodes;
Expand Down Expand Up @@ -76,4 +83,22 @@ public boolean isLogIslandChats() {
public void setLogIslandChats(boolean logIslandChats) {
this.logIslandChats = logIslandChats;
}

public EventPriority getEventPriority() {

EventPriority priority = EventPriority.NORMAL;

try {
priority = EventPriority.valueOf(this.eventPriority.toUpperCase());
}
catch (IllegalArgumentException e){
Chat.addon.logError("EventPriority value: " + eventPriority + " is not valid in configuration. Using default: normal");
}

return priority;
}

public void setEventPriority(EventPriority eventPriority) {
this.eventPriority = eventPriority.toString();
}
}
17 changes: 15 additions & 2 deletions src/main/java/world/bentobox/chat/listeners/ChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

import org.bukkit.plugin.EventExecutor;
import world.bentobox.bentobox.api.events.team.TeamKickEvent;
import world.bentobox.bentobox.api.events.team.TeamLeaveEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
Expand All @@ -26,7 +28,7 @@
* @author tastybento
*
*/
public class ChatListener implements Listener {
public class ChatListener implements Listener, EventExecutor {

private static final String MESSAGE = "[message]";
private final Chat addon;
Expand All @@ -45,8 +47,19 @@ public ChatListener(Chat addon) {
islandSpies = new HashSet<>();
}

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void execute(Listener listener, Event e) {

// Needs to be checked, as we manually registered the listener
// It's a replacement for ignoreCannceled = true
if (((AsyncPlayerChatEvent) e).isCancelled())
return;

// Call the event method
onChat((AsyncPlayerChatEvent) e);
}

public void onChat(final AsyncPlayerChatEvent e) {

Player p = e.getPlayer();
World w = e.getPlayer().getWorld();
// Check world
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ island-chat:
- SkyGrid
# Log island chats to console.
log: false
chat-listener:
# Sets priority of AsyncPlayerChatEvent. Change this if Chat addon
# is conflicting with other plugins which listen to the same event
# Acceptable values: lowest, low, normal, high, highest, monitor
priority: normal

0 comments on commit 78e6fc7

Please sign in to comment.