-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Util to get ItemStacks in Json Format for hover components
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/NBTJsonUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.tr7zw.changeme.nbtapi.utils; | ||
|
||
import java.util.Optional; | ||
|
||
import org.bukkit.inventory.ItemStack; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.JsonOps; | ||
|
||
import de.tr7zw.changeme.nbtapi.NbtApiException; | ||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper; | ||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.MojangToMapping; | ||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod; | ||
|
||
public class NBTJsonUtil { | ||
|
||
/** | ||
* 1.20.3+ only. Used to convert items into Json, used in Chat Hover Components. | ||
* | ||
* @param itemStack | ||
* @return | ||
* @throws NbtApiException | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static JsonElement itemStackToJson(ItemStack itemStack) throws NbtApiException { | ||
try { | ||
Codec<Object> itemStackCodec = (Codec<Object>) ClassWrapper.NMS_ITEMSTACK.getClazz() | ||
.getField(MojangToMapping.getMapping().get("net.minecraft.world.item.ItemStack#CODEC")).get(null); | ||
Object stack = ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, itemStack); | ||
DataResult<JsonElement> result = itemStackCodec.encode(stack, JsonOps.INSTANCE, | ||
JsonOps.INSTANCE.emptyMap()); | ||
Optional<JsonElement> opt = (Optional<JsonElement>) result.getClass().getMethod("result").invoke(result); | ||
return opt.orElse(null); | ||
} catch (Exception ex) { | ||
throw new NbtApiException("Error trying to get Json of an ItemStack.", ex); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
item-nbt-plugin/src/main/java/de/tr7zw/nbtapi/plugin/tests/items/ItemJsonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.tr7zw.nbtapi.plugin.tests.items; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
|
||
import com.google.gson.JsonElement; | ||
|
||
import de.tr7zw.changeme.nbtapi.NBT; | ||
import de.tr7zw.changeme.nbtapi.NBTItem; | ||
import de.tr7zw.changeme.nbtapi.NbtApiException; | ||
import de.tr7zw.changeme.nbtapi.utils.NBTJsonUtil; | ||
import de.tr7zw.nbtapi.plugin.tests.Test; | ||
|
||
public class ItemJsonTest implements Test { | ||
|
||
@Override | ||
public void test() throws Exception { | ||
ItemStack item = new ItemStack(Material.STONE); | ||
ItemMeta meta = item.getItemMeta(); | ||
meta.setDisplayName("test"); | ||
meta.setUnbreakable(true); | ||
item.setItemMeta(meta); | ||
JsonElement elem = NBTJsonUtil.itemStackToJson(item); | ||
if (elem == null) { | ||
throw new NbtApiException("Getting the Json didn't work correctly! " + item); | ||
} | ||
} | ||
|
||
} |