Plugin Executor not set up right???

Discussion in 'Plugin Development' started by Lightcaster5, Jul 25, 2019.

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

    Lightcaster5

    I can't figure out what im doing wrong. Please help
    Main Class (open)

    Code:
    package me.lightcaster5.randomspawn;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.getCommand("rtp").setExecutor(new SetRandomCMD());
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        public void tpRandom(Player p, double pos1x, double pos1z, double pos2x, double pos2z) {
            int Tpos1x = (int) pos1x;
            int Tpos1z = (int) pos1z;
            int Tpos2x = (int) pos2x;
            int Tpos2z = (int) pos2z;
    
            int maxX = Tpos2x, maxZ = Tpos2z, minX = Tpos1x, minZ = Tpos1z;
    
            Random rand = new Random();
    
            if (Tpos1x > Tpos2x) {
                maxX = Tpos1x;
                minX = Tpos2x;
            }
            if (Tpos1z > Tpos2z) {
                maxZ = Tpos1z;
                minZ = Tpos2z;
            }
    
            Double Tposx = new Double((rand.nextInt(maxX - minX + 1) + minX));
            Double Tposz = new Double((rand.nextInt(maxZ - minZ + 1) + minZ));
    
            Location loc = new Location(p.getWorld(), Tposx.doubleValue() + 0.5D, 500.0D, Tposz.doubleValue() + 0.5D);
    
            p.teleport(loc);
            Block block = p.getLocation().getBlock().getRelative(BlockFace.DOWN);
    
            while (block.getType() == Material.AIR) {
                Location location = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() - 1.0D,
                        p.getLocation().getZ());
                p.teleport(location);
                p.setFallDistance(-500.0F);
                block = p.getLocation().getBlock().getRelative(BlockFace.DOWN);
            }
    
            if (getConfig().getBoolean("enable-blacklist")) {
                ArrayList<String> blocks = (ArrayList<String>) getConfig().getStringList("blocks");
                for (int i = 0; i < blocks.size(); i++) {
                    Material b = Material.getMaterial((String) blocks.get(i));
                    if (block.getType() == b) {
                        tpRandom(p, getConfig().getDouble("pos1.x"), getConfig().getDouble("pos1.z"),
                                getConfig().getDouble("pos2.x"), getConfig().getDouble("pos2.z"));
                    }
                }
            }
        }
    
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if (!p.hasPlayedBefore()) {
                tpRandom(p, getConfig().getDouble("pos1.x"), getConfig().getDouble("pos1.z"),
                        getConfig().getDouble("pos2.x"), getConfig().getDouble("pos2.z"));
            }
        }
    
        public void onPlayerDeath(PlayerRespawnEvent e) {
            Player p = e.getPlayer();
            if (p.getBedSpawnLocation() == null) {
                Main.this.tpRandom(p, Main.this.getConfig().getDouble("pos1.x"), Main.this.getConfig().getDouble("pos1.z"),
                        Main.this.getConfig().getDouble("pos2.x"), Main.this.getConfig().getDouble("pos2.z"));
            }
        }
    }
    


    SetRandomCMD Class (open)

    Code:
    package me.lightcaster5.randomspawn;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class SetRandomCMD implements CommandExecutor {
        private Main plugin;
    
        public void RandomSpawn(Main plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players can run this command!");
            } else {
                Player p = (Player) sender;
                if (label.equalsIgnoreCase("rtp") && p.hasPermission("rtp.setpos")) {
                    if (args[0].equalsIgnoreCase("pos1")) {
                        double posX = p.getLocation().getX();
                        double posZ = p.getLocation().getZ();
    
                        posX = Math.floor(posX);
                        posZ = Math.floor(posZ);
    
                        this.plugin.getConfig().set("pos1.x", Double.valueOf(posX));
                        this.plugin.getConfig().set("pos1.z", Double.valueOf(posZ));
                        this.plugin.saveConfig();
                        this.plugin.reloadConfig();
                        p.sendMessage(
                                "§7First position set! (" + Double.toString(posX) + ", " + Double.toString(posZ) + ")");
    
                    } else if (args[0].equalsIgnoreCase("pos2")) {
                        double posX = p.getLocation().getX();
                        double posZ = p.getLocation().getZ();
    
                        posX = Math.floor(posX);
                        posZ = Math.floor(posZ);
    
                        this.plugin.getConfig().set("pos2.x", Double.valueOf(posX));
                        this.plugin.getConfig().set("pos2.z", Double.valueOf(posZ));
                        this.plugin.saveConfig();
                        this.plugin.reloadConfig();
                        p.sendMessage(
                                "§7Second position set! (" + Double.toString(posX) + ", " + Double.toString(posZ) + ")");
    
                    } else if (args[0] == null) {
                        p.sendMessage("§cIncorrect Usage! Please do /rtp help for help.");
    
                    } else if (args[0] == "reload") {
                        this.plugin.saveConfig();
                        this.plugin.reloadConfig();
                        p.sendMessage("§fConfig has been reloaded!");
    
                    } else if (args[0] == "help") {
                        p.sendMessage("§7---------- §fRandom Teleport Help §7----------");
                        p.sendMessage("§f/rtp help §7- §fDisplays the help page");
                        p.sendMessage(
                                "§f/rtp §7<§fpos1§7/§fpos2§7> - §fSets the first or second position of the spawn area");
                        p.sendMessage("§f/rtp reload §7- §fReloads the config");
                    }
    
                } else if (!p.hasPermission("rtp.setpos")) {
                    p.sendMessage("§fUnknown command. Type \"/help\" for help.");
                } else {
                    p.sendMessage("§fUnknown command. Type \"/help\" for help.");
                }
            }
            return true;
        }
    }


    I keep getting this error in my console with an in-game message of "An internal error occurred while attempting to perform this command"

    Console Output (open)

    Code:
    25.07 17:32:00 [Server] ERROR null
    25.07 17:32:00 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'rtp' in plugin RandomSpawn v1.0
    25.07 17:32:00 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1401) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1236) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_191]
    25.07 17:32:00 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_191]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]
    25.07 17:32:00 [Server] INFO Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    25.07 17:32:00 [Server] INFO at me.lightcaster5.randomspawn.SetRandomCMD.onCommand(SetRandomCMD.java:21) ~[?:?]
    25.07 17:32:00 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:32:00 [Server] INFO ... 15 more
     
  2. Offline

    KarimAKL

    @Lightcaster5 You are trying to get the first argument without checking the length of the arguments first.
    The error is at this line:
    Code:Java
    1. if (args[0].equalsIgnoreCase("pos1")) {

    EDIT: I just looked a little more on your code, and it seems you are trying to check if the argument is null, you can't do that because it still tries to get the argument, instead you should use args.length.
    EDIT2: You should also compare strings with 'equals' instead of '==', what you are doing right now will always return false.
     
  3. Offline

    Lightcaster5

    OMG I have been trying to fix this for days now and thats it! Thanks so much!!!

    Edit:
    A few more issues arose...
    When I do /rtp <reload/help> I get no output in chat or the console and when I do /rtp <pos1/pos2> I get this error in the console:
    Code:
    25.07 17:57:41 [Server] ERROR null
    25.07 17:57:41 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'rtp' in plugin RandomSpawn v1.0
    25.07 17:57:41 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1401) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1236) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_191]
    25.07 17:57:41 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_191]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]
    25.07 17:57:41 [Server] INFO Caused by: java.lang.NullPointerException
    25.07 17:57:41 [Server] INFO at me.lightcaster5.randomspawn.SetRandomCMD.onCommand(SetRandomCMD.java:44) ~[?:?]
    25.07 17:57:41 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    25.07 17:57:41 [Server] INFO ... 15 more
     
    Last edited: Jul 25, 2019
    KarimAKL likes this.
  4. Offline

    KarimAKL

    That's probably because you haven't changed from '==' to 'equalsIgnoreCase()'. (see my second edit on the other post)
    What's your updated code? (The line number doesn't match if you've updated the code)
     
  5. Offline

    Lightcaster5

    Sorry for the delayed response...

    I changed the /rtp <reload/help> but the /rtp <pos1/pos2> still doesnt work
    New code:
    Code:
    package me.lightcaster5.randomspawn;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class SetRandomCMD implements CommandExecutor {
        public Main plugin;
    
        public void RandomSpawn(Main plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players can run this command!");
            } else {
                Player p = (Player) sender;
                if (label.equalsIgnoreCase("rtp") && p.hasPermission("rtp.setpos")) {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("pos1")) {
                            double posX = p.getLocation().getX();
                            double posZ = p.getLocation().getZ();
    
                            posX = Math.floor(posX);
                            posZ = Math.floor(posZ);
    
                            this.plugin.getConfig().set("pos1.x", Double.valueOf(posX));
                            this.plugin.getConfig().set("pos1.z", Double.valueOf(posZ));
                            this.plugin.saveConfig();
                            this.plugin.reloadConfig();
                            p.sendMessage(
                                    "§7First position set! (" + Double.toString(posX) + ", " + Double.toString(posZ) + ")");
                            return true;
    
                        } else if (args[0].equalsIgnoreCase("pos2")) {
                            double posX = p.getLocation().getX();
                            double posZ = p.getLocation().getZ();
    
                            posX = Math.floor(posX);
                            posZ = Math.floor(posZ);
    
                            this.plugin.getConfig().set("pos2.x", Double.valueOf(posX));
                            this.plugin.getConfig().set("pos2.z", Double.valueOf(posZ));
                            this.plugin.saveConfig();
                            this.plugin.reloadConfig();
                            p.sendMessage("§7Second position set! (" + Double.toString(posX) + ", " + Double.toString(posZ)
                                    + ")");
                            return true;
    
                        } else if (args[0].equalsIgnoreCase("reload")) {
                            this.plugin.saveConfig();
                            this.plugin.reloadConfig();
                            p.sendMessage("§fConfig has been reloaded!");
                            return true;
    
                        } else if (args[0].equalsIgnoreCase("help")) {
                            p.sendMessage("§7---------- §fRandom Teleport Help §7----------");
                            p.sendMessage("§f/rtp help §7- §fDisplays the help page");
                            p.sendMessage(
                                    "§f/rtp §7<§fpos1§7/§fpos2§7> - §fSets the first or second position of the spawn area");
                            p.sendMessage("§f/rtp reload §7- §fReloads the config");
                            return true;
                        }
                    } else if (args.length == 0) {
                        p.sendMessage("§cIncorrect usage! Please type \"/rtp help\" for help.");
                    }
    
                } else if (!p.hasPermission("rtp.setpos")) {
                    p.sendMessage("§fUnknown command. Type \"/help\" for help.");
                } else {
                    p.sendMessage("§fUnknown command. Type \"/help\" for help.");
                }
            }
            return true;
        }
    }
     
  6. Offline

    KarimAKL

    @Lightcaster5 According to the error from before, the exception is thrown at line 44, which is this:
    Code:Java
    1. this.plugin.getConfig().set("pos2.x", Double.valueOf(posX));

    Unless you've changed the code again, i think 'plugin' is null; can you show your main class? (when you register the command)
     
  7. Offline

    Lightcaster5

    Yes, here is what I currently have:
    Code:
    package me.lightcaster5.randomspawn;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.getCommand("rtp").setExecutor(new SetRandomCMD());
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        public void tpRandom(Player p, double pos1x, double pos1z, double pos2x, double pos2z) {
            int Tpos1x = (int) pos1x;
            int Tpos1z = (int) pos1z;
            int Tpos2x = (int) pos2x;
            int Tpos2z = (int) pos2z;
    
            int maxX = Tpos2x, maxZ = Tpos2z, minX = Tpos1x, minZ = Tpos1z;
    
            Random rand = new Random();
    
            if (Tpos1x > Tpos2x) {
                maxX = Tpos1x;
                minX = Tpos2x;
            }
            if (Tpos1z > Tpos2z) {
                maxZ = Tpos1z;
                minZ = Tpos2z;
            }
    
            Double Tposx = new Double((rand.nextInt(maxX - minX + 1) + minX));
            Double Tposz = new Double((rand.nextInt(maxZ - minZ + 1) + minZ));
    
            Location loc = new Location(p.getWorld(), Tposx.doubleValue() + 0.5D, 500.0D, Tposz.doubleValue() + 0.5D);
    
            p.teleport(loc);
            Block block = p.getLocation().getBlock().getRelative(BlockFace.DOWN);
    
            while (block.getType() == Material.AIR) {
                Location location = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() - 1.0D,
                        p.getLocation().getZ());
                p.teleport(location);
                p.setFallDistance(-500.0F);
                block = p.getLocation().getBlock().getRelative(BlockFace.DOWN);
            }
    
            if (getConfig().getBoolean("enable-blacklist")) {
                ArrayList<String> blocks = (ArrayList<String>) getConfig().getStringList("blocks");
                for (int i = 0; i < blocks.size(); i++) {
                    Material b = Material.getMaterial((String) blocks.get(i));
                    if (block.getType() == b) {
                        tpRandom(p, getConfig().getDouble("pos1.x"), getConfig().getDouble("pos1.z"),
                                getConfig().getDouble("pos2.x"), getConfig().getDouble("pos2.z"));
                    }
                }
            }
        }
    
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if (!p.hasPlayedBefore()) {
                tpRandom(p, getConfig().getDouble("pos1.x"), getConfig().getDouble("pos1.z"),
                        getConfig().getDouble("pos2.x"), getConfig().getDouble("pos2.z"));
            }
        }
    
        public void onPlayerDeath(PlayerRespawnEvent e) {
            Player p = e.getPlayer();
            if (p.getBedSpawnLocation() == null) {
                Main.this.tpRandom(p, Main.this.getConfig().getDouble("pos1.x"), Main.this.getConfig().getDouble("pos1.z"),
                        Main.this.getConfig().getDouble("pos2.x"), Main.this.getConfig().getDouble("pos2.z"));
            }
        }
    }
    
     
  8. Offline

    KarimAKL

    @Lightcaster5 You are initializing 'plugin' in your method 'RandomSpawn(Main)', but it's never called. Remove the method 'RandomSpawn(Main)' and add a constructor 'SetRandomCMD(Main)'.
     
  9. Offline

    Lightcaster5

    Wait so I removed this from the SetRandomCMD:
    Code:
        public void RandomSpawn(Main plugin) {
            this.plugin = plugin;
        }
    
    And what am I to add in the main class?
     
  10. Offline

    KarimAKL

    @Lightcaster5 In the main class you simply do as you've already done, just add the arguments to match the SetRandomCMD constructor parameters.
     
  11. Offline

    Lightcaster5

    Im really confused... I don't even know if this is what I change or if its right...
    Code:
    this.getCommand("rtp").setExecutor(new SetRandomCMD(//something here//));
    Do I put something in the parenthesis
     
  12. Offline

    KarimAKL

  13. Offline

    Lightcaster5

    This is probably a noob question and im sorry for wasting your time but what do I put? I tried "this" or "main" or "new SetRandomCMD" and all of them where wrong and gave errors.
     
  14. Offline

    KarimAKL

    @Lightcaster5 'this' is correct, did you remember to create the constructor 'SetRandomCMD(Main)', if you forgot that then you'll get an error saying the constructor is undefined.
     
  15. Offline

    Lightcaster5

    Ohhhhhhhhh okay
    And in the constructor I just copy and paste all of the code and put it there?
    Code:
    if (!(sender instanceof Player)) {
                sender.sendMessage("Only players can run this command!");
            } else {
                Player p = (Player) sender;
                if (label.equalsIgnoreCase("rtp") && p.hasPermission("rtp.setpos")) {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("pos1")) {
                            double posX = p.getLocation().getX();
                            double posZ = p.getLocation().getZ();
    
                            posX = Math.floor(posX);
                            posZ = Math.floor(posZ);
    
                            this.plugin.getConfig().set("pos1.x", Double.valueOf(posX));
                            this.plugin.getConfig().set("pos1.z", Double.valueOf(posZ));
                            this.plugin.saveConfig();
                            this.plugin.reloadConfig();
                            p.sendMessage(
                                    "§7First position set! (" + Double.toString(posX) + ", " + Double.toString(posZ) + ")");
                            return true;
    
                        } else if (args[0].equalsIgnoreCase("pos2")) {
                            double posX = p.getLocation().getX();
                            double posZ = p.getLocation().getZ();
    
                            posX = Math.floor(posX);
                            posZ = Math.floor(posZ);
    
                            this.plugin.getConfig().set("pos2.x", Double.valueOf(posX));
                            this.plugin.getConfig().set("pos2.z", Double.valueOf(posZ));
                            this.plugin.saveConfig();
                            this.plugin.reloadConfig();
                            p.sendMessage("§7Second position set! (" + Double.toString(posX) + ", " + Double.toString(posZ)
                                    + ")");
                            return true;
    
                        } else if (args[0].equalsIgnoreCase("reload")) {
                            this.plugin.saveConfig();
                            this.plugin.reloadConfig();
                            p.sendMessage("§fConfig has been reloaded!");
                            return true;
    
                        } else if (args[0].equalsIgnoreCase("help")) {
                            p.sendMessage("§7---------- §fRandom Teleport Help §7----------");
                            p.sendMessage("§f/rtp help §7- §fDisplays the help page");
                            p.sendMessage(
                                    "§f/rtp §7<§fpos1§7/§fpos2§7> - §fSets the first or second position of the spawn area");
                            p.sendMessage("§f/rtp reload §7- §fReloads the config");
                            return true;
                        }
                    } else if (args.length == 0) {
                        p.sendMessage("§cIncorrect usage! Please type \"/rtp help\" for help.");
                    }
    
                } else if (!p.hasPermission("rtp.setpos")) {
                    p.sendMessage("§fUnknown command. Type \"/help\" for help.");
                } else {
                    p.sendMessage("§fUnknown command. Type \"/help\" for help.");
                }
            }
            return true;
     
  16. Offline

    KarimAKL

    @Lightcaster5 No, you should initialize your 'plugin' variable inside the constructor.
     
  17. Offline

    Lightcaster5

    @KarimAKL
    Code:
    new SetRandomCMD(main);
    this?

    Doesn't throw any errors in eclipse
     
  18. Offline

    KarimAKL

    @Lightcaster5
    Inside your SetRandomCMD class:
    Code:Java
    1. // Declare the variable
    2. public Main plugin;
    3.  
    4. public SetRandomCMD(Main plugin) {
    5. // Initialize it when you create a new instance of this object
    6. this.plugin = plugin;
    7. }

    Inside your Main class:
    Code:Java
    1. // Register the command with a new instance of the SetRandomCMD object
    2. this.getCommand("rtp").setExecutor(new SetRandomCMD(this));
     
  19. Offline

    Lightcaster5

    Thank you so much. Sorry for all the trouble it just takes a bit more to get me to learn something. Glad you helped me though!
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page