diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java index c0e9ec2..259a487 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java @@ -18,7 +18,6 @@ package at.pcgamingfreaks.Minepacks.Bukkit; import at.pcgamingfreaks.Bukkit.MCVersion; -import at.pcgamingfreaks.Bukkit.NMSReflection; import at.pcgamingfreaks.Bukkit.Util.InventoryUtils; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.InventoryCompressor; import at.pcgamingfreaks.StringUtils; @@ -35,8 +34,6 @@ import lombok.AccessLevel; import lombok.Setter; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -44,15 +41,11 @@ public class Backpack implements at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack { - private final static Method METHOD_GET_INVENTORY = NMSReflection.getOBCMethod("inventory.CraftInventory", "getInventory"); - private final static Method METHOD_CRAFT_CHAT_MESSAGE_FROM_STRING = MCVersion.isAny(MCVersion.MC_1_13) ? NMSReflection.getOBCMethod("util.CraftChatMessage", "wrapOrNull", String.class) : null; - private final static Field FIELD_TITLE = NMSReflection.getOBCField("inventory.CraftInventoryCustom$MinecraftInventory", "title"); @Setter(AccessLevel.PACKAGE) private static ShrinkApproach shrinkApproach = ShrinkApproach.COMPRESS; private static Object titleOwn; - private static String titleOtherFormat, titleOwnString; + private static String titleOtherFormat; private final String titleOther; private final OfflinePlayer owner; - private final Object titleOtherOBC; private final Map opened = new ConcurrentHashMap<>(); //Thanks Minecraft 1.14 private Inventory bp; private int size, ownerID; @@ -60,32 +53,10 @@ public class Backpack implements at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack public static void setTitle(final @NotNull String title, final @NotNull String titleOther) { - titleOwnString = title; - titleOwn = prepareTitle(title); + titleOwn = InventoryUtils.prepareTitleForOpenInventoryWithCustomTitle(title); titleOtherFormat = titleOther; } - private static Object prepareTitle(final @NotNull String title) - { - if(MCVersion.isAny(MCVersion.MC_1_13)) - { - try - { - //noinspection ConstantConditions - return METHOD_CRAFT_CHAT_MESSAGE_FROM_STRING.invoke(null, title); - } - catch(Exception e) - { - e.printStackTrace(); - } - } - else - { - return StringUtils.limitLength(title, 32); - } - return null; - } - public Backpack(OfflinePlayer owner) { this(owner, 9); @@ -106,16 +77,6 @@ public Backpack(OfflinePlayer owner, int size, int ID) this.owner = owner; titleOther = StringUtils.limitLength(String.format(titleOtherFormat, owner.getName()), 32); bp = Bukkit.createInventory(this, size, titleOther); - Object titleOtherOBC = null; - try - { - titleOtherOBC = FIELD_TITLE.get(METHOD_GET_INVENTORY.invoke(bp)); - } - catch(Exception e) - { - e.printStackTrace(); - } - this.titleOtherOBC = titleOtherOBC; this.size = size; ownerID = ID; } @@ -166,14 +127,7 @@ public void setOwnerID(int id) return owner; } - @Override - public void open(@NotNull Player player, boolean editable) - { - open(player, editable, null); - } - - @Override - public void open(@NotNull Player player, boolean editable, final @Nullable String title) + private void checkResize() { if(owner.isOnline()) { @@ -194,36 +148,28 @@ public void open(@NotNull Player player, boolean editable, final @Nullable Strin } } } + } + + @Override + public void open(final @NotNull Player player, final boolean editable) + { + checkResize(); opened.put(player, editable); + if(owner.equals(player)) InventoryUtils.openInventoryWithCustomTitlePrepared(player, bp, titleOwn); + else player.openInventory(bp); + } - //region Set backpack title - if(MCVersion.isOlderThan(MCVersion.MC_1_14)) + @Override + public void open(final @NotNull Player player, final boolean editable, final @Nullable String title) + { + if(title == null) { - // It's not perfect, but it is the only way of doing this. - // This sets the title of the inventory based on the person who is opening it. - // The owner will see an other title, then everyone else. - // This way we can add owner name to the tile for everyone else. - final Object usedTitle = (title == null) ? (player.equals(owner) ? titleOwn : titleOtherOBC) : prepareTitle(title); - if(usedTitle != null && FIELD_TITLE != null && METHOD_GET_INVENTORY != null) - { - try - { - FIELD_TITLE.set(METHOD_GET_INVENTORY.invoke(bp), usedTitle); - } - catch(Exception e) - { - e.printStackTrace(); - } - } - } - //endregion - - player.openInventory(bp); - if(MCVersion.isNewerOrEqualThan(MCVersion.MC_1_14)) - { - final String usedTitle = (title == null) ? (player.equals(owner) ? titleOwnString : titleOther) : title; - Bukkit.getScheduler().runTaskLater(Minepacks.getInstance(), () -> InventoryUtils.updateInventoryTitle(player, usedTitle), 2); + open(player, editable); + return; } + checkResize(); + opened.put(player, editable); + InventoryUtils.openInventoryWithCustomTitle(player, bp, title); } public void close(Player p)