Skip to content

Commit

Permalink
Some improvements for setup menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Feb 18, 2024
1 parent fa96551 commit ab7bd62
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 77 deletions.
10 changes: 5 additions & 5 deletions src/main/java/me/despical/kotl/handlers/ChatManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public class PlaceholderManager extends PlaceholderExpansion {

public PlaceholderManager(Main plugin) {
this.plugin = plugin;

register();
super.register();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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
* <p>
* 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";
}
}
51 changes: 14 additions & 37 deletions src/main/java/me/despical/kotl/handlers/setup/SetupInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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() {
Expand All @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,45 @@
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;

/**
* @author Despical
* <p>
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,13 +38,19 @@
* <p>
* 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);
Expand All @@ -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();
Expand All @@ -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())
Expand All @@ -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();
Expand All @@ -85,15 +93,15 @@ 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())
.name("&e&l Set Arena Region ")
.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();
Expand All @@ -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());
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
}
Loading

0 comments on commit ab7bd62

Please sign in to comment.