From 8b40f415be29bb31a74f27fc224a334861929fe7 Mon Sep 17 00:00:00 2001 From: Nathan Wolf Date: Mon, 16 Apr 2018 18:10:15 -0700 Subject: [PATCH] Use ItemStack display name when describing an ItemStackThing (#441) Use ItemStack display name in ItemStackThing (if available). This means that named items will appear in the rewards list with their name rather than the ItemStack type. This is useful for "tokens" type items. Note that named items are still not supported by the built-in item parser, so this commit only affects ItemStackThings created by custom parsers (for now). --- .../MobArena/things/ItemStackThing.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/garbagemule/MobArena/things/ItemStackThing.java b/src/main/java/com/garbagemule/MobArena/things/ItemStackThing.java index 7f4353b..28c37c0 100644 --- a/src/main/java/com/garbagemule/MobArena/things/ItemStackThing.java +++ b/src/main/java/com/garbagemule/MobArena/things/ItemStackThing.java @@ -2,6 +2,7 @@ package com.garbagemule.MobArena.things; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; public class ItemStackThing implements Thing { private ItemStack stack; @@ -27,14 +28,22 @@ public class ItemStackThing implements Thing { @Override public String toString() { - String item = stack.getType() + String name = getName(); + int amount = stack.getAmount(); + if (amount > 1) { + return amount + "x " + name; + } + return name; + } + + private String getName() { + ItemMeta meta = stack.getItemMeta(); + if (meta.hasDisplayName()) { + return meta.getDisplayName(); + } + return stack.getType() .name() .replace("_", " ") .toLowerCase(); - - if (stack.getAmount() > 1) { - return stack.getAmount() + "x " + item; - } - return item; } }