Skip to content

Commit

Permalink
Upgraded to latest ToberoCore version
Browse files Browse the repository at this point in the history
  • Loading branch information
ToberoCat committed Aug 15, 2023
1 parent 53eb414 commit b349364
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 75 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<jackson.version>2.15.2</jackson.version>
<commons.text.version>1.10.0</commons.text.version>
<jetbrains.annotations.version>23.0.0</jetbrains.annotations.version>
<spigot.api.version>1.16.1-R0.1-SNAPSHOT</spigot.api.version>
<toberocore.version>2.0.1</toberocore.version>
<spigot.api.version>1.18.1-R0.1-SNAPSHOT</spigot.api.version>
<toberocore.version>90d5cd2a78</toberocore.version>

<!-- Server Location -->
<server.location>H:\Development\Minecraft\Spigot\Server1.19\plugins</server.location>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class GuiEngineApiPlugin extends JavaPlugin {
*/
private DefaultGuiViewManager guiViewManager;
private GuiItemManager guiItemManager;
private GuiEngineApi guiApi;

/**
* Called when the plugin is enabled. Initializes the plugin and registers necessary components, functions, actions, and commands.
Expand Down Expand Up @@ -156,6 +157,10 @@ private void registerComponents() {
GuiEngineApi.registerSharedFactory(PagedComponent.TYPE, PagedComponent.class, PagedComponentBuilder.class);
}

public GuiEngineApi getGuiApi() {
return guiApi;
}

/**
* Checks for updates
*/
Expand Down Expand Up @@ -204,7 +209,7 @@ private void registerCommands() {
* If an exception occurs while reloading the API, it is ignored.
*/
private void addDefaultApi() {
GuiEngineApi guiApi = new GuiEngineApi("default", new File(getDataFolder(), "guis"));
guiApi = new GuiEngineApi("default", new File(getDataFolder(), "guis"));
try {
guiApi.reload();
} catch (GuiIORuntimeException ignored) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.toberocat.guiengine.arguments;

import io.github.toberocat.guiengine.GuiEngineApi;
import io.github.toberocat.guiengine.GuiEngineApiPlugin;
import io.github.toberocat.toberocore.command.arguments.Argument;
import io.github.toberocat.toberocore.command.exceptions.CommandException;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;

public class GuiEngineApiArgument implements Argument<GuiEngineApi> {
private final GuiEngineApi defaultApi;

public GuiEngineApiArgument() {
this(GuiEngineApiPlugin.getPlugin().getGuiApi());
}

public GuiEngineApiArgument(GuiEngineApi defaultApi) {
this.defaultApi = defaultApi;
}

@Override
public GuiEngineApi parse(@NotNull Player player, @NotNull String s) throws CommandException {
GuiEngineApi api = GuiEngineApi.APIS.get(s);
if (api == null)
throw new CommandException("§cNo API found with ID" + s, new HashMap<>());
return api;
}

@Override
public GuiEngineApi defaultValue(@NotNull Player player) {
return defaultApi;
}

@Override
public @Nullable List<String> tab(@NotNull Player player) throws CommandException {
return GuiEngineApi.APIS.keySet().stream().toList();
}

@Override
public @NotNull String descriptionKey() {
return "base.gui-engine.args.api";
}

@Override
public @NotNull String usage() {
return "<api>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import io.github.toberocat.guiengine.context.GuiContext;
import io.github.toberocat.guiengine.exception.GuiIORuntimeException;
import io.github.toberocat.guiengine.exception.GuiNotFoundRuntimeException;
import io.github.toberocat.toberocore.command.exceptions.CommandExceptions;
import io.github.toberocat.toberocore.command.exceptions.CommandException;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

/**
* The `DumpCommand` class represents a player sub-command that allows a player to open a GUI and dump its context.
* <p>
Expand All @@ -25,17 +27,9 @@ public DumpCommand() {
super("dump-open");
}

/**
* Executes the `DumpCommand` for the specified player with the provided arguments.
*
* @param player The player executing the command.
* @param args The arguments passed to the command.
* @return `true` if the command execution is successful, `false` otherwise.
* @throws CommandExceptions If an error occurs during command execution.
*/
@Override
protected boolean runPlayer(@NotNull Player player, @NotNull String @NotNull [] args) throws CommandExceptions {
if (0 == args.length) throw new CommandExceptions("This command needs a GUI ID provided");
protected boolean handle(@NotNull Player player, @NotNull String[] args) throws CommandException {
if (0 == args.length) throw new CommandException("This command needs a GUI ID provided", new HashMap<>());

try {
String apiId = args[0];
Expand All @@ -50,7 +44,7 @@ protected boolean runPlayer(@NotNull Player player, @NotNull String @NotNull []
GuiContext context = api.openGui(player, guiId);
GuiEngineApiPlugin.getPlugin().getLogger().info(String.valueOf(context));
} catch (GuiNotFoundRuntimeException | GuiIORuntimeException e) {
throw new CommandExceptions(e.getMessage());
throw new CommandException(e.getMessage(), new HashMap<>());
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import io.github.toberocat.guiengine.GuiEngineApi;
import io.github.toberocat.guiengine.GuiEngineApiPlugin;
import io.github.toberocat.toberocore.command.SubCommand;
import io.github.toberocat.toberocore.command.exceptions.CommandExceptions;
import io.github.toberocat.toberocore.command.arguments.Argument;
import io.github.toberocat.toberocore.command.exceptions.CommandException;
import io.github.toberocat.toberocore.command.options.Options;
import io.github.toberocat.toberocore.util.ItemBuilder;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
Expand All @@ -15,8 +17,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
Expand All @@ -25,8 +27,8 @@
* @author Tobias Madlberger (Tobias)
*/
public class GiveCommand extends SubCommand {
public static @NotNull NamespacedKey API_NAME_KEY;
public static @NotNull NamespacedKey GUI_ID_KEY;
public static NamespacedKey API_NAME_KEY;
public static NamespacedKey GUI_ID_KEY;

private final @NotNull GuiEngineApiPlugin plugin;

Expand All @@ -38,23 +40,23 @@ public GiveCommand(@NotNull GuiEngineApiPlugin plugin) {
}

@Override
protected boolean run(@NotNull CommandSender sender, @NotNull String @NotNull [] args) throws CommandExceptions {
protected boolean handleCommand(@NotNull CommandSender sender, @NotNull String[] args) throws CommandException {
if (3 > args.length)
throw new CommandExceptions("You need to give this command four arguments: §6/guiengine give <item> <api> <gui> <player>§c. The last one is optional");
throw new CommandException("You need to give this command four arguments: §6/guiengine give <item> <api> <gui> <player>§c. The last one is optional", new HashMap<>());
ItemStack stack = plugin.getGuiItemManager().getItem(args[0]);
if (null == stack) throw new CommandExceptions("The item §6'" + args[0] + "'§c can't be found");
if (null == stack) throw new CommandException("The item §6'" + args[0] + "'§c can't be found", new HashMap<>());

GuiEngineApi api = GuiEngineApi.APIS.get(args[1]);
if (null == api) throw new CommandExceptions("§cNo API found with ID " + args[1]);
if (null == api) throw new CommandException("§cNo API found with ID " + args[1], new HashMap<>());

String guiId = args[2];
if (!api.getAvailableGuis().contains(guiId))
throw new CommandExceptions("This gui doesn't exist in the specified api");
throw new CommandException("This gui doesn't exist in the specified api", new HashMap<>());

Player target = null;
if (sender instanceof Player player) target = player;
if (4 == args.length) target = Bukkit.getPlayer(args[3]);
if (null == target) throw new CommandExceptions("Player not found");
if (null == target) throw new CommandException("Player not found", new HashMap<>());


stack = new ItemBuilder(stack.clone()).persistent(API_NAME_KEY, PersistentDataType.STRING, api.getId()).persistent(GUI_ID_KEY, PersistentDataType.STRING, guiId).create(plugin);
Expand All @@ -66,13 +68,27 @@ protected boolean run(@NotNull CommandSender sender, @NotNull String @NotNull []
}

@Override
protected @Nullable List<String> runTab(@NotNull CommandSender player, @NotNull String @NotNull [] args) {
if (1 >= args.length) return new ArrayList<>(plugin.getGuiItemManager().getKnownItems().keySet());
if (2 == args.length) return GuiEngineApi.APIS.keySet().stream().toList();
protected @NotNull Options options() {
return new Options();
}

GuiEngineApi api = GuiEngineApi.APIS.get(args[1]);
@Override
protected @NotNull Argument<?>[] arguments() {
return new Argument[0];
}

@Override
public @Nullable List<String> routeTab(@NotNull CommandSender sender, @NotNull String[] args) throws CommandException {
if (!sender.hasPermission(this.getPermission())) {
return null;
}

String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
if (1 >= newArgs.length) return GuiEngineApi.APIS.keySet().stream().toList();
GuiEngineApi api = GuiEngineApi.APIS.get(newArgs[0]);
if (null == api) {
player.sendMessage("§cNo API found with ID " + args[1]);
sender.sendMessage("§cNo API found with ID " + newArgs[0]);
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.toberocat.guiengine.commands;

import io.github.toberocat.guiengine.GuiEngineApi;
import io.github.toberocat.guiengine.GuiEngineApiPlugin;
import io.github.toberocat.guiengine.exception.GuiIORuntimeException;
import io.github.toberocat.toberocore.command.CommandExecutor;
import io.github.toberocat.toberocore.command.subcommands.QuickSubCommand;

/**
* The `GuiCommands` class sets up and registers GUI-related commands for the `GuiEngineApi`.
Expand All @@ -19,21 +16,10 @@ public class GuiCommands {
* Constructs a new `GuiCommands` and sets up the GUI-related commands.
*/
public GuiCommands() {
CommandExecutor executor = CommandExecutor.createExecutor("", "guiengine");
CommandExecutor executor = CommandExecutor.createExecutor("guiengine");
executor.addChild(new OpenCommand());
executor.addChild(new DumpCommand());
executor.addChild(new GiveCommand(GuiEngineApiPlugin.getPlugin()));
executor.addChild(new QuickSubCommand("reload", (sender, args) -> {
try {
GuiEngineApiPlugin.getPlugin().reloadConfig();

for (GuiEngineApi api : GuiEngineApi.APIS.values())
api.reload();
sender.sendMessage("§aReloaded GUI APIs.");
} catch (GuiIORuntimeException e) {
sender.sendMessage(e.getMessage());
}
return true;
}));
executor.addChild(new ReloadCommand());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import io.github.toberocat.guiengine.GuiEngineApi;
import io.github.toberocat.guiengine.exception.GuiIORuntimeException;
import io.github.toberocat.guiengine.exception.GuiNotFoundRuntimeException;
import io.github.toberocat.toberocore.command.exceptions.CommandExceptions;
import io.github.toberocat.toberocore.command.subcommands.PlayerSubCommand;
import io.github.toberocat.toberocore.command.PlayerSubCommand;
import io.github.toberocat.toberocore.command.arguments.Argument;
import io.github.toberocat.toberocore.command.exceptions.CommandException;
import io.github.toberocat.toberocore.command.options.Options;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
Expand Down Expand Up @@ -37,17 +41,19 @@ public OpenCommand(@NotNull String label) {
super(label);
}

/**
* Executes the `OpenCommand` for the specified player with the provided arguments.
*
* @param player The player executing the command.
* @param args The arguments passed to the command.
* @return `true` if the command execution is successful, `false` otherwise.
* @throws CommandExceptions If an error occurs during command execution.
*/
@Override
protected boolean runPlayer(@NotNull Player player, @NotNull String @NotNull [] args) throws CommandExceptions {
if (0 == args.length) throw new CommandExceptions("This command needs a GUI ID provided");
protected @NotNull Options options() {
return new Options();
}

@Override
protected @NotNull Argument<?>[] arguments() {
return new Argument[0];
}

@Override
protected boolean handle(@NotNull Player player, @NotNull String[] args) throws CommandException {
if (0 == args.length) throw new CommandException("This command needs a GUI ID provided", new HashMap<>());

try {
String apiId = args[0];
Expand All @@ -61,24 +67,23 @@ protected boolean runPlayer(@NotNull Player player, @NotNull String @NotNull []

api.openGui(player, guiId);
} catch (GuiNotFoundRuntimeException | GuiIORuntimeException e) {
throw new CommandExceptions(e.getMessage());
throw new CommandException(e.getMessage(), new HashMap<>());
}
return true;
}

/**
* Returns a list of available API IDs or GUI IDs for tab completion based on the provided arguments.
*
* @param player The player executing the command.
* @param args The arguments passed to the command.
* @return A list of available API IDs or GUI IDs for tab completion.
*/
@Override
protected @Nullable List<String> runPlayerTab(@NotNull Player player, @NotNull String @NotNull [] args) {
if (1 >= args.length) return GuiEngineApi.APIS.keySet().stream().toList();
GuiEngineApi api = GuiEngineApi.APIS.get(args[0]);
public @Nullable List<String> routeTab(@NotNull CommandSender sender, @NotNull String[] args) throws CommandException {
if (!sender.hasPermission(this.getPermission())) {
return null;
}

String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
if (1 >= newArgs.length) return GuiEngineApi.APIS.keySet().stream().toList();
GuiEngineApi api = GuiEngineApi.APIS.get(newArgs[0]);
if (null == api) {
player.sendMessage("§cNo API found with ID " + args[0]);
sender.sendMessage("§cNo API found with ID " + newArgs[0]);
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.toberocat.guiengine.commands;

import io.github.toberocat.guiengine.GuiEngineApi;
import io.github.toberocat.guiengine.GuiEngineApiPlugin;
import io.github.toberocat.guiengine.exception.GuiIORuntimeException;
import io.github.toberocat.toberocore.command.SubCommand;
import io.github.toberocat.toberocore.command.arguments.Argument;
import io.github.toberocat.toberocore.command.exceptions.CommandException;
import io.github.toberocat.toberocore.command.options.Options;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

public class ReloadCommand extends SubCommand {
public ReloadCommand() {
super("reload");
}

@Override
protected @NotNull Options options() {
return new Options();
}

@Override
protected @NotNull Argument<?>[] arguments() {
return new Argument[0];
}

@Override
protected boolean handleCommand(@NotNull CommandSender sender, @NotNull String[] strings) throws CommandException {
try {
GuiEngineApiPlugin.getPlugin().reloadConfig();

for (GuiEngineApi api : GuiEngineApi.APIS.values())
api.reload();
sender.sendMessage("§aReloaded GUI APIs.");
} catch (GuiIORuntimeException e) {
sender.sendMessage(e.getMessage());
}
return true;
}
}
Loading

0 comments on commit b349364

Please sign in to comment.