From 251035cf3c2aba79bc36cd487fbfb00c697c9ee0 Mon Sep 17 00:00:00 2001 From: zrdzn Date: Thu, 28 Apr 2022 23:56:02 +0200 Subject: [PATCH] GH-8 Rework messaging system --- build.gradle | 2 +- .../lovelydrop/LovelyDropCommand.java | 15 ++-- .../lovelydrop/LovelyDropPlugin.java | 20 ++++-- .../minecraft/lovelydrop/MessageParser.java | 71 ------------------- .../lovelydrop/drop/DropCommand.java | 14 ++-- .../lovelydrop/drop/DropListener.java | 13 +++- .../lovelydrop/menu/MenuService.java | 13 +++- .../lovelydrop/message/MessageCache.java | 38 ++++++++++ .../lovelydrop/message/MessageLoader.java | 44 ++++++++++++ .../lovelydrop/message/MessageService.java | 48 +++++++++++++ plugin/src/main/resources/config.yml | 7 +- plugin/src/main/resources/plugin.yml | 2 +- 12 files changed, 189 insertions(+), 98 deletions(-) delete mode 100644 plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/MessageParser.java create mode 100644 plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageCache.java create mode 100644 plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageLoader.java create mode 100644 plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageService.java diff --git a/build.gradle b/build.gradle index 74cae3d..dc845cc 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { allprojects { group "io.github.zrdzn.minecraft" - version "1.2.1" + version "1.3.0" } subprojects { diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropCommand.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropCommand.java index a877b75..5f874ae 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropCommand.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropCommand.java @@ -15,6 +15,7 @@ */ package io.github.zrdzn.minecraft.lovelydrop; +import io.github.zrdzn.minecraft.lovelydrop.message.MessageService; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -22,11 +23,11 @@ class LovelyDropCommand implements CommandExecutor { - private final MessageParser messageParser; + private final MessageService messageService; private final LovelyDropPlugin plugin; - public LovelyDropCommand(MessageParser messageParser, LovelyDropPlugin plugin) { - this.messageParser = messageParser; + public LovelyDropCommand(MessageService messageService, LovelyDropPlugin plugin) { + this.messageService = messageService; this.plugin = plugin; } @@ -34,24 +35,24 @@ public LovelyDropCommand(MessageParser messageParser, LovelyDropPlugin plugin) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { if (!sender.hasPermission("lovelydrop.reload")) { - sender.sendMessage(this.messageParser.getNoPermissions()); + this.messageService.send(sender, "no-permissions"); return true; } if (args.length == 0) { - sender.sendMessage(this.messageParser.getNotEnoughArguments()); + this.messageService.send(sender, "not-enough-arguments"); return true; } if (!args[0].equalsIgnoreCase("reload")) { - sender.sendMessage(this.messageParser.getNotValidArgument()); + this.messageService.send(sender, "not-valid-argument"); return true; } this.plugin.getPluginLoader().disablePlugin(this.plugin); this.plugin.getPluginLoader().enablePlugin(this.plugin); - sender.sendMessage(this.messageParser.getPluginReloaded()); + this.messageService.send(sender, "plugin-reloaded"); return true; } diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropPlugin.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropPlugin.java index d59d240..8c05709 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropPlugin.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/LovelyDropPlugin.java @@ -22,6 +22,9 @@ import io.github.zrdzn.minecraft.lovelydrop.menu.Menu; import io.github.zrdzn.minecraft.lovelydrop.menu.MenuParser; import io.github.zrdzn.minecraft.lovelydrop.menu.MenuService; +import io.github.zrdzn.minecraft.lovelydrop.message.MessageCache; +import io.github.zrdzn.minecraft.lovelydrop.message.MessageLoader; +import io.github.zrdzn.minecraft.lovelydrop.message.MessageService; import io.github.zrdzn.minecraft.lovelydrop.user.UserCache; import io.github.zrdzn.minecraft.lovelydrop.user.UserListener; import io.github.zrdzn.minecraft.spigot.EnchantmentMatcher; @@ -46,7 +49,7 @@ public class LovelyDropPlugin extends JavaPlugin { private ItemCache itemCache; private Menu menu; - private MessageParser messageParser; + private MessageService messageService; @Override public void onEnable() { @@ -59,12 +62,13 @@ public void onEnable() { PluginManager pluginManager = this.getServer().getPluginManager(); pluginManager.registerEvents(new UserListener(this.userCache, this.itemCache), this); - pluginManager.registerEvents(new DropListener(this.logger, spigotAdapter, this.itemCache, this.userCache), this); + pluginManager.registerEvents(new DropListener(this.logger, this.messageService, spigotAdapter, this.itemCache, + this.userCache), this); MenuService menuService = new MenuService(this.logger, this.menu, this.userCache); - this.getCommand("lovelydrop").setExecutor(new LovelyDropCommand(this.messageParser, this)); - this.getCommand("drop").setExecutor(new DropCommand(this.messageParser, menuService)); + this.getCommand("lovelydrop").setExecutor(new LovelyDropCommand(this.messageService, this)); + this.getCommand("drop").setExecutor(new DropCommand(this.messageService, menuService)); } @Override @@ -88,8 +92,12 @@ public void loadConfigurations(SpigotAdapter spigotAdapter) { MenuParser menuParser = new MenuParser(this.logger, this.itemCache); this.menu = menuParser.parse(configuration.getConfigurationSection("menu")); - this.messageParser = new MessageParser(); - this.messageParser.parse(configuration.getConfigurationSection("messages")); + MessageCache messageCache = new MessageCache(); + + MessageLoader messageLoader = new MessageLoader(messageCache); + messageLoader.load(configuration.getConfigurationSection("messages")); + + this.messageService = new MessageService(messageCache); } catch (InvalidConfigurationException exception) { this.logger.severe("Something went wrong while parsing configuration/s."); exception.printStackTrace(); diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/MessageParser.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/MessageParser.java deleted file mode 100644 index 4d917f9..0000000 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/MessageParser.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2022 zrdzn - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.github.zrdzn.minecraft.lovelydrop; - -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; - -public class MessageParser { - - private String executedAsConsole; - private String noPermissions; - private String menuOpenError; - private String pluginReloaded; - private String notEnoughArguments; - private String notValidArgument; - - public void parse(ConfigurationSection section) throws InvalidConfigurationException { - if (section == null) { - throw new InvalidConfigurationException("Provided section is null."); - } - - if (!section.getName().equals("messages")) { - throw new InvalidConfigurationException("Provided section is not 'messages'."); - } - - this.executedAsConsole = LovelyDropPlugin.color(section.getString("executed-as-console")); - this.noPermissions = LovelyDropPlugin.color(section.getString("no-permissions")); - this.menuOpenError = LovelyDropPlugin.color(section.getString("menu-open-error")); - this.pluginReloaded = LovelyDropPlugin.color(section.getString("plugin-reloaded")); - this.notEnoughArguments = LovelyDropPlugin.color(section.getString("not-enough-arguments")); - this.notValidArgument = LovelyDropPlugin.color(section.getString("not-valid-argument")); - } - - public String getExecutedAsConsole() { - return this.executedAsConsole; - } - - public String getNoPermissions() { - return this.noPermissions; - } - - public String getMenuOpenError() { - return this.menuOpenError; - } - - public String getPluginReloaded() { - return this.pluginReloaded; - } - - public String getNotEnoughArguments() { - return this.notEnoughArguments; - } - - public String getNotValidArgument() { - return this.notValidArgument; - } - -} diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropCommand.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropCommand.java index c3b4c0c..9eb2b5d 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropCommand.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropCommand.java @@ -15,8 +15,8 @@ */ package io.github.zrdzn.minecraft.lovelydrop.drop; -import io.github.zrdzn.minecraft.lovelydrop.MessageParser; import io.github.zrdzn.minecraft.lovelydrop.menu.MenuService; +import io.github.zrdzn.minecraft.lovelydrop.message.MessageService; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -25,11 +25,11 @@ public class DropCommand implements CommandExecutor { - private final MessageParser messageParser; + private final MessageService messageService; private final MenuService menuService; - public DropCommand(MessageParser messageParser, MenuService menuService) { - this.messageParser = messageParser; + public DropCommand(MessageService messageService, MenuService menuService) { + this.messageService = messageService; this.menuService = menuService; } @@ -38,20 +38,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command String[] args) { // Check if sender is console. if (!(sender instanceof Player)) { - sender.sendMessage(this.messageParser.getExecutedAsConsole()); + this.messageService.send(sender, "executed-as-console"); return true; } Player player = (Player) sender; if (!player.hasPermission("lovelydrop.menu.open")) { - player.sendMessage(this.messageParser.getNoPermissions()); + this.messageService.send(sender, "no-permissions"); return true; } // Try to open the inventory. if (!this.menuService.open(player)) { - player.sendMessage(this.messageParser.getMenuOpenError()); + this.messageService.send(sender, "menu-open-error"); return true; } diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java index 72ca4ea..d1117ee 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java @@ -17,6 +17,7 @@ import io.github.zrdzn.minecraft.lovelydrop.item.Item; import io.github.zrdzn.minecraft.lovelydrop.item.ItemCache; +import io.github.zrdzn.minecraft.lovelydrop.message.MessageService; import io.github.zrdzn.minecraft.lovelydrop.user.User; import io.github.zrdzn.minecraft.lovelydrop.user.UserCache; import io.github.zrdzn.minecraft.spigot.SpigotAdapter; @@ -42,12 +43,15 @@ public class DropListener implements Listener { private final Logger logger; + private final MessageService messageService; private final SpigotAdapter adapter; private final ItemCache itemCache; private final UserCache userCache; - public DropListener(Logger logger, SpigotAdapter adapter, ItemCache itemCache, UserCache userCache) { + public DropListener(Logger logger, MessageService messageService, SpigotAdapter adapter, ItemCache itemCache, + UserCache userCache) { this.logger = logger; + this.messageService = messageService; this.adapter = adapter; this.itemCache = itemCache; this.userCache = userCache; @@ -97,9 +101,14 @@ public void onSourceBreak(BlockBreakEvent event) { amount = random.nextInt(item.getAmount().getKey(), item.getAmount().getValue()); } + String itemName = item.getDisplayName(); + + String[] placeholders = { "{DROP}", itemName, "{AMOUNT}", String.valueOf(amount) }; + this.messageService.send(player, "drop-successful", placeholders); + ItemStack droppedItem = new ItemStack(item.getType(), amount); ItemMeta droppedItemMeta = droppedItem.getItemMeta(); - droppedItemMeta.setDisplayName(item.getDisplayName()); + droppedItemMeta.setDisplayName(itemName); droppedItemMeta.setLore(item.getLore()); droppedItem.setItemMeta(droppedItemMeta); diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java index 8f1916e..c46c2a4 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java @@ -20,6 +20,7 @@ import dev.triumphteam.gui.guis.Gui; import dev.triumphteam.gui.guis.GuiItem; import io.github.zrdzn.minecraft.lovelydrop.item.Item; +import io.github.zrdzn.minecraft.lovelydrop.message.MessageService; import io.github.zrdzn.minecraft.lovelydrop.user.User; import io.github.zrdzn.minecraft.lovelydrop.user.UserCache; import org.bukkit.entity.Player; @@ -36,11 +37,13 @@ public class MenuService { private final Logger logger; + private final MessageService messageService; private final Menu menu; private final UserCache userCache; - public MenuService(Logger logger, Menu menu, UserCache userCache) { + public MenuService(Logger logger, MessageService messageService, Menu menu, UserCache userCache) { this.logger = logger; + this.messageService = messageService; this.menu = menu; this.userCache = userCache; } @@ -122,6 +125,8 @@ public boolean open(Player player) { Entry dropSwitch = this.menu.getDropSwitch(); Entry inventoryDropSwitch = this.menu.getInventoryDropSwitch(); + String dropName = dropItem.getDisplayName(); + ItemBuilder menuItemBuilder = ItemBuilder.from(item.getType()) .setName(item.getDisplayName()) .setLore(lore.stream() @@ -157,8 +162,11 @@ public boolean open(Player player) { } else { user.disableDrop(dropItem); } + + this.messageService.send(player, "drop-switched", "{DROP}", dropName); } else if (action == MenuAction.SWITCH_DROP_TO_INVENTORY) { user.switchInventoryDrop(itemId, !user.hasSwitchedInventoryDrop(itemId)); + this.messageService.send(player, "drop-switched-inventory", "{DROP}", dropName); } } } else { @@ -172,8 +180,11 @@ public boolean open(Player player) { } else { user.disableDrop(dropItem); } + + this.messageService.send(player, "drop-switched", "{DROP}", dropName); } else if (action == MenuAction.SWITCH_DROP_TO_INVENTORY) { user.switchInventoryDrop(itemId, !user.hasSwitchedInventoryDrop(itemId)); + this.messageService.send(player, "drop-switched-inventory", "{DROP}", dropName); } } diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageCache.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageCache.java new file mode 100644 index 0000000..804f9ce --- /dev/null +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageCache.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 zrdzn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.zrdzn.minecraft.lovelydrop.message; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class MessageCache { + + private final Map messages = new HashMap<>(); + + public void addMessage(String key, String content) { + this.messages.put(key, content); + } + + public String getMessage(String key) { + return this.messages.getOrDefault(key, MessageLoader.DEFAULT_MESSAGE); + } + + public Map getMessages() { + return Collections.unmodifiableMap(this.messages); + } + +} diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageLoader.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageLoader.java new file mode 100644 index 0000000..7be6199 --- /dev/null +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageLoader.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022-2022 zrdzn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.zrdzn.minecraft.lovelydrop.message; + +import io.github.zrdzn.minecraft.lovelydrop.LovelyDropPlugin; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.InvalidConfigurationException; + +public class MessageLoader { + + public static final String DEFAULT_MESSAGE = ""; + + private final MessageCache cache; + + public MessageLoader(MessageCache cache) { + this.cache = cache; + } + + public void load(ConfigurationSection section) throws InvalidConfigurationException { + if (section == null) { + throw new InvalidConfigurationException("Provided section is null."); + } + + if (!section.getName().equals("messages")) { + throw new InvalidConfigurationException("Provided section is not 'messages'."); + } + + section.getKeys(false).forEach(key -> this.cache.addMessage(key, LovelyDropPlugin.color(section.getString(key)))); + } + +} diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageService.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageService.java new file mode 100644 index 0000000..24798ed --- /dev/null +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/message/MessageService.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 zrdzn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.zrdzn.minecraft.lovelydrop.message; + +import org.bukkit.command.CommandSender; + +import java.util.concurrent.CompletableFuture; + +public class MessageService { + + private final MessageCache cache; + + public MessageService(MessageCache cache) { + this.cache = cache; + } + + public CompletableFuture send(CommandSender receiver, String key, String... placeholders) { + return CompletableFuture.runAsync(() -> { + String message = this.cache.getMessage(key); + + int length = placeholders.length; + if (length <= 0 || length % 2 != 0) { + receiver.sendMessage(message); + return; + } + + for (int index = 0; index < length; index += 2) { + message = message.replace(placeholders[index], placeholders[index + 1]); + } + + receiver.sendMessage(message); + }); + } + +} diff --git a/plugin/src/main/resources/config.yml b/plugin/src/main/resources/config.yml index 3b9431c..8175fc4 100644 --- a/plugin/src/main/resources/config.yml +++ b/plugin/src/main/resources/config.yml @@ -329,8 +329,8 @@ drops: experience: 135 - -# All messages that are sent by plugin to players. +# All messages that can be sent by plugin to players. +# If you want to disable specific messages just place a '#' before them. messages: executed-as-console: "&cYou cannot execute this command as console." no-permissions: "&cYou do not have enough permissions to perform that action." @@ -338,3 +338,6 @@ messages: plugin-reloaded: "&aPlugin has been reloaded." not-enough-arguments: "&cYou need to provide arguments." not-valid-argument: "&cYou have provided wrong argument. &eUse /lovelydrop reload." + drop-switched: "&aYou have switched the {DROP} drop." + drop-switched-inventory: "&aYou have switched the {DROP} drop to inventory." + drop-successful: "&aYou have dropped {AMOUNT}x {DROP}!" diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index 516bbfe..7adb66f 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: LovelyDrop -version: 1.2.1 +version: 1.3.0 main: io.github.zrdzn.minecraft.lovelydrop.LovelyDropPlugin api-version: 1.13 author: zrdzn