Skip to content

Commit

Permalink
Added BossBar adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo0111 committed Aug 30, 2021
1 parent 77ddbfe commit 6a7786c
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 21 deletions.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
* SOFTWARE.
*/

rootProject.name = "multilang"
rootProject.name = "MultiLang"
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketListener;
import me.lorenzo0111.multilang.MultiLangPlugin;
import me.lorenzo0111.multilang.protocol.adapter.BossBarAdapter;
import me.lorenzo0111.multilang.protocol.adapter.ChatAdapter;
import me.lorenzo0111.multilang.protocol.adapter.EntityAdapter;
import me.lorenzo0111.multilang.protocol.adapter.InventoryAdapter;
Expand Down Expand Up @@ -56,6 +57,7 @@ public void init() {

listeners.add(new InventoryAdapter(plugin, ListenerPriority.NORMAL));
listeners.add(new ChatAdapter(plugin, ListenerPriority.NORMAL));
listeners.add(new BossBarAdapter(plugin, ListenerPriority.NORMAL));

listeners.forEach(manager::addPacketListener);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.lorenzo0111.multilang.protocol.adapter;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.lorenzo0111.multilang.MultiLangPlugin;
import me.lorenzo0111.multilang.utils.RegexChecker;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public abstract class BaseAdapter extends PacketAdapter {

public BaseAdapter(MultiLangPlugin plugin, ListenerPriority listenerPriority, PacketType type) {
super(plugin, listenerPriority, type);
}

public void handle(Player player, @NotNull WrappedChatComponent component, @NotNull Runnable save) {
JsonObject json = new JsonParser().parse(component.getJson()).getAsJsonObject();

this.update(json, RegexChecker.replace(player, json));

component.setJson(json.toString());

save.run();
}

protected void update(@NotNull JsonObject object, String value) {
object.remove("text");
object.addProperty("text",value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.lorenzo0111.multilang.protocol.adapter;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import me.lorenzo0111.multilang.MultiLangPlugin;
import org.jetbrains.annotations.NotNull;

public class BossBarAdapter extends BaseAdapter {

public BossBarAdapter(MultiLangPlugin plugin, ListenerPriority listenerPriority) {
super(plugin, listenerPriority, PacketType.Play.Server.BOSS);
}

@Override
public void onPacketSending(@NotNull PacketEvent event) {
PacketContainer packet = event.getPacket();

WrappedChatComponent component;

if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
component = packet.getStructures().read(1).getChatComponents().read(0);
} else {
component = packet.getChatComponents().read(0);
}

this.handle(event.getPlayer(),component, () -> {
if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
packet.getStructures().read(1).getChatComponents().write(0, component);
} else {
packet.getChatComponents().write(0, component);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.EnumWrappers;
Expand All @@ -38,7 +37,8 @@
import me.lorenzo0111.multilang.utils.RegexChecker;
import org.bukkit.entity.Player;

public class ChatAdapter extends PacketAdapter {
public class ChatAdapter extends BaseAdapter {

public ChatAdapter(MultiLangPlugin plugin, ListenerPriority listenerPriority) {
super(plugin, listenerPriority, PacketType.Play.Server.CHAT);
}
Expand Down Expand Up @@ -79,11 +79,6 @@ public void onPacketSending(PacketEvent event) {

// Update the component and the packet
component.setJson(json.toString());
packet.getChatComponents().write(0,component);
}

private void update(JsonObject object, String value) {
object.remove("text");
object.addProperty("text",value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.lorenzo0111.multilang.MultiLangPlugin;
import me.lorenzo0111.multilang.utils.RegexChecker;

import java.util.logging.Level;

public class InventoryAdapter extends PacketAdapter {
public class InventoryAdapter extends BaseAdapter {
private final MultiLangPlugin plugin;

public InventoryAdapter(MultiLangPlugin plugin, ListenerPriority listenerPriority) {
Expand All @@ -55,16 +53,8 @@ public void onPacketSending(PacketEvent event) {

if (!json.has("text")) return;

String text = json.get("text").getAsString();
String replaced = RegexChecker.replace(event.getPlayer(), text);

// Replace the text
json.remove("text");
json.addProperty("text", replaced);
component.setJson(json.toString());

// Edit packet
event.getPacket().getChatComponents().write(0,component);
this.handle(event.getPlayer(),component, () -> event.getPacket().getChatComponents().write(0,component));
} catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "An error has occurred while handling packets", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

package me.lorenzo0111.multilang.utils;

import com.google.gson.JsonObject;
import me.lorenzo0111.multilang.MultiLangPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -36,7 +38,11 @@ public final class RegexChecker {

private RegexChecker() {}

public static String replace(Player player, String string) {
public static @NotNull String replace(Player player, @NotNull JsonObject json) {
return replace(player,json.get("text").getAsString());
}

public static @NotNull String replace(Player player, String string) {
final Matcher matcher = PATTERN.matcher(string);

final StringBuffer buffer = new StringBuffer();
Expand Down

0 comments on commit 6a7786c

Please sign in to comment.