From 8d8d971ae557998a319b200a8950840180a4a817 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 23 Sep 2025 05:52:27 +0000 Subject: [PATCH] Refactor project structure for maintainability Separated the command logic from the main plugin class into a dedicated `SetBlockCommand` class within a new `commands` package. This refactoring improves code organization and makes the project more scalable and easier to maintain. Changes include: - Created `src/main/java/com/indra87g/commands/SetBlockCommand.java` - Modified `src/main/java/com/indra87g/Main.java` to use the new command class - Updated `src/main/resources/plugin.yml` to declare the command and add permissions --- src/main/java/com/indra87g/Main.java | 45 ++--------------- .../indra87g/commands/SetBlockCommand.java | 48 +++++++++++++++++++ src/main/resources/plugin.yml | 9 ++++ 3 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/indra87g/commands/SetBlockCommand.java diff --git a/src/main/java/com/indra87g/Main.java b/src/main/java/com/indra87g/Main.java index d05bab8..2ccae0c 100644 --- a/src/main/java/com/indra87g/Main.java +++ b/src/main/java/com/indra87g/Main.java @@ -1,52 +1,13 @@ package com.indra87g; -import cn.nukkit.block.Block; -import cn.nukkit.level.Level; -import cn.nukkit.math.Vector3; import cn.nukkit.plugin.PluginBase; -import cn.nukkit.command.Command; -import cn.nukkit.command.CommandSender; -import cn.nukkit.Player; +import com.indra87g.commands.SetBlockCommand; public class Main extends PluginBase { @Override public void onEnable() { - getLogger().info("plugin activated!"); - - this.getServer().getCommandMap().register("setblock", new Command("setblock") { - @Override - public boolean execute(CommandSender sender, String label, String[] args) { - if (!(sender instanceof Player)) { - sender.sendMessage("This command can only be used by player."); - return false; - } - - Player player = (Player) sender; - Level level = player.getLevel(); - - if (args.length != 4) { - player.sendMessage("§cusage: /setblock "); - return false; - } - - try { - int x = Integer.parseInt(args[0]); - int y = Integer.parseInt(args[1]); - int z = Integer.parseInt(args[2]); - int blockId = Integer.parseInt(args[3]); - - Vector3 pos = new Vector3(x, y, z); - Block block = Block.get(blockId); - - level.setBlock(pos, block); - player.sendMessage("§aBlock " + block.getName() + " successfully placed in (" + x + ", " + y + ", " + z + ")"); - } catch (NumberFormatException e) { - player.sendMessage("§cMake sure all arguments are valid numbers. "); - } - - return true; - } - }); + getLogger().info("ContohPlugin has been enabled."); + this.getServer().getCommandMap().register("setblock", new SetBlockCommand()); } } diff --git a/src/main/java/com/indra87g/commands/SetBlockCommand.java b/src/main/java/com/indra87g/commands/SetBlockCommand.java new file mode 100644 index 0000000..9aa4b21 --- /dev/null +++ b/src/main/java/com/indra87g/commands/SetBlockCommand.java @@ -0,0 +1,48 @@ +package com.indra87g.commands; + +import cn.nukkit.Player; +import cn.nukkit.block.Block; +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; +import cn.nukkit.level.Level; +import cn.nukkit.math.Vector3; + +public class SetBlockCommand extends Command { + + public SetBlockCommand() { + super("setblock", "Sets a block at a specific location", "/setblock "); + } + + @Override + public boolean execute(CommandSender sender, String commandLabel, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("This command can only be used by a player."); + return false; + } + + Player player = (Player) sender; + Level level = player.getLevel(); + + if (args.length != 4) { + player.sendMessage("§cUsage: " + this.getUsage()); + return false; + } + + try { + int x = Integer.parseInt(args[0]); + int y = Integer.parseInt(args[1]); + int z = Integer.parseInt(args[2]); + int blockId = Integer.parseInt(args[3]); + + Vector3 pos = new Vector3(x, y, z); + Block block = Block.get(blockId); + + level.setBlock(pos, block); + player.sendMessage("§aBlock " + block.getName() + " successfully placed at (" + x + ", " + y + ", " + z + ")"); + } catch (NumberFormatException e) { + player.sendMessage("§cPlease provide valid numbers for coordinates and block ID."); + } + + return true; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index deae2b8..d58073f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,3 +4,12 @@ version: 0.1.0 api: 1.0.0 author: indra87g description: Plugin Nukkit dari GitHub Actions +commands: + setblock: + description: "Sets a block at a specific location" + usage: "/setblock " + permission: contohplugin.command.setblock +permissions: + contohplugin.command.setblock: + description: "Allows the user to use the /setblock command" + default: op