start re-writing commands one command at a time

This commit is contained in:
Ryder Belserion 2024-09-08 19:15:15 -04:00
parent 923a3eaea4
commit 5bdd4aae36
No known key found for this signature in database
7 changed files with 295 additions and 36 deletions

View File

@ -0,0 +1,88 @@
package com.badbones69.crazyauctions.api.enums.other;
import com.badbones69.crazyauctions.CrazyAuctions;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import java.util.HashMap;
import java.util.Map;
public enum Permissions {
reload("reload", "Access to /crazyauctions reload", PermissionDefault.OP, false),
help("help", "Access to /crazyauctions help", PermissionDefault.TRUE, false),
use("use", "Access to /crazyauctions", PermissionDefault.TRUE, false);
private final String node;
private final String description;
private final PermissionDefault isDefault;
private final Map<String, Boolean> children;
private final boolean register;
private final PluginManager manager = CrazyAuctions.getPlugin().getServer().getPluginManager();
Permissions(String node, String description, PermissionDefault isDefault, Map<String, Boolean> children, boolean register) {
this.node = node;
this.description = description;
this.isDefault = isDefault;
this.children = children;
this.register = register;
}
Permissions(String node, String description, PermissionDefault isDefault, boolean register) {
this.node = node;
this.description = description;
this.isDefault = isDefault;
this.children = new HashMap<>();
this.register = register;
}
public final String getNode() {
return "crazyauctions." + this.node;
}
public final boolean shouldRegister() {
return this.register;
}
public final String getDescription() {
return this.description;
}
public final PermissionDefault isDefault() {
return this.isDefault;
}
public final Map<String, Boolean> getChildren() {
return this.children;
}
public final boolean hasPermission(final Player player) {
return player.hasPermission(getNode());
}
public final boolean isValid() {
return this.manager.getPermission(getNode()) != null;
}
public final Permission getPermission() {
return new Permission(getNode(), getDescription(), isDefault());
}
public void registerPermission() {
if (isValid()) return;
this.manager.addPermission(getPermission());
}
public void unregisterPermission() {
if (!isValid()) return;
this.manager.removePermission(getNode());
}
}

View File

@ -68,34 +68,6 @@ public class AuctionCommand implements CommandExecutor {
return true;
} else {
switch (args[0].toLowerCase()) {
case "help" -> {
if (!Methods.hasPermission(sender, "access")) {
return true;
}
sender.sendMessage(Messages.HELP.getMessage(sender));
return true;
}
case "reload" -> {
if (!Methods.hasPermission(sender, "reload")) {
return true;
}
Files.config.reload();
Files.data.reload();
Files.messages.reload();
this.fileManager.reloadFiles().init();
this.crazyManager.load();
sender.sendMessage(Messages.RELOAD.getMessage(sender));
return true;
}
case "view" -> {
if (!Methods.hasPermission(sender, "view")) {
return true;

View File

@ -0,0 +1,21 @@
package com.badbones69.crazyauctions.commands.v2;
import ch.jalu.configme.SettingsManager;
import com.badbones69.crazyauctions.CrazyAuctions;
import com.badbones69.crazyauctions.api.CrazyManager;
import com.badbones69.crazyauctions.configs.ConfigManager;
import com.badbones69.crazyauctions.configs.enums.Files;
import com.ryderbelserion.vital.paper.commands.PaperCommand;
import org.bukkit.Server;
public abstract class AbstractCommand extends PaperCommand {
protected final CrazyAuctions plugin = CrazyAuctions.getPlugin();
protected final CrazyManager crazyManager = this.plugin.getCrazyManager();
protected final Server server = this.plugin.getServer();
protected final SettingsManager config = ConfigManager.getConfig();
protected final SettingsManager auctions = ConfigManager.getCustomConfig(Files.auctions.getFileName());
}

View File

@ -0,0 +1,85 @@
package com.badbones69.crazyauctions.commands.v2;
import com.badbones69.crazyauctions.api.enums.other.Permissions;
import com.badbones69.crazyauctions.configs.impl.ConfigKeys;
import com.badbones69.crazyauctions.configs.impl.gui.AuctionKeys;
import com.badbones69.crazyauctions.utils.MiscUtils;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.Gui;
import com.ryderbelserion.vital.paper.api.builders.gui.types.PaginatedGui;
import com.ryderbelserion.vital.paper.commands.context.PaperCommandInfo;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import net.kyori.adventure.sound.Sound;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class BaseCommand extends AbstractCommand {
@Override
public void execute(final PaperCommandInfo info) {
if (!info.isPlayer()) {
//todo() send message to require as a player.
return;
}
final Player player = info.getPlayer();
if (this.config.getProperty(ConfigKeys.category_page_opens_first)) {
//todo() open gui with specific category.
return;
}
final @NotNull PaginatedGui builder = Gui.paginated()
.disableInteractions()
.setTitle(this.auctions.getProperty(AuctionKeys.gui_name)).create();
this.auctions.getProperty(AuctionKeys.expired_item_button).setItem(consumer -> {
MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER);
}, builder);
this.auctions.getProperty(AuctionKeys.selling_item_button).setItem(consumer -> {
MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER);
}, builder);
this.auctions.getProperty(AuctionKeys.bidding_item_button).setItem(consumer -> {
MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER);
}, builder);
this.auctions.getProperty(AuctionKeys.sold_item_button).setItem(consumer -> {
MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER);
}, builder);
builder.open(player);
}
@Override
public @NotNull final String getPermission() {
return Permissions.use.getNode();
}
@Override
public @NotNull final LiteralCommandNode<CommandSourceStack> literal() {
return Commands.literal("crazyauctions")
.requires(source -> source.getSender().hasPermission(getPermission()))
.executes(context -> {
execute(new PaperCommandInfo(context));
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}).build();
}
@Override
public @NotNull final AbstractCommand registerPermission() {
Permissions.use.registerPermission();
return this;
}
}

View File

@ -0,0 +1,45 @@
package com.badbones69.crazyauctions.commands.v2.player;
import com.badbones69.crazyauctions.api.enums.Messages;
import com.badbones69.crazyauctions.api.enums.other.Permissions;
import com.badbones69.crazyauctions.commands.v2.AbstractCommand;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.ryderbelserion.vital.paper.commands.context.PaperCommandInfo;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class CommandHelp extends AbstractCommand {
@Override
public void execute(final PaperCommandInfo info) {
final CommandSender sender = info.getCommandSender();
sender.sendMessage(Messages.HELP.getMessage(sender));
}
@Override
public @NotNull final String getPermission() {
return Permissions.help.getNode();
}
@Override
public @NotNull final LiteralCommandNode<CommandSourceStack> literal() {
final LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal("help").requires(source -> source.getSender().hasPermission(getPermission()));
return root.executes(context -> {
execute(new PaperCommandInfo(context));
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}).build();
}
@Override
public @NotNull final AbstractCommand registerPermission() {
Permissions.help.registerPermission();
return this;
}
}

View File

@ -0,0 +1,54 @@
package com.badbones69.crazyauctions.commands.v2.staff;
import com.badbones69.crazyauctions.api.enums.Messages;
import com.badbones69.crazyauctions.api.enums.other.Permissions;
import com.badbones69.crazyauctions.commands.v2.AbstractCommand;
import com.badbones69.crazyauctions.configs.ConfigManager;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.ryderbelserion.vital.paper.commands.context.PaperCommandInfo;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class CommandReload extends AbstractCommand {
@Override
public void execute(final PaperCommandInfo info) {
this.server.getGlobalRegionScheduler().cancelTasks(this.plugin);
this.server.getAsyncScheduler().cancelTasks(this.plugin);
ConfigManager.refresh();
this.plugin.getFileManager().reloadFiles();
this.crazyManager.load();
final CommandSender sender = info.getCommandSender();
sender.sendMessage(Messages.RELOAD.getMessage(sender));
}
@Override
public @NotNull final String getPermission() {
return Permissions.reload.getNode();
}
@Override
public @NotNull final LiteralCommandNode<CommandSourceStack> literal() {
return Commands.literal("reload")
.requires(source -> source.getSender().hasPermission(getPermission()))
.executes(context -> {
execute(new PaperCommandInfo(context));
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}).build();
}
@Override
public @NotNull final AbstractCommand registerPermission() {
Permissions.reload.registerPermission();
return this;
}
}

View File

@ -11,12 +11,10 @@ folia-supported: true
softdepend: [Vault]
folia-supported: true
commands:
crazyauctions:
ca:
description: Opens the Crazy Auctions GUI.
aliases: [crazyauction, ah, ca]
aliases: [ah]
permissions:
crazyauctions.access:
@ -28,9 +26,6 @@ permissions:
crazyauctions.view:
default: true
crazyauctions.reload:
default: op
crazyauctions.bypass:
default: false
@ -52,5 +47,4 @@ permissions:
default: false
children:
crazyauctions.test: true
crazyauctions.reload: true
crazyauctions.bypass: true