Rewrite JSON text handling and Material handling

Add api-version 1.13
This commit is contained in:
boosik 2020-07-17 20:11:50 +02:00
parent 7a2b32a3d2
commit 857291778e
6 changed files with 69 additions and 47 deletions

View File

@ -81,7 +81,7 @@ public interface JSONClickAction<T> {
@Override
public String getValueString() {
return "\"" + value + "\"";
return value;
}
@Override
@ -130,7 +130,7 @@ public interface JSONClickAction<T> {
@Override
public String getValueString() {
return "\"" + value + "\"";
return value;
}
@Override
@ -179,7 +179,7 @@ public interface JSONClickAction<T> {
@Override
public String getValueString() {
return ("\"" + value + "\"").replace(" ", "%20");
return (value).replace(" ", "%20");
}
@Override

View File

@ -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;

View File

@ -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<T> {
@Override
public String getValueString() {
return value + "\"";
return value;
}
@Override
@ -248,50 +252,53 @@ public interface JSONHoverAction<T> {
@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());
JsonObject jsonTag = new JsonObject();
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 += "display:{Name:\\\"" + (value
.getItemMeta()
.getDisplayName() != null && value
.getItemMeta()
.getDisplayName() != "" ? value
.getItemMeta()
.getDisplayName() : toTitleCase(value.getType().toString().toLowerCase())) + "\\\"";
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 + "\\\","));
jsonDisplay.add("Lore", jsonLore);
}
value2 += "]";
jsonTag.add("display", jsonDisplay);
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);
}
value2 += "}}}\"";
jsonTag.add("Enchantments", jsonEnchantments);
}
return value2;
json.add("tag", jsonTag);
return json.toString();
}
@Override

View File

@ -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<String> lore, final List<String> 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);

View File

@ -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: >

View File

@ -11,7 +11,7 @@
<packaging>pom</packaging>
<url>http://maven.apache.org</url>
<properties>
<boosCooldowns.version>3.16.1</boosCooldowns.version>
<boosCooldowns.version>3.16.2</boosCooldowns.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<minecraft.version>1.16.1</minecraft.version>