From 857291778e037aa106aeacf88e221f92450b2d54 Mon Sep 17 00:00:00 2001 From: boosik Date: Fri, 17 Jul 2020 20:11:50 +0200 Subject: [PATCH] Rewrite JSON text handling and Material handling Add api-version 1.13 --- .../mcapi/json/JSONClickAction.java | 6 +- .../mcapi/json/JSONComponent.java | 21 ++++- .../mcapi/json/JSONHoverAction.java | 81 ++++++++++--------- .../Managers/BoosItemCostManager.java | 3 +- plugin/src/main/resources/plugin.yml | 3 +- pom.xml | 2 +- 6 files changed, 69 insertions(+), 47 deletions(-) diff --git a/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONClickAction.java b/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONClickAction.java index ce9744b..41f9449 100644 --- a/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONClickAction.java +++ b/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONClickAction.java @@ -81,7 +81,7 @@ public interface JSONClickAction { @Override public String getValueString() { - return "\"" + value + "\""; + return value; } @Override @@ -130,7 +130,7 @@ public interface JSONClickAction { @Override public String getValueString() { - return "\"" + value + "\""; + return value; } @Override @@ -179,7 +179,7 @@ public interface JSONClickAction { @Override public String getValueString() { - return ("\"" + value + "\"").replace(" ", "%20"); + return (value).replace(" ", "%20"); } @Override diff --git a/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONComponent.java b/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONComponent.java index 9838ce8..957853a 100644 --- a/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONComponent.java +++ b/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONComponent.java @@ -1,5 +1,11 @@ package com.coloredcarrot.mcapi.json; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import cz.boosik.boosCooldown.BoosCoolDown; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.chat.ComponentSerializer; + /** * All rights reserved. * @@ -45,17 +51,24 @@ public class JSONComponent super.generate(); - generatedJSON = generatedJSON.substring(0, generatedJSON.length() - 1); + JsonObject json = new JsonParser().parse(generatedJSON).getAsJsonObject(); if (hoverAction != null) { - generatedJSON += ",\"hoverEvent\":{\"action\":\"" + hoverAction.getActionName() + "\",\"value\":\"" + hoverAction.getValueString() + "}"; + + JsonObject jsonHover = new JsonObject(); + jsonHover.addProperty("action", hoverAction.getActionName()); + jsonHover.addProperty("value", hoverAction.getValueString()); + json.add("hoverEvent", jsonHover); } if (clickAction != null) { - generatedJSON += ",\"clickEvent\":{\"action\":\"" + clickAction.getActionName() + "\",\"value\":" + clickAction.getValueString() + "}"; + JsonObject jsonClick = new JsonObject(); + jsonClick.addProperty("action", clickAction.getActionName()); + jsonClick.addProperty("value", clickAction.getValueString()); + json.add("clickEvent", jsonClick); } - generatedJSON += "}"; + generatedJSON = json.toString(); return this; diff --git a/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONHoverAction.java b/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONHoverAction.java index 7455216..36a01f6 100644 --- a/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONHoverAction.java +++ b/plugin/src/main/java/com/coloredcarrot/mcapi/json/JSONHoverAction.java @@ -2,8 +2,12 @@ package com.coloredcarrot.mcapi.json; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import cz.boosik.boosCooldown.BoosCoolDown; /** @@ -136,7 +140,7 @@ public interface JSONHoverAction { @Override public String getValueString() { - return value + "\""; + return value; } @Override @@ -248,50 +252,53 @@ public interface JSONHoverAction { @Override public String getValueString() { - final String material = value.getData().getItemType().toString().toLowerCase(); - String value2 = "{id:\\\"minecraft:" + material + "\\\",Damage:" + value.getDurability() + ",Count:" + value.getAmount() + ",tag:{"; + final String material = value.getType().toString().toLowerCase(); + final ItemMeta itemMeta = value.getItemMeta(); + final Integer damage = itemMeta instanceof Damageable ? ((Damageable) itemMeta).getDamage() : null; - if (value.getItemMeta().hasEnchants()) { - value2 += "Enchantments:["; - int i = 0; - final int size = value.getItemMeta().getEnchants().keySet().size(); - for (final Enchantment ench : value.getItemMeta().getEnchants().keySet()) { - if (i + 1 == size) { - value2 += "{lvl:" + value.getItemMeta().getEnchants().get(ench) + ",id:\\\"" + ench.getKey() + "\\\"}"; - } else { - value2 += "{lvl:" + value.getItemMeta().getEnchants().get(ench) + ",id:\\\"" + ench.getKey() + "\\\"},"; - } - i++; - } - value2 += "],"; - } + JsonObject json = new JsonObject(); + json.addProperty("id", "minecraft:"+material); + json.addProperty("Damage", damage); + json.addProperty("Count", value.getAmount()); - value2 += "display:{Name:\\\"" + (value - .getItemMeta() - .getDisplayName() != null && value - .getItemMeta() - .getDisplayName() != "" ? value - .getItemMeta() - .getDisplayName() : toTitleCase(value.getType().toString().toLowerCase())) + "\\\""; + JsonObject jsonTag = new JsonObject(); - if (value.getItemMeta().hasLore()) { - value2 += ",Lore:["; - for (final String lore : value.getItemMeta().getLore()) { - value2 = value2 + (value.getItemMeta().getLore().size() == 1 || value - .getItemMeta() - .getLore() - .get(value - .getItemMeta() - .getLore() - .size() - 1) == lore ? ("\\\"" + lore + "\\\"") : ("\\\"" + lore + "\\\",")); + JsonObject jsonDisplay = new JsonObject(); + JsonObject jsonName = new JsonObject(); + jsonName.addProperty("text", itemMeta.getDisplayName() != null && !itemMeta.getDisplayName().equals("") ? itemMeta + .getDisplayName() : toTitleCase(value.getType().toString().toLowerCase())); + jsonDisplay.addProperty("Name", jsonName.toString()); + + if (itemMeta.hasLore()) { + JsonArray jsonLore = new JsonArray(); + + for (final String lore : itemMeta.getLore()) { + JsonObject jsonLoreItem = new JsonObject(); + jsonLoreItem.addProperty("text", lore); + jsonLore.add(jsonLoreItem.toString()); } - value2 += "]"; + jsonDisplay.add("Lore", jsonLore); } - value2 += "}}}\""; + jsonTag.add("display", jsonDisplay); - return value2; + if (itemMeta.hasEnchants()) { + JsonArray jsonEnchantments = new JsonArray(); + + for (final Enchantment ench : itemMeta.getEnchants().keySet()) { + JsonObject enchantment = new JsonObject(); + enchantment.addProperty("lvl", itemMeta.getEnchants().get(ench)); + enchantment.addProperty("id", ench.getKey().toString()); + jsonEnchantments.add(enchantment); + } + + jsonTag.add("Enchantments", jsonEnchantments); + } + + json.add("tag", jsonTag); + + return json.toString(); } @Override diff --git a/plugin/src/main/java/cz/boosik/boosCooldown/Managers/BoosItemCostManager.java b/plugin/src/main/java/cz/boosik/boosCooldown/Managers/BoosItemCostManager.java index e259f81..17f4d1a 100644 --- a/plugin/src/main/java/cz/boosik/boosCooldown/Managers/BoosItemCostManager.java +++ b/plugin/src/main/java/cz/boosik/boosCooldown/Managers/BoosItemCostManager.java @@ -14,6 +14,7 @@ import com.coloredcarrot.mcapi.json.JSON; import com.coloredcarrot.mcapi.json.JSONColor; import com.coloredcarrot.mcapi.json.JSONComponent; import com.coloredcarrot.mcapi.json.JSONHoverAction; +import cz.boosik.boosCooldown.BoosCoolDown; import util.BoosChat; public class BoosItemCostManager { @@ -74,7 +75,7 @@ public class BoosItemCostManager { } public static ItemStack createItemStack(final String item, final int count, final String name, final List lore, final List enchants) { - final ItemStack itemStack = new ItemStack(Material.getMaterial(item), count); + final ItemStack itemStack = new ItemStack(Material.valueOf(item), count); final ItemMeta itemMeta = itemStack.getItemMeta(); if (name != null) { itemMeta.setDisplayName(name); diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index 1e4fd3a..5d200e7 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -1,6 +1,7 @@ name: boosCooldowns main: cz.boosik.boosCooldown.BoosCoolDown -version: 3.16.1 +version: 3.16.2 +api-version: 1.13 authors: [LordBoos (boosik)] softdepend: [Vault, PlayerPoints] description: > diff --git a/pom.xml b/pom.xml index 8b18845..7a3819f 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ pom http://maven.apache.org - 3.16.1 + 3.16.2 UTF-8 UTF-8 1.16.1