Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b0de9aa3c | ||
|
|
3981992e37 | ||
|
|
439710086e | ||
|
|
f5381b0430 | ||
|
|
3cce52953a | ||
|
|
797de9c602 | ||
|
|
c7f76d9f7b | ||
|
|
3947ea88d5 | ||
|
|
cc30049fdf | ||
|
|
7a964bb97c | ||
|
|
13bbf3e5ca | ||
|
|
46de91b2ff | ||
|
|
cb5b3acab0 | ||
|
|
6f6d4dc263 | ||
|
|
250b119391 | ||
|
|
beee98d5d6 | ||
|
|
4d270891be | ||
|
|
7e9352661e | ||
|
|
3f47d56bed | ||
|
|
1d0cac3a2b | ||
|
|
64da26ea9f | ||
|
|
4e53b6d5b0 | ||
|
|
3b4ede3c22 | ||
|
|
59aade10a7 | ||
|
|
cd5bf7281b | ||
|
|
cd5bd87835 | ||
|
|
9384277901 | ||
|
|
3b43e647a4 | ||
|
|
f28ad44e62 | ||
|
|
0baec19db1 | ||
|
|
8ea83b3fab | ||
|
|
7eaadd9ad6 | ||
|
|
506985c9d1 | ||
|
|
3fb0181ad7 | ||
|
|
fc56bb6017 | ||
|
|
d9c60f45a4 | ||
|
|
342060b86e | ||
|
|
b5b30bd9d8 | ||
|
|
55eaac0bd4 | ||
|
|
7583aeade7 | ||
|
|
4be3712082 | ||
|
|
c1e28709ac | ||
|
|
f0965efe73 | ||
|
|
529cdd1e04 | ||
|
|
ce867d4065 | ||
|
|
1eefb78157 | ||
|
|
ff0c8d2a8d | ||
|
|
5a250d854d | ||
|
|
caacf0d8ae | ||
|
|
0f7d4a0548 | ||
|
|
4529f74f0a | ||
|
|
1eaf32983a | ||
|
|
24b0682e8b | ||
|
|
cbf3326de6 | ||
|
|
c4ae9198a4 |
No files matched your search
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.dadus33</groupId>
|
||||
<artifactId>chatitem</artifactId>
|
||||
<version>2.4</version>
|
||||
<version>2.4.4</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@@ -73,13 +73,13 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.17.1</version>
|
||||
<version>2.17.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.17.1</version>
|
||||
<version>2.17.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -133,7 +133,7 @@
|
||||
<dependency>
|
||||
<groupId>net.kyori</groupId>
|
||||
<artifactId>adventure-api</artifactId>
|
||||
<version>4.10.1</version>
|
||||
<version>4.11.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -145,7 +145,7 @@
|
||||
<dependency>
|
||||
<groupId>com.discordsrv</groupId>
|
||||
<artifactId>discordsrv</artifactId>
|
||||
<version>1.24.1-SNAPSHOT</version>
|
||||
<version>1.25.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ChatItem extends JavaPlugin {
|
||||
|
||||
public static void debug(String msg) {
|
||||
if (getInstance().getStorage().DEBUG)
|
||||
getInstance().getLogger().info("[Debug] " + msg);
|
||||
getInstance().getLogger().info("[Debug] " + msg.replace(ChatManager.SEPARATOR, 'S'));
|
||||
}
|
||||
|
||||
private void chooseManagers() {
|
||||
@@ -145,7 +145,7 @@ public class ChatItem extends JavaPlugin {
|
||||
|
||||
|
||||
getServer().getMessenger().registerIncomingPluginChannel(this, brandChannelName, (chan, p, msg) -> {
|
||||
String client = new String(msg);
|
||||
String client = new String(msg).substring(1);
|
||||
ChatItem.debug("Detected client " + client + " for " + p.getName());
|
||||
ItemPlayer.getPlayer(p).setClientName(client);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package me.dadus33.chatitem;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -11,7 +11,7 @@ import me.dadus33.chatitem.utils.Version;
|
||||
|
||||
public class ItemPlayer {
|
||||
|
||||
private static final HashMap<UUID, ItemPlayer> PLAYERS = new HashMap<>();
|
||||
private static final ConcurrentHashMap<UUID, ItemPlayer> PLAYERS = new ConcurrentHashMap<>();
|
||||
public static ItemPlayer getPlayer(Player p) {
|
||||
return PLAYERS.computeIfAbsent(p.getUniqueId(), ItemPlayer::new);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -17,18 +19,51 @@ import me.dadus33.chatitem.hook.EcoEnchantsSupport;
|
||||
import me.dadus33.chatitem.itemnamer.NamerManager;
|
||||
import me.dadus33.chatitem.utils.ItemUtils;
|
||||
import me.dadus33.chatitem.utils.Storage;
|
||||
import me.dadus33.chatitem.utils.Utils;
|
||||
|
||||
public abstract class ChatManager {
|
||||
|
||||
public final static HashMap<UUID, Long> COOLDOWNS = new HashMap<>();
|
||||
public final static HashMap<UUID, Long> LAST_INFO_MESSAGE = new HashMap<>();
|
||||
public final static String NAME = "{name}";
|
||||
public final static String AMOUNT = "{amount}";
|
||||
public final static String TIMES = "{times}";
|
||||
public final static String LEFT = "{remaining}";
|
||||
private final static HashMap<UUID, Long> COOLDOWNS = new HashMap<>();
|
||||
private final static HashMap<UUID, Long> LAST_INFO_MESSAGE = new HashMap<>();
|
||||
private final static String NAME = "{name}";
|
||||
private final static String AMOUNT = "{amount}";
|
||||
private final static String TIMES = "{times}";
|
||||
private final static String LEFT = "{remaining}";
|
||||
public final static char SEPARATOR = ((char) 0x0007);
|
||||
public final static String SEPARATOR_STR = "\\u0007";
|
||||
|
||||
public static String fixSeparator(String s) {
|
||||
return s.replace(ChatManager.SEPARATOR_STR, Character.toString(ChatManager.SEPARATOR));
|
||||
}
|
||||
|
||||
public static boolean equalsSeparator(String s) {
|
||||
return s.equalsIgnoreCase(SEPARATOR_STR) || s.equalsIgnoreCase(Character.toString(SEPARATOR));
|
||||
}
|
||||
|
||||
public static boolean containsSeparator(String s) {
|
||||
return s.contains(SEPARATOR_STR) || s.contains(Character.toString(SEPARATOR));
|
||||
}
|
||||
|
||||
public static boolean endsWithSeparator(String s) {
|
||||
return s.endsWith(SEPARATOR_STR) || s.endsWith(Character.toString(SEPARATOR));
|
||||
}
|
||||
|
||||
public static String[] splitWithSeparator(String msg) {
|
||||
List<String> list = new ArrayList<>();
|
||||
String current = "";
|
||||
for(String s : msg.split("")) {
|
||||
if(equalsSeparator(s)) {
|
||||
if(!current.isEmpty())
|
||||
list.add(current);
|
||||
current = "";
|
||||
} else
|
||||
current += s;
|
||||
}
|
||||
if(!current.isEmpty())
|
||||
list.add(current);
|
||||
return list.toArray(new String[] {});
|
||||
}
|
||||
|
||||
protected Storage s;
|
||||
|
||||
public ChatManager() {
|
||||
@@ -68,11 +103,38 @@ public abstract class ChatManager {
|
||||
|
||||
meta.setLore(lores);
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
ChatItem.debug("Added " + addLore.size() + " lores from EcoEnchants");
|
||||
} else
|
||||
ChatItem.debug("No lore to add from EcoEnchants");
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all lores lines of an item, to replace the show_item action which is not working with ViaBackwards
|
||||
*
|
||||
* @param item the item to get lines from
|
||||
* @return all lores
|
||||
*/
|
||||
public static List<String> getMaxLinesFromItem(Player p, ItemStack item) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
if (item.hasItemMeta()) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
lines.add(meta.hasDisplayName() ? meta.getDisplayName()
|
||||
: NamerManager.getName(p, item, ChatItem.getInstance().getStorage()));
|
||||
if (meta.hasEnchants()) {
|
||||
meta.getEnchants().forEach((enchant, lvl) -> {
|
||||
lines.add(ChatColor.RESET + Utils.getEnchantName(enchant) + " " + Utils.toRoman(lvl));
|
||||
});
|
||||
}
|
||||
if (meta.hasLore())
|
||||
lines.addAll(meta.getLore());
|
||||
} else {
|
||||
lines.add(NamerManager.getName(p, item, ChatItem.getInstance().getStorage()));
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of item according to player & config<br>
|
||||
* Prefer use {@link #getNameOfItem(Player, ItemStack, Storage)} if you want take in count the empty item
|
||||
@@ -99,6 +161,7 @@ public abstract class ChatManager {
|
||||
}
|
||||
if (dname) {
|
||||
String trp = item.getItemMeta().getDisplayName();
|
||||
ChatItem.debug("trp: " + trp);
|
||||
if (c.COLOR_IF_ALREADY_COLORED) {
|
||||
replacer = replacer.replace(NAME, ChatColor.stripColor(trp));
|
||||
} else {
|
||||
@@ -181,11 +244,12 @@ public abstract class ChatManager {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static boolean canShowItem(Player p, ItemStack item, Cancellable e) {
|
||||
public static boolean canShowItem(Player p, ItemStack item, @Nullable Cancellable e) {
|
||||
Storage c = ChatItem.getInstance().getStorage();
|
||||
if (c.PERMISSION_ENABLED && !p.hasPermission(c.PERMISSION_NAME)) {
|
||||
if (!c.LET_MESSAGE_THROUGH) {
|
||||
e.setCancelled(true);
|
||||
if(e != null)
|
||||
e.setCancelled(true);
|
||||
}
|
||||
if (!c.NO_PERMISSION_MESSAGE.isEmpty() && c.SHOW_NO_PERM_NORMAL) {
|
||||
sendIfNeed(p, c.NO_PERMISSION_MESSAGE);
|
||||
@@ -194,7 +258,8 @@ public abstract class ChatManager {
|
||||
}
|
||||
if (item.getType().equals(Material.AIR)) {
|
||||
if (c.DENY_IF_NO_ITEM) {
|
||||
e.setCancelled(true);
|
||||
if(e != null)
|
||||
e.setCancelled(true);
|
||||
if (!c.DENY_MESSAGE.isEmpty())
|
||||
sendIfNeed(p, c.DENY_MESSAGE);
|
||||
return false;
|
||||
@@ -212,7 +277,8 @@ public abstract class ChatManager {
|
||||
COOLDOWNS.remove(p.getUniqueId());
|
||||
} else {
|
||||
if (!c.LET_MESSAGE_THROUGH) {
|
||||
e.setCancelled(true);
|
||||
if(e != null)
|
||||
e.setCancelled(true);
|
||||
}
|
||||
if (!c.COOLDOWN_MESSAGE.isEmpty()) {
|
||||
long left = (start + c.COOLDOWN) - current;
|
||||
@@ -242,4 +308,8 @@ public abstract class ChatManager {
|
||||
COOLDOWNS.remove(p.getUniqueId());
|
||||
LAST_INFO_MESSAGE.remove(p.getUniqueId());
|
||||
}
|
||||
|
||||
public static void applyCooldown(Player p) {
|
||||
COOLDOWNS.put(p.getUniqueId(), System.currentTimeMillis() / 1000);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package me.dadus33.chatitem.chatmanager.v1;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
@@ -9,6 +11,8 @@ import me.dadus33.chatitem.chatmanager.v1.json.JSONManipulatorCurrent;
|
||||
import me.dadus33.chatitem.chatmanager.v1.listeners.ChatEventListener;
|
||||
import me.dadus33.chatitem.chatmanager.v1.listeners.ChatPacketManager;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacketManager;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketManager;
|
||||
import me.dadus33.chatitem.utils.PacketUtils;
|
||||
import me.dadus33.chatitem.utils.Storage;
|
||||
|
||||
public class PacketEditingChatManager extends ChatManager {
|
||||
@@ -18,52 +22,75 @@ public class PacketEditingChatManager extends ChatManager {
|
||||
private final ChatItemPacketManager packetManager;
|
||||
private final ChatEventListener chatEventListener;
|
||||
private final ChatPacketManager chatPacketManager;
|
||||
|
||||
|
||||
public PacketEditingChatManager(ChatItem pl) {
|
||||
jsonManipulator = new JSONManipulatorCurrent();
|
||||
packetManager = new ChatItemPacketManager(pl);
|
||||
packetManager = new ChatItemPacketManager(pl);
|
||||
chatEventListener = new ChatEventListener(this);
|
||||
chatPacketManager = new ChatPacketManager(this);
|
||||
|
||||
|
||||
//Check for existence of BaseComponent class (only on spigot)
|
||||
try {
|
||||
Class.forName("net.md_5.bungee.api.chat.BaseComponent");
|
||||
} catch (ClassNotFoundException e) {
|
||||
baseComponentAvailable = false;
|
||||
}
|
||||
chatPacketManager = new ChatPacketManager(this);
|
||||
|
||||
// Check for existence of BaseComponent class (only on spigot)
|
||||
try {
|
||||
Class.forName("net.md_5.bungee.api.chat.BaseComponent");
|
||||
} catch (ClassNotFoundException e) {
|
||||
baseComponentAvailable = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "PacketEditing";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "packet";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void load(ChatItem pl, Storage s) {
|
||||
super.load(pl, s);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(chatEventListener, pl);
|
||||
packetManager.getPacketManager().addHandler(chatPacketManager);
|
||||
PacketManager pm = packetManager.getPacketManager();
|
||||
pm.addHandler(chatPacketManager);
|
||||
Bukkit.getOnlinePlayers().forEach(pm::addPlayer); // add actual online players
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void unload(ChatItem pl) {
|
||||
HandlerList.unregisterAll(chatEventListener);
|
||||
packetManager.getPacketManager().removeHandler(chatPacketManager);
|
||||
packetManager.getPacketManager().stop();
|
||||
PacketManager pm = packetManager.getPacketManager();
|
||||
pm.removeHandler(chatPacketManager);
|
||||
pm.stop();
|
||||
}
|
||||
|
||||
public JSONManipulatorCurrent getManipulator(){
|
||||
return jsonManipulator;
|
||||
}
|
||||
|
||||
public boolean supportsChatComponentApi(){
|
||||
return baseComponentAvailable;
|
||||
}
|
||||
public JSONManipulatorCurrent getManipulator() {
|
||||
return jsonManipulator;
|
||||
}
|
||||
|
||||
public boolean supportsChatComponentApi() {
|
||||
return baseComponentAvailable;
|
||||
}
|
||||
|
||||
public static Object createSystemChatPacket(String json) throws Exception {
|
||||
Class<?> packetClass = PacketUtils.getNmsClass("ClientboundSystemChatPacket", "network.protocol.game.");
|
||||
for (Constructor<?> cons : packetClass.getDeclaredConstructors()) {
|
||||
if (!cons.isAccessible())
|
||||
cons.setAccessible(true);
|
||||
if (cons.getParameterCount() == 2 && cons.getParameterTypes()[0].equals(String.class)
|
||||
&& cons.getParameterTypes()[1].equals(int.class)) { // "string, int"
|
||||
return cons.newInstance(json, 1);
|
||||
} else if (cons.getParameterCount() == 3 && cons.getParameterTypes()[1].equals(String.class)) { // "component", "string", <something not checked>
|
||||
Class<?> secondParam = cons.getParameterTypes()[2];
|
||||
if(secondParam.equals(int.class)) // "component", "string", "int"
|
||||
return cons.newInstance(null, json, 1);
|
||||
else if(secondParam.equals(boolean.class)) // "component", "string", "boolean"
|
||||
return cons.newInstance(null, json, false);
|
||||
}
|
||||
}
|
||||
ChatItem.getInstance().getLogger()
|
||||
.warning("Can't create a new packet for json " + json + ": no constructor found.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,13 @@
|
||||
package me.dadus33.chatitem.chatmanager.v1.basecomp;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.dadus33.chatitem.chatmanager.ChatManager;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
|
||||
import me.dadus33.chatitem.utils.Storage;
|
||||
|
||||
public interface IBaseComponentGetter {
|
||||
|
||||
@@ -11,4 +18,44 @@ public interface IBaseComponentGetter {
|
||||
String getBaseComponentAsJSON(ChatItemPacket packet);
|
||||
|
||||
void writeJson(ChatItemPacket packet, String json);
|
||||
|
||||
/**
|
||||
* Use actual base component getter to check if the given json has placeholders
|
||||
*
|
||||
* @param c the storage config
|
||||
* @param json the json
|
||||
* @return the founded placeholder or null
|
||||
*/
|
||||
default @Nullable String hasPlaceholders(Storage c, String json) {
|
||||
if(json == null || json.isEmpty())
|
||||
return null;
|
||||
for (String rep : c.PLACEHOLDERS) {
|
||||
if (json.contains(rep)) {
|
||||
return rep;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
default @Nullable String getNameFromMessage(String json, String toReplace) {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
String name = toReplace + p.getName();
|
||||
if (json.contains(name))
|
||||
return p.getName();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use actual base component getter to remove the player and the name.<br>
|
||||
* This is for special component which split message into sub string
|
||||
*
|
||||
* @param json the global json
|
||||
* @param toReplace the string which contains the placeholder
|
||||
* @param foundedPlayer the player which has the name in message
|
||||
* @return the replaced json
|
||||
*/
|
||||
default String removePlaceholdersAndName(String json, String toReplace, Player foundedPlayer) {
|
||||
return json.replace(toReplace + foundedPlayer.getName(), Character.toString(ChatManager.SEPARATOR));
|
||||
}
|
||||
}
|
||||
+203
-20
@@ -1,6 +1,10 @@
|
||||
package me.dadus33.chatitem.chatmanager.v1.basecomp.hook;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
@@ -8,27 +12,27 @@ import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.chatmanager.ChatManager;
|
||||
import me.dadus33.chatitem.chatmanager.v1.PacketEditingChatManager;
|
||||
import me.dadus33.chatitem.chatmanager.v1.basecomp.IBaseComponentGetter;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
|
||||
import me.dadus33.chatitem.utils.PacketUtils;
|
||||
import me.dadus33.chatitem.utils.ReflectionUtils;
|
||||
import me.dadus33.chatitem.utils.Storage;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
|
||||
public class AdventureComponentGetter implements IBaseComponentGetter {
|
||||
|
||||
private boolean useExtra = false;
|
||||
|
||||
@Override
|
||||
public boolean hasConditions() {
|
||||
try {
|
||||
for (String cl : Arrays.asList("net.kyori.adventure.text.Component",
|
||||
for (String cl : Arrays.asList("net.kyori.adventure.text.Component", "net.kyori.adventure.builder.AbstractBuilder",
|
||||
"net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer"))
|
||||
Class.forName(cl);
|
||||
} catch (ClassNotFoundException e) { // can't support this, adventure comp not found
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
PacketUtils.getNmsClass("PacketPlayOutChat", "network.protocol.game.").getField("adventure$message");
|
||||
} catch (NoSuchFieldException e) {
|
||||
} catch (Exception e) { // can't support this, adventure comp not found
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -37,25 +41,181 @@ public class AdventureComponentGetter implements IBaseComponentGetter {
|
||||
@Override
|
||||
public String getBaseComponentAsJSON(ChatItemPacket packet) {
|
||||
Component comp = packet.getContent().getSpecificModifier(Component.class).readSafely(0);
|
||||
if(comp == null)
|
||||
if(comp == null) {
|
||||
ChatItem.debug("The component is null.");
|
||||
return null;
|
||||
String json = ComponentSerializer.toString(BungeeComponentSerializer.legacy().serialize(comp).clone());
|
||||
}
|
||||
String json = ComponentSerializer.toString(BungeeComponentSerializer.get().serialize(comp).clone());
|
||||
ChatItem.debug("AdventureJSON : " + json);
|
||||
JsonObject jsonObj = JsonParser.parseString(json).getAsJsonObject();
|
||||
JsonObject next = new JsonObject();
|
||||
next.add("extra", jsonObj.get("with"));
|
||||
return next.toString();
|
||||
if(jsonObj.has("with")) {
|
||||
JsonObject next = new JsonObject();
|
||||
next.add("extra", jsonObj.get("with"));
|
||||
return next.toString();
|
||||
}
|
||||
useExtra = true;
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String hasPlaceholders(Storage c, String json) {
|
||||
if(useExtra)
|
||||
return IBaseComponentGetter.super.hasPlaceholders(c, json);
|
||||
JsonObject jsonObj = JsonParser.parseString(json).getAsJsonObject();
|
||||
if(!jsonObj.has("extra"))
|
||||
return null;
|
||||
for(JsonElement element : jsonObj.get("extra").getAsJsonArray()) {
|
||||
if(element.isJsonObject()) {
|
||||
JsonObject withObj = element.getAsJsonObject();
|
||||
if(withObj.has("extra")) {
|
||||
String text = "";
|
||||
for(JsonElement extra : withObj.get("extra").getAsJsonArray()) {
|
||||
if(extra.isJsonObject()) {
|
||||
JsonObject extraObj = extra.getAsJsonObject();
|
||||
if(extraObj.has("text") && extraObj.get("text").isJsonPrimitive())
|
||||
text += extraObj.get("text").getAsString();
|
||||
}
|
||||
}
|
||||
String placeholder = IBaseComponentGetter.super.hasPlaceholders(c, text);
|
||||
if(placeholder != null)
|
||||
return placeholder;
|
||||
} else if(withObj.has("text")) {
|
||||
String placeholder = IBaseComponentGetter.super.hasPlaceholders(c, withObj.get("text").getAsString());
|
||||
if(placeholder != null)
|
||||
return placeholder;
|
||||
}
|
||||
} // ignoring all others because it should not appear
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameFromMessage(String json, String toReplace) {
|
||||
if(useExtra)
|
||||
return IBaseComponentGetter.super.getNameFromMessage(json, toReplace);
|
||||
String tmpName = IBaseComponentGetter.super.getNameFromMessage(json, toReplace);
|
||||
if(tmpName != null)
|
||||
return tmpName; // don't need to check for json if basically found
|
||||
JsonObject jsonObj = JsonParser.parseString(json).getAsJsonObject();
|
||||
if(!jsonObj.has("extra"))
|
||||
return null;
|
||||
for(JsonElement element : jsonObj.get("extra").getAsJsonArray()) {
|
||||
if(element.isJsonObject()) {
|
||||
JsonObject withObj = element.getAsJsonObject();
|
||||
if(withObj.has("extra")) {
|
||||
String text = "";
|
||||
for(JsonElement extra : withObj.get("extra").getAsJsonArray()) {
|
||||
if(extra.isJsonObject()) {
|
||||
JsonObject extraObj = extra.getAsJsonObject();
|
||||
if(extraObj.has("text") && extraObj.get("text").isJsonPrimitive())
|
||||
text += extraObj.get("text").getAsString();
|
||||
}
|
||||
}
|
||||
String possibleName = IBaseComponentGetter.super.getNameFromMessage(text, toReplace);
|
||||
if(possibleName != null)
|
||||
return possibleName;
|
||||
} else if(withObj.has("text")) {
|
||||
String possibleName = IBaseComponentGetter.super.getNameFromMessage(withObj.get("text").getAsString(), toReplace);
|
||||
if(possibleName != null)
|
||||
return possibleName;
|
||||
}
|
||||
} // ignoring all others because it should not appear
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String removePlaceholdersAndName(String json, String toReplace, Player foundedPlayer) {
|
||||
if(useExtra)
|
||||
return IBaseComponentGetter.super.removePlaceholdersAndName(json, toReplace, foundedPlayer);
|
||||
String tmpName = IBaseComponentGetter.super.removePlaceholdersAndName(json, toReplace, foundedPlayer);
|
||||
if(!tmpName.equals(json)) // if not the same -> name found and removed
|
||||
return tmpName;
|
||||
JsonObject jsonObj = JsonParser.parseString(json).getAsJsonObject();
|
||||
if(!jsonObj.has("extra"))
|
||||
return json;
|
||||
String searched = toReplace + foundedPlayer.getName();
|
||||
for(JsonElement element : jsonObj.get("extra").getAsJsonArray()) {
|
||||
if(element.isJsonObject()) {
|
||||
JsonObject withObj = element.getAsJsonObject();
|
||||
if(withObj.has("extra")) {
|
||||
String text = "";
|
||||
int possibleId = -1;
|
||||
JsonArray extraArray = withObj.get("extra").getAsJsonArray();
|
||||
for(int i = 0; i < extraArray.size(); i++) {
|
||||
JsonElement extra = extraArray.get(i);
|
||||
if(!extra.isJsonObject())
|
||||
continue; // prevent error but should not appear
|
||||
JsonObject extraObj = extra.getAsJsonObject();
|
||||
if(extraObj.has("text") && extraObj.get("text").isJsonPrimitive()) {
|
||||
String extraTxt = extraObj.get("text").getAsString();
|
||||
if(possibleId == -1) { // don't found the begin yet
|
||||
if(searched.startsWith(extraTxt)) { // begin of replaced
|
||||
possibleId = i;
|
||||
text = extraTxt;
|
||||
}
|
||||
} else {
|
||||
if(searched.startsWith(extraTxt, text.length()) || ChatManager.equalsSeparator(extraTxt)) {
|
||||
text += extraTxt;
|
||||
if(searched.equals(text)) { // found everything
|
||||
// change actual, then remove all old
|
||||
extraArray.set(i, JsonParser.parseString("{\"text\":\"" + Character.toString(ChatManager.SEPARATOR) + "\"}"));
|
||||
for(int x = possibleId; x < i; x++)
|
||||
extraArray.remove(possibleId); // remove elements
|
||||
ChatItem.debug("Cleaned: " + jsonObj.toString());
|
||||
return jsonObj.toString();
|
||||
}
|
||||
} else {
|
||||
possibleId = -1; // was not good things
|
||||
text = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ChatItem.debug("Failed to find: " + text + ", id: " + possibleId + " for " + withObj);
|
||||
}
|
||||
} // ignoring all others because it should not appear
|
||||
}
|
||||
ChatItem.debug("Nothing founded while trying to remove placeholders");
|
||||
return IBaseComponentGetter.super.removePlaceholdersAndName(json, toReplace, foundedPlayer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeJson(ChatItemPacket packet, String json) {
|
||||
JsonObject next = new JsonObject();
|
||||
next.addProperty("translate", "chat.type.text");
|
||||
next.add("with", fixParsedArray(JsonParser.parseString(json).getAsJsonObject().getAsJsonArray("extra")));
|
||||
ChatItem.debug("Adventure Json: " + next.toString());
|
||||
packet.getContent().getSpecificModifier(Component.class).write(0, BungeeComponentSerializer.legacy().deserialize(ComponentSerializer.parse(next.toString())));
|
||||
JsonElement globalElement = JsonParser.parseString(json);
|
||||
JsonArray extraArray = fixParsedArray(globalElement.getAsJsonObject().getAsJsonArray("extra"));
|
||||
String localJson;
|
||||
if(useExtra) {
|
||||
globalElement.getAsJsonObject().add("extra", extraArray);
|
||||
localJson = globalElement.toString();
|
||||
} else {
|
||||
JsonObject next = new JsonObject();
|
||||
next.addProperty("translate", "chat.type.text");
|
||||
next.add("with", extraArray);
|
||||
localJson = next.toString();
|
||||
}
|
||||
ChatItem.debug("Adventure Json: " + json);
|
||||
try {
|
||||
Class<?> packetClass = packet.getPacket().getClass();
|
||||
if(packetClass.getSimpleName().equalsIgnoreCase("PacketPlayOutChat")) {
|
||||
Field componentField = ReflectionUtils.getFirstFieldWith(packetClass, Component.class);
|
||||
if(componentField != null) {
|
||||
componentField.setAccessible(true);
|
||||
componentField.set(packet.getPacket(), BungeeComponentSerializer.legacy().deserialize(ComponentSerializer.parse(localJson)));
|
||||
} else {
|
||||
throw new UnsupportedOperationException("The packet PacketPlayOutChat doesn't have Kyori's field. It has: " + Arrays.asList(packetClass.getDeclaredFields()).stream().map(f -> f.getName() + ": " + f.getType().getName()).collect(Collectors.toList()));
|
||||
}
|
||||
} else if(packetClass.getSimpleName().equalsIgnoreCase("ClientboundSystemChatPacket")) {
|
||||
packet.setPacket(PacketEditingChatManager.createSystemChatPacket(localJson));
|
||||
} else {
|
||||
throw new UnsupportedOperationException("The packet " + packetClass.getSimpleName() + " isn't supported by the AdventureGetter. Please report this.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private JsonArray fixParsedArray(JsonArray arr) {
|
||||
public JsonArray fixParsedArray(JsonArray arr) {
|
||||
JsonArray next = new JsonArray();
|
||||
for(JsonElement e : arr) {
|
||||
if(e.isJsonArray()) {
|
||||
@@ -90,7 +250,30 @@ public class AdventureComponentGetter implements IBaseComponentGetter {
|
||||
val = val.replaceAll("\\\"", "\"");
|
||||
ChatItem.debug("Fixed json: " + ("{" + val.substring(1, val.length() - 1) + "}"));
|
||||
hoverObject.remove("value");
|
||||
hoverObject.add("contents", JsonParser.parseString("{" + val.substring(1, val.length() - 1) + "}"));
|
||||
JsonElement tagElement = JsonParser.parseString("{" + val.substring(1, val.length() - 1) + "}");
|
||||
tagElement.getAsJsonObject().remove("tag");
|
||||
/*if(tagElement.getAsJsonObject().has("tag")) {
|
||||
JsonElement tag = tagElement.getAsJsonObject().get("tag");
|
||||
if(tag.isJsonObject()) {
|
||||
JsonObject tagObj = tag.getAsJsonObject();
|
||||
tagObj.entrySet().forEach(entry -> {
|
||||
ChatItem.debug("Tag json: " + entry.getKey() + " > " + entry.getValue());
|
||||
JsonElement value = entry.getValue();
|
||||
String sval = value.getAsString();
|
||||
if(Utils.isInteger(sval)) {
|
||||
if(Utils.isByte(sval))
|
||||
tagObj.addProperty(entry.getKey(), sval + "b");
|
||||
else if(Utils.isShort(sval))
|
||||
tagObj.addProperty(entry.getKey(), sval + "s");
|
||||
else if(Utils.isLong(sval))
|
||||
tagObj.addProperty(entry.getKey(), sval + "l");
|
||||
else
|
||||
tagObj.addProperty(entry.getKey(), value + "i");
|
||||
}
|
||||
});
|
||||
}
|
||||
}*/
|
||||
hoverObject.add("contents", tagElement);
|
||||
o.add("hoverEvent", hoverObject);
|
||||
}
|
||||
}
|
||||
|
||||
+44
-4
@@ -1,16 +1,22 @@
|
||||
package me.dadus33.chatitem.chatmanager.v1.basecomp.hook;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.chatmanager.v1.basecomp.IBaseComponentGetter;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketContent;
|
||||
import me.dadus33.chatitem.utils.PacketUtils;
|
||||
import me.dadus33.chatitem.utils.ReflectionUtils;
|
||||
|
||||
public class IChatBaseComponentGetter implements IBaseComponentGetter {
|
||||
|
||||
private Method serializerGetJson;
|
||||
|
||||
private boolean canEditVariable = true;
|
||||
|
||||
public IChatBaseComponentGetter() {
|
||||
try {
|
||||
for (Method m : PacketUtils.CHAT_SERIALIZER.getDeclaredMethods()) {
|
||||
@@ -27,12 +33,12 @@ public class IChatBaseComponentGetter implements IBaseComponentGetter {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasConditions() {
|
||||
return serializerGetJson != null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getBaseComponentAsJSON(ChatItemPacket packet) {
|
||||
try {
|
||||
@@ -47,7 +53,41 @@ public class IChatBaseComponentGetter implements IBaseComponentGetter {
|
||||
@Override
|
||||
public void writeJson(ChatItemPacket packet, String json) {
|
||||
try {
|
||||
packet.getContent().getChatComponents().write(0, PacketUtils.CHAT_SERIALIZER.getMethod("a", String.class).invoke(null, json));
|
||||
PacketContent content = packet.getContent();
|
||||
Object chatComponent = PacketUtils.CHAT_SERIALIZER.getMethod("a", String.class).invoke(null, json);
|
||||
if (canEditVariable)
|
||||
content.getChatComponents().write(0, chatComponent);
|
||||
else {
|
||||
try {
|
||||
Class<?> packetClass = packet.getPacket().getClass();
|
||||
if (packetClass.getSimpleName().equalsIgnoreCase("ClientboundSystemChatPacket")) {
|
||||
for (Constructor<?> cons : packetClass.getConstructors()) {
|
||||
if (cons.getParameterCount() == 6) {
|
||||
Object newPacket = cons.newInstance(chatComponent, Optional.empty(),
|
||||
content.getIntegers().readSafely(0, 0),
|
||||
content.getSpecificModifier(
|
||||
PacketUtils.getNmsClass("ChatSender", "network.chat.")).read(0),
|
||||
content.getSpecificModifier(Instant.class).read(0),
|
||||
ReflectionUtils.getObject(packet.getPacket(), "f"));
|
||||
packet.setPacket(newPacket);
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException("Failed to find valid constructor for packet "
|
||||
+ packetClass.getSimpleName() + ". Please report this.");
|
||||
} else {
|
||||
throw new UnsupportedOperationException("The packet " + packetClass.getSimpleName()
|
||||
+ " isn't supported by the AdventureGetter. Please report this.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
if (canEditVariable) {
|
||||
canEditVariable = false;
|
||||
writeJson(packet, json); // try again with good config
|
||||
} else
|
||||
e.printStackTrace(); // something else
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
+24
-4
@@ -1,21 +1,41 @@
|
||||
package me.dadus33.chatitem.chatmanager.v1.basecomp.hook;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.chatmanager.v1.PacketEditingChatManager;
|
||||
import me.dadus33.chatitem.chatmanager.v1.basecomp.IBaseComponentGetter;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
|
||||
import me.dadus33.chatitem.utils.PacketUtils;
|
||||
|
||||
public class StringComponentGetter implements IBaseComponentGetter{
|
||||
|
||||
@Override
|
||||
public String getBaseComponentAsJSON(ChatItemPacket packet) {
|
||||
return packet.getContent().getStrings().read(0);
|
||||
String json = packet.getContent().getStrings().readSafely(0);
|
||||
if(json != null && json.startsWith("[") && json.endsWith("]")) { // if used as array instead of json obj
|
||||
JsonArray extra = new JsonArray();
|
||||
for(JsonElement element : JsonParser.parseString(json).getAsJsonArray()) {
|
||||
if(element.isJsonObject()) { // ignore this
|
||||
extra.add(element);
|
||||
} else {
|
||||
ChatItem.debug("Ignoring element " + element);
|
||||
}
|
||||
}
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("text", "");
|
||||
jsonObject.add("extra", extra);
|
||||
json = jsonObject.toString();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeJson(ChatItemPacket packet, String json) {
|
||||
try {
|
||||
Class<?> packetClass = PacketUtils.getNmsClass("ClientboundSystemChatPacket", "network.protocol.game.");
|
||||
packet.setPacket(packetClass.getConstructor(String.class, int.class).newInstance(json, 1));
|
||||
packet.setPacket(PacketEditingChatManager.createSystemChatPacket(json));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -23,6 +22,7 @@ import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.chatmanager.ChatManager;
|
||||
import me.dadus33.chatitem.utils.PacketUtils;
|
||||
import me.dadus33.chatitem.utils.Reflect;
|
||||
import me.dadus33.chatitem.utils.Version;
|
||||
@@ -44,9 +44,8 @@ public class JSONManipulatorCurrent {
|
||||
private JsonObject itemTooltip;
|
||||
private JsonArray classicTooltip;
|
||||
|
||||
public String parse(String json, String placeholder, ItemStack item, String replacement, int protocol)
|
||||
public String parse(String json, ItemStack item, String replacement, int protocol)
|
||||
throws Exception {
|
||||
ChatItem.debug("Parsing: " + json);
|
||||
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
|
||||
JsonArray array = obj.has("extra") ? obj.getAsJsonArray("extra") : new JsonArray();
|
||||
final AbstractMap.SimpleEntry<Version, ItemStack> p = new AbstractMap.SimpleEntry<>(
|
||||
@@ -55,15 +54,13 @@ public class JSONManipulatorCurrent {
|
||||
if ((itemTooltip = STACKS.get(p)) == null) {
|
||||
JsonArray use = Translator.toJson(replacement); // We get the json representation of the old color
|
||||
// formatting method
|
||||
// There's no public clone method for JSONObjects so we need to parse them every
|
||||
// time
|
||||
JsonObject hover = JsonParser.parseString("{\"action\":\"show_item\", \"value\": \"\"}").getAsJsonObject();
|
||||
ChatItem.debug("Remplacement: " + replacement + " use: " + use.toString());
|
||||
// There's no public clone method for JSONObjects so we need to parse them every time
|
||||
JsonObject hover = new JsonObject();
|
||||
hover.addProperty("action", "show_item");
|
||||
|
||||
String jsonRep = stringifyItem(this, item, protocolVersion);// stringifyItem(item); // Get the JSON
|
||||
// representation of the item (well, not really
|
||||
// JSON, but
|
||||
// rather a string representation of NBT data)
|
||||
hover.addProperty("value", jsonRep);
|
||||
// Get the JSON representation of the item (well, not really JSON, but rather a string representation of NBT data)
|
||||
hover.addProperty("value", stringifyItem(this, item, protocolVersion));
|
||||
|
||||
JsonObject wrapper = new JsonObject(); // Create a wrapper object for the whole array
|
||||
wrapper.addProperty("text", ""); // The text field is compulsory, even if it's empty
|
||||
@@ -77,14 +74,15 @@ public class JSONManipulatorCurrent {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(ChatItem.getInstance(), () -> STACKS.remove(p), 100L);
|
||||
}
|
||||
|
||||
obj.add("extra", parseArray(placeholder, array, itemTooltip));
|
||||
obj.add("extra", parseArray(array, itemTooltip));
|
||||
if (!obj.has("text")) {
|
||||
obj.addProperty("text", "");
|
||||
}
|
||||
ChatItem.debug("Parsed array for item: " + obj.toString());
|
||||
return obj.toString();
|
||||
}
|
||||
|
||||
public String parseEmpty(String json, String placeholder, String repl, List<String> tooltip, Player sender) {
|
||||
public String parseEmpty(String json, String repl, List<String> tooltip, Player sender) {
|
||||
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
|
||||
JsonArray array = obj.has("extra") ? obj.getAsJsonArray("extra") : new JsonArray();
|
||||
JsonArray use = Translator
|
||||
@@ -108,42 +106,42 @@ public class JSONManipulatorCurrent {
|
||||
ob.getAsJsonObject().add("hoverEvent", hover);
|
||||
classicTooltip = use;
|
||||
}
|
||||
obj.add("extra", parseArray(placeholder, array, classicTooltip));
|
||||
obj.add("extra", parseArray(array, classicTooltip));
|
||||
if (!obj.has("text")) {
|
||||
obj.addProperty("text", "");
|
||||
}
|
||||
return obj.toString();
|
||||
}
|
||||
|
||||
private JsonArray parseArray(String placeholder, JsonArray arr, JsonElement tooltip) {
|
||||
private JsonArray parseArray(JsonArray arr, JsonElement tooltip) {
|
||||
JsonArray replacer = new JsonArray();
|
||||
for (int i = 0; i < arr.size(); ++i) {
|
||||
if (arr.get(i).isJsonNull()) {
|
||||
continue;
|
||||
} else if (arr.get(i).isJsonObject()) {
|
||||
addParsedJsonObjectToArray(arr.get(i).getAsJsonObject(), placeholder, replacer, tooltip);
|
||||
addParsedJsonObjectToArray(arr.get(i).getAsJsonObject(), replacer, tooltip);
|
||||
} else if (arr.get(i).isJsonArray()) {
|
||||
JsonArray jar = arr.get(i).getAsJsonArray();
|
||||
if (jar.size() != 0) {
|
||||
jar = parseArray(placeholder, arr.get(i).getAsJsonArray(), tooltip);
|
||||
jar = parseArray(arr.get(i).getAsJsonArray(), tooltip);
|
||||
replacer.set(i, jar);
|
||||
}
|
||||
} else {
|
||||
addParsedStringToArray(arr.get(i).getAsString(), placeholder, replacer, arr.get(i), tooltip);
|
||||
addParsedStringToArray(arr.get(i).getAsString(), replacer, arr.get(i), tooltip);
|
||||
}
|
||||
|
||||
}
|
||||
return replacer;
|
||||
}
|
||||
|
||||
private void addParsedJsonObjectToArray(JsonObject o, String placeholder, JsonArray rep, JsonElement tooltip) {
|
||||
private void addParsedJsonObjectToArray(JsonObject o, JsonArray rep, JsonElement tooltip) {
|
||||
JsonElement text = o.get("text");
|
||||
if (text == null) {
|
||||
JsonElement el = o.get("extra");
|
||||
if (el != null) {
|
||||
JsonArray jar = el.getAsJsonArray();
|
||||
if (jar.size() != 0) {
|
||||
o.add("extra", parseArray(placeholder, jar, tooltip));
|
||||
o.add("extra", parseArray(jar, tooltip));
|
||||
} else {
|
||||
o.remove("extra");
|
||||
}
|
||||
@@ -155,7 +153,7 @@ public class JSONManipulatorCurrent {
|
||||
if (el != null) {
|
||||
JsonArray jar = el.getAsJsonArray();
|
||||
if (jar.size() != 0) {
|
||||
o.add("extra", parseArray(placeholder, jar, tooltip));
|
||||
o.add("extra", parseArray(jar, tooltip));
|
||||
} else {
|
||||
o.remove("extra");
|
||||
}
|
||||
@@ -163,35 +161,29 @@ public class JSONManipulatorCurrent {
|
||||
}
|
||||
}
|
||||
|
||||
addParsedStringToArray(text.getAsString(), placeholder, rep, o, tooltip);
|
||||
addParsedStringToArray(text.getAsString(), rep, o, tooltip);
|
||||
}
|
||||
|
||||
private void addParsedStringToArray(String msg, String placeholder, JsonArray rep, JsonElement o, JsonElement tooltip) {
|
||||
boolean isLast = msg.endsWith(placeholder);
|
||||
if (isLast) {
|
||||
msg = msg.concat(".");
|
||||
}
|
||||
String[] splits = msg.split(Pattern.quote(placeholder));
|
||||
if (splits.length != 1) {
|
||||
for (int j = 0; j < splits.length; ++j) {
|
||||
boolean endDot = (j == splits.length - 1) && isLast;
|
||||
if (!splits[j].isEmpty() && !endDot) {
|
||||
String st = o.toString();
|
||||
JsonElement element = JsonParser.parseString(st);
|
||||
if(element.isJsonObject()) {
|
||||
JsonObject fix = element.getAsJsonObject();
|
||||
fix.addProperty("text", splits[j]);
|
||||
rep.add(fix);
|
||||
} else
|
||||
rep.add(new JsonPrimitive(splits[j]));
|
||||
}
|
||||
if (j != splits.length - 1) {
|
||||
rep.add(tooltip);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
private void addParsedStringToArray(String msg, JsonArray rep, JsonElement o, JsonElement tooltip) {
|
||||
ChatItem.debug("Checking " + msg + " > " + rep + ", o: " + o);
|
||||
if(!ChatManager.containsSeparator(msg)) {
|
||||
rep.add(o);
|
||||
return;
|
||||
}
|
||||
String current = "";
|
||||
for(String parts : msg.split("")) {
|
||||
if(ChatManager.equalsSeparator(parts)) {
|
||||
if(!current.isEmpty()) {
|
||||
rep.add(current);
|
||||
current = "";
|
||||
}
|
||||
rep.add(tooltip);
|
||||
} else {
|
||||
current += parts;
|
||||
}
|
||||
}
|
||||
if(!current.isEmpty())
|
||||
rep.add(current);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "deprecation" })
|
||||
@@ -236,9 +228,21 @@ public class JSONManipulatorCurrent {
|
||||
if (!first)
|
||||
sb.append(",");
|
||||
first = false;
|
||||
if (!entry.getKey().isEmpty())
|
||||
sb.append(entry.getKey()).append(":");
|
||||
sb.append(cleanStr(entry.getValue()));
|
||||
if (!entry.getKey().isEmpty()) {
|
||||
if(entry.getKey().contains(";"))
|
||||
sb.append("\"" + entry.getKey() + "\"");
|
||||
else
|
||||
sb.append(entry.getKey());
|
||||
sb.append(":");
|
||||
}
|
||||
if(entry.getKey().equalsIgnoreCase("display") && Version.getVersion().isNewerOrEquals(Version.V1_16)) {
|
||||
String replaced = entry.getValue().replace("','", ",").replace("\"}',", "\"},");
|
||||
ChatItem.debug("Cleaning display (custom): " + entry.getValue() + " > " + replaced);
|
||||
sb.append(replaced);
|
||||
} else {
|
||||
ChatItem.debug("Cleaning " + entry.getKey() + ": " + entry.getValue() + " > " + cleanStr(entry.getValue()));
|
||||
sb.append(cleanStr(entry.getValue()));
|
||||
}
|
||||
}
|
||||
sb.append("}}"); // End of tag and end of item
|
||||
return sb.toString();
|
||||
|
||||
@@ -3,14 +3,81 @@ package me.dadus33.chatitem.chatmanager.v1.json;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.utils.ColorManager;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
//Based on DarkSeraphim's system, but using Gson and supporting some more edge cases
|
||||
public class Translator {
|
||||
|
||||
private static final String STYLES = "klmnor"; //All style codes
|
||||
|
||||
|
||||
public static JsonArray toJson(String old){
|
||||
if(old.lastIndexOf(ChatColor.COLOR_CHAR) == -1){
|
||||
JsonArray arr = new JsonArray();
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("text", old);
|
||||
arr.add(obj);
|
||||
return arr;
|
||||
}
|
||||
JsonArray message = new JsonArray();
|
||||
JsonObject next = new JsonObject();
|
||||
String colorCode = "", text = "";
|
||||
boolean waiting = false;
|
||||
for (char args : old.toCharArray()) {
|
||||
if (args == '§') { // begin of color
|
||||
if(!text.isEmpty()) {
|
||||
next.addProperty("text", text);
|
||||
message.add(next);
|
||||
next = new JsonObject();
|
||||
text = "";
|
||||
}
|
||||
waiting = true; // waiting for color code
|
||||
} else if (waiting) { // if waiting for code and valid str
|
||||
// if it's hexademical value and with enough space for full color
|
||||
waiting = false;
|
||||
if(isStyle(args)) { // is style and not making rich color code
|
||||
next.addProperty(getStyleName(args), true);
|
||||
continue;
|
||||
}
|
||||
if(args == 'x' && !colorCode.isEmpty()) {
|
||||
if(!text.isEmpty()) // ignore this if no text before
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
colorCode = "x";
|
||||
} else
|
||||
colorCode += args; // a color by itself
|
||||
} else {
|
||||
waiting = false;
|
||||
if(!colorCode.isEmpty()) { // manage color
|
||||
if(colorCode.startsWith("x") && colorCode.length() == 7) { // hex color code
|
||||
next.addProperty("color", ColorManager.getColor(colorCode).getName());
|
||||
} else if(colorCode.length() == 1) { // if only one color code
|
||||
if(next.has("color"))
|
||||
text += ColorManager.getColor(colorCode);
|
||||
else
|
||||
next.addProperty("color", ColorManager.getColor(colorCode).getName());
|
||||
} else if(!text.isEmpty())// no text before -> color will be used as "color"
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
|
||||
ChatItem.debug("Add " + colorCode + " to " + text + ", msg: " + message);
|
||||
colorCode = "";
|
||||
}
|
||||
// basic text, not waiting for code after '§'
|
||||
text += args;
|
||||
}
|
||||
}
|
||||
if(!text.isEmpty() || message.isEmpty()) {
|
||||
next.addProperty("text", text);
|
||||
if(!next.has("color"))
|
||||
next.addProperty("color", "white");
|
||||
message.add(next);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
public static JsonArray toJsonOld(String old){
|
||||
if(old.lastIndexOf(ChatColor.COLOR_CHAR) == -1){
|
||||
JsonArray arr = new JsonArray();
|
||||
JsonObject obj = new JsonObject();
|
||||
@@ -104,7 +171,7 @@ public class Translator {
|
||||
}
|
||||
|
||||
private static String getColorName(char code){
|
||||
return ChatColor.getByChar(code).name().toLowerCase();
|
||||
return ChatColor.getByChar(code).getName().toLowerCase();
|
||||
}
|
||||
|
||||
private static String getStyleName(char code){
|
||||
@@ -115,7 +182,9 @@ public class Translator {
|
||||
case 'n': return "underlined";
|
||||
case 'o': return "italic";
|
||||
case 'r': return "reset";
|
||||
default: return null; //Should never happen. Made it return null to throw errors if a new format pops up and it really happens
|
||||
default:
|
||||
ChatItem.getInstance().getLogger().severe("Can't find code for style " + code);
|
||||
return null; //Should never happen. Made it return null to throw errors if a new format pops up and it really happens
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,9 +87,6 @@ public class ChatEventListener implements Listener {
|
||||
} catch (Exception exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
if (getStorage().COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown")) {
|
||||
ChatManager.COOLDOWNS.put(p.getUniqueId(), System.currentTimeMillis() / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
@@ -146,7 +143,7 @@ public class ChatEventListener implements Listener {
|
||||
StringJoiner msg = new StringJoiner(" ");
|
||||
for(String part : s.split(" ")) {
|
||||
if(part.equalsIgnoreCase(firstPlaceholder)) {
|
||||
msg.add(firstPlaceholder + SEPARATOR + p.getName());
|
||||
msg.add(firstPlaceholder + Character.toString(SEPARATOR) + p.getName());
|
||||
} else {
|
||||
msg.add(part);
|
||||
}
|
||||
@@ -155,8 +152,5 @@ public class ChatEventListener implements Listener {
|
||||
} catch (Exception exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
if (getStorage().COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown")) {
|
||||
ChatManager.COOLDOWNS.put(p.getUniqueId(), System.currentTimeMillis() / 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.ShulkerBox;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -33,11 +33,9 @@ import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketContent;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketHandler;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketType;
|
||||
import me.dadus33.chatitem.itemnamer.NamerManager;
|
||||
import me.dadus33.chatitem.utils.ItemUtils;
|
||||
import me.dadus33.chatitem.utils.PacketUtils;
|
||||
import me.dadus33.chatitem.utils.Storage;
|
||||
import me.dadus33.chatitem.utils.Utils;
|
||||
import me.dadus33.chatitem.utils.Version;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
@@ -65,17 +63,19 @@ public class ChatPacketManager extends PacketHandler {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for(IBaseComponentGetter getter : Arrays.asList(new IChatBaseComponentGetter(), new BaseComponentGetter(), new ComponentGetter(), new AdventureComponentGetter())) {
|
||||
for(IBaseComponentGetter getter : Arrays.asList(new IChatBaseComponentGetter(), new BaseComponentGetter(), new ComponentGetter(), new StringComponentGetter(), new AdventureComponentGetter())) {
|
||||
if(getter.hasConditions())
|
||||
baseComponentGetter.add(getter);
|
||||
}
|
||||
ChatItem.getInstance().getLogger().info("Loaded " + baseComponentGetter.size() + " getter for base components.");
|
||||
ChatItem.debug("BaseComponentGetters: " + String.join(", ", baseComponentGetter.stream().map(IBaseComponentGetter::getClass).map(Class::getSimpleName).collect(Collectors.toList())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSend(ChatItemPacket e) {
|
||||
if (!e.hasPlayer() || !e.getPacketType().equals(PacketType.Server.CHAT))
|
||||
return;
|
||||
ChatItem.debug("Checking: " + e.getPacketType().getFullName() + " > " + e.getPacket().getClass().getSimpleName());
|
||||
if (lastSentPacket != null && lastSentPacket == e.getPacket())
|
||||
return; // prevent infinite loop
|
||||
PacketContent packet = e.getContent();
|
||||
@@ -83,11 +83,12 @@ public class ChatPacketManager extends PacketHandler {
|
||||
String json = "{}";
|
||||
IBaseComponentGetter choosedGetter = null;
|
||||
if (version.isNewerOrEquals(Version.V1_19)) {
|
||||
if(packet.getIntegers().readSafely(0) != 1)
|
||||
if(packet.getIntegers().readSafely(0, 0) > 1) { // not parsed chat message type
|
||||
ChatItem.debug("Invalid int: " + packet.getIntegers().read(0));
|
||||
return;
|
||||
String foundedJson = packet.getStrings().read(0); // for final things
|
||||
json = foundedJson;
|
||||
}
|
||||
choosedGetter = new StringComponentGetter();
|
||||
json = choosedGetter.getBaseComponentAsJSON(e); // if null, will be re-checked so anyway
|
||||
} else if (version.isNewerOrEquals(Version.V1_12)) {
|
||||
// only if action bar messages are supported in this version of minecraft
|
||||
try {
|
||||
@@ -106,65 +107,51 @@ public class ChatPacketManager extends PacketHandler {
|
||||
for(IBaseComponentGetter getters : baseComponentGetter) {
|
||||
String tmpJson = getters.getBaseComponentAsJSON(e);
|
||||
if(tmpJson != null) {
|
||||
ChatItem.debug("Found " + tmpJson + " with " + getters.getClass().getName());
|
||||
json = tmpJson;
|
||||
json = ChatManager.fixSeparator(tmpJson);
|
||||
choosedGetter = getters;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean found = false;
|
||||
for (String rep : getStorage().PLACEHOLDERS) {
|
||||
if (json.contains(rep)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if(json == null || choosedGetter == null) {
|
||||
ChatItem.debug("Can't find valid json getter or json itself");
|
||||
ChatItem.debug("String: " + packet.getStrings().getContent());
|
||||
return; // can't find something
|
||||
}
|
||||
if (!found) {
|
||||
String placeholder = choosedGetter.hasPlaceholders(getStorage(), json);
|
||||
if (placeholder == null) {
|
||||
ChatItem.debug("No placeholders founded in " + json);
|
||||
ChatItem.debug("String: " + packet.getStrings().getContent());
|
||||
return; // then it's just a normal message without placeholders, so we leave it alone
|
||||
}
|
||||
Object toReplace = null;
|
||||
ChatItem.debug("Found " + placeholder + " with " + choosedGetter.getClass().getName());
|
||||
String toReplace = null;
|
||||
if (json.lastIndexOf(SEPARATOR) != -1)
|
||||
toReplace = SEPARATOR;
|
||||
toReplace = ((Object) SEPARATOR).toString();
|
||||
if (json.lastIndexOf(SEPARATOR_STR) != -1)
|
||||
toReplace = SEPARATOR_STR;
|
||||
if (toReplace == null) { // if the message doesn't contain the BELL separator
|
||||
ChatItem.debug("Not containing bell " + json);
|
||||
return;
|
||||
}
|
||||
ChatItem.debug("Add packet meta to json: " + json);
|
||||
toReplace = placeholder + toReplace; // add placeholder
|
||||
String name = choosedGetter.getNameFromMessage(json, toReplace);
|
||||
if (name == null) { // something went really bad, so we run away and hide (AKA the player left or is
|
||||
// on another server)
|
||||
ChatItem.debug("Name null for " + json);
|
||||
return;
|
||||
}
|
||||
Player itemPlayer = Bukkit.getPlayer(name);
|
||||
String localJson = choosedGetter.removePlaceholdersAndName(json, toReplace, itemPlayer);
|
||||
ChatItem.debug("Local json: " + localJson);
|
||||
IBaseComponentGetter fchoosedGetter = choosedGetter;
|
||||
String fjson = json, toReplaceStr = toReplace.toString();
|
||||
e.setCancelled(true); // We cancel the packet as we're going to resend it anyways (ignoring listeners
|
||||
// this time)
|
||||
Bukkit.getScheduler().runTaskAsynchronously(ChatItem.getInstance(), () -> {
|
||||
Player p = e.getPlayer();
|
||||
int topIndex = -1;
|
||||
String name = null;
|
||||
for (Player pl : Bukkit.getOnlinePlayers()) {
|
||||
String pname = toReplaceStr + pl.getName();
|
||||
if (!fjson.contains(pname)) {
|
||||
continue;
|
||||
}
|
||||
int index = fjson.lastIndexOf(pname) + pname.length();
|
||||
if (index > topIndex) {
|
||||
topIndex = index;
|
||||
name = pname.replace(toReplaceStr, "");
|
||||
}
|
||||
if (getStorage().COOLDOWN > 0 && !itemPlayer.hasPermission("chatitem.ignore-cooldown")) {
|
||||
ChatManager.applyCooldown(itemPlayer);
|
||||
}
|
||||
if (name == null) { // something went really bad, so we run away and hide (AKA the player left or is
|
||||
// on another server)
|
||||
ChatItem.debug("Name null for " + fjson);
|
||||
return;
|
||||
}
|
||||
|
||||
Player itemPlayer = Bukkit.getPlayer(name);
|
||||
StringBuilder builder = new StringBuilder(fjson);
|
||||
builder.replace(topIndex - (name.length() + 6), topIndex, ""); // we remove both the name and the separator
|
||||
// from the json string
|
||||
String localJson = fjson.replace(toReplaceStr + itemPlayer.getName(), "");// builder.toString();
|
||||
|
||||
String message = null;
|
||||
try {
|
||||
ItemStack item = ChatManager.getUsableItem(itemPlayer);
|
||||
@@ -177,13 +164,13 @@ public class ChatPacketManager extends PacketHandler {
|
||||
if (act.equalsIgnoreCase("tooltip"))
|
||||
tooltip = getStorage().BUGGED_CLIENTS_TOOLTIP;
|
||||
else if (act.equalsIgnoreCase("item"))
|
||||
tooltip = getMaxLinesFromItem(p, copy);
|
||||
tooltip = ChatManager.getMaxLinesFromItem(p, copy);
|
||||
else if (act.equalsIgnoreCase("show_both")) {
|
||||
tooltip = getMaxLinesFromItem(p, copy);
|
||||
tooltip = ChatManager.getMaxLinesFromItem(p, copy);
|
||||
tooltip.addAll(getStorage().BUGGED_CLIENTS_TOOLTIP);
|
||||
} else
|
||||
tooltip = new ArrayList<>();
|
||||
message = manager.getManipulator().parseEmpty(localJson, getStorage().PLACEHOLDERS.get(0),
|
||||
message = manager.getManipulator().parseEmpty(localJson,
|
||||
ChatManager.styleItem(p, copy, getStorage()), tooltip, itemPlayer);
|
||||
if (!manager.supportsChatComponentApi()) {
|
||||
ChatItem.debug("Use basic for 1.7 lunar");
|
||||
@@ -197,31 +184,29 @@ public class ChatPacketManager extends PacketHandler {
|
||||
PacketUtils.sendPacket(p, lastSentPacket);
|
||||
return;
|
||||
}
|
||||
if (copy.getType().name().contains("_BOOK")) { // filtering written books
|
||||
BookMeta bm = (BookMeta) copy.getItemMeta();
|
||||
bm.setPages(Collections.emptyList());
|
||||
copy.setItemMeta(bm);
|
||||
} else {
|
||||
if (copy.getType().name().contains("SHULKER_BOX")) { // if it's shulker
|
||||
if (copy.hasItemMeta()) {
|
||||
BlockStateMeta bsm = (BlockStateMeta) copy.getItemMeta();
|
||||
if (bsm.hasBlockState()) {
|
||||
ShulkerBox sb = (ShulkerBox) bsm.getBlockState();
|
||||
for (ItemStack itemInv : sb.getInventory()) {
|
||||
stripData(itemInv);
|
||||
}
|
||||
bsm.setBlockState(sb);
|
||||
if(copy.hasItemMeta()) {
|
||||
ItemMeta meta = copy.getItemMeta();
|
||||
if (meta instanceof BookMeta) { // filtering written books
|
||||
BookMeta bm = (BookMeta) copy.getItemMeta();
|
||||
bm.setPages(Collections.emptyList());
|
||||
copy.setItemMeta(bm);
|
||||
} else if (meta instanceof BlockStateMeta && Version.getVersion().isNewerOrEquals(Version.V1_9)) { // if it's a block
|
||||
BlockStateMeta bsm = (BlockStateMeta) copy.getItemMeta();
|
||||
if (bsm.hasBlockState() && bsm.getBlockState() instanceof ShulkerBox) {
|
||||
ShulkerBox sb = (ShulkerBox) bsm.getBlockState();
|
||||
for (ItemStack itemInv : sb.getInventory()) {
|
||||
stripData(itemInv);
|
||||
}
|
||||
copy.setItemMeta(bsm);
|
||||
bsm.setBlockState(sb);
|
||||
}
|
||||
copy.setItemMeta(bsm);
|
||||
}
|
||||
}
|
||||
message = manager.getManipulator().parse(localJson, getStorage().PLACEHOLDERS.get(0), copy,
|
||||
message = manager.getManipulator().parse(localJson, copy,
|
||||
ChatManager.styleItem(p, copy, getStorage()), ItemPlayer.getPlayer(itemPlayer).getProtocolVersion());
|
||||
} else {
|
||||
if (!getStorage().HAND_DISABLED) {
|
||||
message = manager.getManipulator().parseEmpty(localJson, getStorage().PLACEHOLDERS.get(0),
|
||||
getStorage().HAND_NAME, getStorage().HAND_TOOLTIP, itemPlayer);
|
||||
message = manager.getManipulator().parseEmpty(localJson, getStorage().HAND_NAME, getStorage().HAND_TOOLTIP, itemPlayer);
|
||||
}
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
@@ -266,23 +251,4 @@ public class ChatPacketManager extends PacketHandler {
|
||||
public Storage getStorage() {
|
||||
return manager.getStorage();
|
||||
}
|
||||
|
||||
private List<String> getMaxLinesFromItem(Player p, ItemStack item) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
if (item.hasItemMeta()) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
lines.add(meta.hasDisplayName() ? meta.getDisplayName()
|
||||
: NamerManager.getName(p, item, getStorage()));
|
||||
if (meta.hasEnchants()) {
|
||||
meta.getEnchants().forEach((enchant, lvl) -> {
|
||||
lines.add(ChatColor.RESET + Utils.getEnchantName(enchant) + " " + Utils.toRoman(lvl));
|
||||
});
|
||||
}
|
||||
if (meta.hasLore())
|
||||
lines.addAll(meta.getLore());
|
||||
} else {
|
||||
lines.add(NamerManager.getName(p, item, getStorage()));
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public interface PacketType {
|
||||
|
||||
public static enum Server implements PacketType {
|
||||
|
||||
CHAT("Chat", "ClientboundSystemChatPacket"),
|
||||
CHAT("Chat", "ClientboundPlayerChatPacket", "ClientboundSystemChatPacket"),
|
||||
UNSET("Unset");
|
||||
|
||||
private final String packetName, fullName;
|
||||
|
||||
-2
@@ -10,7 +10,6 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketManager;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketType;
|
||||
@@ -84,7 +83,6 @@ public class CustomPacketManager extends PacketManager implements Listener {
|
||||
|
||||
public ChatItemPacket onPacketSent(PacketType type, Player sender, Object packet) {
|
||||
if(type == null) {
|
||||
ChatItem.debug("Failed to find packet type for " + packet.getClass().getSimpleName());
|
||||
return null;
|
||||
}
|
||||
ChatItemPacket customPacket = new ChatItemPacket(type, packet, sender);
|
||||
|
||||
+10
-1
@@ -4,6 +4,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@@ -12,6 +14,7 @@ import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketContent;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketType;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.PacketContent.ContentModifier;
|
||||
import me.dadus33.chatitem.chatmanager.v1.packets.custom.CustomPacketManager;
|
||||
|
||||
public class ChannelInboundHandler extends ChannelInboundHandlerAdapter {
|
||||
@@ -71,7 +74,13 @@ public class ChannelInboundHandler extends ChannelInboundHandlerAdapter {
|
||||
try {
|
||||
PacketType packetType = PacketType.getType(packet.getClass().getSimpleName());
|
||||
if(packetType != null && packetType == PacketType.Handshake.IS_SET_PROTOCOL) {
|
||||
packetManager.protocolVersionPerChannel.put(channel, new PacketContent(packet).getIntegers().readSafely(0, 0));
|
||||
ContentModifier<Integer> ints = new PacketContent(packet).getIntegers();
|
||||
int possibleProtocol = ints.readSafely(0, 0);
|
||||
if(possibleProtocol == Bukkit.getPort())
|
||||
possibleProtocol = ints.readSafely(1, 0);
|
||||
if(possibleProtocol == -1 || possibleProtocol == 255)
|
||||
possibleProtocol = ints.readSafely(2, 0);
|
||||
packetManager.protocolVersionPerChannel.put(channel, possibleProtocol);
|
||||
}
|
||||
super.channelRead(ctx, packet);
|
||||
} catch (Exception e) {
|
||||
|
||||
+11
-4
@@ -1,6 +1,7 @@
|
||||
package me.dadus33.chatitem.chatmanager.v1.packets.protocollib;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -23,14 +24,20 @@ public class ProtocollibPacketManager extends PacketManager {
|
||||
|
||||
public ProtocollibPacketManager(ChatItem pl) {
|
||||
protocolManager = ProtocolLibrary.getProtocolManager();
|
||||
protocolManager.addPacketListener(new PacketAdapter(pl, ListenerPriority.LOWEST, Arrays.asList(Play.Server.CHAT)) {
|
||||
List<com.comphenix.protocol.PacketType> list = new ArrayList<>();
|
||||
list.add(Play.Server.CHAT);
|
||||
try {
|
||||
list.add((com.comphenix.protocol.PacketType) Play.Server.class.getDeclaredField("SYSTEM_CHAT").get(null));
|
||||
} catch (Exception e) {
|
||||
// ignore because using old version
|
||||
}
|
||||
protocolManager.addPacketListener(new PacketAdapter(pl, ListenerPriority.LOWEST, list) {
|
||||
@Override
|
||||
public void onPacketSending(PacketEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
if (p == null || e.isPlayerTemporary())
|
||||
return;
|
||||
ChatItemPacket packet = onPacketSent(PacketType.getType(e.getPacket().getHandle().getClass().getSimpleName()),
|
||||
p, e.getPacket().getHandle());
|
||||
ChatItemPacket packet = onPacketSent(PacketType.getType(e.getPacket().getHandle().getClass().getSimpleName()), p, e.getPacket().getHandle());
|
||||
if(packet == null) {
|
||||
ChatItem.debug("Can't find packet: " + e.getPacket().getHandle().getClass().getName());
|
||||
return;
|
||||
|
||||
@@ -6,6 +6,8 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -15,6 +17,7 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.ItemPlayer;
|
||||
import me.dadus33.chatitem.chatmanager.ChatManager;
|
||||
import me.dadus33.chatitem.hook.DiscordSrvSupport;
|
||||
import me.dadus33.chatitem.playernamer.PlayerNamerManager;
|
||||
@@ -30,18 +33,14 @@ import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.HoverEvent.Action;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.chat.hover.content.Text;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ChatListener implements Listener {
|
||||
|
||||
private ChatListenerChatManager manage;
|
||||
private Method saveMethod;
|
||||
private boolean shouldUseAppendMethod = false;
|
||||
|
||||
public ChatListener(ChatListenerChatManager manage) {
|
||||
this.manage = manage;
|
||||
|
||||
|
||||
private static Method saveMethod;
|
||||
private static boolean shouldUseAppendMethod = false;
|
||||
|
||||
static {
|
||||
try {
|
||||
Class<?> nbtTag = PacketUtils.getNmsClass("NBTTagCompound", "nbt.");
|
||||
Class<?> itemClass = PacketUtils.getNmsClass("ItemStack", "world.item.");
|
||||
@@ -69,6 +68,12 @@ public class ChatListener implements Listener {
|
||||
else
|
||||
log.info("Save method founded: " + saveMethod.getName() + ". " + appendMethodMsg);
|
||||
}
|
||||
|
||||
private ChatListenerChatManager manage;
|
||||
|
||||
public ChatListener(ChatListenerChatManager manage) {
|
||||
this.manage = manage;
|
||||
}
|
||||
|
||||
public Storage getStorage() {
|
||||
return manage.getStorage();
|
||||
@@ -147,16 +152,15 @@ public class ChatListener implements Listener {
|
||||
.replace("{display-name}", p.getDisplayName()));
|
||||
ChatItem.debug("Msg: " + msg.replace(ChatColor.COLOR_CHAR, '&') + ", format: " + format);
|
||||
e.getRecipients().forEach((pl) -> showItem(pl, p, item, msg));
|
||||
if (c.COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown")) {
|
||||
ChatManager.COOLDOWNS.put(p.getUniqueId(), System.currentTimeMillis() / 1000);
|
||||
}
|
||||
if (c.COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown"))
|
||||
ChatManager.applyCooldown(p);
|
||||
}
|
||||
|
||||
private void showItem(Player to, Player origin, ItemStack item, String msg) {
|
||||
public static void showItem(Player to, Player origin, ItemStack item, String msg) {
|
||||
ComponentBuilder builder = new ComponentBuilder("");
|
||||
ChatColor color = ChatColor.RESET;
|
||||
ChatColor color = ChatColor.WHITE;
|
||||
String colorCode = "", text = "";
|
||||
boolean waiting = false, isHex = false;
|
||||
boolean waiting = false;
|
||||
for (char args : msg.toCharArray()) {
|
||||
if (args == '§') { // begin of color
|
||||
if (colorCode.isEmpty() && !text.isEmpty()) { // text before this char
|
||||
@@ -172,15 +176,23 @@ public class ChatListener implements Listener {
|
||||
color = ChatColor.RESET;
|
||||
continue;
|
||||
}
|
||||
if(args == 'x' && colorCode.isEmpty())
|
||||
isHex = true;
|
||||
colorCode += args; // a color by itself
|
||||
if(args == 'x' && !colorCode.isEmpty()) {
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
colorCode = "x";
|
||||
} else
|
||||
colorCode += args; // a color by itself
|
||||
} else {
|
||||
waiting = false;
|
||||
if(!colorCode.isEmpty()) {
|
||||
if(isHex && colorCode.length() >= 7)
|
||||
color = ColorManager.getColor(colorCode);
|
||||
else if(colorCode.length() == 1) // if only one color code
|
||||
if(colorCode.startsWith("x") && colorCode.length() >= 7) {
|
||||
if(colorCode.length() == 7)
|
||||
color = ColorManager.getColor(colorCode);
|
||||
else {
|
||||
color = ColorManager.getColor(colorCode.substring(0, 7)); // only the hex code
|
||||
ChatItem.debug("Adding color for " + colorCode.substring(7, colorCode.length()) + " (in " + colorCode + ")");
|
||||
text += ColorManager.getColorString(colorCode.substring(7, colorCode.length()));
|
||||
}
|
||||
} else if(colorCode.length() == 1) // if only one color code
|
||||
color = ColorManager.getColor(colorCode);
|
||||
else
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
@@ -188,7 +200,7 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
if (args == ChatManager.SEPARATOR) {
|
||||
// here put the item
|
||||
appendToComponentBuilder(builder, new ComponentBuilder(text).color(color).create());
|
||||
appendToComponentBuilder(builder, fixColorComponent(text, color));
|
||||
addItem(builder, to, origin, item);
|
||||
text = "";
|
||||
} else {
|
||||
@@ -209,7 +221,7 @@ public class ChatListener implements Listener {
|
||||
* @param itemStack the item to convert
|
||||
* @return the Json string representation of the item
|
||||
*/
|
||||
public String convertItemStackToJson(ItemStack itemStack) {
|
||||
public static String convertItemStackToJson(ItemStack itemStack) {
|
||||
try {
|
||||
Class<?> nbtTag = PacketUtils.getNmsClass("NBTTagCompound", "nbt.");
|
||||
Class<?> craftItemClass = PacketUtils.getObcClass("inventory.CraftItemStack");
|
||||
@@ -228,40 +240,43 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
public void addItem(ComponentBuilder builder, Player to, Player origin, ItemStack item) {
|
||||
public static void addItem(ComponentBuilder builder, Player to, Player origin, ItemStack item) {
|
||||
Storage c = ChatItem.getInstance().getStorage();
|
||||
if (!ItemUtils.isEmpty(item)) {
|
||||
ComponentBuilder itemComponent = new ComponentBuilder(ChatManager.styleItem(to, item, getStorage()));
|
||||
ComponentBuilder itemComponent = new ComponentBuilder("");
|
||||
String itemJson = convertItemStackToJson(item);
|
||||
itemComponent.event(new HoverEvent(Action.SHOW_ITEM, new ComponentBuilder(itemJson).create()));
|
||||
BaseComponent[] itemBaseComponent = new ComponentBuilder(itemJson).create();
|
||||
appendToComponentBuilder(itemComponent, fixColorComponent(ChatManager.getNameOfItem(to, item, c), ChatColor.WHITE, itemBaseComponent));
|
||||
ChatItem.debug("Item for " + to.getName() + " (ver: " + ItemPlayer.getPlayer(to).getVersion().name() + ") : " + itemJson);
|
||||
//itemComponent.event(new HoverEvent(Action.SHOW_ITEM, itemBaseComponent));
|
||||
appendToComponentBuilder(builder, itemComponent.create());
|
||||
} else {
|
||||
String handName = getStorage().HAND_NAME;
|
||||
String handName = c.HAND_NAME;
|
||||
ComponentBuilder handComp = new ComponentBuilder("");
|
||||
ComponentBuilder handTooltip = new ComponentBuilder("");
|
||||
int stay = getStorage().HAND_TOOLTIP.size();
|
||||
for (String line : getStorage().HAND_TOOLTIP) {
|
||||
int stay = c.HAND_TOOLTIP.size();
|
||||
for (String line : c.HAND_TOOLTIP) {
|
||||
stay--;
|
||||
handTooltip.append(
|
||||
line.replace("{name}", origin.getName()).replace("{display-name}", origin.getDisplayName()));
|
||||
handTooltip.append(ColorManager.fixColor(line.replace("{name}", origin.getName()).replace("{display-name}", origin.getDisplayName())));
|
||||
if (stay > 0)
|
||||
handTooltip.append("\n");
|
||||
}
|
||||
handComp.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(handTooltip.create())));
|
||||
if (handName.contains("{name}")) {
|
||||
String[] splitted = handName.split("\\{name\\}");
|
||||
handComp.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, handTooltip.create()));
|
||||
if (handName.contains("{display-name}")) {
|
||||
String[] splitted = handName.split("\\{display-name\\}");
|
||||
for (int i = 0; i < (splitted.length - 1); i++) {
|
||||
handComp.append(splitted[i]);
|
||||
appendToComponentBuilder(handComp, PlayerNamerManager.getPlayerNamer().getName(origin));
|
||||
}
|
||||
handComp.append(splitted[splitted.length - 1]);
|
||||
} else
|
||||
handComp.append(handName.replace("{display-name}", origin.getDisplayName()));
|
||||
handComp.append(handName.replace("{name}", origin.getName()));
|
||||
appendToComponentBuilder(builder, handComp.create());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void appendToComponentBuilder(ComponentBuilder builder, BaseComponent[] comps) {
|
||||
public static void appendToComponentBuilder(ComponentBuilder builder, BaseComponent[] comps) {
|
||||
if (shouldUseAppendMethod) {
|
||||
try {
|
||||
builder.append(comps);
|
||||
@@ -282,4 +297,68 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static BaseComponent[] fixColorComponent(String message, ChatColor color) {
|
||||
return fixColorComponent(message, color, null);
|
||||
}
|
||||
|
||||
public static BaseComponent[] fixColorComponent(String message, ChatColor color, @Nullable BaseComponent[] jsonItem) {
|
||||
ComponentBuilder builder = new ComponentBuilder("");
|
||||
String colorCode = "", text = "";
|
||||
boolean waiting = false;
|
||||
for (char args : message.toCharArray()) {
|
||||
if (args == '§') { // begin of color
|
||||
if (colorCode.isEmpty() && !text.isEmpty()) { // text before this char
|
||||
ChatItem.debug("Append while fixing name " + (ColorManager.isHexColor(color) && builder.getParts().isEmpty() ? ColorManager.removeColorAtBegin(text) : text));
|
||||
ComponentBuilder littleBuilder = new ComponentBuilder(ColorManager.isHexColor(color) ? ColorManager.removeColorAtBegin(text) : text).color(color);
|
||||
if(jsonItem != null) { // add to all possible sub parts
|
||||
littleBuilder.event(new HoverEvent(Action.SHOW_ITEM, jsonItem));
|
||||
}
|
||||
appendToComponentBuilder(builder, littleBuilder.create());
|
||||
text = "";
|
||||
}
|
||||
waiting = true; // waiting for color code
|
||||
} else if (waiting) { // if waiting for code and valid str
|
||||
// if it's hexademical value and with enough space for full color
|
||||
waiting = false;
|
||||
if(args == 'r' && colorCode.isEmpty()) {
|
||||
color = ChatColor.RESET;
|
||||
continue;
|
||||
}
|
||||
if(args == 'x' && !colorCode.isEmpty()) {
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
colorCode = "x";
|
||||
} else
|
||||
colorCode += args; // a color by itself
|
||||
} else {
|
||||
waiting = false;
|
||||
if(!colorCode.isEmpty()) {
|
||||
if(colorCode.startsWith("x") && colorCode.length() >= 7) {
|
||||
if(colorCode.length() == 7)
|
||||
color = ColorManager.getColor(colorCode);
|
||||
else {
|
||||
color = ColorManager.getColor(colorCode.substring(0, 7)); // only the hex code
|
||||
ChatItem.debug("Adding color for " + colorCode.substring(7, colorCode.length()) + " (in " + colorCode + ")");
|
||||
text += ColorManager.getColorString(colorCode.substring(7, colorCode.length()));
|
||||
}
|
||||
} else if(colorCode.length() == 1) // if only one color code
|
||||
color = ColorManager.getColor(colorCode);
|
||||
else
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
ChatItem.debug("Color: " + color + ", text: " + text + ", code: " + colorCode);
|
||||
colorCode = "";
|
||||
}
|
||||
// basic text, not waiting for code after '§'
|
||||
text += args;
|
||||
}
|
||||
}
|
||||
if(!text.isEmpty()) {
|
||||
ComponentBuilder littleBuilder = new ComponentBuilder(text).color(color);
|
||||
if(jsonItem != null) { // add to all possible sub parts
|
||||
littleBuilder.event(new HoverEvent(Action.SHOW_ITEM, jsonItem));
|
||||
}
|
||||
appendToComponentBuilder(builder, littleBuilder.create());
|
||||
}
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,18 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import me.dadus33.chatitem.chatmanager.ChatManager;
|
||||
import me.dadus33.chatitem.chatmanager.v2.ChatListener;
|
||||
import me.dadus33.chatitem.listeners.InventoryListener;
|
||||
import me.dadus33.chatitem.utils.Messages;
|
||||
import me.dadus33.chatitem.utils.Storage;
|
||||
@@ -20,10 +24,10 @@ import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.chat.hover.content.Text;
|
||||
|
||||
public class ChatItemCommand implements CommandExecutor, TabExecutor {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
@@ -37,6 +41,25 @@ public class ChatItemCommand implements CommandExecutor, TabExecutor {
|
||||
InventoryListener.open(p);
|
||||
} else if (args[0].equalsIgnoreCase("reload")) {
|
||||
ChatItem.reload(sender);
|
||||
} else if (args[0].equalsIgnoreCase("show")) {
|
||||
Player cible = args.length == 1 ? p : Bukkit.getPlayer(args[1]);
|
||||
if(cible == null) {
|
||||
Messages.sendMessage(p, "player-not-found", "%arg%", args[1]);
|
||||
return false;
|
||||
}
|
||||
Storage c = ChatItem.getInstance().getStorage();
|
||||
ItemStack item = ChatManager.getUsableItem(cible);
|
||||
ChatListener.showItem(p, cible, item, c.COMMAND_FORMAT.replace("%name%", cible.getName()).replace("%item%", ChatManager.SEPARATOR + ""));
|
||||
} else if (args[0].equalsIgnoreCase("broadcast")) {
|
||||
Player cible = args.length == 1 ? p : Bukkit.getPlayer(args[1]);
|
||||
if(cible == null) {
|
||||
Messages.sendMessage(p, "player-not-found", "%arg%", args[1]);
|
||||
return false;
|
||||
}
|
||||
Storage c = ChatItem.getInstance().getStorage();
|
||||
ItemStack item = ChatManager.getUsableItem(cible);
|
||||
for(Player all : Bukkit.getOnlinePlayers())
|
||||
ChatListener.showItem(all, cible, item, c.COMMAND_FORMAT.replace("%name%", cible.getName()).replace("%item%", ChatManager.SEPARATOR + ""));
|
||||
} else if (args[0].equalsIgnoreCase("link") || args[0].equalsIgnoreCase("links")) {
|
||||
ConfigurationSection config = ChatItem.getInstance().getConfig()
|
||||
.getConfigurationSection("messages.chatitem-cmd.links");
|
||||
@@ -47,7 +70,7 @@ public class ChatItemCommand implements CommandExecutor, TabExecutor {
|
||||
String hover = Storage.color(linkConfig.getString("hover"));
|
||||
if (hover != null)
|
||||
linkComp.setHoverEvent(
|
||||
new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hover).create()));
|
||||
new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(hover).create())));
|
||||
String link = linkConfig.getString("link");
|
||||
if (link != null)
|
||||
linkComp.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link));
|
||||
@@ -64,7 +87,7 @@ public class ChatItemCommand implements CommandExecutor, TabExecutor {
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] arg) {
|
||||
List<String> list = new ArrayList<>();
|
||||
String prefix = arg[arg.length - 1].toLowerCase(Locale.ROOT);
|
||||
for (String s : Arrays.asList("help", "admin", "reload", "link"))
|
||||
for (String s : Arrays.asList("help", "admin", "reload", "link", "show", "broadcast"))
|
||||
if (prefix.isEmpty() || s.startsWith(prefix))
|
||||
list.add(s);
|
||||
return list.isEmpty() ? null : list;
|
||||
|
||||
@@ -56,8 +56,9 @@ public class NamerManager {
|
||||
/**
|
||||
* Get the name of this item, according to this storage.
|
||||
*
|
||||
* @param item
|
||||
* @param storage
|
||||
* @param p the player that will see the name
|
||||
* @param item the item to show
|
||||
* @param storage the storage
|
||||
* @return the name (should NOT return null)
|
||||
*/
|
||||
public static String getName(Player p, ItemStack item, Storage storage) {
|
||||
|
||||
@@ -50,6 +50,9 @@ public class InventoryListener implements Listener {
|
||||
} else if(type.equals(Material.STICK)) {
|
||||
setInConfig("general.hand.disabled", c.HAND_DISABLED = !c.HAND_DISABLED);
|
||||
open(p);
|
||||
} else if(type.equals(Material.BOOK)) {
|
||||
setInConfig("general.check-update", c.CHECK_UPDATE = !c.CHECK_UPDATE);
|
||||
open(p);
|
||||
} else if(type.equals(ItemUtils.FIREWORK_CHARGE)) {
|
||||
setInConfig("debug", c.DEBUG = !c.DEBUG);
|
||||
open(p);
|
||||
@@ -97,6 +100,7 @@ public class InventoryListener implements Listener {
|
||||
inv.setItem(20, getBoolChangeItem(Material.STICK, "hand-disabled", c.HAND_DISABLED));
|
||||
inv.setItem(21, getAmountChangeItem(Material.IRON_DOOR, "limit-per-message", c.LIMIT));
|
||||
inv.setItem(22, getAmountChangeItem(Material.APPLE, "cooldown", c.COOLDOWN));
|
||||
inv.setItem(23, getBoolChangeItem(Material.BOOK, "check-update", c.CHECK_UPDATE));
|
||||
|
||||
inv.setItem(26, createItem(ItemUtils.MATERIAL_CLOSE, Messages.getMessage("admin-inv.close")));
|
||||
p.openInventory(inv);
|
||||
|
||||
@@ -12,10 +12,10 @@ import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.chat.hover.content.Text;
|
||||
|
||||
public class JoinListener implements Listener {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
@@ -24,7 +24,7 @@ public class JoinListener implements Listener {
|
||||
ChatItem pl = ChatItem.getInstance();
|
||||
if(pl.isHasNewVersion()) {
|
||||
TextComponent text = new TextComponent(pl.getStorage().JOIN_UPDATE_MESSAGE);
|
||||
text.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(pl.getStorage().JOIN_UPDATE_HOVER).create()));
|
||||
text.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(pl.getStorage().JOIN_UPDATE_HOVER).create())));
|
||||
text.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.spigotmc.org/resources/19064/"));
|
||||
p.spigot().sendMessage(text);
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ public class PlayerVersionManager {
|
||||
|
||||
static {
|
||||
ChatItem pl = ChatItem.getInstance();
|
||||
if (Bukkit.getPluginManager().getPlugin("ViaVersion") != null) {
|
||||
playerVersionAdapter = new ViaVersionHook();
|
||||
pl.getLogger().info("Loading ViaVersion support ...");
|
||||
} else if (Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null) {
|
||||
if (Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null) {
|
||||
playerVersionAdapter = new ProtocolSupportHook();
|
||||
pl.getLogger().info("Loading ProtocolSupport support ...");
|
||||
} else if (Bukkit.getPluginManager().getPlugin("ViaVersion") != null) {
|
||||
playerVersionAdapter = new ViaVersionHook();
|
||||
pl.getLogger().info("Loading ViaVersion support ...");
|
||||
} else {
|
||||
playerVersionAdapter = new DefaultVersionHook();
|
||||
}
|
||||
|
||||
@@ -1,38 +1,77 @@
|
||||
package me.dadus33.chatitem.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class ColorManager {
|
||||
|
||||
public static final List<String> COLORS = Arrays.asList("4", "c", "6", "e", "2", "a", "b", "3", "1", "9", "d", "5", "f", "7", "8", "0");
|
||||
|
||||
public static boolean isHexColor(ChatColor c) {
|
||||
return Version.getVersion().isNewerOrEquals(Version.V1_16) && c.getName().startsWith("#");
|
||||
}
|
||||
|
||||
public static boolean isHexColor(String c) {
|
||||
return Version.getVersion().isNewerOrEquals(Version.V1_16) && c.startsWith("x");
|
||||
}
|
||||
|
||||
public static String removeColorAtBegin(String s) {
|
||||
if(s == null || s.isEmpty())
|
||||
return "";
|
||||
boolean wasColorChar = s.startsWith(Character.toString(ChatColor.COLOR_CHAR));
|
||||
if(!wasColorChar)
|
||||
return s;
|
||||
String next = "";
|
||||
for(int i = 1; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
if(wasColorChar) {
|
||||
if(!COLORS.contains(Character.toString(c)))
|
||||
next = next + ChatColor.COLOR_CHAR + "" + c;
|
||||
wasColorChar = false;
|
||||
} else {
|
||||
wasColorChar = c == ChatColor.COLOR_CHAR;
|
||||
if(!wasColorChar) { // something other than color code
|
||||
next += s.substring(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(next.isEmpty())
|
||||
next = s;
|
||||
return next;
|
||||
}
|
||||
|
||||
public static String getColorString(String input) {
|
||||
if(input == null || input.isEmpty())
|
||||
if (input == null || input.isEmpty())
|
||||
return "";
|
||||
StringBuilder str = new StringBuilder();
|
||||
if(Version.getVersion().isNewerOrEquals(Version.V1_16) && input.startsWith("x")) { // x mean it's an hex
|
||||
if (isHexColor(input)) { // x mean it's an hex
|
||||
ChatItem.debug("Removing char x at begin: " + input.substring(1));
|
||||
input = input.substring(1);
|
||||
if(input.length() >= 6) { // at least hex
|
||||
if (input.length() >= 6) { // at least hex
|
||||
str.append(ChatColor.of("#" + input.substring(0, 6))); // get first hex color code
|
||||
ChatItem.debug("Str with hex: " + str);
|
||||
if(input.length() > 6) // if as another color code
|
||||
if (input.length() > 6) // if as another color code
|
||||
str.append(getColorString(input.substring(6))); // get color for after
|
||||
return str.toString();
|
||||
} else
|
||||
ChatItem.debug("Low len: " + input.length());
|
||||
}
|
||||
// not hex
|
||||
for(char c : input.toCharArray())
|
||||
for (char c : input.toCharArray())
|
||||
str.append(ChatColor.getByChar(c));
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public static ChatColor getColor(String input) {
|
||||
if(input == null || input.isEmpty())
|
||||
if (input == null || input.isEmpty())
|
||||
return ChatColor.RESET;
|
||||
if(Version.getVersion().isNewerOrEquals(Version.V1_16) && input.startsWith("x")) { // x mean it's an hex
|
||||
if (isHexColor(input)) { // x mean it's an hex
|
||||
ChatItem.debug("Removing char x at begin: " + input.substring(1));
|
||||
if(input.length() >= 7) { // at least hex, and 7 because we count the "x"
|
||||
if (input.length() >= 7) { // at least hex, and 7 because we count the "x"
|
||||
return ChatColor.of("#" + input.substring(1, 7)); // get first hex color code
|
||||
} else
|
||||
ChatItem.debug("Low len: " + input.length());
|
||||
@@ -40,4 +79,47 @@ public class ColorManager {
|
||||
// not hex
|
||||
return ChatColor.getByChar(input.charAt(0));
|
||||
}
|
||||
|
||||
public static String fixColor(String message) {
|
||||
String colorCode = "", text = "";
|
||||
boolean waiting = false;
|
||||
for (char args : message.toCharArray()) {
|
||||
if (args == '§') { // begin of color
|
||||
waiting = true; // waiting for color code
|
||||
} else if (waiting) { // if waiting for code and valid str
|
||||
// if it's hexademical value and with enough space for full color
|
||||
waiting = false;
|
||||
if (args == 'r' && colorCode.isEmpty()) {
|
||||
text += ChatColor.RESET;
|
||||
continue;
|
||||
}
|
||||
if (args == 'x' && !colorCode.isEmpty()) {
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
colorCode = "x";
|
||||
} else if(Character.digit(Character.toLowerCase(args), 16) != -1)
|
||||
colorCode += args; // a color by itself
|
||||
} else {
|
||||
waiting = false;
|
||||
if (!colorCode.isEmpty()) {
|
||||
if (isHexColor(colorCode) && colorCode.length() >= 7) {
|
||||
if (colorCode.length() == 7)
|
||||
text += ColorManager.getColor(colorCode);
|
||||
else {
|
||||
text += ColorManager.getColor(colorCode.substring(0, 7)); // only the hex code
|
||||
ChatItem.debug("Adding color for " + colorCode.substring(7, colorCode.length()) + " (in "
|
||||
+ colorCode + ")");
|
||||
text += ColorManager.getColorString(colorCode.substring(7, colorCode.length()));
|
||||
}
|
||||
} else if (colorCode.length() == 1) // if only one color code
|
||||
text += ColorManager.getColor(colorCode);
|
||||
else
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
colorCode = "";
|
||||
}
|
||||
// basic text, not waiting for code after '§'
|
||||
text += args;
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -84,7 +84,7 @@ public class ReflectionUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first field which have the searching for class type
|
||||
* Get the first object which have the searching for class type
|
||||
*
|
||||
* @param from the object where we will try to find the field
|
||||
* @param clazz the class that have to define the field
|
||||
@@ -102,4 +102,23 @@ public class ReflectionUtils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first field which have the searching for class type
|
||||
*
|
||||
* @param from the object where we will try to find the field
|
||||
* @param clazz the class that have to define the field
|
||||
* @param searchingFor the class of the required field
|
||||
* @return the field (or null if not found)
|
||||
* @throws Exception if something gone wrong
|
||||
*/
|
||||
public static Field getFirstFieldWith(Class<?> clazz, Class<?> searchingFor) throws Exception {
|
||||
for (Field f : clazz.getDeclaredFields()) {
|
||||
if (f.getType().equals(searchingFor) && !Modifier.isStatic(f.getModifiers())) {
|
||||
f.setAccessible(true);
|
||||
return f;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import java.util.stream.Collectors;
|
||||
public class Storage {
|
||||
|
||||
public final HashMap<String, HashMap<Short, String>> TRANSLATIONS = new HashMap<>();
|
||||
public final String HAND_NAME, NAME_FORMAT, AMOUNT_FORMAT, MANAGER, PERMISSION_NAME, BUGGED_CLIENT_ACTION;
|
||||
public final String HAND_NAME, NAME_FORMAT, AMOUNT_FORMAT, COMMAND_FORMAT, MANAGER, PERMISSION_NAME, BUGGED_CLIENT_ACTION;
|
||||
public final String NO_PERMISSION_MESSAGE, DENY_MESSAGE, RELOAD_MESSAGE, COOLDOWN_MESSAGE, LIMIT_MESSAGE;
|
||||
public final String SECONDS, MINUTES, HOURS;
|
||||
public final String JOIN_UPDATE_MESSAGE, JOIN_UPDATE_HOVER;
|
||||
@@ -48,6 +48,7 @@ public class Storage {
|
||||
PLACEHOLDERS = ImmutableList.copyOf(conf.getStringList("general.placeholders"));
|
||||
NAME_FORMAT = color(conf.getString("general.name-format", "&b&l&o{name} {amount}&r"));
|
||||
AMOUNT_FORMAT = color(conf.getString("general.amount-format", "x{times}"));
|
||||
COMMAND_FORMAT = color(conf.getString("general.command-format", "&6%name%'s item is %item%"));
|
||||
COLOR_IF_ALREADY_COLORED = conf.getBoolean("general.color-if-already-colored", true);
|
||||
LET_MESSAGE_THROUGH = conf.getBoolean("general.let-message-through", true);
|
||||
FORCE_ADD_AMOUNT = conf.getBoolean("general.force-add-amount", true);
|
||||
|
||||
@@ -54,7 +54,7 @@ public class Utils {
|
||||
ENCHANTS_NAMES.put("LURE", "Lure");
|
||||
ENCHANTS_NAMES.put("MENDING", "Mending");
|
||||
ENCHANTS_NAMES.put("OXYGEN", "Respiration");
|
||||
ENCHANTS_NAMES.put("PROTECTION_ENVIRONMENTAL ", "Protection");
|
||||
ENCHANTS_NAMES.put("PROTECTION_ENVIRONMENTAL", "Protection");
|
||||
ENCHANTS_NAMES.put("PROTECTION_EXPLOSIONS", "Blast Protection");
|
||||
ENCHANTS_NAMES.put("PROTECTION_FALL", "Feather Falling");
|
||||
ENCHANTS_NAMES.put("PROTECTION_FIRE", "Fire Protection");
|
||||
@@ -82,7 +82,6 @@ public class Utils {
|
||||
public static String getEnchantName(Enchantment enchant) {
|
||||
String name = ENCHANTS_NAMES.get(enchant.getName());
|
||||
if (name == null) {
|
||||
|
||||
ChatItem.getInstance().getLogger()
|
||||
.warning("Unknown enchant " + enchant.getName() + ". Please report this to Elikill58.");
|
||||
}
|
||||
@@ -138,4 +137,40 @@ public class Utils {
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
public static boolean isInteger(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isByte(String s) {
|
||||
try {
|
||||
Byte.parseByte(s);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isShort(String s) {
|
||||
try {
|
||||
Short.parseShort(s);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLong(String s) {
|
||||
try {
|
||||
Long.parseLong(s);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public enum Version {
|
||||
V1_16(100, 700, 754),
|
||||
V1_17(100, 755, 756),
|
||||
V1_18(100, 757, 758),
|
||||
V1_19(100, 759, 1000),
|
||||
V1_19(100, 761, 1000),
|
||||
HIGHER(Integer.MAX_VALUE, -1, Integer.MAX_VALUE);
|
||||
|
||||
// Latest version should always have the upper limit set to Integer.MAX_VALUE so
|
||||
|
||||
@@ -65,7 +65,10 @@ general:
|
||||
action: "show_both"
|
||||
tooltip:
|
||||
- "&7Failed to parse item for your client"
|
||||
|
||||
|
||||
# Format used with using /chatitem show
|
||||
command-format: "&6%name%'s item is %item%"
|
||||
|
||||
# Here you can choose which commands will the plugin attempt to parse. Console commands will all be ignored,
|
||||
# however player commands will be taken into consideration. To add a command, simply add an element to the list
|
||||
# bellow containing an alias (or name) for that command.
|
||||
@@ -113,6 +116,8 @@ messages:
|
||||
hover: "&6Click to go to spigot page !"
|
||||
# For all commands ran by console by reserved to player
|
||||
only-players: "&cOnly players can run this command"
|
||||
# The player is not found
|
||||
player-not-found: "&cThe player %arg% can't be founded."
|
||||
# All messages for the /chatitem command
|
||||
chatitem-cmd:
|
||||
help:
|
||||
@@ -158,6 +163,7 @@ messages:
|
||||
limit-per-message: "&6Limit per message: &a%state%"
|
||||
cooldown: "&6Cooldown: &a%state%"
|
||||
debug: "&6Debug: &a%state%"
|
||||
check-update: "&6Check updates: &a%state%"
|
||||
# Change name and lore of all managers
|
||||
manager:
|
||||
both:
|
||||
|
||||
Reference in new issue
Block a user