Skip to content

Commit

Permalink
Add options for logging usernames, and keeping original message event
Browse files Browse the repository at this point in the history
Resolves TODO on #2, adding the config option log_username which will prepend logged messages with (username).
Fixes bug wherein a user sending a message containing %member% will have it replaced with their system hover.
Adds config option keep_original_message_event, which will keep the original message event but attempt to clear its recipient. In this case, the original message is logged to console.
  • Loading branch information
Allymonies committed Feb 9, 2022
1 parent bdd700c commit a596a1a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 67 deletions.
40 changes: 21 additions & 19 deletions PluralKitMC.iml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,35 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.18.1-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.16-R0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.26" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-api:1.16-R0.4-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-config:1.16-R0.4-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-event:1.16-R0.4-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-protocol:1.16-R0.4-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:31.0.1-jre" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:failureaccess:1.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.checkerframework:checker-qual:3.12.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.errorprone:error_prone_annotations:2.7.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.j2objc:j2objc-annotations:1.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.16-R0.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.30" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-api:1.18-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-config:1.18-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-event:1.18-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-protocol:1.18-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:brigadier:1.0.16-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-codec:4.1.53.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-codec:4.1.72.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.sf.trove4j:core:3.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: se.llbit:jo-nbt:1.3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-transport-native-unix-common:4.1.53.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-common:4.1.53.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-buffer:4.1.53.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-transport:4.1.53.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-resolver:4.1.53.Final" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.22" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.11.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-transport-native-unix-common:4.1.72.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-common:4.1.72.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-buffer:4.1.72.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-transport:4.1.72.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.netty:netty-resolver:4.1.72.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.MilkBowl:VaultAPI:1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.13.1-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.discordsrv:discordsrv:1.22.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.dv8tion:JDA:4.2.0_249" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:16.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.squareup.okhttp3:okhttp:3.13.0" level="project" />
Expand Down Expand Up @@ -72,7 +75,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.kyori:adventure-api:4.3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.kyori:examination-api:1.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.kyori:examination-string:1.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.checkerframework:checker-qual:3.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.kyori:adventure-text-serializer-legacy:4.3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.kyori:adventure-text-serializer-gson:4.3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.kyori:adventure-platform-bukkit:all:4.0.0-20210117.231600" level="project" />
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>com.omnipico.pluralkitmc</groupId>
<artifactId>PluralKitMC</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<version>v0_4_12</version>

<repositories>
<repository>
Expand Down
119 changes: 76 additions & 43 deletions src/main/java/com/omnipico/pluralkitmc/ProxyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,63 @@ public void setConfig(FileConfiguration config) {
}
}

private BaseComponent[] getOutputComponent(BaseComponent[] resultComponents, Player player, PluralKitSystem system, String memberName) {
return getOutputComponent(resultComponents, player, system, memberName, false);
}

private BaseComponent[] getOutputComponent(BaseComponent[] resultComponents, Player player, PluralKitSystem system, String memberName, Boolean logUsername) {
ArrayList<BaseComponent> components = new ArrayList<>();
boolean convertedMember = false;
if (logUsername) {
components.add(new TextComponent("(" + player.getName() + ") "));
}
for (BaseComponent component : resultComponents) {
if (!convertedMember && component.toPlainText().startsWith("%member%")) {
convertedMember = true;
ComponentBuilder hoverTextBuilder = new ComponentBuilder("User: ").color(ChatColor.GREEN)
.append(TextComponent.fromLegacyText(player.getDisplayName())).append("\nSystem: ").color(ChatColor.GREEN);
if (system.getName() != null && system.getName().length() > 0) {
hoverTextBuilder.append(system.getName()).color(ChatColor.AQUA);
} else {
hoverTextBuilder.append(system.getId()).color(ChatColor.GRAY);
}
Text hoverText = new Text(hoverTextBuilder.create());
components.addAll(Arrays.asList(new ComponentBuilder(memberName)
.color(component.getColor())
.bold(component.isBold())
.italic(component.isItalic())
.obfuscated(component.isObfuscated())
.strikethrough(component.isStrikethrough())
.underlined(component.isUnderlined())
.font(component.getFont())
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
hoverText
))
.create()));
if (component.toPlainText().length() > 8) {
BaseComponent[] post = new ComponentBuilder(component.toPlainText().substring(8))
.color(component.getColor())
.bold(component.isBold())
.italic(component.isItalic())
.obfuscated(component.isObfuscated())
.strikethrough(component.isStrikethrough())
.underlined(component.isUnderlined())
.font(component.getFont())
.create();
components.addAll(Arrays.asList(post));
}
} else {
components.add(component);
}
}
return components.toArray(new BaseComponent[0]);
}

@EventHandler(priority = EventPriority.HIGHEST) // Listening for the event.
public void onChat(AsyncPlayerChatEvent event) {
// Called when a player sends a chat message.
Player player = event.getPlayer(); // Getting the player who sent the message.
String originalFormat = event.getFormat();
String message = event.getMessage();
PluralKitSystem system = data.getSystem(player.getUniqueId());
PluralKitMember proxiedMember = data.getProxiedUser(player.getUniqueId(), message);
Expand Down Expand Up @@ -84,47 +137,7 @@ public void onChat(AsyncPlayerChatEvent event) {
if (config.getBoolean("hover_text", false)) {
String resultMessage = String.format(event.getFormat(), player.getDisplayName(), event.getMessage());
BaseComponent[] resultComponents = TextComponent.fromLegacyText(resultMessage);
ArrayList<BaseComponent> components = new ArrayList<>();
for (BaseComponent component : resultComponents) {
if (component.toPlainText().startsWith("%member%")) {
ComponentBuilder hoverTextBuilder = new ComponentBuilder("User: ").color(ChatColor.GREEN)
.append(TextComponent.fromLegacyText(player.getDisplayName())).append("\nSystem: ").color(ChatColor.GREEN);
if (system.getName() != null && system.getName().length() > 0) {
hoverTextBuilder.append(system.getName()).color(ChatColor.AQUA);
} else {
hoverTextBuilder.append(system.getId()).color(ChatColor.GRAY);
}
Text hoverText = new Text(hoverTextBuilder.create());
components.addAll(Arrays.asList(new ComponentBuilder(memberName)
.color(component.getColor())
.bold(component.isBold())
.italic(component.isItalic())
.obfuscated(component.isObfuscated())
.strikethrough(component.isStrikethrough())
.underlined(component.isUnderlined())
.font(component.getFont())
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
hoverText
))
.create()));
config.set("test", player.getDisplayName());
if (component.toPlainText().length() > 8) {
BaseComponent[] post = new ComponentBuilder(component.toPlainText().substring(8))
.color(component.getColor())
.bold(component.isBold())
.italic(component.isItalic())
.obfuscated(component.isObfuscated())
.strikethrough(component.isStrikethrough())
.underlined(component.isUnderlined())
.font(component.getFont())
.create();
components.addAll(Arrays.asList(post));
}
} else {
components.add(component);
}
}
BaseComponent[] sendable = components.toArray(new BaseComponent[0]);
BaseComponent[] sendable = getOutputComponent(resultComponents, player, system, memberName);
for (Player p : event.getRecipients()) {
p.spigot().sendMessage(sendable);
}
Expand All @@ -138,8 +151,28 @@ public void onChat(AsyncPlayerChatEvent event) {
discord.processChatMessage(player, message, "global", false);
}
}
Bukkit.getConsoleSender().spigot().sendMessage(sendable);
event.setCancelled(true);
if (config.getBoolean("keep_original_message_event", false)) {
/*
Don't cancel the original message event
Just get rid of recipients, if possible,
this means the console will not see the member name
*/
try {
event.setFormat(originalFormat);
event.getRecipients().clear();
} catch (UnsupportedOperationException e) {
event.setCancelled(true);
}
} else {
if (config.getBoolean("log_username", true)) {
BaseComponent[] loggable = getOutputComponent(resultComponents, player, system, memberName, true);
Bukkit.getConsoleSender().spigot().sendMessage(loggable);
} else {
Bukkit.getConsoleSender().spigot().sendMessage(sendable);
}
event.setCancelled(true);
}

}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ default_name_color: "&b"
message_format: "&f%prefix% %member% %suffix%&8>&f %message%"
#NOTE: Adding hover text may break some plugins! If you're having issues, set this to no
hover_text: yes
#If enabled, will not cancel the chat event, increasing plugin compatibility
#Note that this will cause the server console to see username only, not member name
keep_original_message_event: no
#If enabled, will make sure DiscordSRV (if installed) sees
#messages sent through pluralkitmc when hover_text is enabled.
#REQUIRES RespectChatPlugins to be true, or else you will encounter double messages!
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PluralKitMC
version: 0.4.11
version: 0.4.12
author: Allymonies
main: com.omnipico.pluralkitmc.PluralKitMC
api-version: 1.18
Expand Down
3 changes: 3 additions & 0 deletions target/classes/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ default_name_color: "&b"
message_format: "&f%prefix% %member% %suffix%&8>&f %message%"
#NOTE: Adding hover text may break some plugins! If you're having issues, set this to no
hover_text: yes
#If enabled, will not cancel the chat event, increasing plugin compatibility
#Note that this will cause the server console to see username only, not member name
keep_original_message_event: no
#If enabled, will make sure DiscordSRV (if installed) sees
#messages sent through pluralkitmc when hover_text is enabled.
#REQUIRES RespectChatPlugins to be true, or else you will encounter double messages!
Expand Down
2 changes: 1 addition & 1 deletion target/classes/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PluralKitMC
version: 0.4.11
version: 0.4.12
author: Allymonies
main: com.omnipico.pluralkitmc.PluralKitMC
api-version: 1.18
Expand Down
4 changes: 2 additions & 2 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven
#Fri Jan 07 16:14:47 PST 2022
#Tue Feb 08 20:49:25 EST 2022
groupId=com.omnipico.pluralkitmc
artifactId=PluralKitMC
version=1.0-SNAPSHOT
version=v0_4_12
Empty file.

0 comments on commit a596a1a

Please sign in to comment.