From 581d03d79bad80e939876d2facee4dd88eb6adad Mon Sep 17 00:00:00 2001 From: Stijn Date: Wed, 31 Aug 2022 18:49:18 +0200 Subject: [PATCH 01/14] Added a way to display all info on a shop --- .../listener/TransactionListener.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index a53a94d..d57746a 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -43,7 +43,7 @@ public class TransactionListener extends EventListener { return; Player player = event.getPlayer(); - if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) + if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK) && !(event.getAction() == Action.LEFT_CLICK_BLOCK)) return; Block block = event.getClickedBlock(); @@ -76,6 +76,11 @@ public class TransactionListener extends EventListener { return; } + if (event.getAction() == Action.LEFT_CLICK_BLOCK) { + giveInfo(playerShop, player); + return; + } + if (ShopUtil.canManageShop(player, playerShop)) { if (player.isSneaking()) return; @@ -91,6 +96,31 @@ public class TransactionListener extends EventListener { executeTransaction(player, playerShop); } + private void giveInfo(PlayerShop playerShop, Player player) { + if (player.isSneaking()) + return; + if (!playerShop.isInitialized()) + return; + + String message = "This shop () for "; + + TagResolver.Single action; + switch (playerShop.getType()) { + case SELL -> action = Placeholder.unparsed("action", "sells"); + case BUY -> action = Placeholder.unparsed("action", "buys"); + case GAMBLE -> action = Placeholder.unparsed("action", "gambles"); + default -> action = Placeholder.unparsed("action", "UNKNOWN"); + } + TagResolver placeholders = TagResolver.resolver(action, + Placeholder.parsed("amount", "" + playerShop.getAmount()), + Placeholder.component("item", playerShop.getItemStack().displayName()), + Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), + Placeholder.parsed("price", "" + playerShop.getPrice()) + ); + + player.sendMiniMessage(message, placeholders); + } + private void executeTransaction(Player player, PlayerShop shop) { if (shop == null || shop.getItemStack() == null) return; From 387a56b1d3f3a9a231da25645b18f764f5a94deb Mon Sep 17 00:00:00 2001 From: Stijn Date: Wed, 31 Aug 2022 19:03:54 +0200 Subject: [PATCH 02/14] Changed shop info to show the actual item in the shop when hovering over its name --- .../playershops/config/MessageConfig.java | 5 ++ .../listener/TransactionListener.java | 50 +++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/alttd/playershops/config/MessageConfig.java b/src/main/java/com/alttd/playershops/config/MessageConfig.java index 66f6f79..dd8fa7d 100644 --- a/src/main/java/com/alttd/playershops/config/MessageConfig.java +++ b/src/main/java/com/alttd/playershops/config/MessageConfig.java @@ -31,4 +31,9 @@ public class MessageConfig extends AbstractConfiguration { SHOP_LIMIT_REACHED = getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED); BREAK_SHOP_WHILE_CONVERSING = getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING); } + + public static String SHOP_INFO = "This shop for ."; + void otherMessages() { + SHOP_INFO = getString("messages.shop-info", SHOP_INFO); + } } diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index d57746a..7ca7b38 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -1,6 +1,7 @@ package com.alttd.playershops.listener; import com.alttd.playershops.PlayerShops; +import com.alttd.playershops.config.MessageConfig; import com.alttd.playershops.gui.ShopManagementGui; import com.alttd.playershops.handler.ShopHandler; import com.alttd.playershops.hook.WorldGuardHook; @@ -9,10 +10,14 @@ import com.alttd.playershops.shop.TransactionError; import com.alttd.playershops.utils.Logger; import com.alttd.playershops.utils.ShopUtil; import com.alttd.playershops.utils.Util; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.Tag; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -20,6 +25,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; /** * Dedicated class to listen to transactions for shops. @@ -102,8 +108,6 @@ public class TransactionListener extends EventListener { if (!playerShop.isInitialized()) return; - String message = "This shop () for "; - TagResolver.Single action; switch (playerShop.getType()) { case SELL -> action = Placeholder.unparsed("action", "sells"); @@ -113,12 +117,50 @@ public class TransactionListener extends EventListener { } TagResolver placeholders = TagResolver.resolver(action, Placeholder.parsed("amount", "" + playerShop.getAmount()), - Placeholder.component("item", playerShop.getItemStack().displayName()), + Placeholder.component("item", itemComponent(playerShop.getItemStack())), Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), Placeholder.parsed("price", "" + playerShop.getPrice()) ); - player.sendMiniMessage(message, placeholders); + player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders); + } + + private Component itemComponent(ItemStack item) { + Component component; + MiniMessage miniMessage = MiniMessage.miniMessage(); + if (item.getType().equals(Material.AIR)) + return miniMessage.deserialize("NONE"); + boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); + if (dname) { + component = item.getItemMeta().displayName(); + } else { + component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE); + } + component = component.hoverEvent(item.asHoverEvent()); + return component; + } + + private String materialToName(Material m) { + if (m.equals(Material.TNT)) { + return "TNT"; + } + String orig = m.toString().toLowerCase(); + String[] splits = orig.split("_"); + StringBuilder sb = new StringBuilder(orig.length()); + int pos = 0; + for (String split : splits) { + sb.append(split); + int loc = sb.lastIndexOf(split); + char charLoc = sb.charAt(loc); + if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || + split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) + sb.setCharAt(loc, Character.toUpperCase(charLoc)); + if (pos != splits.length - 1) + sb.append(' '); + ++pos; + } + + return sb.toString(); } private void executeTransaction(Player player, PlayerShop shop) { From 064595475f0a3a0b8d1b06c54fad89643e30eb1d Mon Sep 17 00:00:00 2001 From: Stijn Date: Wed, 31 Aug 2022 19:20:40 +0200 Subject: [PATCH 03/14] Trimmed item name and price on signs --- .../listener/TransactionListener.java | 40 +-------------- .../alttd/playershops/shop/PlayerShop.java | 11 ++++- .../com/alttd/playershops/utils/ShopUtil.java | 49 ++++++++++++++++--- 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index 7ca7b38..91446d3 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -117,7 +117,7 @@ public class TransactionListener extends EventListener { } TagResolver placeholders = TagResolver.resolver(action, Placeholder.parsed("amount", "" + playerShop.getAmount()), - Placeholder.component("item", itemComponent(playerShop.getItemStack())), + Placeholder.component("item", ShopUtil.itemNameComponent(playerShop.getItemStack())), Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), Placeholder.parsed("price", "" + playerShop.getPrice()) ); @@ -125,44 +125,6 @@ public class TransactionListener extends EventListener { player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders); } - private Component itemComponent(ItemStack item) { - Component component; - MiniMessage miniMessage = MiniMessage.miniMessage(); - if (item.getType().equals(Material.AIR)) - return miniMessage.deserialize("NONE"); - boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); - if (dname) { - component = item.getItemMeta().displayName(); - } else { - component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE); - } - component = component.hoverEvent(item.asHoverEvent()); - return component; - } - - private String materialToName(Material m) { - if (m.equals(Material.TNT)) { - return "TNT"; - } - String orig = m.toString().toLowerCase(); - String[] splits = orig.split("_"); - StringBuilder sb = new StringBuilder(orig.length()); - int pos = 0; - for (String split : splits) { - sb.append(split); - int loc = sb.lastIndexOf(split); - char charLoc = sb.charAt(loc); - if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || - split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) - sb.setCharAt(loc, Character.toUpperCase(charLoc)); - if (pos != splits.length - 1) - sb.append(' '); - ++pos; - } - - return sb.toString(); - } - private void executeTransaction(Player player, PlayerShop shop) { if (shop == null || shop.getItemStack() == null) return; diff --git a/src/main/java/com/alttd/playershops/shop/PlayerShop.java b/src/main/java/com/alttd/playershops/shop/PlayerShop.java index 6e4e0f1..9e204e8 100644 --- a/src/main/java/com/alttd/playershops/shop/PlayerShop.java +++ b/src/main/java/com/alttd/playershops/shop/PlayerShop.java @@ -152,9 +152,9 @@ public class PlayerShop { MiniMessage miniMessage = MiniMessage.miniMessage(); TagResolver tagResolver = TagResolver.resolver( Placeholder.unparsed("ownername", getOwnerName()), - Placeholder.unparsed("price", String.valueOf(getPrice())), + Placeholder.unparsed("price", trimPrice(String.valueOf(getPrice()))), Placeholder.unparsed("amount", String.valueOf(getAmount())), - Placeholder.component("itemname", ShopUtil.itemNameComponent(getItemStack())) + Placeholder.component("itemname", ShopUtil.trimmedItemNameComponent(getItemStack())) ); for (int i = 0; i < 4; i++) { signBlock.line(i, miniMessage.deserialize(signLines.get(i), tagResolver)); @@ -164,6 +164,13 @@ public class PlayerShop { }.runTaskLater(PlayerShops.getInstance(), 2L); } + private String trimPrice(String price) { + if (price.length() > 15) { + return price.substring(0, 13) + "!"; + } + return price; + } + public double getPricePerItem() { return this.getPrice() / this.getAmount(); } diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index 5ccb5ae..b6eb86c 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -3,6 +3,7 @@ package com.alttd.playershops.utils; import com.alttd.playershops.shop.PlayerShop; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -156,17 +157,49 @@ public class ShopUtil { return false; } - public static Component itemNameComponent(ItemStack item) { - Component component = Component.empty(); + public static Component trimmedItemNameComponent(ItemStack item) { if(item == null || item.getType().equals(Material.AIR)) return Component.text("Nothing"); - boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); - if(dname) { - component = component.append(item.getItemMeta().displayName()); - } else { - component = component.append(Component.text(materialToName(item.getType()), NamedTextColor.WHITE)); + return materialToTrimmedName(item.getType()); + } + + private static Component materialToTrimmedName(Material m) { + if (m.equals(Material.TNT)) { + return Component.text("TNT", NamedTextColor.WHITE); + } + String orig = m.name().toLowerCase(); + String[] splits = orig.split("_"); + StringBuilder sb = new StringBuilder(orig.length()); + int pos = 0; + for (String split : splits) { + sb.append(split); + int loc = sb.lastIndexOf(split); + char charLoc = sb.charAt(loc); + if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || + split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) + sb.setCharAt(loc, Character.toUpperCase(charLoc)); + if (pos != splits.length - 1) + sb.append(' '); + ++pos; } + if (sb.length() > 10) + return Component.text(sb.substring(0, 8), NamedTextColor.WHITE).append(Component.text("!", NamedTextColor.RED)); + return Component.text(sb.toString(), NamedTextColor.WHITE); + } + + public static Component itemNameComponent(ItemStack item) { + Component component; + MiniMessage miniMessage = MiniMessage.miniMessage(); + if (item.getType().equals(Material.AIR)) + return miniMessage.deserialize("NONE"); + boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); + if (dname) { + component = item.getItemMeta().displayName(); + } else { + component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE); + } + component = component.hoverEvent(item.asHoverEvent()); return component; } @@ -174,7 +207,7 @@ public class ShopUtil { if (m.equals(Material.TNT)) { return "TNT"; } - String orig = m.name().toLowerCase(); + String orig = m.toString().toLowerCase(); String[] splits = orig.split("_"); StringBuilder sb = new StringBuilder(orig.length()); int pos = 0; From b677ebace80fdedf39aea3fe0872c8989f6be20f Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:31:42 +0200 Subject: [PATCH 04/14] Fixed Message Config and reloading --- .../playershops/commands/ShopCommand.java | 4 ++++ .../playershops/config/MessageConfig.java | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/alttd/playershops/commands/ShopCommand.java b/src/main/java/com/alttd/playershops/commands/ShopCommand.java index 2e6c1d2..cfb9a66 100644 --- a/src/main/java/com/alttd/playershops/commands/ShopCommand.java +++ b/src/main/java/com/alttd/playershops/commands/ShopCommand.java @@ -1,5 +1,8 @@ package com.alttd.playershops.commands; +import com.alttd.playershops.PlayerShops; +import com.alttd.playershops.config.Config; +import com.alttd.playershops.config.MessageConfig; import com.alttd.playershops.gui.HomeGui; import com.alttd.playershops.gui.ShopManagementGui; import com.alttd.playershops.shop.PlayerShop; @@ -24,6 +27,7 @@ public class ShopCommand implements CommandExecutor, TabCompleter { } switch (args[0].toLowerCase()) { case "reload": + PlayerShops.getInstance().reloadConfigs(); break; case "open": HomeGui gui = new HomeGui(player.getUniqueId()); diff --git a/src/main/java/com/alttd/playershops/config/MessageConfig.java b/src/main/java/com/alttd/playershops/config/MessageConfig.java index dd8fa7d..8ecd16b 100644 --- a/src/main/java/com/alttd/playershops/config/MessageConfig.java +++ b/src/main/java/com/alttd/playershops/config/MessageConfig.java @@ -17,7 +17,7 @@ public class MessageConfig extends AbstractConfiguration { version = config.getInt("config-version", 1); config.set("config-version", 1); - config.readConfig(Config.class, null); + config.readConfig(MessageConfig.class, null); } public static String SHOP_ALREADY_EXISTS = "This block is already a Shop"; @@ -25,15 +25,15 @@ public class MessageConfig extends AbstractConfiguration { public static String SHOP_LIMIT_REACHED = "You cannot create this shop as you already have reached the limit ()."; public static String BREAK_SHOP_WHILE_CONVERSING = "You can not break shop signs while editing them."; - void loadErrorMessages() { - SHOP_ALREADY_EXISTS = getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS); - NO_SHOP_CREATE_PERMISSION = getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION); - SHOP_LIMIT_REACHED = getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED); - BREAK_SHOP_WHILE_CONVERSING = getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING); + private static void loadErrorMessages() { + SHOP_ALREADY_EXISTS = config.getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS); + NO_SHOP_CREATE_PERMISSION = config.getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION); + SHOP_LIMIT_REACHED = config.getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED); + BREAK_SHOP_WHILE_CONVERSING = config.getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING); } - public static String SHOP_INFO = "This shop for ."; - void otherMessages() { - SHOP_INFO = getString("messages.shop-info", SHOP_INFO); + public static String SHOP_INFO = "This shop for ."; + private static void otherMessages() { + SHOP_INFO = config.getString("messages.shop-info", SHOP_INFO); } } From 1eaf51d6d159a4b248f47aa4a9e5bd398c13b9d6 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:38:43 +0200 Subject: [PATCH 05/14] Better fix for prices rounding --- src/main/java/com/alttd/playershops/shop/PlayerShop.java | 2 +- src/main/java/com/alttd/playershops/utils/ShopUtil.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/shop/PlayerShop.java b/src/main/java/com/alttd/playershops/shop/PlayerShop.java index 9e204e8..f56d66e 100644 --- a/src/main/java/com/alttd/playershops/shop/PlayerShop.java +++ b/src/main/java/com/alttd/playershops/shop/PlayerShop.java @@ -152,7 +152,7 @@ public class PlayerShop { MiniMessage miniMessage = MiniMessage.miniMessage(); TagResolver tagResolver = TagResolver.resolver( Placeholder.unparsed("ownername", getOwnerName()), - Placeholder.unparsed("price", trimPrice(String.valueOf(getPrice()))), + Placeholder.unparsed("price", trimPrice(String.valueOf(ShopUtil.round(getPrice())))), Placeholder.unparsed("amount", String.valueOf(getAmount())), Placeholder.component("itemname", ShopUtil.trimmedItemNameComponent(getItemStack())) ); diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index b6eb86c..9cf0d9e 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -226,4 +226,7 @@ public class ShopUtil { return sb.toString(); } + public static double round(double price) { + return (double) Math.round(price * 100) / 100; + } } From d6393670261cb4844e88aa7b9efb13d647344278 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:56:49 +0200 Subject: [PATCH 06/14] Show rounded price in info message --- .../com/alttd/playershops/listener/TransactionListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index 91446d3..a903770 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -119,7 +119,7 @@ public class TransactionListener extends EventListener { Placeholder.parsed("amount", "" + playerShop.getAmount()), Placeholder.component("item", ShopUtil.itemNameComponent(playerShop.getItemStack())), Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), - Placeholder.parsed("price", "" + playerShop.getPrice()) + Placeholder.parsed("price", "" + ShopUtil.round(playerShop.getPrice())) ); player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders); From 9051b755f5b7c92a0015ff1d1b5197ce3f5ec239 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:57:02 +0200 Subject: [PATCH 07/14] Fix NPE for item component --- src/main/java/com/alttd/playershops/utils/ShopUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index 9cf0d9e..fc48bc7 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -191,7 +191,7 @@ public class ShopUtil { public static Component itemNameComponent(ItemStack item) { Component component; MiniMessage miniMessage = MiniMessage.miniMessage(); - if (item.getType().equals(Material.AIR)) + if (item == null || item.getType().equals(Material.AIR)) return miniMessage.deserialize("NONE"); boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); if (dname) { From cda2a0b216372b1d534f06e49069c8631512d218 Mon Sep 17 00:00:00 2001 From: Stijn Date: Wed, 31 Aug 2022 18:49:18 +0200 Subject: [PATCH 08/14] Added a way to display all info on a shop --- .../listener/TransactionListener.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index 23f766b..8a72a94 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -45,7 +45,7 @@ public class TransactionListener extends EventListener { return; Player player = event.getPlayer(); - if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) + if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK) && !(event.getAction() == Action.LEFT_CLICK_BLOCK)) return; Block block = event.getClickedBlock(); @@ -79,6 +79,11 @@ public class TransactionListener extends EventListener { return; } + if (event.getAction() == Action.LEFT_CLICK_BLOCK) { + giveInfo(playerShop, player); + return; + } + if (ShopUtil.canManageShop(player, playerShop)) { if (player.isSneaking()) return; @@ -94,6 +99,31 @@ public class TransactionListener extends EventListener { executeTransaction(player, playerShop); } + private void giveInfo(PlayerShop playerShop, Player player) { + if (player.isSneaking()) + return; + if (!playerShop.isInitialized()) + return; + + String message = "This shop () for "; + + TagResolver.Single action; + switch (playerShop.getType()) { + case SELL -> action = Placeholder.unparsed("action", "sells"); + case BUY -> action = Placeholder.unparsed("action", "buys"); + case GAMBLE -> action = Placeholder.unparsed("action", "gambles"); + default -> action = Placeholder.unparsed("action", "UNKNOWN"); + } + TagResolver placeholders = TagResolver.resolver(action, + Placeholder.parsed("amount", "" + playerShop.getAmount()), + Placeholder.component("item", playerShop.getItemStack().displayName()), + Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), + Placeholder.parsed("price", "" + playerShop.getPrice()) + ); + + player.sendMiniMessage(message, placeholders); + } + private void executeTransaction(Player player, PlayerShop shop) { if (shop == null || shop.getItemStack() == null) return; From 41cb2b2b65e68bb2b5b3de13fafac03586b13870 Mon Sep 17 00:00:00 2001 From: Stijn Date: Wed, 31 Aug 2022 19:03:54 +0200 Subject: [PATCH 09/14] Changed shop info to show the actual item in the shop when hovering over its name --- .../playershops/config/MessageConfig.java | 5 ++ .../listener/TransactionListener.java | 49 +++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/alttd/playershops/config/MessageConfig.java b/src/main/java/com/alttd/playershops/config/MessageConfig.java index b093709..bb27b80 100644 --- a/src/main/java/com/alttd/playershops/config/MessageConfig.java +++ b/src/main/java/com/alttd/playershops/config/MessageConfig.java @@ -33,4 +33,9 @@ public class MessageConfig extends AbstractConfiguration { BREAK_SHOP_WHILE_CONVERSING = getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING); NO_PERMISSION_FOR_SHOP_TYPE = getString("permissions-messages.shop-type", NO_PERMISSION_FOR_SHOP_TYPE); } + + public static String SHOP_INFO = "This shop for ."; + void otherMessages() { + SHOP_INFO = getString("messages.shop-info", SHOP_INFO); + } } diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index 8a72a94..5de9de9 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -11,10 +11,14 @@ import com.alttd.playershops.shop.TransactionError; import com.alttd.playershops.utils.Logger; import com.alttd.playershops.utils.ShopUtil; import com.alttd.playershops.utils.Util; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.Tag; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -22,6 +26,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; /** * Dedicated class to listen to transactions for shops. @@ -105,8 +110,6 @@ public class TransactionListener extends EventListener { if (!playerShop.isInitialized()) return; - String message = "This shop () for "; - TagResolver.Single action; switch (playerShop.getType()) { case SELL -> action = Placeholder.unparsed("action", "sells"); @@ -116,12 +119,50 @@ public class TransactionListener extends EventListener { } TagResolver placeholders = TagResolver.resolver(action, Placeholder.parsed("amount", "" + playerShop.getAmount()), - Placeholder.component("item", playerShop.getItemStack().displayName()), + Placeholder.component("item", itemComponent(playerShop.getItemStack())), Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), Placeholder.parsed("price", "" + playerShop.getPrice()) ); - player.sendMiniMessage(message, placeholders); + player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders); + } + + private Component itemComponent(ItemStack item) { + Component component; + MiniMessage miniMessage = MiniMessage.miniMessage(); + if (item.getType().equals(Material.AIR)) + return miniMessage.deserialize("NONE"); + boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); + if (dname) { + component = item.getItemMeta().displayName(); + } else { + component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE); + } + component = component.hoverEvent(item.asHoverEvent()); + return component; + } + + private String materialToName(Material m) { + if (m.equals(Material.TNT)) { + return "TNT"; + } + String orig = m.toString().toLowerCase(); + String[] splits = orig.split("_"); + StringBuilder sb = new StringBuilder(orig.length()); + int pos = 0; + for (String split : splits) { + sb.append(split); + int loc = sb.lastIndexOf(split); + char charLoc = sb.charAt(loc); + if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || + split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) + sb.setCharAt(loc, Character.toUpperCase(charLoc)); + if (pos != splits.length - 1) + sb.append(' '); + ++pos; + } + + return sb.toString(); } private void executeTransaction(Player player, PlayerShop shop) { From aa00c8d1f7ccbf97f2614e52e7d8ef9eeac4d17c Mon Sep 17 00:00:00 2001 From: Stijn Date: Wed, 31 Aug 2022 19:20:40 +0200 Subject: [PATCH 10/14] Trimmed item name and price on signs --- .../listener/TransactionListener.java | 40 +-------------- .../alttd/playershops/shop/PlayerShop.java | 11 ++++- .../com/alttd/playershops/utils/ShopUtil.java | 49 ++++++++++++++++--- 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index 5de9de9..172ae49 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -119,7 +119,7 @@ public class TransactionListener extends EventListener { } TagResolver placeholders = TagResolver.resolver(action, Placeholder.parsed("amount", "" + playerShop.getAmount()), - Placeholder.component("item", itemComponent(playerShop.getItemStack())), + Placeholder.component("item", ShopUtil.itemNameComponent(playerShop.getItemStack())), Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), Placeholder.parsed("price", "" + playerShop.getPrice()) ); @@ -127,44 +127,6 @@ public class TransactionListener extends EventListener { player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders); } - private Component itemComponent(ItemStack item) { - Component component; - MiniMessage miniMessage = MiniMessage.miniMessage(); - if (item.getType().equals(Material.AIR)) - return miniMessage.deserialize("NONE"); - boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); - if (dname) { - component = item.getItemMeta().displayName(); - } else { - component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE); - } - component = component.hoverEvent(item.asHoverEvent()); - return component; - } - - private String materialToName(Material m) { - if (m.equals(Material.TNT)) { - return "TNT"; - } - String orig = m.toString().toLowerCase(); - String[] splits = orig.split("_"); - StringBuilder sb = new StringBuilder(orig.length()); - int pos = 0; - for (String split : splits) { - sb.append(split); - int loc = sb.lastIndexOf(split); - char charLoc = sb.charAt(loc); - if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || - split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) - sb.setCharAt(loc, Character.toUpperCase(charLoc)); - if (pos != splits.length - 1) - sb.append(' '); - ++pos; - } - - return sb.toString(); - } - private void executeTransaction(Player player, PlayerShop shop) { if (shop == null || shop.getItemStack() == null) return; diff --git a/src/main/java/com/alttd/playershops/shop/PlayerShop.java b/src/main/java/com/alttd/playershops/shop/PlayerShop.java index 3285486..ab08747 100644 --- a/src/main/java/com/alttd/playershops/shop/PlayerShop.java +++ b/src/main/java/com/alttd/playershops/shop/PlayerShop.java @@ -152,9 +152,9 @@ public class PlayerShop { MiniMessage miniMessage = MiniMessage.miniMessage(); TagResolver tagResolver = TagResolver.resolver( Placeholder.unparsed("ownername", getOwnerName()), - Placeholder.unparsed("price", String.valueOf(getPrice())), + Placeholder.unparsed("price", trimPrice(String.valueOf(getPrice()))), Placeholder.unparsed("amount", String.valueOf(getAmount())), - Placeholder.component("itemname", ShopUtil.itemNameComponent(getItemStack())) + Placeholder.component("itemname", ShopUtil.trimmedItemNameComponent(getItemStack())) ); for (int i = 0; i < 4; i++) { signBlock.line(i, miniMessage.deserialize(signLines.get(i), tagResolver)); @@ -164,6 +164,13 @@ public class PlayerShop { }.runTaskLater(PlayerShops.getInstance(), 2L); } + private String trimPrice(String price) { + if (price.length() > 15) { + return price.substring(0, 13) + "!"; + } + return price; + } + public double getPricePerItem() { return this.getPrice() / this.getAmount(); } diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index 5ccb5ae..b6eb86c 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -3,6 +3,7 @@ package com.alttd.playershops.utils; import com.alttd.playershops.shop.PlayerShop; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -156,17 +157,49 @@ public class ShopUtil { return false; } - public static Component itemNameComponent(ItemStack item) { - Component component = Component.empty(); + public static Component trimmedItemNameComponent(ItemStack item) { if(item == null || item.getType().equals(Material.AIR)) return Component.text("Nothing"); - boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); - if(dname) { - component = component.append(item.getItemMeta().displayName()); - } else { - component = component.append(Component.text(materialToName(item.getType()), NamedTextColor.WHITE)); + return materialToTrimmedName(item.getType()); + } + + private static Component materialToTrimmedName(Material m) { + if (m.equals(Material.TNT)) { + return Component.text("TNT", NamedTextColor.WHITE); + } + String orig = m.name().toLowerCase(); + String[] splits = orig.split("_"); + StringBuilder sb = new StringBuilder(orig.length()); + int pos = 0; + for (String split : splits) { + sb.append(split); + int loc = sb.lastIndexOf(split); + char charLoc = sb.charAt(loc); + if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || + split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) + sb.setCharAt(loc, Character.toUpperCase(charLoc)); + if (pos != splits.length - 1) + sb.append(' '); + ++pos; } + if (sb.length() > 10) + return Component.text(sb.substring(0, 8), NamedTextColor.WHITE).append(Component.text("!", NamedTextColor.RED)); + return Component.text(sb.toString(), NamedTextColor.WHITE); + } + + public static Component itemNameComponent(ItemStack item) { + Component component; + MiniMessage miniMessage = MiniMessage.miniMessage(); + if (item.getType().equals(Material.AIR)) + return miniMessage.deserialize("NONE"); + boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); + if (dname) { + component = item.getItemMeta().displayName(); + } else { + component = Component.text(materialToName(item.getType()), NamedTextColor.WHITE); + } + component = component.hoverEvent(item.asHoverEvent()); return component; } @@ -174,7 +207,7 @@ public class ShopUtil { if (m.equals(Material.TNT)) { return "TNT"; } - String orig = m.name().toLowerCase(); + String orig = m.toString().toLowerCase(); String[] splits = orig.split("_"); StringBuilder sb = new StringBuilder(orig.length()); int pos = 0; From 4215d18ca570385618819c9e830f471e0be12fae Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:31:42 +0200 Subject: [PATCH 11/14] Fixed Message Config and reloading --- .../playershops/commands/ShopCommand.java | 4 ++++ .../playershops/config/MessageConfig.java | 20 +++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/alttd/playershops/commands/ShopCommand.java b/src/main/java/com/alttd/playershops/commands/ShopCommand.java index 2e6c1d2..cfb9a66 100644 --- a/src/main/java/com/alttd/playershops/commands/ShopCommand.java +++ b/src/main/java/com/alttd/playershops/commands/ShopCommand.java @@ -1,5 +1,8 @@ package com.alttd.playershops.commands; +import com.alttd.playershops.PlayerShops; +import com.alttd.playershops.config.Config; +import com.alttd.playershops.config.MessageConfig; import com.alttd.playershops.gui.HomeGui; import com.alttd.playershops.gui.ShopManagementGui; import com.alttd.playershops.shop.PlayerShop; @@ -24,6 +27,7 @@ public class ShopCommand implements CommandExecutor, TabCompleter { } switch (args[0].toLowerCase()) { case "reload": + PlayerShops.getInstance().reloadConfigs(); break; case "open": HomeGui gui = new HomeGui(player.getUniqueId()); diff --git a/src/main/java/com/alttd/playershops/config/MessageConfig.java b/src/main/java/com/alttd/playershops/config/MessageConfig.java index bb27b80..bd0f1f1 100644 --- a/src/main/java/com/alttd/playershops/config/MessageConfig.java +++ b/src/main/java/com/alttd/playershops/config/MessageConfig.java @@ -17,7 +17,7 @@ public class MessageConfig extends AbstractConfiguration { version = config.getInt("config-version", 1); config.set("config-version", 1); - config.readConfig(Config.class, null); + config.readConfig(MessageConfig.class, null); } public static String SHOP_ALREADY_EXISTS = "This block is already a Shop"; @@ -26,16 +26,16 @@ public class MessageConfig extends AbstractConfiguration { public static String BREAK_SHOP_WHILE_CONVERSING = "You can not break shop signs while editing them."; public static String NO_PERMISSION_FOR_SHOP_TYPE = "You do not have permission to use shops."; - void loadErrorMessages() { - SHOP_ALREADY_EXISTS = getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS); - NO_SHOP_CREATE_PERMISSION = getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION); - SHOP_LIMIT_REACHED = getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED); - BREAK_SHOP_WHILE_CONVERSING = getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING); - NO_PERMISSION_FOR_SHOP_TYPE = getString("permissions-messages.shop-type", NO_PERMISSION_FOR_SHOP_TYPE); + private static void loadErrorMessages() { + SHOP_ALREADY_EXISTS = config.getString("errors.shop-already-exists", SHOP_ALREADY_EXISTS); + NO_SHOP_CREATE_PERMISSION = config.getString("errors.no-shop-create-permission", NO_SHOP_CREATE_PERMISSION); + SHOP_LIMIT_REACHED = config.getString("errors.shop-limit-reached", SHOP_LIMIT_REACHED); + BREAK_SHOP_WHILE_CONVERSING = config.getString("errors.break-shop-while-conversing", BREAK_SHOP_WHILE_CONVERSING); + NO_PERMISSION_FOR_SHOP_TYPE = config.getString("permissions-messages.shop-type", NO_PERMISSION_FOR_SHOP_TYPE); } - public static String SHOP_INFO = "This shop for ."; - void otherMessages() { - SHOP_INFO = getString("messages.shop-info", SHOP_INFO); + public static String SHOP_INFO = "This shop for ."; + private static void otherMessages() { + SHOP_INFO = config.getString("messages.shop-info", SHOP_INFO); } } From a4065ee41c738162a8f3c0b6ce082dbafbf645e4 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:38:43 +0200 Subject: [PATCH 12/14] Better fix for prices rounding --- src/main/java/com/alttd/playershops/shop/PlayerShop.java | 2 +- src/main/java/com/alttd/playershops/utils/ShopUtil.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/shop/PlayerShop.java b/src/main/java/com/alttd/playershops/shop/PlayerShop.java index ab08747..75a2f15 100644 --- a/src/main/java/com/alttd/playershops/shop/PlayerShop.java +++ b/src/main/java/com/alttd/playershops/shop/PlayerShop.java @@ -152,7 +152,7 @@ public class PlayerShop { MiniMessage miniMessage = MiniMessage.miniMessage(); TagResolver tagResolver = TagResolver.resolver( Placeholder.unparsed("ownername", getOwnerName()), - Placeholder.unparsed("price", trimPrice(String.valueOf(getPrice()))), + Placeholder.unparsed("price", trimPrice(String.valueOf(ShopUtil.round(getPrice())))), Placeholder.unparsed("amount", String.valueOf(getAmount())), Placeholder.component("itemname", ShopUtil.trimmedItemNameComponent(getItemStack())) ); diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index b6eb86c..9cf0d9e 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -226,4 +226,7 @@ public class ShopUtil { return sb.toString(); } + public static double round(double price) { + return (double) Math.round(price * 100) / 100; + } } From d0dc044ca9e79a65abaeca30a5a5b01d77093829 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:56:49 +0200 Subject: [PATCH 13/14] Show rounded price in info message --- .../com/alttd/playershops/listener/TransactionListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/listener/TransactionListener.java b/src/main/java/com/alttd/playershops/listener/TransactionListener.java index 172ae49..b7baac7 100644 --- a/src/main/java/com/alttd/playershops/listener/TransactionListener.java +++ b/src/main/java/com/alttd/playershops/listener/TransactionListener.java @@ -121,7 +121,7 @@ public class TransactionListener extends EventListener { Placeholder.parsed("amount", "" + playerShop.getAmount()), Placeholder.component("item", ShopUtil.itemNameComponent(playerShop.getItemStack())), Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), - Placeholder.parsed("price", "" + playerShop.getPrice()) + Placeholder.parsed("price", "" + ShopUtil.round(playerShop.getPrice())) ); player.sendMiniMessage(MessageConfig.SHOP_INFO, placeholders); From a17e484f799c72c86b986571b3f83e54df66b840 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 2 Sep 2022 03:57:02 +0200 Subject: [PATCH 14/14] Fix NPE for item component --- src/main/java/com/alttd/playershops/utils/ShopUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index 9cf0d9e..fc48bc7 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -191,7 +191,7 @@ public class ShopUtil { public static Component itemNameComponent(ItemStack item) { Component component; MiniMessage miniMessage = MiniMessage.miniMessage(); - if (item.getType().equals(Material.AIR)) + if (item == null || item.getType().equals(Material.AIR)) return miniMessage.deserialize("NONE"); boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); if (dname) {