Compare commits

...
7 Commits
Author SHA1 Message Date
Elikill58 3fd14b3bdb Upgrade to 2.4.5 2022-10-18 19:40:45 +02:00
Elikill58 6c3784e662 Fix multiple quick issues 2022-10-12 17:27:34 +02:00
Elikill58 6256de065e Change base component 2022-10-03 16:10:59 +02:00
Elikill58 4c1d64f04c Fix v1 detection in v2 2022-09-26 09:20:34 +02:00
Elikill58 4475f19268 Upgrade to snapshot 2022-09-14 15:50:01 +02:00
Elikill58 2b5065dbec Update multiple things into v1 2022-09-14 15:49:27 +02:00
Elikill58 ba54582f7f Fix only first line take in count 2022-08-30 12:31:08 +02:00
8 changed files with 172 additions and 119 deletions

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>me.dadus33</groupId>
<artifactId>chatitem</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -5,6 +5,13 @@ import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
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.chatmanager.v1.packets.ChatItemPacket;
import me.dadus33.chatitem.utils.Storage;
@@ -27,6 +34,42 @@ public interface IBaseComponentGetter {
* @return the founded placeholder or null
*/
default @Nullable String hasPlaceholders(Storage c, String json) {
if(json == null || json.isEmpty())
return null;
String placeholder = hasPlaceholdersSpecificMessage(c, json);
if(placeholder != null)
return placeholder;
try {
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();
}
}
placeholder = hasPlaceholdersSpecificMessage(c, text);
if(placeholder != null)
return placeholder;
} else if(withObj.has("text")) {
placeholder = hasPlaceholdersSpecificMessage(c, withObj.get("text").getAsString());
if(placeholder != null)
return placeholder;
}
} // ignoring all others because it should not appear
}
} catch (Exception e) {} // not JSON
return null;
}
default @Nullable String hasPlaceholdersSpecificMessage(Storage c, String json) {
if(json == null || json.isEmpty())
return null;
for (String rep : c.PLACEHOLDERS) {
@@ -38,6 +81,38 @@ public interface IBaseComponentGetter {
}
default @Nullable String getNameFromMessage(String json, String toReplace) {
String checkedName = getNameFromSpecificMessage(json, toReplace);
if(checkedName != null)
return checkedName;
try {
JsonObject jsonObj = JsonParser.parseString(json).getAsJsonObject();
if(!jsonObj.has("extra"))
return null;
String text = "";
for(JsonElement element : jsonObj.get("extra").getAsJsonArray()) {
if(element.isJsonObject()) {
JsonObject withObj = element.getAsJsonObject();
if(withObj.has("extra")) {
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();
}
}
} else if(withObj.has("text")) {
text += withObj.get("text").getAsString();
}
} // ignoring all others because it should not appear
}
String possibleName = getNameFromSpecificMessage(text, toReplace);
if(possibleName != null)
return possibleName;
} catch (Exception e) {} // not JSON
return null;
}
default @Nullable String getNameFromSpecificMessage(String json, String toReplace) {
for (Player p : Bukkit.getOnlinePlayers()) {
String name = toReplace + p.getName();
if (json.contains(name))
@@ -56,6 +131,58 @@ public interface IBaseComponentGetter {
* @return the replaced json
*/
default String removePlaceholdersAndName(String json, String toReplace, Player foundedPlayer) {
return json.replace(toReplace + foundedPlayer.getName(), Character.toString(ChatManager.SEPARATOR));
String remove = toReplace + foundedPlayer.getName();
boolean firstRemove = true;
try {
JsonObject jsonObj = JsonParser.parseString(json).getAsJsonObject();
if(!jsonObj.has("extra")) {
ChatItem.debug("[IBase] No extra in json");
return json.replace(remove, Character.toString(ChatManager.SEPARATOR));
}
JsonArray extraArray = jsonObj.get("extra").getAsJsonArray();
for(int i = 0; i < extraArray.size(); i++) {
JsonElement element = extraArray.get(i);
if(element.isJsonObject()) {
JsonObject withObj = element.getAsJsonObject();
if(withObj.has("extra")) {
for(JsonElement extra : withObj.get("extra").getAsJsonArray()) {
if(extra.isJsonObject()) {
JsonObject extraObj = extra.getAsJsonObject();
if(extraObj.has("text") && extraObj.get("text").isJsonPrimitive()) {
String s = extraObj.get("text").getAsString();
if(remove.startsWith(s)) {
extraObj.addProperty("text", firstRemove ? Character.toString(ChatManager.SEPARATOR) : "");
firstRemove = false;
remove = remove.substring(s.length());
if(remove.isEmpty())
return jsonObj.toString();
}
}
}
}
} else if(withObj.has("text")) {
String s = withObj.get("text").getAsString();
if(remove.startsWith(s)) {
withObj.addProperty("text", firstRemove ? Character.toString(ChatManager.SEPARATOR) : "");
firstRemove = false;
remove = remove.substring(s.length());
if(remove.isEmpty())
return jsonObj.toString();
}
}
} else if(element.isJsonPrimitive()) {
String s = element.getAsString();
if(s.contains(remove)) {
extraArray.set(i, new JsonPrimitive(s.replace(remove, Character.toString(ChatManager.SEPARATOR))));
return jsonObj.toString();
}
}
// ignoring all others because it should not appear
}
ChatItem.debug("[IBase] Remove not fully managed: " + remove + " > " + jsonObj.toString());
return jsonObj.toString();
} catch (Exception e) {} // not JSON
return json.replace(remove, Character.toString(ChatManager.SEPARATOR));
}
}
@@ -17,7 +17,6 @@ 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.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;
@@ -46,8 +45,8 @@ public class AdventureComponentGetter implements IBaseComponentGetter {
return null;
}
String json = ComponentSerializer.toString(BungeeComponentSerializer.get().serialize(comp).clone());
ChatItem.debug("AdventureJSON : " + json);
JsonObject jsonObj = JsonParser.parseString(json).getAsJsonObject();
ChatItem.debug("AdventureJSON : " + json + ", use extra: " + (!jsonObj.has("with")));
if(jsonObj.has("with")) {
JsonObject next = new JsonObject();
next.add("extra", jsonObj.get("with"));
@@ -57,77 +56,8 @@ public class AdventureComponentGetter implements IBaseComponentGetter {
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;
@@ -194,7 +124,7 @@ public class AdventureComponentGetter implements IBaseComponentGetter {
next.add("with", extraArray);
localJson = next.toString();
}
ChatItem.debug("Adventure Json: " + json);
ChatItem.debug("Adventure Json: " + localJson);
try {
Class<?> packetClass = packet.getPacket().getClass();
if(packetClass.getSimpleName().equalsIgnoreCase("PacketPlayOutChat")) {
@@ -27,7 +27,6 @@ import me.dadus33.chatitem.utils.PacketUtils;
import me.dadus33.chatitem.utils.Reflect;
import me.dadus33.chatitem.utils.Version;
@SuppressWarnings({ "unchecked" })
public class JSONManipulatorCurrent {
private static final Class<?> CRAFT_ITEM_STACK_CLASS = PacketUtils.getObcClass("inventory.CraftItemStack");
@@ -47,10 +46,10 @@ public class JSONManipulatorCurrent {
public String parse(String json, ItemStack item, String replacement, int protocol)
throws Exception {
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<>(
protocolVersion = Version.getVersion(protocol), item);
JsonObject wrapper = new JsonObject(); // Create a wrapper object for the whole array
if ((itemTooltip = STACKS.get(p)) == null) {
JsonArray use = Translator.toJson(replacement); // We get the json representation of the old color
// formatting method
@@ -62,7 +61,6 @@ public class JSONManipulatorCurrent {
// 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
wrapper.add("extra", use);
wrapper.add("hoverEvent", hover);
@@ -73,12 +71,16 @@ public class JSONManipulatorCurrent {
// We remove it later when no longer needed to save memory
Bukkit.getScheduler().runTaskLaterAsynchronously(ChatItem.getInstance(), () -> STACKS.remove(p), 100L);
}
obj.add("extra", parseArray(array, itemTooltip));
if(obj.size() == 1 && obj.has("text")) {
wrapper.add("text", obj.get("text"));
ChatItem.debug("[JsonManipulator] Parsed quick: " + obj.toString() + ", wrapper: " + wrapper);
return wrapper.toString().replace(Character.toString(ChatManager.SEPARATOR), "").replace(ChatManager.SEPARATOR_STR, "");
}
obj.add("extra", parseArray(obj.has("extra") ? obj.getAsJsonArray("extra") : new JsonArray(), wrapper));
if (!obj.has("text")) {
obj.addProperty("text", "");
}
ChatItem.debug("Parsed array for item: " + obj.toString());
ChatItem.debug("Parsed array for item: " + obj.toString() + ", wrapper: " + wrapper);
return obj.toString();
}
@@ -141,7 +143,9 @@ public class JSONManipulatorCurrent {
if (el != null) {
JsonArray jar = el.getAsJsonArray();
if (jar.size() != 0) {
o.add("extra", parseArray(jar, tooltip));
JsonArray tmpArray = parseArray(jar, tooltip);
if(!tmpArray.isEmpty())
o.add("extra", tmpArray);
} else {
o.remove("extra");
}
@@ -152,8 +156,10 @@ public class JSONManipulatorCurrent {
JsonElement el = o.get("extra");
if (el != null) {
JsonArray jar = el.getAsJsonArray();
if (jar.size() != 0) {
o.add("extra", parseArray(jar, tooltip));
if (!jar.isEmpty()) {
JsonArray tmpArray = parseArray(jar, tooltip);
if(!tmpArray.isEmpty())
o.add("extra", tmpArray);
} else {
o.remove("extra");
}
@@ -235,14 +241,8 @@ public class JSONManipulatorCurrent {
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()));
}
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();
@@ -76,7 +76,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(SEPARATOR + p.getName());
} else {
msg.add(part);
}
@@ -143,7 +143,7 @@ public class ChatEventListener implements Listener {
StringJoiner msg = new StringJoiner(" ");
for(String part : s.split(" ")) {
if(part.equalsIgnoreCase(firstPlaceholder)) {
msg.add(firstPlaceholder + Character.toString(SEPARATOR) + p.getName());
msg.add(SEPARATOR + p.getName());
} else {
msg.add(part);
}
@@ -75,7 +75,7 @@ public class ChatPacketManager extends PacketHandler {
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());
ChatItem.debug("Checking: " + e.getPacketType().getFullName() + " to " + e.getPlayername());
if (lastSentPacket != null && lastSentPacket == e.getPacket())
return; // prevent infinite loop
PacketContent packet = e.getContent();
@@ -118,13 +118,7 @@ public class ChatPacketManager extends PacketHandler {
ChatItem.debug("String: " + packet.getStrings().getContent());
return; // can't find something
}
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
}
ChatItem.debug("Found " + placeholder + " with " + choosedGetter.getClass().getName());
ChatItem.debug("Found with " + choosedGetter.getClass().getName());
String toReplace = null;
if (json.lastIndexOf(SEPARATOR) != -1)
toReplace = ((Object) SEPARATOR).toString();
@@ -134,7 +128,6 @@ public class ChatPacketManager extends PacketHandler {
ChatItem.debug("Not containing bell " + json);
return;
}
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)
@@ -87,8 +87,9 @@ public class INC2Channel extends ChannelAbstract {
@Override
public Channel getChannel(Player p) throws Exception {
Object playerConnection = ReflectionUtils.getObject(getPlayerConnection(p), Version.getVersion().isNewerOrEquals(Version.V1_19) ? "b" : "a");
return ReflectionUtils.getFirstWith(playerConnection, playerConnection.getClass(), Channel.class);//(Channel) networkManager.getClass().getDeclaredField("channel").get(networkManager);
Class<?> networkClass = PacketUtils.getNmsClass("NetworkManager", "network.");
Object networkManager = ReflectionUtils.getFirstWith(getPlayerConnection(p), PacketUtils.getNmsClass("PlayerConnection", "server.network."), networkClass);
return ReflectionUtils.getFirstWith(networkManager, networkClass, Channel.class);
}
private class ChannelHandlerSent extends ChannelOutboundHandlerAdapter {
@@ -99,17 +99,17 @@ public class ChatListener implements Listener {
Player p = e.getPlayer();
boolean hasv1 = false;
String placeholder = null;
for (String rep : c.PLACEHOLDERS) {
// v1 try to manage it, but the message have not been changed by another plugin
if (e.getMessage().contains(rep + ChatManager.SEPARATOR + p.getName())) {
hasv1 = true;
ChatItem.debug("Found v1 placeholders in v2");
placeholder = rep;
break;
}
if (e.getMessage().contains(rep)) {
placeholder = rep;
break;
// v1 try to manage it, but the message have not been changed by another plugin
if (e.getMessage().contains(ChatManager.SEPARATOR + p.getName())) {
hasv1 = true;
ChatItem.debug("Found v1 placeholders in v2");
placeholder = "";
} else {
for (String rep : c.PLACEHOLDERS) {
if (e.getMessage().contains(rep)) {
placeholder = rep;
break;
}
}
}
@@ -125,7 +125,7 @@ public class ChatListener implements Listener {
String msg, defMsg = e.getMessage();
for (String rep : c.PLACEHOLDERS) {
if (hasv1) // remove v1 char
defMsg = defMsg.replace(rep + ChatManager.SEPARATOR + p.getName(), String.valueOf(ChatManager.SEPARATOR));
defMsg = defMsg.replace(ChatManager.SEPARATOR + p.getName(), String.valueOf(ChatManager.SEPARATOR));
else
defMsg = defMsg.replace(rep, String.valueOf(ChatManager.SEPARATOR));
}
@@ -150,8 +150,11 @@ public class ChatListener implements Listener {
if (ChatItem.discordSrvSupport)
DiscordSrvSupport.sendChatMessage(p, defMsg.replace(ChatManager.SEPARATOR + "", itemName).replace("{name}", p.getName())
.replace("{display-name}", p.getDisplayName()));
ChatItem.debug("Msg: " + msg.replace(ChatColor.COLOR_CHAR, '&') + ", format: " + format);
e.getRecipients().forEach((pl) -> showItem(pl, p, item, msg));
ChatItem.debug("Msg: " + msg.replace(ChatColor.COLOR_CHAR, '&') + ", format: " + format + " to " + e.getRecipients().size() + " players");
if(e.getRecipients().isEmpty()) {// should not appear
ChatItem.debug("Empty recipients ???");
} else
e.getRecipients().forEach((pl) -> showItem(pl, p, item, msg));
if (c.COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown"))
ChatManager.applyCooldown(p);
}
@@ -275,7 +278,6 @@ public class ChatListener implements Listener {
}
}
@SuppressWarnings("unchecked")
public static void appendToComponentBuilder(ComponentBuilder builder, BaseComponent[] comps) {
if (shouldUseAppendMethod) {
try {