From 26d28787be8f598cac10bd059f267ee71b9fecbf Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Fri, 10 Jul 2015 11:39:01 +0200 Subject: [PATCH] =?UTF-8?q?Removed=20useless=20and=20ugly=20vanilla=20info?= =?UTF-8?q?s=20(such=20as=20=E2=80=9CUnknown=20map=E2=80=9D)=20from=20the?= =?UTF-8?q?=20tooltips.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * NEW: Added a reflection-based API to remove all vanilla infos from the tooltips. Only effective on Bukkit 1.8.3+. * NEW: Now using this API to remove the “Unknown map” or “Scaled at 1:8” texts from the maps in the GUI. --- .../moribus/imageonmap/gui/core/GuiUtils.java | 69 +++++++++++++++++++ .../gui/list/CategorySelectionGui.java | 1 + .../imageonmap/gui/list/MapListGui.java | 2 + 3 files changed, 72 insertions(+) create mode 100644 src/main/java/fr/moribus/imageonmap/gui/core/GuiUtils.java diff --git a/src/main/java/fr/moribus/imageonmap/gui/core/GuiUtils.java b/src/main/java/fr/moribus/imageonmap/gui/core/GuiUtils.java new file mode 100644 index 0000000..b7135c7 --- /dev/null +++ b/src/main/java/fr/moribus/imageonmap/gui/core/GuiUtils.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2013 Moribus + * Copyright (C) 2015 ProkopyL + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package fr.moribus.imageonmap.gui.core; + +import org.bukkit.*; +import org.bukkit.inventory.*; +import org.bukkit.inventory.meta.*; + +import java.lang.reflect.*; + + +public class GuiUtils { + + private static boolean supported = true; + + private static Object[] itemFlags; + + static + { + try { + Class itemFlagEnumClass = Class.forName("org.bukkit.inventory.ItemFlag"); + + Method valuesMethod = itemFlagEnumClass.getDeclaredMethod("values"); + itemFlags = (Object[]) valuesMethod.invoke(null); + + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + // Not supported :c + supported = false; + } + } + + public static void removeVanillaInfos(ItemStack stack) + { + ItemMeta meta = stack.getItemMeta(); + removeVanillaInfos(meta); + stack.setItemMeta(meta); + + } + + public static void removeVanillaInfos(ItemMeta meta) + { + if(!supported) return; + + try { + Method addItemFlagsMethod = meta.getClass().getMethod("addItemFlags", itemFlags.getClass()); + addItemFlagsMethod.setAccessible(true); + addItemFlagsMethod.invoke(meta, (Object) itemFlags); + + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) { + // Should never happens, or only with breaking changes in the Bukkit API. + } + } +} diff --git a/src/main/java/fr/moribus/imageonmap/gui/list/CategorySelectionGui.java b/src/main/java/fr/moribus/imageonmap/gui/list/CategorySelectionGui.java index 8123f3f..da5fdac 100644 --- a/src/main/java/fr/moribus/imageonmap/gui/list/CategorySelectionGui.java +++ b/src/main/java/fr/moribus/imageonmap/gui/list/CategorySelectionGui.java @@ -38,6 +38,7 @@ public class CategorySelectionGui extends AbstractGui { ItemStack singleMaps = new ItemStack(Material.MAP); ItemMeta meta = singleMaps.getItemMeta(); meta.setDisplayName(ChatColor.GREEN + "Single maps"); + GuiUtils.removeVanillaInfos(meta); singleMaps.setItemMeta(meta); diff --git a/src/main/java/fr/moribus/imageonmap/gui/list/MapListGui.java b/src/main/java/fr/moribus/imageonmap/gui/list/MapListGui.java index 04705df..ad9e177 100644 --- a/src/main/java/fr/moribus/imageonmap/gui/list/MapListGui.java +++ b/src/main/java/fr/moribus/imageonmap/gui/list/MapListGui.java @@ -247,6 +247,8 @@ public class MapListGui extends AbstractGui { )); meta.setLore(lore); + GuiUtils.removeVanillaInfos(meta); + icon.setItemMeta(meta);