From 4cfe1318b3f6495df7939ae62ef7cdf135ff0505 Mon Sep 17 00:00:00 2001 From: Terry <68700354+teraprath@users.noreply.github.com> Date: Thu, 11 May 2023 18:18:20 +0200 Subject: [PATCH] Update module items & inventory configuration --- pom.xml | 2 +- .../java/net/spigotcloud/lobby/Lobby.java | 110 +++++++++++++++ .../net/spigotcloud/lobby/api/LobbyAPI.java | 12 ++ .../lobby/command/BaseCommand.java | 77 +++++++++++ .../lobby/command/BuildCommand.java | 44 ++++++ .../lobby/command/HelpCommand.java | 31 +++++ .../lobby/command/ModulesCommand.java | 33 +++++ .../lobby/command/ReloadCommand.java | 31 +++++ .../lobby/command/SetSpawnCommand.java | 41 ++++++ .../spigotcloud/lobby/command/SubCommand.java | 28 ++++ .../lobby/handler/PlayerHandler.java | 26 ++++ .../lobby/listener/ItemListener.java | 30 ++++ .../lobby/listener/JoinQuitListener.java | 27 ++++ .../lobby/listener/ProtectListener.java | 69 ++++++++++ .../lobby/manager/FileManager.java | 41 ++++++ .../lobby/manager/InventoryManager.java | 44 ++++++ .../lobby/manager/LanguageManager.java | 83 +++++++++++ .../lobby/manager/LocationManager.java | 39 ++++++ .../net/spigotcloud/lobby/module/Module.java | 83 +++++++++++ .../lobby/module/ModuleDescriptionFile.java | 28 ++++ .../spigotcloud/lobby/module/ModuleItem.java | 50 +++++++ .../lobby/module/ModuleLoader.java | 130 ++++++++++++++++++ src/main/resources/plugin.yml | 13 ++ 23 files changed, 1071 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/spigotcloud/lobby/Lobby.java create mode 100644 src/main/java/net/spigotcloud/lobby/api/LobbyAPI.java create mode 100644 src/main/java/net/spigotcloud/lobby/command/BaseCommand.java create mode 100644 src/main/java/net/spigotcloud/lobby/command/BuildCommand.java create mode 100644 src/main/java/net/spigotcloud/lobby/command/HelpCommand.java create mode 100644 src/main/java/net/spigotcloud/lobby/command/ModulesCommand.java create mode 100644 src/main/java/net/spigotcloud/lobby/command/ReloadCommand.java create mode 100644 src/main/java/net/spigotcloud/lobby/command/SetSpawnCommand.java create mode 100644 src/main/java/net/spigotcloud/lobby/command/SubCommand.java create mode 100644 src/main/java/net/spigotcloud/lobby/handler/PlayerHandler.java create mode 100644 src/main/java/net/spigotcloud/lobby/listener/ItemListener.java create mode 100644 src/main/java/net/spigotcloud/lobby/listener/JoinQuitListener.java create mode 100644 src/main/java/net/spigotcloud/lobby/listener/ProtectListener.java create mode 100644 src/main/java/net/spigotcloud/lobby/manager/FileManager.java create mode 100644 src/main/java/net/spigotcloud/lobby/manager/InventoryManager.java create mode 100644 src/main/java/net/spigotcloud/lobby/manager/LanguageManager.java create mode 100644 src/main/java/net/spigotcloud/lobby/manager/LocationManager.java create mode 100644 src/main/java/net/spigotcloud/lobby/module/Module.java create mode 100644 src/main/java/net/spigotcloud/lobby/module/ModuleDescriptionFile.java create mode 100644 src/main/java/net/spigotcloud/lobby/module/ModuleItem.java create mode 100644 src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java create mode 100644 src/main/resources/plugin.yml diff --git a/pom.xml b/pom.xml index e149c4b..6736417 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.spigotcloud lobby - 1.4.0-STABLE + 1.4.2-STABLE jar Lobby diff --git a/src/main/java/net/spigotcloud/lobby/Lobby.java b/src/main/java/net/spigotcloud/lobby/Lobby.java new file mode 100644 index 0000000..ce31cf0 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/Lobby.java @@ -0,0 +1,110 @@ +package net.spigotcloud.lobby; + +import net.spigotcloud.lobby.command.*; +import net.spigotcloud.lobby.listener.ItemListener; +import net.spigotcloud.lobby.listener.JoinQuitListener; +import net.spigotcloud.lobby.listener.ProtectListener; +import net.spigotcloud.lobby.manager.InventoryManager; +import net.spigotcloud.lobby.manager.LanguageManager; +import net.spigotcloud.lobby.manager.LocationManager; +import net.spigotcloud.lobby.module.ModuleLoader; +import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.util.HashSet; +import java.util.Set; + +public final class Lobby extends JavaPlugin { + + private static Lobby instance; + private final BaseCommand command = new BaseCommand(); + private final Set buildPlayers = new HashSet<>(); + + private ModuleLoader moduleLoader; + private LanguageManager languageManager; + private LocationManager locationManager; + private InventoryManager inventoryManager; + + @Override + public void onEnable() { + + instance = this; + + this.moduleLoader = new ModuleLoader(); + + this.languageManager = new LanguageManager(new File(getDataFolder(), "language.yml")); + this.locationManager = new LocationManager(new File(getDataFolder(), "locations.yml")); + languageManager.reload(); + locationManager.reload(); + + registerCommands(); + registerEvents(); + + + moduleLoader.reload(); + + this.inventoryManager = new InventoryManager(new File(getDataFolder(), "inventory.yml")); + inventoryManager.reload(); + + log(); + } + + @Override + public void onDisable() { + moduleLoader.disable(); + } + + private void registerCommands() { + getCommand("lobby").setExecutor(this.command); + command.registerSubCommand("help", new HelpCommand()); + command.registerSubCommand("reload", new ReloadCommand()); + command.registerSubCommand("modules", new ModulesCommand()); + command.registerSubCommand("build", new BuildCommand()); + command.registerSubCommand("setspawn", new SetSpawnCommand()); + } + + private void registerEvents() { + final PluginManager pm = getServer().getPluginManager(); + pm.registerEvents(new ItemListener(), this); + pm.registerEvents(new JoinQuitListener(), this); + pm.registerEvents(new ProtectListener(), this); + } + + + private void log() { + getLogger().info("Registered " + moduleLoader.getModules().size() + " modules."); + getLogger().info("Registered " + getBaseCommand().getSubCommands().size() + " commands."); + getLogger().info("Registered " + getInventoryManager().getItems().size() + " items."); + } + + public BaseCommand getBaseCommand() { + return this.command; + } + + + public ModuleLoader getModuleLoader() { + return this.moduleLoader; + } + + public Set getBuildPlayers() { + return this.buildPlayers; + } + + public LanguageManager getLanguageManager() { + return languageManager; + } + + public LocationManager getLocationManager() { + return locationManager; + } + + public InventoryManager getInventoryManager() { return inventoryManager; } + + public static Lobby getInstance() { + return instance; + } + + +} diff --git a/src/main/java/net/spigotcloud/lobby/api/LobbyAPI.java b/src/main/java/net/spigotcloud/lobby/api/LobbyAPI.java new file mode 100644 index 0000000..26888ab --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/api/LobbyAPI.java @@ -0,0 +1,12 @@ +package net.spigotcloud.lobby.api; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +public class LobbyAPI { + + public static Plugin getPlugin() { + return Bukkit.getServer().getPluginManager().getPlugin("Lobby"); + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/command/BaseCommand.java b/src/main/java/net/spigotcloud/lobby/command/BaseCommand.java new file mode 100644 index 0000000..3d94e17 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/command/BaseCommand.java @@ -0,0 +1,77 @@ +package net.spigotcloud.lobby.command; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import javax.annotation.Nonnull; +import java.util.*; + +public class BaseCommand implements CommandExecutor, TabCompleter { + + private final HashMap subCommands; + + public BaseCommand() { + this.subCommands = new HashMap<>(); + } + + public Map getSubCommands() { + return this.subCommands; + } + + public void registerSubCommand(@Nonnull String command, @Nonnull SubCommand subCommand) { + this.subCommands.put(command, subCommand); + } + + public void unregister(@Nonnull String command) { + this.subCommands.remove(command); + } + + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + + if (args.length > 0) { + + if (subCommands.containsKey(args[0])) { + SubCommand subCommand = subCommands.get(args[0]); + if (sender.hasPermission(subCommand.getPermission())) { + subCommand.onCommand(sender, command, label, args); + } else { + sender.sendMessage(Lobby.getInstance().getLanguageManager().getNoPermission(subCommand.getPermission())); + } + } else { + sender.sendMessage(Lobby.getInstance().getLanguageManager().getWrongUsage("help")); + } + + } else { + sender.sendMessage(Lobby.getInstance().getLanguageManager().getWrongUsage("help")); + } + + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + + ArrayList list = new ArrayList<>(); + String current = args[args.length - 1].toLowerCase(); + + + if (args.length == 1) { + subCommands.forEach(((s, subCommand) -> { + list.add(s.toLowerCase()); + })); + } else { + SubCommand subCommand = subCommands.get(args[0]); + if (subCommand != null) { + return subCommand.onTabComplete(sender, command, label, args); + } + } + + list.removeIf(s -> !s.toLowerCase().startsWith(current)); + return list; + } +} diff --git a/src/main/java/net/spigotcloud/lobby/command/BuildCommand.java b/src/main/java/net/spigotcloud/lobby/command/BuildCommand.java new file mode 100644 index 0000000..528b281 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/command/BuildCommand.java @@ -0,0 +1,44 @@ +package net.spigotcloud.lobby.command; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.GameMode; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.List; + +public class BuildCommand extends SubCommand { + + public BuildCommand() { + super("build", "Toggle build mode"); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + + if (!(sender instanceof Player)) { + sender.sendMessage(Lobby.getInstance().getLanguageManager().getNoPlayerInstance()); + return false; + } + + final Player player = (Player) sender; + + if (Lobby.getInstance().getBuildPlayers().contains(player)) { + Lobby.getInstance().getBuildPlayers().remove(player); + player.setGameMode(GameMode.SURVIVAL); + player.sendMessage(Lobby.getInstance().getLanguageManager().getMessage("build_mode_on")); + } else { + Lobby.getInstance().getBuildPlayers().add(player); + player.setGameMode(GameMode.CREATIVE); + player.sendMessage(Lobby.getInstance().getLanguageManager().getMessage("build_mode_off")); + } + + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + return null; + } +} diff --git a/src/main/java/net/spigotcloud/lobby/command/HelpCommand.java b/src/main/java/net/spigotcloud/lobby/command/HelpCommand.java new file mode 100644 index 0000000..076f715 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/command/HelpCommand.java @@ -0,0 +1,31 @@ +package net.spigotcloud.lobby.command; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.PluginDescriptionFile; + +import java.util.List; + +public class HelpCommand extends SubCommand { + + public HelpCommand() { + super("help", "Show this page"); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + PluginDescriptionFile desc = Lobby.getInstance().getDescription(); + sender.sendMessage(""); + Lobby.getInstance().getBaseCommand().getSubCommands().forEach((cmd, subCommand) -> { + sender.sendMessage(Lobby.getInstance().getLanguageManager().getMessage("help_list").replace("%command%", cmd).replace("%description%", subCommand.getDescription()).replace("%permission%", subCommand.getPermission())); + }); + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + return null; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/command/ModulesCommand.java b/src/main/java/net/spigotcloud/lobby/command/ModulesCommand.java new file mode 100644 index 0000000..f786927 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/command/ModulesCommand.java @@ -0,0 +1,33 @@ +package net.spigotcloud.lobby.command; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.PluginDescriptionFile; + +import java.util.List; + +public class ModulesCommand extends SubCommand { + + public ModulesCommand() { + super("modules", "List all modules"); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + Lobby plugin = Lobby.getInstance(); + sender.sendMessage(""); + sender.sendMessage(plugin.getLanguageManager().getMessage("registered_modules").replace("%amount%", plugin.getModuleLoader().getModules().size() + "")); + plugin.getModuleLoader().getModules().forEach(module -> { + sender.sendMessage(plugin.getLanguageManager().getMessage("module_list").replace("%module%", module.getDescription().getName()).replace("%version%", module.getDescription().getVersion()).replace("%author%", module.getDescription().getAuthor())); + }); + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + return null; + } + +} + diff --git a/src/main/java/net/spigotcloud/lobby/command/ReloadCommand.java b/src/main/java/net/spigotcloud/lobby/command/ReloadCommand.java new file mode 100644 index 0000000..95e9a0e --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/command/ReloadCommand.java @@ -0,0 +1,31 @@ +package net.spigotcloud.lobby.command; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.util.List; + +public class ReloadCommand extends SubCommand { + + public ReloadCommand() { + super("reload", "Reloads the plugin"); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + Lobby plugin = Lobby.getInstance(); + plugin.getLocationManager().reload(); + plugin.getLanguageManager().reload(); + plugin.getModuleLoader().reload(); + plugin.getInventoryManager().reload(); + sender.sendMessage(Lobby.getInstance().getLanguageManager().getMessage("reload")); + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + return null; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/command/SetSpawnCommand.java b/src/main/java/net/spigotcloud/lobby/command/SetSpawnCommand.java new file mode 100644 index 0000000..6de36a6 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/command/SetSpawnCommand.java @@ -0,0 +1,41 @@ +package net.spigotcloud.lobby.command; + +import net.spigotcloud.lobby.Lobby; +import net.spigotcloud.lobby.manager.LanguageManager; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.List; + +public class SetSpawnCommand extends SubCommand { + + public SetSpawnCommand() { + super("setspawn", "Set spawn location"); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + + LanguageManager languageManager = Lobby.getInstance().getLanguageManager(); + + if (!(sender instanceof Player)) { + sender.sendMessage(languageManager.getNoPlayerInstance()); + return false; + } + + final Player player = (Player) sender; + final Location location = player.getLocation(); + Lobby.getInstance().getLocationManager().setSpawn(location); + player.sendMessage(languageManager.getMessage("spawn_set")); + + return false; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + return null; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/command/SubCommand.java b/src/main/java/net/spigotcloud/lobby/command/SubCommand.java new file mode 100644 index 0000000..48eff0c --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/command/SubCommand.java @@ -0,0 +1,28 @@ +package net.spigotcloud.lobby.command; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.util.List; + +public abstract class SubCommand { + + private final String permission; + private final String description; + + public SubCommand(String permission, String description) { + this.permission = permission; + this.description = description; + } + + public abstract boolean onCommand(CommandSender sender, Command command, String label, String[] args); + public abstract List onTabComplete(CommandSender sender, Command command, String label, String[] args); + + public String getPermission() { + return "lobby.command." + this.permission; + } + + public String getDescription() { + return description; + } +} diff --git a/src/main/java/net/spigotcloud/lobby/handler/PlayerHandler.java b/src/main/java/net/spigotcloud/lobby/handler/PlayerHandler.java new file mode 100644 index 0000000..3166564 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/handler/PlayerHandler.java @@ -0,0 +1,26 @@ +package net.spigotcloud.lobby.handler; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; + +import javax.annotation.Nonnull; + +public class PlayerHandler { + + public static void reset(@Nonnull Player player) { + player.getInventory().clear(); + player.setHealth(20); + player.setFoodLevel(20); + player.setExp(0); + player.setTotalExperience(0); + player.setLevel(0); + player.setGameMode(GameMode.SURVIVAL); + Lobby.getInstance().getModuleLoader().getModules().forEach(module -> { + if (module.getItem() != null) { + player.getInventory().setItem(module.getItem().getSlot(), module.getItem().get()); + } + }); + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/listener/ItemListener.java b/src/main/java/net/spigotcloud/lobby/listener/ItemListener.java new file mode 100644 index 0000000..bb47392 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/listener/ItemListener.java @@ -0,0 +1,30 @@ +package net.spigotcloud.lobby.listener; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; + +public class ItemListener implements Listener { + + @EventHandler + public void onItem(PlayerInteractEvent e) { + + final Player player = e.getPlayer(); + if (player.getGameMode() == GameMode.CREATIVE || e.getItem() == null) { + return; + } + + e.setCancelled(true); + + Lobby.getInstance().getModuleLoader().getModules().forEach(module -> { + if (module.getItem() != null && e.getItem().equals(module.getItem().get())) { + module.onItemClick(e); + } + }); + + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/listener/JoinQuitListener.java b/src/main/java/net/spigotcloud/lobby/listener/JoinQuitListener.java new file mode 100644 index 0000000..57d6425 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/listener/JoinQuitListener.java @@ -0,0 +1,27 @@ +package net.spigotcloud.lobby.listener; + +import net.spigotcloud.lobby.Lobby; +import net.spigotcloud.lobby.handler.PlayerHandler; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +public class JoinQuitListener implements Listener { + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent e) { + final Player player = e.getPlayer(); + e.setJoinMessage(null); + PlayerHandler.reset(player); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + final Player player = e.getPlayer(); + e.setQuitMessage(null); + Lobby.getInstance().getBuildPlayers().remove(player); + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/listener/ProtectListener.java b/src/main/java/net/spigotcloud/lobby/listener/ProtectListener.java new file mode 100644 index 0000000..64538b7 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/listener/ProtectListener.java @@ -0,0 +1,69 @@ +package net.spigotcloud.lobby.listener; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.FoodLevelChangeEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerSwapHandItemsEvent; + +import javax.annotation.Nonnull; + +public class ProtectListener implements Listener { + + @EventHandler + public void onPlayerDamage(EntityDamageEvent e) { + if (e.getEntity() instanceof Player) { + e.setCancelled(true); + } + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent e) { + protectEvent(e, true, e.getPlayer()); + } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent e) { + protectEvent(e, true, e.getPlayer()); + } + + @EventHandler + public void onItemDrop(PlayerDropItemEvent e) { + protectEvent(e, true, e.getPlayer()); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + protectEvent(e, true, (Player) e.getWhoClicked()); + } + + @EventHandler + public void onInteract(PlayerInteractEvent e) { + protectEvent(e, true, e.getPlayer()); + } + + @EventHandler + public void onFoodLevelChange(FoodLevelChangeEvent e) { + protectEvent(e, false, null); + } + + @EventHandler + public void onItemSwap(PlayerSwapHandItemsEvent e) { + protectEvent(e, true, e.getPlayer()); + } + + private void protectEvent(@Nonnull Cancellable event, boolean bypass, Player player) { + if (!Lobby.getInstance().getBuildPlayers().contains(player)) { + event.setCancelled(true); + } + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/manager/FileManager.java b/src/main/java/net/spigotcloud/lobby/manager/FileManager.java new file mode 100644 index 0000000..c4c672e --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/manager/FileManager.java @@ -0,0 +1,41 @@ +package net.spigotcloud.lobby.manager; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; + +public abstract class FileManager { + + private final File file; + private final FileConfiguration configuration; + + public FileManager(File file) { + this.file = file; + this.configuration = YamlConfiguration.loadConfiguration(file); + if (!file.exists()) { + getDefaults().forEach(((s, o) -> { + getConfiguration().set(s, o); + })); + save(); + } + } + + public FileConfiguration getConfiguration() { + return this.configuration; + } + + public void save() { + try { + configuration.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public abstract HashMap getDefaults(); + public abstract void reload(); + +} diff --git a/src/main/java/net/spigotcloud/lobby/manager/InventoryManager.java b/src/main/java/net/spigotcloud/lobby/manager/InventoryManager.java new file mode 100644 index 0000000..93b7daf --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/manager/InventoryManager.java @@ -0,0 +1,44 @@ +package net.spigotcloud.lobby.manager; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.inventory.ItemStack; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +public class InventoryManager extends FileManager{ + + private final Map items; + + public InventoryManager(File file) { + super(file); + this.items = new HashMap<>(); + } + + @Override + public HashMap getDefaults() { + HashMap defaults = new HashMap<>(); + Lobby.getInstance().getModuleLoader().getModules().forEach(module -> { + if (module.getItem() != null && getConfiguration().get(module.getDescription().getName()) == null) { + defaults.put(module.getDescription().getName(), module.getItem().getSlot()); + } + }); + return defaults; + } + + @Override + public void reload() { + this.items.clear(); + Lobby.getInstance().getModuleLoader().getModules().forEach(module -> { + if (module.getItem() != null && getConfiguration().get(module.getDescription().getName()) != null) { + this.items.put(module.getItem().get(), getConfiguration().getInt( module.getDescription().getName())); + } + }); + } + + public Map getItems() { + return items; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/manager/LanguageManager.java b/src/main/java/net/spigotcloud/lobby/manager/LanguageManager.java new file mode 100644 index 0000000..4915dac --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/manager/LanguageManager.java @@ -0,0 +1,83 @@ +package net.spigotcloud.lobby.manager; + +import org.bukkit.ChatColor; +import org.bukkit.configuration.ConfigurationSection; + +import javax.annotation.Nonnull; +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +public class LanguageManager extends FileManager { + + private final Map messages; + private String prefix; + private String noPlayerInstance; + private String wrongUsage; + private String noPermission; + + public LanguageManager(File file) { + super(file); + this.messages = new HashMap<>(); + } + + @Override + public HashMap getDefaults() { + HashMap defaults = new HashMap<>(); + defaults.put("messages.spawn_set", "&7Location &fSpawn &7has been set."); + defaults.put("messages.reload", "&7Modules and configurations has been reloaded."); + defaults.put("messages.build_mode_on", "&7Build Mode: &fOn"); + defaults.put("messages.build_mode_off", "&7Build Mode: &cOff"); + defaults.put("messages.help_list", "&f/%command% &7&o%description% &8(&c%permission%&8)"); + defaults.put("messages.registered_modules", "&fRegistered modules &7(%amount%):"); + defaults.put("messages.module_list", "&f%module% v%version% &7&oby %author%"); + defaults.put("general.no_player_instance", "&cConsole cannot execute player commands."); + defaults.put("general.wrong_usage", "&7Please use: &f%usage%"); + defaults.put("general.no_permission", "&7Permission &c%permission% &7required to do this."); + defaults.put("general.prefix", "&7[&fLobby&7] "); + return defaults; + } + + @Override + public void reload() { + + this.prefix = getConfiguration().getString("general.prefix"); + this.noPlayerInstance = getConfiguration().getString("general.no_player_instance"); + this.wrongUsage = getConfiguration().getString("general.wrong_usage"); + this.noPermission = getConfiguration().getString("general.no_permission"); + + ConfigurationSection section = getConfiguration().getConfigurationSection("messages"); + if (section != null) { + this.messages.clear(); + for (String key : section.getKeys(false)) { + this.messages.put(key, section.getString(key)); + } + } + + } + + public String getPrefix() { + return format(prefix); + } + + public String getNoPlayerInstance() { + return format(prefix + noPlayerInstance); + } + + public String getWrongUsage(@Nonnull String usage) { + return format(prefix + wrongUsage).replace("%usage%", "/lobby " + usage); + } + + public String getNoPermission(@Nonnull String permission) { + return format(prefix + noPermission).replace("%permission%", permission); + } + + public String getMessage(@Nonnull String key) { + return format(prefix + this.messages.get(key)); + } + + private String format(String text) { + return ChatColor.translateAlternateColorCodes('&', text); + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/manager/LocationManager.java b/src/main/java/net/spigotcloud/lobby/manager/LocationManager.java new file mode 100644 index 0000000..97ad4c9 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/manager/LocationManager.java @@ -0,0 +1,39 @@ +package net.spigotcloud.lobby.manager; + +import org.bukkit.Location; + +import javax.annotation.Nonnull; +import java.io.File; +import java.util.HashMap; + +public class LocationManager extends FileManager { + + private Location spawn; + + + public LocationManager(File file) { + super(file); + } + + @Override + public HashMap getDefaults() { + HashMap defaults = new HashMap<>(); + defaults.put("spawn", null); + return defaults; + } + + @Override + public void reload() { + this.spawn = getConfiguration().getLocation("spawn"); + } + + public void setSpawn(@Nonnull Location location) { + getConfiguration().set("spawn", location); + save(); + } + + public Location getSpawn() { + return this.spawn; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/module/Module.java b/src/main/java/net/spigotcloud/lobby/module/Module.java new file mode 100644 index 0000000..6739d8d --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/module/Module.java @@ -0,0 +1,83 @@ +package net.spigotcloud.lobby.module; + +import net.spigotcloud.lobby.command.SubCommand; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; + +import javax.annotation.Nonnull; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public abstract class Module { + + private final Set listeners = new HashSet<>(); + private final Map subCommands = new HashMap<>(); + private ModuleDescriptionFile descriptionFile; + private final ModuleItem item; + private File dataFolder; + private File file; + private FileConfiguration config; + + public Module(ModuleItem moduleItem) { + this.item = moduleItem; + } + + + public void init(@Nonnull ModuleDescriptionFile moduleDescriptionFile) { + this.descriptionFile = moduleDescriptionFile; + this.dataFolder = new File("plugins/Lobby/modules/" + this.descriptionFile.getName()); + this.file = new File(this.dataFolder + File.separator + "config.yml"); + this.config = YamlConfiguration.loadConfiguration(this.file); + } + + public FileConfiguration getConfig() { + return this.config; + } + + public void saveConfig() { + try { + this.config.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void registerListener(@Nonnull Listener listener) { + this.listeners.add(listener); + } + + public Set getListeners() { + return this.listeners; + } + + public void registerSubCommand(@Nonnull String command, @Nonnull SubCommand subCommand) { + this.subCommands.put(command, subCommand); + } + + public ModuleItem getItem() { + return this.item; + } + + public Map getSubCommands() { + return this.subCommands; + } + + public ModuleDescriptionFile getDescription() { + return descriptionFile; + } + + public File getDataFolder() { + return this.dataFolder; + } + + public abstract void onEnable(); + public abstract void onDisable(); + public abstract void onItemClick(PlayerInteractEvent event); + +} diff --git a/src/main/java/net/spigotcloud/lobby/module/ModuleDescriptionFile.java b/src/main/java/net/spigotcloud/lobby/module/ModuleDescriptionFile.java new file mode 100644 index 0000000..4d74343 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/module/ModuleDescriptionFile.java @@ -0,0 +1,28 @@ +package net.spigotcloud.lobby.module; + +public class ModuleDescriptionFile { + + private final String name; + private final String author; + private final String version; + + + public ModuleDescriptionFile(String name, String author, String version) { + this.name = name; + this.author = author; + this.version = version; + } + + public String getName() { + return name; + } + + public String getAuthor() { + return author; + } + + public String getVersion() { + return version; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/module/ModuleItem.java b/src/main/java/net/spigotcloud/lobby/module/ModuleItem.java new file mode 100644 index 0000000..ec11356 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/module/ModuleItem.java @@ -0,0 +1,50 @@ +package net.spigotcloud.lobby.module; + +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; +import java.util.Arrays; + +public class ModuleItem { + + private final ItemStack itemStack; + private final int slot; + + public ModuleItem(@Nonnull ItemStack itemStack, @Nonnegative int defaultSlot) { + ItemMeta meta = itemStack.getItemMeta(); + assert meta != null; + meta.setUnbreakable(true); + meta.addItemFlags(ItemFlag.values()); + itemStack.setItemMeta(meta); + this.itemStack = itemStack; + this.slot = defaultSlot; + } + + public ModuleItem setName(@Nonnull String displayName) { + ItemMeta meta = itemStack.getItemMeta(); + assert meta != null; + meta.setDisplayName(displayName); + itemStack.setItemMeta(meta); + return this; + } + + public ModuleItem setLore(@Nonnull String... lore) { + ItemMeta meta = itemStack.getItemMeta(); + assert meta != null; + meta.setLore(Arrays.asList(lore)); + itemStack.setItemMeta(meta); + return this; + } + + public ItemStack get() { + return this.itemStack; + } + + public int getSlot() { + return this.slot; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java b/src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java new file mode 100644 index 0000000..ac512b3 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java @@ -0,0 +1,130 @@ +package net.spigotcloud.lobby.module; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.HandlerList; + +import java.io.*; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.logging.Level; +import java.util.zip.ZipFile; + +public class ModuleLoader { + + private final Set modules = new HashSet<>(); + private final Map, ModuleDescriptionFile> classes = new HashMap(); + private final File file; + + public ModuleLoader() { + this.file = new File(Lobby.getInstance().getDataFolder(), "modules"); + } + + + public void load() { + + if (file.exists() && file.isDirectory()) { + + this.classes.clear(); + this.modules.clear(); + + File[] files = file.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith(".jar"); + } + }); + + for (File jar : files) { + String mainClass = null; + try { + ZipFile zipFile = new ZipFile(jar); + + InputStream is = zipFile.getInputStream(zipFile.getEntry("module.yml")); + + YamlConfiguration config = YamlConfiguration.loadConfiguration(new InputStreamReader(is)); + mainClass = config.getString("main"); + + String name = config.getString("name"); + String author = config.getString("author"); + String version = config.getString("version"); + + if (mainClass == null || name == null || version == null) { + Lobby.getInstance().getLogger().log(Level.SEVERE, "Error while loading module file " + jar.getName() + " (Please check 'module.yml' file in Module"); + return; + } + + + ClassLoader l = URLClassLoader.newInstance(new URL[]{jar.toURI().toURL()}, getClass().getClassLoader()); + + Class clazz = l.loadClass(mainClass); + classes.put(clazz, new ModuleDescriptionFile(name, author != null ? author : "Unknown", version)); + + } catch (IOException e) { + Lobby.getInstance().getLogger().log(Level.SEVERE, "Error while loading module file " + jar.getName()); + e.printStackTrace(); + } catch (ClassNotFoundException e) { + Lobby.getInstance().getLogger().log(Level.SEVERE, "Class not found! Wrong main defined in module.yml?: " + jar.getName() + " class: " + mainClass); + e.printStackTrace(); + } + + + } + + + } + } + + public void enable() { + this.classes.forEach((clazz, desc) -> { + try { + Object object = clazz.newInstance(); + + if (object instanceof Module) { + Module module = (Module) object; + module.init(desc); + module.onEnable(); + + module.getListeners().forEach(listener -> { + Lobby.getInstance().getServer().getPluginManager().registerEvents(listener, Lobby.getInstance()); + }); + module.getSubCommands().forEach(((s, subCommand) -> { + Lobby.getInstance().getBaseCommand().registerSubCommand(s, subCommand); + })); + modules.add(module); + Lobby.getInstance().getLogger().log(Level.INFO, "Module registered: " + desc.getName() + " v" + desc.getVersion() + " by " + desc.getAuthor()); + } + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + }); + } + + public void disable() { + for (Module module : modules) { + module.getListeners().forEach(HandlerList::unregisterAll); + module.getSubCommands().forEach(((s, subCommand) -> { + Lobby.getInstance().getBaseCommand().unregister(s); + })); + module.onDisable(); + Lobby.getInstance().getLogger().log(Level.INFO, "[Module] Disabled: " + module.getDescription().getName() + " v" + module.getDescription().getVersion() + " by " + module.getDescription().getAuthor()); + + } + } + + public Set getModules() { + return this.modules; + } + + public void reload() { + disable(); + load(); + enable(); + } + + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..6d699a8 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,13 @@ +name: Lobby +version: '${project.version}' +main: net.spigotcloud.lobby.Lobby +api-version: 1.19 +softdepend: [ PointsAPI ] +authors: [ TerryGHG ] +description: Modular Lobby Plugin for Spigot 1.19+ +website: https://github.com/teraprath +commands: + lobby: + description: Base Command + permission: lobby.command + aliases: [ l, lb, hub ]