From 4a665f4e0c19b6d9c783fd26f814174af24d4e5a Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Sun, 26 Sep 2021 18:00:19 +0800 Subject: [PATCH] Use paper adventure lib for --- pom.xml | 6 ++ .../quickshop/shop/ContainerShop.java | 98 ++++++++++++++++--- .../org/maxgamer/quickshop/shop/Shop.java | 8 +- .../maxgamer/quickshop/shop/ShopManager.java | 4 + .../org/maxgamer/quickshop/util/MsgUtil.java | 69 ++----------- .../org/maxgamer/quickshop/util/Util.java | 27 +++++ 6 files changed, 137 insertions(+), 75 deletions(-) diff --git a/pom.xml b/pom.xml index 56c0afb27..3c5317d8c 100644 --- a/pom.xml +++ b/pom.xml @@ -955,5 +955,11 @@ 4.0.0 compile + + org.maxgamer.quickshop.externalhelper + PaperAdventureLib + 1.0-SNAPSHOT + compile + diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index a9c0d4ae3..4884556cc 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -22,6 +22,8 @@ package org.maxgamer.quickshop.shop; import com.lishid.openinv.OpenInv; import io.papermc.lib.PaperLib; import lombok.EqualsAndHashCode; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -42,6 +44,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.event.*; +import org.maxgamer.quickshop.externalhelper.paperadventurelib.PaperAdventureLib; +import org.maxgamer.quickshop.externalhelper.paperadventurelib.SignAdventure; import org.maxgamer.quickshop.util.PriceLimiter; import org.maxgamer.quickshop.util.Util; @@ -605,15 +609,78 @@ public class ContainerShop implements Shop { return true; } +// @Override +// public Component[] getSignText() { +// Util.ensureThread(false); +// Component[] lines = new Component[4]; +// +// //Line 1 +// OfflinePlayer player = plugin.getServer().getOfflinePlayer(this.getOwner()); +// String statusStringKey = inventoryAvailable() ? "signs.status-available" : "signs.status-unavailable"; +// lines[0] = Component.text(plugin.text().of("signs.header", this.ownerName(false), plugin.text().of(statusStringKey).forLocale()).forLocale()); +// +// //Line 2 +// String tradingStringKey; +// String noRemainingStringKey; +// int shopRemaining; +// +// switch (shopType) { +// case BUYING: +// shopRemaining = getRemainingSpace(); +// tradingStringKey = isStackingShop() ? "signs.stack-buying" : "signs.buying"; +// noRemainingStringKey = "signs.out-of-space"; +// break; +// case SELLING: +// shopRemaining = getRemainingStock(); +// tradingStringKey = isStackingShop() ? "signs.stack-selling" : "signs.selling"; +// noRemainingStringKey = "signs.out-of-stock"; +// break; +// default: +// shopRemaining = 0; +// tradingStringKey = "MissingKey for shop type:" + shopType; +// noRemainingStringKey = "MissingKey for shop type:" + shopType; +// } +// switch (shopRemaining) { +// //Unlimited +// case -1: +// lines[1] = Component.text(plugin.text().of(tradingStringKey, plugin.text().of("signs.unlimited").forLocale()).forLocale()); +// break; +// //No remaining +// case 0: +// lines[1] =Component.text(plugin.text().of(noRemainingStringKey).forLocale()); +// break; +// //Has remaining +// default: +// lines[1] = Component.text(plugin.text().of(tradingStringKey, Integer.toString(shopRemaining)).forLocale()); +// } +// +// //line 3 +// lines[2] = Component.text(plugin.text().of("signs.item", Util.getItemStackName(this.getItem())).forLocale()); +// +// //line 4 +// if (this.isStackingShop()) { +// lines[3] = Component.text(plugin.text().of("signs.stack-price", +// Util.format(this.getPrice(), this), Integer.toString(item.getAmount()), +// Util.getItemStackName(item)).forLocale()); +// } else { +// lines[3] = Component.text( plugin.text().of("signs.price", Util.format(this.getPrice(), this)).forLocale()); +// } +// +// //New pattern for recognizing shop sign +// lines[1] = Component.text(shopSignPrefix + lines[1] + " "); +// +// return lines; +// } + @Override - public String[] getSignText() { + public Component[] getSignText() { Util.ensureThread(false); - String[] lines = new String[4]; + Component[] lines = new Component[4]; //Line 1 OfflinePlayer player = plugin.getServer().getOfflinePlayer(this.getOwner()); String statusStringKey = inventoryAvailable() ? "signs.status-available" : "signs.status-unavailable"; - lines[0] = plugin.text().of("signs.header", this.ownerName(false), plugin.text().of(statusStringKey).forLocale()).forLocale(); + lines[0] = Component.text(plugin.text().of("signs.header", this.ownerName(false), plugin.text().of(statusStringKey).forLocale()).forLocale()); //Line 2 String tradingStringKey; @@ -639,42 +706,43 @@ public class ContainerShop implements Shop { switch (shopRemaining) { //Unlimited case -1: - lines[1] = plugin.text().of(tradingStringKey, plugin.text().of("signs.unlimited").forLocale()).forLocale(); + lines[1] = Component.text(plugin.text().of(tradingStringKey, plugin.text().of("signs.unlimited").forLocale()).forLocale()); break; //No remaining case 0: - lines[1] =plugin.text().of(noRemainingStringKey).forLocale(); + lines[1] =Component.text(plugin.text().of(noRemainingStringKey).forLocale()); break; //Has remaining default: - lines[1] = plugin.text().of(tradingStringKey, Integer.toString(shopRemaining)).forLocale(); + lines[1] = Component.text(plugin.text().of(tradingStringKey, Integer.toString(shopRemaining)).forLocale()); } //line 3 - lines[2] = plugin.text().of("signs.item", Util.getItemStackName(this.getItem())).forLocale(); + lines[2] = Component.text(plugin.text().of("signs.item", Util.getItemStackName(this.getItem())).forLocale()); //line 4 if (this.isStackingShop()) { - lines[3] = plugin.text().of("signs.stack-price", + lines[3] = Component.text(plugin.text().of("signs.stack-price", Util.format(this.getPrice(), this), Integer.toString(item.getAmount()), - Util.getItemStackName(item)).forLocale(); + Util.getItemStackName(item)).forLocale()); } else { - lines[3] = plugin.text().of("signs.price", Util.format(this.getPrice(), this)).forLocale(); + lines[3] = Component.text( plugin.text().of("signs.price", Util.format(this.getPrice(), this)).forLocale()); } //New pattern for recognizing shop sign - lines[1] = shopSignPrefix + lines[1] + " "; + lines[1] = Component.text(shopSignPrefix + lines[1] + " "); return lines; } + /** * Changes all lines of text on a sign near the shop * * @param lines The array of lines to change. Index is line number. */ @Override - public void setSignText(@NotNull String[] lines) { + public void setSignText(@NotNull Component[] lines) { Util.ensureThread(false); List signs = this.getSigns(); for (Sign sign : signs) { @@ -682,7 +750,11 @@ public class ContainerShop implements Shop { continue; } for (int i = 0; i < lines.length; i++) { - sign.setLine(i, lines[i]); + if(PaperAdventureLib.isSupported()){ + SignAdventure.set(sign,i,lines[i]); + }else{ + sign.setLine(i, LegacyComponentSerializer.legacySection().serialize(lines[i])); + } } if (plugin.getGameVersion().isSignTextDyeSupport()) { DyeColor dyeColor = Util.getDyeColor(); diff --git a/src/main/java/org/maxgamer/quickshop/shop/Shop.java b/src/main/java/org/maxgamer/quickshop/shop/Shop.java index 8fe3ccced..fbb69222a 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/Shop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/Shop.java @@ -19,6 +19,7 @@ package org.maxgamer.quickshop.shop; +import net.kyori.adventure.text.Component; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.block.Sign; @@ -164,6 +165,7 @@ public interface Shop { */ void sell(@NotNull UUID seller, @NotNull Inventory sellerInventory, @NotNull Location loc2Drop, int paramInt); + /** * Generate new sign texts on shop's sign. */ @@ -180,7 +182,7 @@ public interface Shop { * Line 3: Price */ @NotNull - default String[] getSignText() { + default Component[] getSignText() { //backward support throw new UnsupportedOperationException(); } @@ -188,9 +190,9 @@ public interface Shop { /** * Set texts on shop's sign * - * @param paramArrayOfString The texts you want set + * @param contents The texts you want set */ - void setSignText(@NotNull String[] paramArrayOfString); + void setSignText(@NotNull Component[] contents); /** * Update shop data to database diff --git a/src/main/java/org/maxgamer/quickshop/shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/shop/ShopManager.java index c8cf47ecb..567017fac 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ShopManager.java @@ -26,6 +26,8 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import io.papermc.lib.PaperLib; import lombok.Getter; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.translation.Translatable; import org.apache.commons.lang.StringUtils; import org.bukkit.*; import org.bukkit.block.*; @@ -1102,6 +1104,8 @@ public class ShopManager implements Reloadable { Util.getItemStackName(shop.getItem())).forLocale(); } + plugin.adventure().sender(Component.translatable(T)) + MsgUtil.TransactionMessage transactionMessage = new MsgUtil.TransactionMessage(msg, Util.serialize(shop.getItem()), null); MsgUtil.send(shop, shop.getOwner(), transactionMessage); diff --git a/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java b/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java index d4cbaf627..e239e9363 100644 --- a/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java +++ b/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java @@ -26,10 +26,7 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.Getter; -import net.kyori.adventure.key.Key; -import net.kyori.adventure.nbt.api.BinaryTagHolder; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; @@ -60,7 +57,6 @@ import org.maxgamer.quickshop.util.language.game.MojangGameLanguageImpl; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import java.lang.reflect.InvocationTargetException; import java.sql.ResultSet; import java.sql.SQLException; import java.text.DecimalFormat; @@ -119,26 +115,12 @@ public class MsgUtil { if (data == null) { MsgUtil.sendDirectMessage(p.getPlayer(), msg.getMessage()); } else { - plugin.adventure().sender(player).sendMessage( - Component.text(msg.getMessage()) - .hoverEvent(HoverEvent.showItem( - HoverEvent.ShowItem.of( - Key.key(data.getType().getKey().getNamespace(),data.getType().getKey().getKey()), - data.getAmount(), - BinaryTagHolder.of(ReflectFactory.convertBukkitItemStackToJson(data)))))); - - //plugin.getQuickChat().sendItemHologramChat(player, msg.getMessage(), data); + plugin.adventure().sender(player).sendMessage( + Component.text(msg.getMessage()) + .hoverEvent(Util.generateItemHoverEvent(data))); } } catch (InvalidConfigurationException e) { MsgUtil.sendDirectMessage(p.getPlayer(), msg.getMessage()); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); } } } @@ -517,11 +499,7 @@ public class MsgUtil { ItemStack data = Util.deserialize(transactionMessage.getHoverItem()); plugin.adventure().sender(p.getPlayer()).sendMessage( Component.text(transactionMessage.getMessage()) - .hoverEvent(HoverEvent.showItem( - HoverEvent.ShowItem.of( - Key.key(data.getType().getKey().getNamespace(),data.getType().getKey().getKey()), - data.getAmount(), - BinaryTagHolder.of(ReflectFactory.convertBukkitItemStackToJson(data)))))); + .hoverEvent(Util.generateItemHoverEvent(data))); // plugin.getQuickChat().sendItemHologramChat(p.getPlayer(), transactionMessage.getMessage(), Objects.requireNonNull(Util.deserialize(transactionMessage.getHoverItem()))); } catch (Exception any) { Util.debugLog("Unknown error, send by plain text."); @@ -557,24 +535,11 @@ public class MsgUtil { if (p.getPlayer() != null) { if (transactionMessage.getHoverItem() != null) { try { - try { ItemStack data = Util.deserialize(transactionMessage.getHoverItem()); plugin.adventure().sender(p.getPlayer()).sendMessage( Component.text(transactionMessage.getMessage()) - .hoverEvent(HoverEvent.showItem( - HoverEvent.ShowItem.of( - Key.key(data.getType().getKey().getNamespace(),data.getType().getKey().getKey()), - data.getAmount(), - BinaryTagHolder.of(ReflectFactory.convertBukkitItemStackToJson(data)))))); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } + .hoverEvent(Util.generateItemHoverEvent(data))); + // plugin.getQuickChat().sendItemHologramChat(p.getPlayer(), transactionMessage.getMessage(), Objects.requireNonNull(Util.deserialize(transactionMessage.getHoverItem()))); } catch (Exception any) { Util.debugLog("Unknown error, send by plain text."); @@ -943,24 +908,10 @@ public class MsgUtil { chatSheetPrinter.printLine(plugin.text().of(p,"menu.shop-information").forLocale()); chatSheetPrinter.printLine(plugin.text().of(p,"menu.owner", shop.ownerName()).forLocale()); // Enabled - - try { - plugin.adventure().sender(p).sendMessage( - Component.text(ChatColor.DARK_PURPLE + plugin.text().of(p,"tableformat.left_begin").forLocale() + plugin.text().of(p,"menu.item", Util.getItemStackName(items)) + " ") - .hoverEvent(HoverEvent.showItem( - HoverEvent.ShowItem.of( - Key.key(items.getType().getKey().getNamespace(),items.getType().getKey().getKey()), - items.getAmount(), - BinaryTagHolder.of(ReflectFactory.convertBukkitItemStackToJson(items)))))); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } + String msg = ChatColor.DARK_PURPLE + plugin.text().of(p,"tableformat.left_begin").forLocale() + plugin.text().of(p,"menu.item", Util.getItemStackName(items)) + " "; + plugin.adventure().sender(p).sendMessage( + Component.text(msg) + .hoverEvent(Util.generateItemHoverEvent(items))); //plugin.getQuickChat().sendItemHologramChat(player, msg.getMessage(), data); //plugin.getQuickChat().send(p, plugin.getQuickChat().getItemHologramChat(shop, items, p); diff --git a/src/main/java/org/maxgamer/quickshop/util/Util.java b/src/main/java/org/maxgamer/quickshop/util/Util.java index caa66ecb1..ad0ec5531 100644 --- a/src/main/java/org/maxgamer/quickshop/util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/util/Util.java @@ -25,7 +25,10 @@ import io.papermc.lib.PaperLib; import lombok.Getter; import lombok.NonNull; import lombok.SneakyThrows; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.nbt.api.BinaryTagHolder; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.apache.commons.lang.StringUtils; import org.bukkit.*; @@ -52,6 +55,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.database.MySQLCore; +import org.maxgamer.quickshop.externalhelper.paperadventurelib.ItemStackAdventure; +import org.maxgamer.quickshop.externalhelper.paperadventurelib.PaperAdventureLib; import org.maxgamer.quickshop.shop.DisplayItem; import org.maxgamer.quickshop.shop.Shop; import org.yaml.snakeyaml.DumperOptions; @@ -1392,4 +1397,26 @@ public class Util { } } + public static HoverEvent generateItemHoverEvent(ItemStack stack){ + if(PaperAdventureLib.isSupported()) + return ItemStackAdventure.convertHoverItem(stack); + try { + return HoverEvent.showItem( + HoverEvent.ShowItem.of( + Key.key(stack.getType().getKey().getNamespace(), stack.getType().getKey().getKey()), + stack.getAmount(), + BinaryTagHolder.of(ReflectFactory.convertBukkitItemStackToJson(stack)))); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + }finally { + return HoverEvent.showText(Component.text("Failed to generate item preview. Consider switch to Paper.")); + } + } + }