From ab7bd628f37d78fe624c824acc7bc7e31a20e6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berke=20Ak=C3=A7en?= Date: Sun, 18 Feb 2024 12:55:32 +0300 Subject: [PATCH] Some improvements for setup menu --- .../despical/kotl/handlers/ChatManager.java | 10 +-- .../kotl/handlers/PlaceholderManager.java | 3 +- .../handlers/setup/AbstractComponent.java | 64 +++++++++++++++++++ .../kotl/handlers/setup/SetupInventory.java | 51 ++++----------- .../components/ArenaOptionComponents.java | 32 ++++------ .../setup/components/MainMenuComponents.java | 29 +++++---- .../components/PressurePlateComponents.java | 14 +++- 7 files changed, 126 insertions(+), 77 deletions(-) create mode 100644 src/main/java/me/despical/kotl/handlers/setup/AbstractComponent.java diff --git a/src/main/java/me/despical/kotl/handlers/ChatManager.java b/src/main/java/me/despical/kotl/handlers/ChatManager.java index ca5b4e1..1ea9873 100644 --- a/src/main/java/me/despical/kotl/handlers/ChatManager.java +++ b/src/main/java/me/despical/kotl/handlers/ChatManager.java @@ -36,17 +36,17 @@ */ public class ChatManager { + private String prefix; + private FileConfiguration config; + private final Main plugin; private final boolean papiEnabled; - private FileConfiguration config; - private String prefix; - public ChatManager(Main plugin) { this.plugin = plugin; - this.config = ConfigUtils.getConfig(plugin, "messages"); - this.prefix = message("in_game.plugin_prefix"); this.papiEnabled = plugin.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI"); + this.prefix = message("in_game.plugin_prefix"); + this.config = ConfigUtils.getConfig(plugin, "messages"); } public String coloredRawMessage(String message) { diff --git a/src/main/java/me/despical/kotl/handlers/PlaceholderManager.java b/src/main/java/me/despical/kotl/handlers/PlaceholderManager.java index 31074d9..b6f864e 100644 --- a/src/main/java/me/despical/kotl/handlers/PlaceholderManager.java +++ b/src/main/java/me/despical/kotl/handlers/PlaceholderManager.java @@ -35,8 +35,7 @@ public class PlaceholderManager extends PlaceholderExpansion { public PlaceholderManager(Main plugin) { this.plugin = plugin; - - register(); + super.register(); } @Override diff --git a/src/main/java/me/despical/kotl/handlers/setup/AbstractComponent.java b/src/main/java/me/despical/kotl/handlers/setup/AbstractComponent.java new file mode 100644 index 0000000..9d53482 --- /dev/null +++ b/src/main/java/me/despical/kotl/handlers/setup/AbstractComponent.java @@ -0,0 +1,64 @@ +/* + * KOTL - Don't let others climb to top of the ladders! + * Copyright (C) 2024 Berke Akçen + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.despical.kotl.handlers.setup; + +import me.despical.commons.compat.XMaterial; +import me.despical.commons.item.ItemBuilder; +import me.despical.commons.serializer.LocationSerializer; +import me.despical.kotl.Main; +import me.despical.kotl.handlers.ChatManager; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; + +/** + * @author Despical + *

+ * Created at 18.02.2024 + */ +public abstract class AbstractComponent { + + protected static final ItemStack mainMenuItem; + protected static boolean supportsParticle; + + protected final Main plugin; + protected final ChatManager chatManager; + + static { + mainMenuItem = new ItemBuilder(XMaterial.REDSTONE).name("&c&lReturn KOTL Menu").lore("&7Click to return last page!").build(); + + try { + Class.forName("org.bukkit.Particle"); + + supportsParticle = true; + } catch (ClassNotFoundException exception) { + supportsParticle = false; + } + } + + public AbstractComponent(Main plugin) { + this.plugin = plugin; + this.chatManager = plugin.getChatManager(); + } + + public abstract void injectComponents(SetupInventory inventory); + + protected final String isOptionDoneBool(FileConfiguration config, String path) { + return config.isSet(path) ? LocationSerializer.isDefaultLocation(config.getString(path)) ? "&c&l✘ Not Completed" : "&a&l✔ Completed" : "&c&l✘ Not Completed"; + } +} diff --git a/src/main/java/me/despical/kotl/handlers/setup/SetupInventory.java b/src/main/java/me/despical/kotl/handlers/setup/SetupInventory.java index f500f0a..0697da8 100644 --- a/src/main/java/me/despical/kotl/handlers/setup/SetupInventory.java +++ b/src/main/java/me/despical/kotl/handlers/setup/SetupInventory.java @@ -49,13 +49,14 @@ public class SetupInventory { private final Player player; private PaginatedPane paginatedPane; + private StaticPane pane; public static final String TUTORIAL_VIDEO = "https://www.youtube.com/watch?v=O_vkf_J4OgY"; - public SetupInventory(Arena arena, Player player) { + public SetupInventory(Main plugin, Arena arena, Player player) { + this.plugin = plugin; this.arena = arena; this.player = player; - this.plugin = JavaPlugin.getPlugin(Main.class); prepareGui(); } @@ -76,14 +77,14 @@ private void prepareGui() { } private void prepareComponents(StaticPane pane) { - final var spawnComponents = new MainMenuComponents(); - spawnComponents.injectComponents(this, pane); + AbstractComponent spawnComponents = new MainMenuComponents(plugin); + spawnComponents.injectComponents(this); - final var pressurePlateComponents = new PressurePlateComponents(); - pressurePlateComponents.injectComponents(this, pane); + AbstractComponent pressurePlateComponents = new PressurePlateComponents(plugin); + pressurePlateComponents.injectComponents(this); - final var arenaOptionComponents = new ArenaOptionComponents(); - arenaOptionComponents.injectComponents(this, pane); + AbstractComponent arenaOptionComponents = new ArenaOptionComponents(plugin); + arenaOptionComponents.injectComponents(this); } public void openInventory() { @@ -110,6 +111,10 @@ public PaginatedPane getPaginatedPane() { return paginatedPane; } + public StaticPane getPane() { + return pane; + } + public void setPage(String title, int rows, int page) { this.gui.setTitle(title != null ? title : this.gui.getTitle()); this.gui.setRows(rows); @@ -118,34 +123,6 @@ public void setPage(String title, int rows, int page) { } public void restorePage() { - new LastPageCache(this, " KOTL Arena Editor", 4, 0).restore(); - } - - public interface SetupComponent { - - Main plugin = JavaPlugin.getPlugin(Main.class); - ChatManager chatManager = plugin.getChatManager(); - FileConfiguration config = ConfigUtils.getConfig(plugin, "arenas"); - ItemStack mainMenuItem = new ItemBuilder(XMaterial.REDSTONE).name("&c&lReturn KOTL Menu").lore("&7Click to return last page!").build(); - - void injectComponents(SetupInventory setup, StaticPane pane); - - default void saveConfig() { - ConfigUtils.saveConfig(plugin, config, "arenas"); - } - - default String isOptionDoneBool(String path) { - return config.isSet(path) ? LocationSerializer.isDefaultLocation(config.getString(path)) ? "&c&l✘ Not Completed" : "&a&l✔ Completed" : "&c&l✘ Not Completed"; - } - } - - private record LastPageCache(SetupInventory setup, String title, int rows, int page) { - - void restore() { - setup.paginatedPane.setPage(page); - setup.gui.setRows(rows); - setup.gui.setTitle(title); - setup.gui.update(); - } + this.setPage(" KOTL Arena Editor", 4, 0); } } \ No newline at end of file diff --git a/src/main/java/me/despical/kotl/handlers/setup/components/ArenaOptionComponents.java b/src/main/java/me/despical/kotl/handlers/setup/components/ArenaOptionComponents.java index 84122eb..3c522b5 100644 --- a/src/main/java/me/despical/kotl/handlers/setup/components/ArenaOptionComponents.java +++ b/src/main/java/me/despical/kotl/handlers/setup/components/ArenaOptionComponents.java @@ -19,9 +19,12 @@ package me.despical.kotl.handlers.setup.components; import me.despical.commons.compat.XMaterial; +import me.despical.commons.configuration.ConfigUtils; import me.despical.commons.item.ItemBuilder; import me.despical.inventoryframework.GuiItem; import me.despical.inventoryframework.pane.StaticPane; +import me.despical.kotl.Main; +import me.despical.kotl.handlers.setup.AbstractComponent; import me.despical.kotl.handlers.setup.SetupInventory; /** @@ -29,41 +32,32 @@ *

* Created at 30.09.2023 */ -public class ArenaOptionComponents implements SetupInventory.SetupComponent { +public class ArenaOptionComponents extends AbstractComponent { - private static boolean supportsParticle; + public ArenaOptionComponents(Main plugin) { + super(plugin); + } @Override - public void injectComponents(SetupInventory setup, StaticPane pane) { + public void injectComponents(SetupInventory setup) { final var arena = setup.getArena(); + final var config = ConfigUtils.getConfig(plugin, "arenas"); final var path = "instances.%s.".formatted(arena.getId()); final var arenaOptions = new StaticPane(9, 3); - final var outlineItem = supportsParticle ? new ItemBuilder(arena.isShowOutlines() ? XMaterial.ENDER_PEARL : XMaterial.ENDER_EYE).name(" " + (arena.isShowOutlines() ? "&c&lDisable" : "&e&lEnable") + " Outline Particles ").lore("&7You can create particles around the game arena.") : new ItemBuilder(XMaterial.BARREL).name("&c&lYour server does not support Particles!"); arenaOptions.fillWith(new ItemBuilder(XMaterial.BLACK_STAINED_GLASS_PANE).name("&7Current plate: &a" + arena.getArenaPlate().toString()).build()); - - setup.getPaginatedPane().addPane(3, arenaOptions); - + arenaOptions.addItem(GuiItem.of(mainMenuItem, event -> setup.restorePage()), 8, 2); arenaOptions.addItem(GuiItem.of(outlineItem.build(), e -> { arena.setShowOutlines(!arena.isShowOutlines()); config.set(path + "showOutlines", arena.isShowOutlines()); - saveConfig(); + ConfigUtils.saveConfig(plugin, config, "arenas"); + setup.getPlayer().closeInventory(); }), 4, 1); - arenaOptions.addItem(GuiItem.of(mainMenuItem, event -> setup.restorePage()), 8, 2); - } - - static { - try { - Class.forName("org.bukkit.Particle"); - - supportsParticle = true; - } catch (ClassNotFoundException exception) { - supportsParticle = false; - } + setup.getPaginatedPane().addPane(3, arenaOptions); } } \ No newline at end of file diff --git a/src/main/java/me/despical/kotl/handlers/setup/components/MainMenuComponents.java b/src/main/java/me/despical/kotl/handlers/setup/components/MainMenuComponents.java index 0be913b..e105baf 100644 --- a/src/main/java/me/despical/kotl/handlers/setup/components/MainMenuComponents.java +++ b/src/main/java/me/despical/kotl/handlers/setup/components/MainMenuComponents.java @@ -20,11 +20,13 @@ import me.despical.commons.ReflectionUtils; import me.despical.commons.compat.XMaterial; +import me.despical.commons.configuration.ConfigUtils; import me.despical.commons.item.ItemBuilder; import me.despical.commons.serializer.LocationSerializer; import me.despical.inventoryframework.GuiItem; -import me.despical.inventoryframework.pane.StaticPane; +import me.despical.kotl.Main; import me.despical.kotl.arena.managers.schedulers.ArenaScheduler; +import me.despical.kotl.handlers.setup.AbstractComponent; import me.despical.kotl.handlers.setup.SetupInventory; import org.bukkit.Material; import org.bukkit.block.BlockFace; @@ -36,13 +38,19 @@ *

* Created at 22.06.2020 */ -public class MainMenuComponents implements SetupInventory.SetupComponent { +public class MainMenuComponents extends AbstractComponent { + + public MainMenuComponents(Main plugin) { + super(plugin); + } @Override - public void injectComponents(SetupInventory setup, StaticPane pane) { + public void injectComponents(SetupInventory setup) { final var player = setup.getPlayer(); + final var config = ConfigUtils.getConfig(plugin, "arenas"); final var arena = setup.getArena(); final var path = "instances.%s.".formatted(arena.getId()); + final var pane = setup.getPane(); final var optionsItem = new ItemBuilder(XMaterial.CLOCK).name("&e&l Additional Options").lore("&7Click to open additional options menu.").enchantment(Enchantment.ARROW_INFINITE).flag(ItemFlag.HIDE_ENCHANTS); pane.addItem(GuiItem.of(optionsItem.build(), event -> setup.setPage(" Set Additional Arena Options", 3, 3)), 4, 2); @@ -53,7 +61,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { .lore("&7the place where you are standing.") .lore("&8(location where players will be") .lore("&8teleported after the reloading)") - .lore("", isOptionDoneBool(path + "endLocation")) + .lore("", isOptionDoneBool(config, path + "endLocation")) .build(), e -> { player.closeInventory(); @@ -64,7 +72,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { arena.setEndLocation(location); config.set(path + "endLocation", LocationSerializer.toString(location)); - saveConfig(); + ConfigUtils.saveConfig(plugin, config, "arenas"); }), 1, 1); pane.addItem(GuiItem.of(new ItemBuilder(arena.getArenaPlate()) @@ -73,7 +81,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { .lore("&7the place where you are standing.") .lore("&8(location where players will try to") .lore("&8reach)") - .lore("", isOptionDoneBool(path + "plateLocation")) + .lore("", isOptionDoneBool(config, path + "plateLocation")) .build(), e -> { player.closeInventory(); @@ -85,7 +93,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { player.sendMessage(chatManager.coloredRawMessage("&e✔ Completed | &aPlate location for arena &e" + arena.getId() + " &aset at your location!")); config.set(path + "plateLocation", LocationSerializer.toString(location.getBlock().getRelative(BlockFace.DOWN).getLocation())); - saveConfig(); + ConfigUtils.saveConfig(plugin, config, "arenas"); }), 5, 1); pane.addItem(GuiItem.of(new ItemBuilder(XMaterial.BLAZE_ROD.parseItem()) @@ -93,7 +101,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { .lore("&7Click to set arena's region") .lore("&7with the cuboid selector.") .lore("&8(area where game will be playing)") - .lore("", isOptionDoneBool(path + "areaMax")) + .lore("", isOptionDoneBool(config, path + "areaMax")) .build(), e -> { player.closeInventory(); @@ -110,6 +118,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { config.set(path + "areaMin", LocationSerializer.toString(selection.firstPos())); config.set(path + "areaMax", LocationSerializer.toString(selection.secondPos())); + ConfigUtils.saveConfig(plugin, config, "arenas"); arena.setMinCorner(selection.firstPos()); arena.setMaxCorner(selection.secondPos()); @@ -118,8 +127,6 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { selector.removeSelection(player); arena.handleOutlines(); - - saveConfig(); }), 3, 1); pane.addItem(GuiItem.of(new ItemBuilder(XMaterial.ENCHANTED_BOOK) @@ -185,7 +192,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { player.sendMessage(chatManager.coloredRawMessage("&a&l✔ &aValidation succeeded! Registering new arena instance: &e" + arena.getId())); config.set(path + "isdone", true); - saveConfig(); + ConfigUtils.saveConfig(plugin, config, "arenas"); }), 8, 3); } } \ No newline at end of file diff --git a/src/main/java/me/despical/kotl/handlers/setup/components/PressurePlateComponents.java b/src/main/java/me/despical/kotl/handlers/setup/components/PressurePlateComponents.java index cdd73ab..7908024 100644 --- a/src/main/java/me/despical/kotl/handlers/setup/components/PressurePlateComponents.java +++ b/src/main/java/me/despical/kotl/handlers/setup/components/PressurePlateComponents.java @@ -20,10 +20,13 @@ import me.despical.commons.ReflectionUtils; import me.despical.commons.compat.XMaterial; +import me.despical.commons.configuration.ConfigUtils; import me.despical.commons.item.ItemBuilder; import me.despical.commons.serializer.LocationSerializer; import me.despical.inventoryframework.GuiItem; import me.despical.inventoryframework.pane.StaticPane; +import me.despical.kotl.Main; +import me.despical.kotl.handlers.setup.AbstractComponent; import me.despical.kotl.handlers.setup.SetupInventory; import org.bukkit.block.BlockFace; import org.bukkit.enchantments.Enchantment; @@ -38,13 +41,18 @@ *

* Created at 30.09.2022 */ -public class PressurePlateComponents implements SetupInventory.SetupComponent { +public class PressurePlateComponents extends AbstractComponent { + + public PressurePlateComponents(Main plugin) { + super(plugin); + } @Override - public void injectComponents(SetupInventory setup, StaticPane pane) { + public void injectComponents(SetupInventory setup) { final var player = setup.getPlayer(); final var arena = setup.getArena(); final var path = "instances.%s.".formatted(arena.getId()); + final var config = ConfigUtils.getConfig(plugin, "arenas"); final var pressurePlatesPane = new StaticPane(9, 6); pressurePlatesPane.fillWith(new ItemBuilder(XMaterial.BLACK_STAINED_GLASS_PANE).name("&7Current plate: &a" + arena.getArenaPlate().toString()).build()); @@ -89,7 +97,7 @@ public void injectComponents(SetupInventory setup, StaticPane pane) { player.closeInventory(); config.set(path + "arenaPlate", plate.name()); - saveConfig(); + ConfigUtils.saveConfig(plugin, config, "arenas"); player.sendMessage(chatManager.coloredRawMessage("&e✔ Completed | &aArena plate for arena &e" + arena.getId() + " &achanged to &e" + plate));