Work around issue with serialising explorer maps

This is due to a bug in CraftBukkit not properly supporting localised/translatable display names when serialising item meta.
This also adds/improves the message when no valid item ID could be generated.
This commit is contained in:
Phoenix616 2019-03-19 19:08:53 +01:00
parent 355fa58ac4
commit 8cd89bf8ee
3 changed files with 22 additions and 4 deletions

View File

@ -74,12 +74,15 @@ public class MaterialUtil {
return true; return true;
} }
// Additional checks as serialisation and de-serialisation might lead to different item meta
// This would only be done if the items share the same item meta type so it shouldn't be too inefficient
// Special check for books as their pages might change when serialising (See SPIGOT-3206) // Special check for books as their pages might change when serialising (See SPIGOT-3206)
// Special check for explorer maps/every item with a localised name (See SPIGOT-4672)
return one.getType() == two.getType() return one.getType() == two.getType()
&& one.getDurability() == two.getDurability() && one.getDurability() == two.getDurability()
&& one.getData().equals(two.getData()) && one.getData().equals(two.getData())
&& one.hasItemMeta() && two.hasItemMeta() && one.hasItemMeta() && two.hasItemMeta()
&& one.getItemMeta() instanceof BookMeta && two.getItemMeta() instanceof BookMeta && one.getItemMeta().getClass() == two.getItemMeta().getClass()
&& one.getItemMeta().serialize().equals(two.getItemMeta().serialize()); && one.getItemMeta().serialize().equals(two.getItemMeta().serialize());
} }

View File

@ -14,6 +14,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.logging.Level;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo;
/** /**
@ -43,13 +45,17 @@ public class ItemInfo implements CommandExecutor {
try { try {
sender.sendMessage(ChatColor.WHITE + "Full Name: " + ChatColor.GRAY + MaterialUtil.getName(item)); sender.sendMessage(ChatColor.WHITE + "Full Name: " + ChatColor.GRAY + MaterialUtil.getName(item));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "Full Name Error: " + e.getMessage()); sender.sendMessage(ChatColor.RED + "Error while generating full name. Please contact an admin or take a look at the console/log!");
ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating full item name", e);
return true;
} }
try { try {
sender.sendMessage(ChatColor.WHITE + "Shop Sign: " + ChatColor.GRAY + MaterialUtil.getSignName(item)); sender.sendMessage(ChatColor.WHITE + "Shop Sign: " + ChatColor.GRAY + MaterialUtil.getSignName(item));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "Shop Sign Error: " + e.getMessage()); sender.sendMessage(ChatColor.RED + "Error while generating shop sign name. Please contact an admin or take a look at the console/log!");
ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e);
return true;
} }
ItemInfoEvent event = new ItemInfoEvent(sender, item); ItemInfoEvent event = new ItemInfoEvent(sender, item);

View File

@ -32,6 +32,8 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.logging.Level;
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign; import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
@ -82,7 +84,14 @@ public class PlayerInteract implements Listener {
if (ChestShopSign.hasPermission(player, OTHER_NAME_CREATE, sign)) { if (ChestShopSign.hasPermission(player, OTHER_NAME_CREATE, sign)) {
ItemStack item = player.getInventory().getItemInMainHand(); ItemStack item = player.getInventory().getItemInMainHand();
if (!MaterialUtil.isEmpty(item)) { if (!MaterialUtil.isEmpty(item)) {
String itemCode = MaterialUtil.getSignName(item); String itemCode;
try {
itemCode = MaterialUtil.getSignName(item);
} catch (IllegalArgumentException e) {
player.sendMessage(ChatColor.RED + "Error while generating shop sign item name. Please contact an admin or take a look at the console/log!");
com.Acrobot.ChestShop.ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e);
return;
}
String[] lines = sign.getLines(); String[] lines = sign.getLines();
lines[ITEM_LINE] = itemCode; lines[ITEM_LINE] = itemCode;