Added reload command and permission improvements

This commit is contained in:
cuftica 2023-04-25 23:08:23 +02:00
parent e633b8cbe6
commit 8c80e58757
6 changed files with 75 additions and 8 deletions

View File

@ -24,6 +24,8 @@ import os.arcadiadevs.playerservers.hubcore.models.Allocation;
import os.arcadiadevs.playerservers.hubcore.models.Node;
import os.arcadiadevs.playerservers.hubcore.models.Server;
import os.arcadiadevs.playerservers.hubcore.placeholders.PlayerCount;
import os.arcadiadevs.playerservers.hubcore.statics.Permissions;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
/**
* Main class of the plugin.
@ -102,6 +104,17 @@ public class PsHubCore extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new HubEvents(getConfig()), this);
Objects.requireNonNull(getCommand("servers")).setExecutor(new CommandManager());
Objects.requireNonNull(getCommand("playermenu")).setExecutor(new CommandManager());
Objects.requireNonNull(getCommand("pshubreload")).setExecutor((sender, command, label, args) -> {
if (sender.hasPermission(Permissions.RELOAD)) {
reloadConfig();
ChatUtil.sendMessage(sender, "&9PSHubCore> &aConfig reloaded.");
} else {
ChatUtil.sendMessage(sender, "&9PSHubCore> &cYou don't have permission to do that.");
}
return true;
});
// Initialize SpiGUI
spiGui = new SpiGUI(this);

View File

@ -5,7 +5,9 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import os.arcadiadevs.playerservers.hubcore.PsHubCore;
import os.arcadiadevs.playerservers.hubcore.guis.PlayerMenuGui;
import os.arcadiadevs.playerservers.hubcore.guis.SelectorGui;
import os.arcadiadevs.playerservers.hubcore.statics.Permissions;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
/**
@ -13,28 +15,58 @@ import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
*
* @author ArcadiaDevs
*/
@SuppressWarnings("NullableProblems")
public class CommandManager implements CommandExecutor {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s,
String[] strings) {
if (!(commandSender instanceof Player player)) {
ChatUtil.sendMessage(commandSender,
"&9Error> &cYou must be a player to execute this command.");
return true;
}
if (!PsHubCore.getInstance().getConfig().getBoolean("gui.selector.enabled")) {
ChatUtil.sendMessage(player, "&9PlayerServers> &7Oops, this feature is disabled.");
final var enableGuiPermissions = PsHubCore.getInstance().getConfig().getBoolean("gui.enable-permissions");
final var commandName = command.getName();
if (!enableGuiPermissions) {
if (isPlayerMenu(commandName)) {
PlayerMenuGui.openGui(player);
return true;
}
if (command.getName().equalsIgnoreCase("servers")
|| command.getName().equalsIgnoreCase("menu")
|| command.getName().equalsIgnoreCase("opengui")) {
if (isSelector(commandName)) {
SelectorGui.openGui(player);
return true;
}
}
if (isPlayerMenu(commandName) && player.hasPermission(Permissions.PLAYER_MENU) ) {
PlayerMenuGui.openGui(player);
return true;
}
if (isSelector(commandName) && player.hasPermission(Permissions.SERVER_SELECTOR)) {
SelectorGui.openGui(player);
return true;
}
ChatUtil.sendMessage(player, PsHubCore.getInstance().getConfig().getString("messages.no-permission"));
return true;
}
private boolean isSelector(String commandName) {
return commandName.equalsIgnoreCase("servers")
|| commandName.equalsIgnoreCase("menu")
|| commandName.equalsIgnoreCase("opengui");
}
private boolean isPlayerMenu(String commandName) {
return commandName.equalsIgnoreCase("playermenu")
|| commandName.equalsIgnoreCase("pmenu")
|| commandName.equalsIgnoreCase("playergui")
|| commandName.equalsIgnoreCase("pgui");
}
}

View File

@ -11,6 +11,7 @@ import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import os.arcadiadevs.playerservers.hubcore.PsHubCore;
import os.arcadiadevs.playerservers.hubcore.guis.SelectorGui;
import os.arcadiadevs.playerservers.hubcore.statics.Permissions;
import os.arcadiadevs.playerservers.hubcore.utils.ChatUtil;
import os.arcadiadevs.playerservers.hubcore.guis.PlayerMenuGui;
@ -35,6 +36,7 @@ public class ClickEvent implements Listener {
@EventHandler
public void onClick(PlayerInteractEvent event) {
final var player = event.getPlayer();
final boolean enableGuiPermissions = PsHubCore.getInstance().getConfig().getBoolean("gui.enable-permissions");
if (event.getAction() != Action.RIGHT_CLICK_AIR
&& event.getAction() != Action.RIGHT_CLICK_BLOCK) {
@ -44,7 +46,7 @@ public class ClickEvent implements Listener {
if (instance.getConfig().getBoolean("gui.selector.item.enabled")
&& player.getInventory().getHeldItemSlot()
== PsHubCore.getInstance().getConfig().getInt("gui.selector.item.location")) {
if (!player.hasPermission("hubcore.selector")) {
if (!player.hasPermission(Permissions.SERVER_SELECTOR) && enableGuiPermissions) {
ChatUtil.sendMessage(player, instance.getConfig().getString("messages.no-permission"));
return;
}
@ -55,7 +57,7 @@ public class ClickEvent implements Listener {
if (instance.getConfig().getBoolean("gui.player-menu.item.enabled")
&& player.getInventory().getHeldItemSlot()
== PsHubCore.getInstance().getConfig().getInt("gui.player-menu.item.location")) {
if (!player.hasPermission("hubcore.player-menu")) {
if (!player.hasPermission(Permissions.PLAYER_MENU) && enableGuiPermissions) {
ChatUtil.sendMessage(player, instance.getConfig().getString("messages.no-permission"));
return;
}

View File

@ -0,0 +1,8 @@
package os.arcadiadevs.playerservers.hubcore.statics;
public class Permissions {
public static String SERVER_SELECTOR = "playerservers.hubcore.selector";
public static String PLAYER_MENU = "playerservers.hubcore.player-menu";
public static String RELOAD = "playerservers.hubcore.reload";
}

View File

@ -25,6 +25,8 @@ messages:
no-permission: "&9Error> &7Oops, it seems like you don't have permission to do that."
gui:
enable-permissions: false
selector:
enabled: true

View File

@ -6,9 +6,19 @@ author: OpenSource
softdepend: [PlaceholderAPI]
commands:
pshubreload:
usage: /pshubreload
description: Reloads the plugin
servers:
usage: /servers
description: Opens up server selector GUI
aliases:
- menu
- opengui
playermenu:
usage: /playermenu
description: Opens up player menu GUI
aliases:
- pmenu
- playergui
- pgui