Skip to content

Commit

Permalink
Update 1.3.4: Merge pull request #24 from DiscordSRV/v1.3.x
Browse files Browse the repository at this point in the history
- Hook into DiscordSRV if it's loaded late (fix #23)
- Add bStats metrics
- Add prefixed staff-chat messages
- Improve debugging
  • Loading branch information
RezzedUp authored Apr 16, 2021
2 parents 0ce42a9 + 4848eb1 commit d7073e7
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 10 deletions.
47 changes: 47 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<shade.relocation>com.rezzedup.discordsrv.staffchat.shaded</shade.relocation>
</properties>

<repositories>
Expand Down Expand Up @@ -50,6 +51,12 @@
<version>2.10.6</version>
<scope>provided</scope>
</dependency>
<!-- bStats (via maven-central) -->
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
</dependency>
<!-- Nullness Annotations: @NullOr (via maven-central) -->
<dependency>
<groupId>pl.tlinkowski.annotation</groupId>
Expand All @@ -73,6 +80,46 @@
<target>1.8</target>
</configuration>
</plugin>
<!-- Shade dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>${shade.relocation}.org.bstats</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<!-- Exclude annotations from built jar -->
<excludes>
<exclude>com.google.code.findbugs:jsr305</exclude>
<exclude>org.jetbrains.kotlin:kotlin-annotations-jvm</exclude>
<exclude>pl.tlinkowski.annotation:pl.tlinkowski.annotation.basic</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<!-- Exclude redundant metadata files -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<!-- Filter resource files -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public enum Permissions

public String getPermissionNode() { return this.permission; }

public boolean isAllowedBy(Permissible permissible) { return permissible.hasPermission(permission); }
public boolean allows(Permissible permissible) { return permissible.hasPermission(permission); }

public boolean denies(Permissible permissible) { return !allows(permissible); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.rezzedup.discordsrv.staffchat.events.PlayerStaffChatMessageEvent;
import com.rezzedup.discordsrv.staffchat.listeners.DiscordSrvLoadedLaterListener;
import com.rezzedup.discordsrv.staffchat.listeners.DiscordStaffChatListener;
import com.rezzedup.discordsrv.staffchat.listeners.PlayerPrefixedMessageListener;
import com.rezzedup.discordsrv.staffchat.listeners.PlayerStaffChatToggleListener;
import com.rezzedup.discordsrv.staffchat.util.Events;
import com.rezzedup.discordsrv.staffchat.util.MappedPlaceholder;
Expand All @@ -15,11 +16,14 @@
import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel;
import github.scarsz.discordsrv.dependencies.jda.api.entities.User;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import pl.tlinkowski.annotation.basic.NullOr;

Expand All @@ -30,6 +34,9 @@
@SuppressWarnings("NotNullFieldNotInitialized")
public class StaffChatPlugin extends JavaPlugin implements StaffChatAPI
{
// https://bstats.org/plugin/bukkit/DiscordSRV-Staff-Chat/11056
public static final int BSTATS = 11056;

public static final String CHANNEL = "staff-chat";

public static final String DISCORDSRV = "DiscordSRV";
Expand All @@ -48,8 +55,12 @@ public void onEnable()

debug(getClass()).header(() -> "Starting Plugin: " + this);
debugger().schedulePluginStatus(getClass(), "Enable");

PluginManager plugins = getServer().getPluginManager();

plugins.registerEvents(inGameToggles, this);
plugins.registerEvents(new PlayerPrefixedMessageListener(this), this);

getServer().getPluginManager().registerEvents(inGameToggles, this);
saveDefaultConfig();

@NullOr Plugin discordSrv = getServer().getPluginManager().getPlugin(DISCORDSRV);
Expand All @@ -69,6 +80,8 @@ public void onEnable()
// Subscribe to DiscordSRV later because it somehow wasn't enabled yet.
getServer().getPluginManager().registerEvents(new DiscordSrvLoadedLaterListener(this), this);
}

startMetrics();
}

@Override
Expand All @@ -93,6 +106,32 @@ public void onDisable()
debug(getClass()).header(() -> "Disabled Plugin: " + this);
}

private void startMetrics()
{
if (!getConfig().getBoolean("metrics", true))
{
debug(getClass()).log("Metrics", () -> "Aborting: metrics are disabled in the config");
return;
}

debug(getClass()).log("Metrics", () -> "Scheduling metrics to start one minute from now");

getServer().getScheduler().runTaskLater(this, () ->
{
Metrics metrics = new Metrics(this, BSTATS);

metrics.addCustomChart(new SimplePie(
"hooked_into_discordsrv", () -> String.valueOf(isDiscordSrvHookEnabled())
));

metrics.addCustomChart(new SimplePie(
"has_valid_staff-chat_channel", () -> String.valueOf(getDiscordChannelOrNull() != null)
));

debug(getClass()).log("Metrics", () -> "Started bStats metrics");
}, 60 * 20L); // Start a minute later to get the most accurate data.
}

public Debugger debugger() { return debugger; }

public Debugger.DebugLogger debug(Class<?> clazz) { return debugger().debug(clazz); }
Expand Down Expand Up @@ -135,7 +174,7 @@ private void inGameAnnounce(String message)
String content = colorful(message);

getServer().getOnlinePlayers().stream()
.filter(Permissions.ACCESS::isAllowedBy)
.filter(Permissions.ACCESS::allows)
.forEach(staff -> staff.sendMessage(content));

getServer().getConsoleSender().sendMessage(content);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.rezzedup.discordsrv.staffchat.listeners;

import com.rezzedup.discordsrv.staffchat.Permissions;
import com.rezzedup.discordsrv.staffchat.StaffChatPlugin;
import com.rezzedup.discordsrv.staffchat.util.Strings;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

@SuppressWarnings("unused")
public class PlayerPrefixedMessageListener implements Listener
{
private final StaffChatPlugin plugin;

public PlayerPrefixedMessageListener(StaffChatPlugin plugin) { this.plugin = plugin; }

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPrefixedChat(AsyncPlayerChatEvent event)
{
if (!plugin.getConfig().getBoolean("enable-prefixed-chat-messages", false)) { return; }

Player player = event.getPlayer();
if (Permissions.ACCESS.denies(player)) { return; }

String identifier = Strings.orEmpty(plugin.getConfig(), "prefixed-chat-identifier");
if (Strings.isEmptyOrNull(identifier))
{
plugin.debug(getClass()).log(event, () -> "Prefixed chat is enabled but identifier is undefined");
return;
}

String message = event.getMessage();
if (!message.startsWith(identifier)) { return; }

String unprefixed = message.substring(identifier.length()).trim();

plugin.debug(getClass()).log(event, () ->
"Sending prefixed chat from player(" + player.getName() + ") identified " +
"by prefix(\"" + identifier + "\"): message(\"" + unprefixed + "\")"
);

event.setCancelled(true); // Cancel this message from getting sent to global chat.

// Handle this on the main thread next tick.
plugin.getServer().getScheduler().runTask(plugin, () ->
plugin.submitMessageFromInGame(player, (Strings.isEmptyOrNull(unprefixed)) ? message : unprefixed)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,28 @@ private void forceToggle(Player player, boolean state)
public void toggle(Player player) { forceToggle(player, !isChatToggled(player)); }

@EventHandler(priority = EventPriority.LOWEST)
public void onGameChat(AsyncPlayerChatEvent event)
public void onToggledChat(AsyncPlayerChatEvent event)
{
if (!isChatToggled(event.getPlayer())) { return; }
Player player = event.getPlayer();
if (!isChatToggled(player)) { return; }

if (Permissions.ACCESS.isAllowedBy(event.getPlayer()))
if (Permissions.ACCESS.allows(player))
{
plugin.debug(getClass()).log(event, () ->
"Player " + event.getPlayer().getName() + " has automatic staff-chat enabled"
"Player " + player.getName() + " has automatic staff-chat enabled"
);

event.setCancelled(true); // Cancel this message from getting sent to global chat.

// Handle this on the main thread next tick.
plugin.getServer().getScheduler().runTask(plugin, () ->
plugin.submitMessageFromInGame(event.getPlayer(), event.getMessage())
plugin.submitMessageFromInGame(player, event.getMessage())
);
}
else
{
plugin.debug(getClass()).log(event, () ->
"Player " + event.getPlayer().getName() + " has automatic staff-chat enabled " +
"Player " + player.getName() + " has automatic staff-chat enabled " +
"but they don't have permission to use the staff chat"
);

Expand All @@ -84,7 +85,7 @@ public void onPlayerJoin(PlayerJoinEvent event)
Player player = event.getPlayer();

boolean isNotifiable = plugin.getConfig().getBoolean("notify-staff-chat-enabled-on-join")
&& Permissions.ACCESS.isAllowedBy(player)
&& Permissions.ACCESS.allows(player)
&& autoChatToggles.contains(player.getUniqueId());

if (!isNotifiable) { return; }
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
in-game-message-format: "&d(&5&l&oStaff&d) %name%&7:&f %message%"
discord-message-format: "&9&ldiscord &f-> &d(&5&l&oStaff&d) %name%&7:&f %message%"

#
# Send chat messages to staff-chat if they start with a specific prefix.
# e.g. If the prefix is "@", then saying "@hello" will send "hello" to the staff chat.
#

enable-prefixed-chat-messages: false
prefixed-chat-identifier: "@"

#
# Automatic staff-chat toggle messages.
#
Expand All @@ -75,3 +83,10 @@ disable-staff-chat: "&d(&5&l&oStaff&d) &4->&c &nDisabled&c automatic staff chat"

notify-staff-chat-enabled-on-join: true
staff-chat-enabled-notification: "&d(&5&l&oStaff&d) &2->&a Automatic staff chat is &nenabled"

#
# Should the plugin collect basic anonymous usage metrics?
# Metrics can be found at: https://bstats.org/plugin/bukkit/DiscordSRV-Staff-Chat/11056
#

metrics: true

0 comments on commit d7073e7

Please sign in to comment.