From c0bb58fe5c485cf9cff308314c43d5a969f1a2e5 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 10 Jul 2022 21:00:12 +0200 Subject: [PATCH] Added error messages and message config --- .../com/alttd/playershops/PlayerShops.java | 2 + .../playershops/config/MessageConfig.java | 42 +++++++++++++++++++ .../playershops/listener/PlayerListener.java | 11 +++-- 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 plugin/src/main/java/com/alttd/playershops/config/MessageConfig.java diff --git a/plugin/src/main/java/com/alttd/playershops/PlayerShops.java b/plugin/src/main/java/com/alttd/playershops/PlayerShops.java index 4f60de5..2d34956 100644 --- a/plugin/src/main/java/com/alttd/playershops/PlayerShops.java +++ b/plugin/src/main/java/com/alttd/playershops/PlayerShops.java @@ -1,6 +1,7 @@ package com.alttd.playershops; import com.alttd.playershops.api.ShopHandler; +import com.alttd.playershops.config.MessageConfig; import com.alttd.playershops.listener.PlayerListener; import com.alttd.playershops.listener.ShopListener; import com.alttd.playershops.config.Config; @@ -75,6 +76,7 @@ public class PlayerShops extends JavaPlugin { public void reloadConfigs() { Config.reload(); + MessageConfig.reload(); } } diff --git a/plugin/src/main/java/com/alttd/playershops/config/MessageConfig.java b/plugin/src/main/java/com/alttd/playershops/config/MessageConfig.java new file mode 100644 index 0000000..2ba7ee0 --- /dev/null +++ b/plugin/src/main/java/com/alttd/playershops/config/MessageConfig.java @@ -0,0 +1,42 @@ +package com.alttd.playershops.config; + +import com.alttd.galaxy.configuration.AbstractConfiguration; +import com.alttd.playershops.api.ShopType; + +import java.io.File; +import java.util.HashMap; + +public class MessageConfig extends AbstractConfiguration { + + public MessageConfig() { + super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "com/alttd/playershops"), "messageconfig"); + } + + static MessageConfig config; + static int version; + static HashMap shopTypeConfigs; + + public static void reload() { + config = new MessageConfig(); + + version = config.getInt("config-version", 1); + config.set("config-version", 1); + + config.readConfig(Config.class, null); + + shopTypeConfigs = new HashMap<>(); + for (ShopType shopType : ShopType.values()) { + shopTypeConfigs.put(shopType, new ShopTypeConfig(shopType.toString())); + } + } + + public static String SHOP_ALREADY_EXISTS = "This block is already a Shop"; + public static String NO_SHOP_CREATE_PERMISSION = "You don't have permission to create shops."; + public static String SHOP_LIMIT_REACHED = "You cannot create this shop as you already have reached the limit ()."; + + 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); + } +} diff --git a/plugin/src/main/java/com/alttd/playershops/listener/PlayerListener.java b/plugin/src/main/java/com/alttd/playershops/listener/PlayerListener.java index f0b3ed8..820240f 100644 --- a/plugin/src/main/java/com/alttd/playershops/listener/PlayerListener.java +++ b/plugin/src/main/java/com/alttd/playershops/listener/PlayerListener.java @@ -4,7 +4,9 @@ import com.alttd.playershops.PlayerShops; import com.alttd.playershops.api.Shop; import com.alttd.playershops.api.ShopHandler; import com.alttd.playershops.config.Config; +import com.alttd.playershops.config.MessageConfig; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -99,17 +101,18 @@ public class PlayerListener extends EventListener { return; Shop shop = shopHandler.getShop(bRelative.getLocation()); + Player player = event.getPlayer(); if(shop != null) { event.setCancelled(true); - // TODO MESSAGE: There is a shop here already. + player.sendMiniMessage(MessageConfig.SHOP_ALREADY_EXISTS, null); return; } - Player player = event.getPlayer(); + UUID playerUUID = player.getUniqueId(); if (!player.hasPermission("shop.create")) { event.setCancelled(true); - // TODO MESSAGE: You don't have permission to create shops. + player.sendMiniMessage(MessageConfig.NO_SHOP_CREATE_PERMISSION, null); return; } @@ -118,7 +121,7 @@ public class PlayerListener extends EventListener { if (numberOfShops >= shopLimit) { event.setCancelled(true); - // TODO MESSAGE: You cannot create this shop as you already have reached the limit. + player.sendMiniMessage(MessageConfig.SHOP_LIMIT_REACHED, Placeholder.parsed("limit", String.valueOf(shopLimit))); return; }