Skip to content

Commit

Permalink
Hook into EssentialsChat
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisawesomeness committed Jul 27, 2022
1 parent 4760629 commit 0a00ea4
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 13 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# BetterPreview

Previews chat previes client-side to improve privacy, stability, and performance.
Processes chat previews client-side to improve privacy, stability, and performance.

Must be installed on both client and server to work. Currently supports Fabric clients and Spigot servers.

**Minecraft Version:** 1.19

## Supported Chat Plugins

- [EssentialsChat](https://essentialsx.net/)

More to come
3 changes: 3 additions & 0 deletions bp-spigot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ dependencies {
compileOnly 'org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT'

implementation "net.kyori:adventure-platform-bukkit:4.1.2-SNAPSHOT"

// Some code from the Essentials project included, licensed under GPL3
// https://github.com/EssentialsX/Essentials
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.tisawesomeness.betterpreview.spigot;

import com.tisawesomeness.betterpreview.BetterPreview;
import com.tisawesomeness.betterpreview.format.ChatFormatter;
import com.tisawesomeness.betterpreview.format.ClassicFormatter;
import com.tisawesomeness.betterpreview.format.FormatterRegistry;
import com.tisawesomeness.betterpreview.spigot.adapter.EssentialsChatAdapter;
import com.tisawesomeness.betterpreview.spigot.adapter.FormatAdapter;

import io.netty.buffer.Unpooled;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;

public class BetterPreviewSpigot extends JavaPlugin {

public static final String CHANNEL = BetterPreview.CHANNEL.asString();

// Dummy example for now
private final ChatFormatter chatFormatter = new ClassicFormatter('&');
private @Nullable FormatAdapter adapter;

@Override
public void onEnable() {
boolean hasEssentialsChat = Bukkit.getPluginManager().getPlugin("EssentialsChat") != null;
if (hasEssentialsChat) {
adapter = new EssentialsChatAdapter();
getLogger().info("Found chat plugin: EssentialsChat");
}

getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL);
getServer().getPluginManager().registerEvents(new JoinListener(this), this);
}
Expand All @@ -28,13 +35,13 @@ public void onDisable() {
}

public void sendFormatter(Player player) {
player.sendPluginMessage(this, CHANNEL, getFormatterData());
}
private byte[] getFormatterData() {
var buf = Unpooled.buffer();
FormatterRegistry.write(buf, chatFormatter);
assert buf.hasArray();
return buf.array();
if (adapter != null) {
var buf = Unpooled.buffer();
var formatter = adapter.buildChatFormatter(player);
FormatterRegistry.write(buf, formatter);
assert buf.hasArray();
player.sendPluginMessage(this, CHANNEL, buf.array());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tisawesomeness.betterpreview.spigot;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class Util {

// Permission checker that supports wildcards without needing Vault
// Adapted from EssentialsChat
// https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/main/java/com/earth2me/essentials/perm/impl/SuperpermsHandler.java

/**
* Checks if the player has the given permission, checking wildcards.
* Use {@link Player#isPermissionSet(String)} to check if a permission is set.
* @param player the player to check
* @param permission the permission to check
* @return whether the player has the permission
*/
public static boolean hasPermission(Player player, String permission) {
String perm = permission;
String permToCheck = permission;
int idx;
while (true) {
if (player.isPermissionSet(permToCheck) || isDeniedToOps(permToCheck)) {
return player.hasPermission(permToCheck);
}
idx = perm.lastIndexOf('.');
if (idx < 1) {
return player.hasPermission("*");
}
perm = perm.substring(0, idx);
permToCheck = perm + ".*";
}
}
private static boolean isDeniedToOps(String node) {
var perm = Bukkit.getServer().getPluginManager().getPermission(node);
return perm != null && !perm.getDefault().getValue(true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.tisawesomeness.betterpreview.spigot.adapter;

import com.tisawesomeness.betterpreview.format.ChatFormatter;
import com.tisawesomeness.betterpreview.format.ClassicFormatter;
import com.tisawesomeness.betterpreview.spigot.Util;

import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.entity.Player;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

public class EssentialsChatAdapter implements FormatAdapter {

private static final String BASE_PERMISSION = "essentials.chat";
private static final Set<TextDecoration> FORMAT_DECORATIONS = EnumSet.of(
TextDecoration.BOLD,
TextDecoration.ITALIC,
TextDecoration.STRIKETHROUGH,
TextDecoration.UNDERLINED
);

@Override
public ChatFormatter buildChatFormatter(Player player) {
var colors = new HashSet<NamedTextColor>();
if (Util.hasPermission(player, BASE_PERMISSION + ".color")) {
colors.addAll(ClassicFormatter.ALL_NAMED_COLORS);
}
for (var color : ClassicFormatter.ALL_NAMED_COLORS) {
String perm = BASE_PERMISSION + "." + color.toString();
if (player.isPermissionSet(perm)) {
if (Util.hasPermission(player, perm)) {
colors.add(color);
} else {
colors.remove(color);
}
}
}

var decorations = EnumSet.noneOf(TextDecoration.class);
boolean resetAllowed = false;
if (Util.hasPermission(player, BASE_PERMISSION + ".format")) {
decorations.addAll(FORMAT_DECORATIONS);
resetAllowed = true;
}
if (Util.hasPermission(player, BASE_PERMISSION + ".magic")) {
decorations.add(TextDecoration.OBFUSCATED);
}

return new ClassicFormatter('&', colors, decorations, resetAllowed);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tisawesomeness.betterpreview.spigot.adapter;

import com.tisawesomeness.betterpreview.format.ChatFormatter;

import org.bukkit.entity.Player;

/**
* Adapts a plugin's chat system to a BetterPreview chat formatter.
*/
public interface FormatAdapter {

/**
* Builds a chat formatter for the player based on the player's permissions and the chat plugin config.
* @param player the player who will be sent the formatter
* @return the formatter
*/
ChatFormatter buildChatFormatter(Player player);

}
3 changes: 2 additions & 1 deletion bp-spigot/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ name: BetterPreview
version: '${version}'
main: com.tisawesomeness.betterpreview.spigot.BetterPreviewSpigot
api-version: 1.19
authors: [ Tis_awesomeness ]
authors: [ Tis_awesomeness ]
softdepend: [ EssentialsChat ]

0 comments on commit 0a00ea4

Please sign in to comment.