The code is right, but my class won't load (if that is the right term)

Discussion in 'Plugin Development' started by xpaintall, Dec 12, 2020.

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

    xpaintall

    Hello bukkit, xpaintall here.
    So I have been more into coding now and learnt a couple of things, but something that always used to work now doesn't. I am talking about commands. See, throughout the weeks coding I always made the same thing with commands (implements CommandExecutor, public boolean onCommand, etc), but yesterday it stopped working. At first, I thought it was the code, so I made it better and still didn't do anything. I copied and replaced the code with a player#SendMessage("") method, but still nothing. I made a whole new plugin and guess what? STILL NOTHING. Now, the plugin loads in the server and the plugin.yml works, but my command classes just don't work. Here is my code if you think that will help:
    commands class:
    Code:
    package com.xpaintall.Main.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class commands implements CommandExecutor{
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            Player player = (Player) sender;
    
            if(cmd.getName().equalsIgnoreCase("rd")) {
    
                Random r = new Random();
    
                World skywars = Bukkit.getWorld("Skywars");
                World lobby = Bukkit.getWorld("lobby");
    
                List<Location> e = new ArrayList<Location>();
                e.add(new Location(lobby, 292, 11, 1920, -180, 0));
                e.add(new Location(skywars, 1, 29, 2, 0, 0));
    
                player.teleport(e.get(r.nextInt(e.size())));
            }
    
    
            return true;
        }
    }
    
    main class:
    Code:
    package com.xpaintall.Main;
    
    import com.xpaintall.Main.commands.commands;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin {
    
        @Override
        public void onEnable() {
            getCommand("rd").setExecutor(new commands());
            getServer().getConsoleSender().sendMessage("§b[Randomizer]: Loaded!");
        }
    
        @Override
        public void onDisable() {
            getServer().getConsoleSender().sendMessage("§b[Randomizer]: Disabled!");
        }
    
    }
    And lastly, my plugin.yml:
    Code:
    name: Randomizer
    main: com.xpaintall.Main.main
    description: Cool randomizer plugin!
    api-version: 1.8.8
    version: 1.0
    author: xpaintall
    commands:
      rd:
        description: Random world teleportation!
        usage: </random>
    Now, I have tried lots of stuff. I tried reinstalling the spigot 1.8.8 API, I even watched youtube tutorials on how to make a command and I tried executing the command in the console. If this helps, my pc has been acting weird lately but I think that is not the case. Just saying, when I type /rd, I get a blank space in the chat no matter if it is a player#SendMessage or a player#Teleport method, still nothing.
    HELP
     
  2. Online

    timtower Administrator Administrator Moderator

    @xpaintall Don't log your own plugins.
    Check your console.
     
  3. Offline

    xpaintall

    What do you mean by "don't log your own plugins"? I have said that the plugin was loaded into the server, but why should I check my console? I did every time I reloaded the server.
     
  4. Offline

    CraftCreeper6

    @xpaintall
    You don't have to use getServer()...sendMessage() because Bukkit will do that for you.

    Are there any errors in the console?

    From the knowledge of this problem I have had in the past:
    You are getting a space because when you return true, a blank space is printed. You want to change your return true to return false, and add a return true inside of the if statement.

    You want to essentially return if the execution was successful. If it reaches the end of the method, it probably wasn't successful. If it activates one of the if statements, it was probably a success.
     
  5. Offline

    xpaintall

    @CraftCreeper6
    There are no errors in the console, as I have said the plugin loads completely fine, just the command doesn't work. I tried what you told me to do and still nothing (there isn't a blank space anymore).
     
    Last edited: Dec 12, 2020
  6. Offline

    CraftCreeper6

    @xpaintall
    Even if the plugin loads fine that doesn't mean your commands will work fine.

    Run the command, is there any output to the console once you do?

    A good way to check if your command is working is by adding a debug, send the player a message or broadcast a message, helps to narrow down the problem.
     
  7. Offline

    xpaintall

    @CraftCreeper6 I did check the console once I ran the command and I got "xpaintall issued server command: /rd", I made a debug and still nothing.
     
  8. Offline

    CraftCreeper6

    @xpaintall
    Can you send the updated code, the one that includes the debug.
     
  9. Offline

    xpaintall

    Here you go @CraftCreeper6 :
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            Player player = (Player) sender;
    
            if(cmd.getName().equalsIgnoreCase("rd")) {
    
                    Random r = new Random();
    
                    World skywars = Bukkit.getWorld("Skywars");
                    World lobby = Bukkit.getWorld("lobby");
    
                    List<Location> e = new ArrayList<Location>();
                    e.add(new Location(lobby, 292, 11, 1920, -180, 0));
                    e.add(new Location(skywars, 1, 29, 2, 0, 0));
    
                    player.teleport(e.get(r.nextInt(e.size())));
                    Bukkit.broadcastMessage("Working!");
                    return true;
            }
    
    
            return false;
        }
    }
    
     
  10. Offline

    CraftCreeper6

    @xpaintall
    Add another debug before Random r = new Random();
     
  11. Offline

    xpaintall

    I did, still nothing. This is weird
     
  12. Offline

    CraftCreeper6

    @xpaintall
    Then there's no explanation as to why this isn't working.

    Are you using a hosting service or your own machine?
     
  13. Offline

    xpaintall

    I am using my own machine.
    Can I maybe contact you on discord so we can solve this easier?
     
  14. Offline

    CraftCreeper6

    @xpaintall
    Afraid not, it's more useful here in any case as someone may have a similar problem in the future.

    Do the worlds Skywars and lobby definitely exist?
     
  15. Offline

    xpaintall

    Yes, I checked about twice and teleported to them with multiverse
     
  16. Offline

    CraftCreeper6

    @xpaintall
    Then I have no idea. There's no explanation as to why it isn't working from the code you've provided.
     
  17. Offline

    xpaintall

    At this point I am starting to think there's something wrong with the server. Do you need the server log?

    @CraftCreeper6 I MADE IT I FIXEEED IT WHOOOOOOOOOOOOOOOOOOOOOOO.
    The problem was in the plugin.yml (you need to set your API version to 1.8 instead of 1.8.8) and I gave the package that it came from a different name.
    Update: yep, it teleports me randomly

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 12, 2020
  18. Offline

    CraftCreeper6

  19. Offline

    Strahan

    Just a suggestion; you should check if sender is a Player before you cast it and you shouldn't be instantiating a new Random object every time the command is ran. Use ThreadLocalRandom instead.
     
Thread Status:
Not open for further replies.

Share This Page