From 7247c5dcf428b627ca4b473a993710e0b5c2fc45 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Tue, 1 Jun 2021 20:14:54 +0200 Subject: [PATCH] Add support for RGB in titles (Closes #151) --- .../Bukkit/ExtendedAPI/BackpackExtended.java | 13 ++++++++- .../Minepacks/Bukkit/Backpack.java | 29 +++++++++++++++---- .../Minepacks/Bukkit/Minepacks.java | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Minepacks-API-Extended/src/at/pcgamingfreaks/Minepacks/Bukkit/ExtendedAPI/BackpackExtended.java b/Minepacks-API-Extended/src/at/pcgamingfreaks/Minepacks/Bukkit/ExtendedAPI/BackpackExtended.java index 6872093..cc354b3 100644 --- a/Minepacks-API-Extended/src/at/pcgamingfreaks/Minepacks/Bukkit/ExtendedAPI/BackpackExtended.java +++ b/Minepacks-API-Extended/src/at/pcgamingfreaks/Minepacks/Bukkit/ExtendedAPI/BackpackExtended.java @@ -17,13 +17,24 @@ package at.pcgamingfreaks.Minepacks.Bukkit.ExtendedAPI; +import at.pcgamingfreaks.Bukkit.Message.Message; import at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack; -import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksPlayer; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public interface BackpackExtended extends Backpack { @Override @NotNull MinepacksPlayerExtended getOwner(); + + /** + * Let a given player open this backpack. + * + * @param player The player who opens the backpack. + * @param editable Defines if the player who has opened the backpack can change the items inside. + * @param title Custom title for the backpack (will be shown to the player who opened the backpack. + */ + void open(@NotNull Player player, boolean editable, @Nullable Message title); } \ No newline at end of file diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java index 1a34b28..e00d1df 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 GeorgH93 + * Copyright (C) 2021 GeorgH93 * * 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 @@ -18,6 +18,7 @@ package at.pcgamingfreaks.Minepacks.Bukkit; import at.pcgamingfreaks.Bukkit.MCVersion; +import at.pcgamingfreaks.Bukkit.Message.Message; import at.pcgamingfreaks.Bukkit.Util.InventoryUtils; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Enums.ShrinkApproach; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.InventoryCompressor; @@ -46,7 +47,8 @@ public class Backpack implements BackpackExtended { @Setter(AccessLevel.PACKAGE) private static ShrinkApproach shrinkApproach = ShrinkApproach.COMPRESS; private static Object titleOwn; - private static String titleOtherFormat; + private static Message titleOtherFormat; + private final Object titleOtherComponent; private final String titleOther; @Getter private final MinepacksPlayerData owner; private final Map opened = new ConcurrentHashMap<>(); //Thanks Minecraft 1.14 @@ -54,9 +56,9 @@ public class Backpack implements BackpackExtended private int size; private boolean hasChanged; - public static void setTitle(final @NotNull String title, final @NotNull String titleOther) + public static void setTitle(final @NotNull Message title, final @NotNull Message titleOther) { - titleOwn = InventoryUtils.prepareTitleForOpenInventoryWithCustomTitle(title); + titleOwn = MCVersion.isNewerOrEqualThan(MCVersion.MC_1_16) ? title.prepareChatComponent() : InventoryUtils.prepareTitleForOpenInventoryWithCustomTitle(title.prepareChatLegacy()); titleOtherFormat = titleOther; } @@ -73,7 +75,9 @@ public Backpack(MinepacksPlayerData owner, int size) Minepacks.getInstance().getLogger().warning("Backpacks with more than 6 rows are no longer supported on Minecraft 1.14 and up!"); } this.owner = owner; - titleOther = StringUtils.limitLength(String.format(titleOtherFormat, owner.getName()), 32); + titleOther = StringUtils.limitLength(titleOtherFormat.prepareChatLegacy(owner.getName()), 32); + if(MCVersion.isNewerOrEqualThan(MCVersion.MC_1_16)) titleOtherComponent = titleOtherFormat.prepareChatComponent(owner.getName()); + else titleOtherComponent = null; bp = Bukkit.createInventory(this, size, titleOther); this.size = size; } @@ -137,6 +141,7 @@ public void open(final @NotNull Player player, final boolean editable) checkResize(); opened.put(player, editable); if(owner.getPlayer().equals(player)) InventoryUtils.openInventoryWithCustomTitlePrepared(player, bp, titleOwn); + else if(titleOtherComponent != null) InventoryUtils.openInventoryWithCustomTitlePrepared(player, bp, titleOtherComponent); else player.openInventory(bp); } @@ -153,6 +158,20 @@ public void open(final @NotNull Player player, final boolean editable, final @Nu InventoryUtils.openInventoryWithCustomTitle(player, bp, title); } + @Override + public void open(@NotNull Player player, boolean editable, @Nullable Message title) + { + if(title == null) + { + open(player, editable); + return; + } + checkResize(); + opened.put(player, editable); + if(MCVersion.isNewerOrEqualThan(MCVersion.MC_1_16)) InventoryUtils.openInventoryWithCustomTitlePrepared(player, bp, title.prepareChatComponent()); + else InventoryUtils.openInventoryWithCustomTitle(player, bp, title.prepareChatLegacy()); + } + public void close(Player p) { opened.remove(p); diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java index ca45593..4f0c2f4 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java @@ -191,7 +191,7 @@ private void load() } maxSize = configuration.getBackpackMaxSize(); at.pcgamingfreaks.Minepacks.Bukkit.Backpack.setShrinkApproach(configuration.getShrinkApproach()); - at.pcgamingfreaks.Minepacks.Bukkit.Backpack.setTitle(configuration.getBPTitle(), configuration.getBPTitleOther()); + at.pcgamingfreaks.Minepacks.Bukkit.Backpack.setTitle(new Message(configuration.getBPTitle()), new Message(configuration.getBPTitleOther())); messageNotFromConsole = language.getMessage("NotFromConsole"); messageNoPermission = language.getMessage("Ingame.NoPermission"); messageWorldDisabled = language.getMessage("Ingame.WorldDisabled");