Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3fd14b3bdb | ||
|
|
6c3784e662 | ||
|
|
6256de065e | ||
|
|
4c1d64f04c | ||
|
|
4475f19268 | ||
|
|
2b5065dbec | ||
|
|
ba54582f7f | ||
|
|
2b0de9aa3c | ||
|
|
3981992e37 | ||
|
|
439710086e | ||
|
|
f5381b0430 | ||
|
|
3cce52953a | ||
|
|
797de9c602 | ||
|
|
c7f76d9f7b | ||
|
|
3947ea88d5 | ||
|
|
cc30049fdf | ||
|
|
7a964bb97c | ||
|
|
13bbf3e5ca | ||
|
|
46de91b2ff | ||
|
|
cb5b3acab0 | ||
|
|
6f6d4dc263 | ||
|
|
250b119391 |
No files matched your search
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.dadus33</groupId>
|
||||
<artifactId>chatitem</artifactId>
|
||||
<version>2.4.3</version>
|
||||
<version>2.4.5</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
@@ -103,7 +103,9 @@ 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;
|
||||
}
|
||||
|
||||
@@ -81,9 +81,12 @@ public class PacketEditingChatManager extends ChatManager {
|
||||
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)
|
||||
&& cons.getParameterTypes()[2].equals(int.class)) { // "component", "string", "int"
|
||||
return cons.newInstance(null, 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()
|
||||
|
||||
+128
-1
@@ -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));
|
||||
}
|
||||
}
|
||||
+2
-72
@@ -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,11 +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() + ", wrapper: " + wrapper);
|
||||
return obj.toString();
|
||||
}
|
||||
|
||||
@@ -140,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");
|
||||
}
|
||||
@@ -151,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");
|
||||
}
|
||||
|
||||
@@ -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,6 +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() + " to " + e.getPlayername());
|
||||
if (lastSentPacket != null && lastSentPacket == e.getPacket())
|
||||
return; // prevent infinite loop
|
||||
PacketContent packet = e.getContent();
|
||||
@@ -82,8 +83,10 @@ public class ChatPacketManager extends PacketHandler {
|
||||
String json = "{}";
|
||||
IBaseComponentGetter choosedGetter = null;
|
||||
if (version.isNewerOrEquals(Version.V1_19)) {
|
||||
if(packet.getIntegers().readSafely(0, 0) != 1)
|
||||
if(packet.getIntegers().readSafely(0, 0) > 1) { // not parsed chat message type
|
||||
ChatItem.debug("Invalid int: " + packet.getIntegers().read(0));
|
||||
return;
|
||||
}
|
||||
choosedGetter = new StringComponentGetter();
|
||||
json = choosedGetter.getBaseComponentAsJSON(e); // if null, will be re-checked so anyway
|
||||
} else if (version.isNewerOrEquals(Version.V1_12)) {
|
||||
@@ -110,15 +113,12 @@ public class ChatPacketManager extends PacketHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
if(json == null)
|
||||
return; // can't find something
|
||||
String placeholder = choosedGetter.hasPlaceholders(getStorage(), json);
|
||||
if (placeholder == null) {
|
||||
ChatItem.debug("No placeholders founded in " + json);
|
||||
if(json == null || choosedGetter == null) {
|
||||
ChatItem.debug("Can't find valid json getter or json itself");
|
||||
ChatItem.debug("String: " + packet.getStrings().getContent());
|
||||
return; // then it's just a normal message without placeholders, so we leave it alone
|
||||
return; // can't find something
|
||||
}
|
||||
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();
|
||||
@@ -128,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)
|
||||
@@ -178,23 +177,22 @@ 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, copy,
|
||||
|
||||
+3
-2
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
@@ -31,7 +33,6 @@ 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 {
|
||||
@@ -98,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,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));
|
||||
}
|
||||
@@ -149,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);
|
||||
}
|
||||
@@ -242,10 +246,12 @@ public class ChatListener implements Listener {
|
||||
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, c));
|
||||
ComponentBuilder itemComponent = new ComponentBuilder("");
|
||||
String itemJson = convertItemStackToJson(item);
|
||||
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, new ComponentBuilder(itemJson).create()));
|
||||
//itemComponent.event(new HoverEvent(Action.SHOW_ITEM, itemBaseComponent));
|
||||
appendToComponentBuilder(builder, itemComponent.create());
|
||||
} else {
|
||||
String handName = c.HAND_NAME;
|
||||
@@ -258,7 +264,7 @@ public class ChatListener implements Listener {
|
||||
if (stay > 0)
|
||||
handTooltip.append("\n");
|
||||
}
|
||||
handComp.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(handTooltip.create())));
|
||||
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++) {
|
||||
@@ -272,7 +278,6 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void appendToComponentBuilder(ComponentBuilder builder, BaseComponent[] comps) {
|
||||
if (shouldUseAppendMethod) {
|
||||
try {
|
||||
@@ -296,14 +301,22 @@ 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 " + text);
|
||||
appendToComponentBuilder(builder, new ComponentBuilder(text).color(color).create());
|
||||
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
|
||||
@@ -334,14 +347,20 @@ public class ChatListener implements Listener {
|
||||
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())
|
||||
appendToComponentBuilder(builder, new ComponentBuilder(text).color(color).create());
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -45,7 +44,7 @@ public class ChatItemCommand implements CommandExecutor, TabExecutor {
|
||||
} else if (args[0].equalsIgnoreCase("show")) {
|
||||
Player cible = args.length == 1 ? p : Bukkit.getPlayer(args[1]);
|
||||
if(cible == null) {
|
||||
p.sendMessage(ChatColor.RED + "The player " + args[1] + " can't be found.");
|
||||
Messages.sendMessage(p, "player-not-found", "%arg%", args[1]);
|
||||
return false;
|
||||
}
|
||||
Storage c = ChatItem.getInstance().getStorage();
|
||||
@@ -54,7 +53,7 @@ public class ChatItemCommand implements CommandExecutor, TabExecutor {
|
||||
} else if (args[0].equalsIgnoreCase("broadcast")) {
|
||||
Player cible = args.length == 1 ? p : Bukkit.getPlayer(args[1]);
|
||||
if(cible == null) {
|
||||
p.sendMessage(ChatColor.RED + "The player " + args[1] + " can't be found.");
|
||||
Messages.sendMessage(p, "player-not-found", "%arg%", args[1]);
|
||||
return false;
|
||||
}
|
||||
Storage c = ChatItem.getInstance().getStorage();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,15 +1,54 @@
|
||||
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())
|
||||
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
|
||||
@@ -30,7 +69,7 @@ public class ColorManager {
|
||||
public static ChatColor getColor(String input) {
|
||||
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"
|
||||
return ChatColor.of("#" + input.substring(1, 7)); // get first hex color code
|
||||
@@ -57,12 +96,12 @@ public class ColorManager {
|
||||
if (args == 'x' && !colorCode.isEmpty()) {
|
||||
text += ColorManager.getColorString(colorCode);
|
||||
colorCode = "x";
|
||||
} else
|
||||
} else if(Character.digit(Character.toLowerCase(args), 16) != -1)
|
||||
colorCode += args; // a color by itself
|
||||
} else {
|
||||
waiting = false;
|
||||
if (!colorCode.isEmpty()) {
|
||||
if (colorCode.startsWith("x") && colorCode.length() >= 7) {
|
||||
if (isHexColor(colorCode) && colorCode.length() >= 7) {
|
||||
if (colorCode.length() == 7)
|
||||
text += ColorManager.getColor(colorCode);
|
||||
else {
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -116,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:
|
||||
@@ -161,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