Skip to content

Commit

Permalink
Move listeners to separate inner classes to allow the plugin to succe…
Browse files Browse the repository at this point in the history
…ssfully load without DiscordSRV.
  • Loading branch information
RezzedUp committed Sep 30, 2017
1 parent 0ddf4c9 commit 4941381
Showing 1 changed file with 67 additions and 57 deletions.
124 changes: 67 additions & 57 deletions src/main/java/com/rezzedup/discordsrv/staffchat/StaffChatPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static String color(String message)

private final Set<UUID> toggles = new HashSet<>();

private final DiscordListener discord = new DiscordListener();

private boolean isDiscordSrvHookEnabled = false;

private Debugger debugger;
Expand All @@ -64,14 +66,14 @@ public void onEnable()
saveDefaultConfig();

PluginManager plugins = getServer().getPluginManager();
plugins.registerEvents(this, this);
plugins.registerEvents(new InGameListener(), this);

if (plugins.isPluginEnabled("DiscordSRV"))
{
debugger.debug("DiscordSRV is enabled: subscribing to API.");

this.isDiscordSrvHookEnabled = true;
DiscordSRV.api.subscribe(this);
DiscordSRV.api.subscribe(discord);
}
else
{
Expand All @@ -97,7 +99,7 @@ public void onDisable()
if (isDiscordSrvHookEnabled)
{
debugger.debug("Unsubscribing from DiscordSRV's API.");
DiscordSRV.api.unsubscribe(this);
DiscordSRV.api.unsubscribe(discord);
}

debugger.debug("----- Disabled. -----");
Expand All @@ -108,58 +110,6 @@ public TextChannel getDiscordChannel()
return (isDiscordSrvHookEnabled) ? DiscordSRV.getPlugin().getDestinationTextChannelForGameChannelName(CHANNEL) : null;
}

@Subscribe
public void onDiscordChat(DiscordGuildMessagePreProcessEvent event)
{
if (event.getChannel().equals(getDiscordChannel()))
{
submitFromDiscord(event.getAuthor(), event.getMessage());
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void onGameChat(AsyncPlayerChatEvent event)
{
if (toggles.contains(event.getPlayer().getUniqueId()))
{
if (Permissions.any(Permissions.ALL, Permissions.ACCESS).test(event.getPlayer()))
{
debugger.debug("Player %s has automatic staff-chat enabled.", event.getPlayer().getName());

submitFromInGame(event.getPlayer(), event.getMessage());
event.setCancelled(true);
}
else
{
debugger.debug
(
"Player %s has automatic staff-chat enabled but they don't have permission to use the staff chat.",
event.getPlayer().getName()
);

forceToggle(event.getPlayer(), false);
}
}
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();

boolean isNotifiable = getConfig().getBoolean("notify-staff-chat-enabled-on-join")
&& Permissions.any(Permissions.ALL, Permissions.ACCESS).test(player)
&& toggles.contains(player.getUniqueId());

if (!isNotifiable) { return; }

debugger.debug("Player %s joined: reminding them that they have automatic staff-chat enabled.", event.getPlayer().getName());

getServer().getScheduler()
.runTaskLater(this, () -> player.sendMessage(color(getConfig().getString("staff-chat-enabled-notification"))), 10L);
}

public void inGameAnnounce(String message)
{
String content = color(message);
Expand All @@ -170,7 +120,7 @@ public void inGameAnnounce(String message)
.filter(Permissions.any(Permissions.ALL, Permissions.ACCESS)).forEach(p -> p.sendMessage(content));
}

public void inGameUpdateThenAnnounce(String format, MappedPlaceholder placeholder)
private void inGameUpdateThenAnnounce(String format, MappedPlaceholder placeholder)
{
// If the value of %message% doesn't exist for some reason, don't announce.
if (Placeholder.isValid(placeholder.get("message")))
Expand Down Expand Up @@ -320,7 +270,67 @@ else if ("managestaffchat".equals(command.getName()))
return true;
}

public class MessagePlaceholder extends MappedPlaceholder
class DiscordListener
{
@Subscribe
public void onDiscordChat(DiscordGuildMessagePreProcessEvent event)
{
if (event.getChannel().equals(getDiscordChannel()))
{
submitFromDiscord(event.getAuthor(), event.getMessage());
event.setCancelled(true);
}
}
}

class InGameListener implements Listener
{
@EventHandler(priority = EventPriority.LOWEST)
public void onGameChat(AsyncPlayerChatEvent event)
{
if (!toggles.contains(event.getPlayer().getUniqueId()))
{
return;
}

if (Permissions.any(Permissions.ALL, Permissions.ACCESS).test(event.getPlayer()))
{
debugger.debug("Player %s has automatic staff-chat enabled.", event.getPlayer().getName());

submitFromInGame(event.getPlayer(), event.getMessage());
event.setCancelled(true);
}
else
{
debugger.debug
(
"Player %s has automatic staff-chat enabled but they don't have permission to use the staff chat.",
event.getPlayer().getName()
);

forceToggle(event.getPlayer(), false);
}
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();

boolean isNotifiable = getConfig().getBoolean("notify-staff-chat-enabled-on-join")
&& Permissions.any(Permissions.ALL, Permissions.ACCESS).test(player)
&& toggles.contains(player.getUniqueId());

if (!isNotifiable) { return; }

debugger.debug("Player %s joined: reminding them that they have automatic staff-chat enabled.", event.getPlayer().getName());

getServer().getScheduler()
.runTaskLater(StaffChatPlugin.this, () -> player.sendMessage(color(getConfig().getString("staff-chat-enabled-notification"))), 10L);
}
}

class MessagePlaceholder extends MappedPlaceholder
{
MessagePlaceholder(Player player, String message)
{
Expand Down

0 comments on commit 4941381

Please sign in to comment.