Commands Broken?

Discussion in 'Plugin Development' started by Is3kai, Mar 2, 2024.

Thread Status:
Not open for further replies.
  1. Offline

    Is3kai

    Hi! Would someone mind taking a look at these codes and letting me know why the command isn't being recognized? I was working on this for 4-5 hours this morning, and though it best to get some new eyes on the problem, see if i can get a fresh view.

    Commands Code:
    Code:
    package org.smpmakers.dimensionsmp.commands;
    
    import net.kyori.adventure.text.Component;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.smpmakers.dimensionsmp.Items.ItemManager;
    
    public class GiveWarpItem implements CommandExecutor {
    
    @Overridepublic boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!(sender instanceof Player)) {
    sender.sendMessage(Component.text("Only Players can Use this command."));
    return true;
    }
    
    Player player = (Player) sender;
    
    if (args.length == 0) {
    player.sendMessage(Component.text("Usage: /givewarp <warpNumber>"));
    return true;
    }
    
    int warpNumber;
    try {
    warpNumber = Integer.parseInt(args[0]);
    } catch (NumberFormatException e) {
    player.sendMessage(Component.text("Invalid warp number!"));
    return true;
    }
    
    // Give warp item based on warp numberItemStack warpItem = null;
    switch (warpNumber) {
    case 1:
    warpItem = ItemManager.warp1;
    break;
    case 2:
    warpItem = ItemManager.warp2;
    break;
    case 3:
    warpItem = ItemManager.warp3;
    break;
    case 4:
    warpItem = ItemManager.warp4;
    break;
    case 5:
    warpItem = ItemManager.warp5;
    break;
    case 6:
    warpItem = ItemManager.warp6;
    break;
    case 7:
    warpItem = ItemManager.warp7;
    break;
    case 8:
    warpItem = ItemManager.warp8;
    break;
    default:
    player.sendMessage("Invalid warp number!");
    return true;
    }
    
    if (warpItem != null) {
    player.getInventory().addItem(warpItem);
    player.sendMessage(Component.text("Invalid warp number!"));
    } else {
    player.sendMessage(Component.text("Warp Item not found."));
    }
    
    return true;
    }
    }
    Plugin YML:
    name: DimensionSMP
    version: '${project.version}'main: org.smpmakers.dimensionsmp.DimensionSMP
    api-version: '1.20'commands:
    givewarp:
    description: Gives a specific warp item.
    aliases: gw
    usage: /givewarp <warpNumber>

    Code Initialization:
    Code:
    PluginCommand giveWarpCommand = getCommand("givewarp");
    if (giveWarpCommand != null) {
    giveWarpCommand.setExecutor(new GiveWarpItem());
    } else {
    getLogger().warning("Failed to register 'givewarp' command!");
    }
     
    Last edited: Mar 2, 2024
  2. Offline

    timtower Administrator Administrator Moderator

    @Is3kai 1. [code]<code here>[/code] for nice formatting
    2. Any errors in the console?
     
  3. Offline

    Is3kai

    below is the error received on the console
    Code:
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:287) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]at io.papermc.paper.plugin.manager.PaperPluginInstanceManager.enablePlugin(PaperPluginInstanceManager.java:188) ~[paper-1.20.4.jar:git-Paper-430]at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.enablePlugin(PaperPluginManagerImpl.java:104) ~[paper-1.20.4.jar:git-Paper-430]at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:507) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugin(CraftServer.java:639) ~[paper-1.20.4.jar:git-Paper-430]at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugins(CraftServer.java:550) ~[paper-1.20.4.jar:git-Paper-430]at net.minecraft.server.MinecraftServer.loadWorld0(MinecraftServer.java:671) ~[paper-1.20.4.jar:git-Paper-430]at net.minecraft.server.MinecraftServer.loadLevel(MinecraftServer.java:431) ~[paper-1.20.4.jar:git-Paper-430]at net.minecraft.server.dedicated.DedicatedServer.initServer(DedicatedServer.java:309) ~[paper-1.20.4.jar:git-Paper-430]at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1131) ~[paper-1.20.4.jar:git-Paper-430]at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:319) ~[paper-1.20.4.jar:git-Paper-430]at java.lang.Thread.run(Thread.java:833) ~[?:?]
     
    Last edited by a moderator: Mar 3, 2024
  4. Offline

    timtower Administrator Administrator Moderator

    Please post your full server log using https://pastebin.com or similar.
    People tend to trim away the wrong parts.
     
  5. Offline

    Is3kai

  6. Offline

    timtower Administrator Administrator Moderator

    That is impressive, never seen something like that, does it only happen if your plugin is there?
     
  7. Offline

    Is3kai

    Yep. No clue why. every other part of the plugin works, but the commands just break it
     
  8. Offline

    timtower Administrator Administrator Moderator

    Show your full onEnable
     
  9. Offline

    Is3kai

    Alright, here is the full one. I had to alter it to get the error. If I had the nullable, it just doesnt find the plugin. Anyways,

    Code:
    
    package org.smpmakers.dimensionsmp;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.smpmakers.dimensionsmp.Handlers.EventScript;
    import org.smpmakers.dimensionsmp.Handlers.PlayerHandler;
    import org.smpmakers.dimensionsmp.Items.ItemManager;
    import org.smpmakers.dimensionsmp.commands.GiveWarpItem;
    
    public final class DimensionSMP extends JavaPlugin {
    
    @Override
    public void onEnable() {
    // Plugin startup logic
    getServer().getLogger().info("Plugin Loaded");
    getCommand("givewarp").setExecutor(new GiveWarpItem());
    ItemManager.init();
    
    
    new PlayerHandler(this);
    new EventScript(this);
    
    }
    
    @Override
    public void onDisable() {
    // Plugin shutdown logic
    getServer().getLogger().info("Plugin Shutdown");
    }
    
    
    }
    
    prior to this, the onEnable was
    Code:
    @Override
    public void onEnable() {
    // Plugin startup logic
    getServer().getLogger().info("Plugin Loaded");
    PluginCommand giveWarpCommand = getCommand("givewarp");
    if (giveWarpCommand != null) {
    giveWarpCommand.setExecutor(new GiveWarpItem());
    } else {
    getLogger().warning("Failed to register 'givewarp' command!");
    }
    ItemManager.init();
    
    
    new PlayerHandler(this);
    new EventScript(this);
    
    }
    
    so it would run even if the command wasn't found.
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Is3kai That makes me think that the plugin.yml is wrong.
    And please don't log your own plugins, Bukkit does that for you already. After it is loaded, not when it starts loading and fails (in your case)
     
  11. Offline

    Is3kai

    Alright. I dont see how the yml would be wrong, but here it is.
    Code:
    name: DimensionSMP
    version: '${project.version}'
    main: org.smpmakers.dimensionsmp.DimensionSMP
    api-version: '1.20'commands:
    givewarp:
    description: Gives a specific warp item.
    aliases: gw
    usage: /givewarp <warpNumber>
    
     
  12. Offline

    timtower Administrator Administrator Moderator

    I don't see any spaces here, is that how it is?
    Not to mention the commands node being attached to the api-version
     
  13. Offline

    Is3kai

    Hold on, It just didn't copy correctly. Let me quick fix that:
    Code:
    name: DimensionSMP
    version: '${project.version}'
    main: org.smpmakers.dimensionsmp.DimensionSMP
    api-version: '1.20'
    commands:
      givewarp:
        description: Gives a specific warp item.
        aliases: gw
        usage: /givewarp <warpNumber>
    
     
  14. Offline

    timtower Administrator Administrator Moderator

    @Is3kai Comment the code that loads the command, login, can you tab complete the command?
    Does the server know it is there?
     
  15. Offline

    Is3kai

    It does not.
     
  16. Offline

    timtower Administrator Administrator Moderator

    What does the exported plugin.yml look like?
     
  17. Offline

    Is3kai

    I don't even see the plugin.yml in the plugin folder of the server, so that may be the reason lmao
     
  18. Offline

    timtower Administrator Administrator Moderator

    plugin.yml is inside the jar, not in a folder.
     
  19. Offline

    Is3kai

    It reads:
    Code:
    
    name: DimensionSMP
    version: '0.5-SNAPSHOT'
    main: org.smpmakers.dimensionsmp.DimensionSMP
    api-version: '1.20'
    commands:
      givewarp:
        description: Gives a specific warp item.
        aliases: gw
        usage: /givewarp <warpNumber>
    
     
  20. Offline

    timtower Administrator Administrator Moderator

    @Is3kai Can you run it on Spigot or CraftBukkit instead of Paper?
    Might be that Paper is doing silly stuff.
     
  21. Offline

    Is3kai

    I can try.

    It works, spigot registers it, but now the arguments don't function when I test them. its supposed to accepts something like /giveitems "warps" 8 and give the warp8 item. this is the code below:

    Code:
    package org.smpmakers.dimensionsmpspigot.commands;
    
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.smpmakers.dimensionsmpspigot.items.ItemManager;
    
    
    public class GivePluginItems implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender commandSender,  Command command,  String s,  String[] strings) {
            if (!(commandSender instanceof Player)) {
                commandSender.sendMessage("Only Players can use this command");
                return true;
            }
            Player player = (Player) commandSender;
    
            if (strings.length != 2) {
                player.sendMessage("Usage: /giveitems <itemType> <typeNum>");
                return true;
            }
    
            String itemType = strings[0];
            int typeNum;
            try {
                typeNum = Integer.parseInt(strings[1]);
            } catch (NumberFormatException e) {
                player.sendMessage("Invalid type number.");
                return false;
            }
    
            // Get the custom item based on the itemType and typeNum
            if(itemType.equals("warps")){
                ItemStack warpItem;
                switch (typeNum) {
                    case 1:
                        warpItem = ItemManager.warp1;
                        break;
                    case 2:
                        warpItem = ItemManager.warp2;
                        break;
                    case 3:
                        warpItem = ItemManager.warp3;
                        break;
                    case 4:
                        warpItem = ItemManager.warp4;
                        break;
                    case 5:
                        warpItem = ItemManager.warp5;
                        break;
                    case 6:
                        warpItem = ItemManager.warp6;
                        break;
                    case 7:
                        warpItem = ItemManager.warp7;
                        break;
                    case 8:
                        warpItem = ItemManager.warp8;
                        break;
                    default:
                        player.sendMessage("Invalid warp number!");
                        return true;
                }
            }else if(itemType.equals("tablets")){
                ItemStack totemItem = null;
                switch (typeNum) {
                    case 1:
                        totemItem = ItemManager.totem1;
                        break;
                    case 2:
                        totemItem = ItemManager.totem2;
                        break;
                    case 3:
                        totemItem = ItemManager.totem3;
                        break;
                    case 4:
                        totemItem = ItemManager.totem4;
                        break;
                    case 5:
                        totemItem = ItemManager.totem5;
                        break;
                    case 6:
                        totemItem = ItemManager.totem6;
                        break;
                    case 7:
                        totemItem = ItemManager.totem7;
                        break;
                    case 8:
                        totemItem = ItemManager.totem8;
                        break;
                    default:
                        player.sendMessage("Invalid tablet number!");
                        return true;
                }
            }
    
            return false;
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 4, 2024
  22. Offline

    timtower Administrator Administrator Moderator

    @Is3kai Did you remember to turn on the command registration again?
    And on which check does it fail?
     
  23. Offline

    Is3kai

    The command registers, but the actual arguments are not accepted.
     
  24. Offline

    timtower Administrator Administrator Moderator

    Tried printing what the arguments are according to your code?
     
  25. Offline

    Is3kai

    I did, exactly what should have worked. /giveitems warps 8 should have given me the warp8 item, but it just returned the
    Code:
    if (strings.length != 2) {
                player.sendMessage("Usage: /giveitems <itemType> <typeNum>");
                return true;
            }
    part of my code
     
  26. Offline

    timtower Administrator Administrator Moderator

    Considered printing the length? The content?
     
  27. Offline

    Is3kai

    Mind explaining what you mean by that? Your wording is slightly confusing to me.
     
  28. Offline

    timtower Administrator Administrator Moderator

    player.sendMessage(strings.length) before the if statement.
     
  29. Offline

    Is3kai

    It gives me the length of 2, exactly what I'm looking for.
     
  30. Offline

    timtower Administrator Administrator Moderator

    Can you show the code next to a screenshot from the result in-game?
     
Thread Status:
Not open for further replies.

Share This Page