Solved Some codes.

Discussion in 'Spigot Plugin Development' started by Wilsoon, Jan 27, 2020.

Thread Status:
Not open for further replies.
  1. Code (Java):
    package com.gmail.calorious.start;

    import java.io.File;
    import java.io.IOException;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;

    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle.EnumTitleAction;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    import org.bukkit.block.Block;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
           @Override
           public void onEnable() {
               File config;
               try {
                config = File.createTempFile("config", ".yml");
                boolean exists = config.exists();
                if(exists == true) {
                    getLogger().info("Config found!");
                    getLogger().info("Saving Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");  
                } else {
                    getLogger().info("Creating Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");
                }
            } catch (IOException e) {
                getLogger().info(ChatColor.GOLD + "An exception has occurred while enabling the plugin" + getDescription().getName());
                e.printStackTrace();
            }
             
             
           }
         
           @Override
           public void onDisable() {
               getLogger().info(ChatColor.RED + "SafeHouse Plugin has been disabled.");
           }
           @SuppressWarnings({ "deprecation" })
        @Override
           public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
               Player p = null;
               if(cmd.getName().equalsIgnoreCase("safehouse")) {
                   if(args.length == 1 && args[0].equals("reload")) {
                         reloadConfig();
                         sender.sendMessage(ChatColor.GREEN + "Plugin has been reloaded!");
                   }
                  if(!(sender instanceof Player)) {
                     getLogger().info(ChatColor.RED + "You must be a player to run this command.");
               }
                  if(sender instanceof Player)   {
                     if(args.length == 0) {
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse - Base Command for SafeHouse Plugin");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse (player) - Challenge someone to SafeHouse");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse reload - Reload SafeHouse plugin");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                   }
                     
                                      }
                     if(args.length == 1 && !(args[0].equals("reload"))) {
                         String argsString = String.join("", args[0]);
                         p = Bukkit.getServer().getPlayer(argsString);
                         if(Bukkit.getPlayer(argsString) != null) { //Player is ONLINE!
                            TextComponent acceptmessage = new TextComponent("[Accept]");
                            TextComponent denymessage = new TextComponent("[Deny]");
                            TextComponent spacemessage = new TextComponent(" ");
                            acceptmessage.setColor(ChatColor.GREEN);
                            acceptmessage.setBold(true);
                            acceptmessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/accepted" )); //Insert command after ,
                            acceptmessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to accept!").create()));
                            denymessage.setColor(ChatColor.RED);
                            denymessage.setBold(true);
                            denymessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/declined" )); //Insert command after ,
                            denymessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to deny").create()));
                            p.sendMessage(ChatColor.GOLD + "You have received a SafeHouse challenge from " + sender);
                            p.sendMessage(ChatColor.GOLD + " Press the respective buttons to accept/deny.");
                            p.spigot().sendMessage(acceptmessage, spacemessage, denymessage);
                            //COMPLETE
                         } else if(Bukkit.getOfflinePlayer(argsString) != null) {
                            sender.sendMessage(ChatColor.RED + "This player is offline or does not exist!");
                         }
                       
                       
                     }

                     }
             if(cmd.getName().equalsIgnoreCase("accepted")) {
                 if(!(sender instanceof Player)) {
                     getLogger().info(ChatColor.DARK_RED + "This command is not to be run alone, you are not a player.");
                 }
                 if(sender instanceof Player) {
                     sender.sendMessage(ChatColor.GREEN + "Your challenge to " + p + "has been accepted.");
                     Random random = new Random();
                     if (random.nextBoolean()) {
                         p = (Player) sender; //sender defends
                         sender = (Player) p; //p attacks
                     } else {
                         p = (Player) p;
                         sender = (Player) sender;
                     }
                     sender.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                     p.sendMessage(ChatColor.GREEN + "You have accepted the challenge.");
                     p.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                     Location tplocattacker = p.getLocation();
                     Location tplocdefender = ((Player) sender).getLocation();
                     tplocattacker.setX(getConfig().getDouble("startloc.attacker.x"));
                     tplocattacker.setY(getConfig().getDouble("startloc.attacker.y"));
                     tplocattacker.setZ(getConfig().getDouble("startloc.attacker.z"));
                     tplocdefender.setX(getConfig().getDouble("startloc.defender.x"));
                     tplocdefender.setY(getConfig().getDouble("startloc.defender.y"));
                     tplocdefender.setZ(getConfig().getDouble("startloc.defender.z"));
                     p.teleport(tplocattacker);
                     ((Player) sender).teleport(tplocdefender);
                     p.getInventory().clear();
                     PacketPlayOutTitle countdown3 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"3\",\"bold\":true,\"color\":\"green\"}]"));
                     PacketPlayOutTitle countdown2 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"2\",\"bold\":true,\"color\":\"gold\"}]"));
                     PacketPlayOutTitle countdown1 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"1\",\"bold\":true,\"color\":\"red\"}]"));
                     PacketPlayOutTitle countdownstart = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"Start!\",\"bold\":true,\"color\":\"green\"}]"));
                   
                     ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown3);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown3);
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown2);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown2);
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown1);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown1);
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdownstart);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdownstart);
                     Location attackerdoor = new Location(Bukkit.getWorld("world"), getConfig().getDouble("blockloc.attacker.x"), getConfig().getDouble("blockloc.attacker.y"), getConfig().getDouble("blockloc.attacker.z"));
                     Block attackdoorblock = attackerdoor.getBlock();
                     attackdoorblock.setType(Material.AIR);
                     p.sendMessage(ChatColor.DARK_RED + "You are now able to leave the box, the box will be locked in 30 seconds.");
                     try {
                        Thread.sleep(30000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     attackdoorblock.setType(Material.OBSIDIAN);
                     p.sendMessage(ChatColor.GREEN + "The box has been locked.");
                     sender.sendMessage(ChatColor.GOLD + "SAFEHOUSE PLUGIN HAS TERMINATED.");
                     sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                     p.sendMessage(ChatColor.GOLD + "SAFEHOUSE PLUGIN HAS TERMINATED.");
                     p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                     return true;
                 }
             }
             if(cmd.getName().equalsIgnoreCase("declined")) {
                 sender.sendMessage(ChatColor.RED + "Your challenge to " + p + "has been declined.");
                 p.sendMessage(ChatColor.RED + "You declined the challenge.");
             }
            return true;
    }
    }
     

    This is my full code.
    1. My problem is why is "p" at the accepted command "null"?

    2. Why is it when the area at "You received a challenge from ", sender returns "CraftPlayer{player=(Player Name)}"?

    3. Why is it that sometimes I accept, the code stops at the "Your inventory will be cleared..." then it shows an internal error, this is the error:
    Code (Text):


    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'accepted' in plugin SafestHouse v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[paper.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:146) ~[paper.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:666) ~[paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1189) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1001) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [paper.jar:git-PaperSpigot-"4c7641d"]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:774) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:713) [paper.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:616) [paper.jar:git-PaperSpigot-"4c7641d"]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
            at com.gmail.calorious.start.Main.onCommand(Main.java:123) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[paper.jar:git-PaperSpigot-"4c7641d"]
            ... 15 more
     
     
  2. Im wondering how in the world this is not giving you an error:
    Code (Java):
    if (random.nextBoolean()) {
         p = (Player) sender; //sender defends
         sender = (Player) p; //p attacks
    } else {
         p = (Player) p;
         sender = (Player) sender;
    }
    sender is the method param, and you can't set it. This is probably a big part of your issue.
    because at the time of sending the message, p is never set.

    because you are sending a player object, and CraftPlayer#toString() sends just that, you need to use p.getName()

    Probably because of all the issues in your code.
    I would assume you would get getting errors in your IDE and/or compiling errors.
     
    • Agree Agree x 1
    • Useful Useful x 1
  3. You're converting the sender to a String. What you want is CommandSender#getName
    Look at the line where the error occurs in the stacktrace
    Code (Text):
    Caused by: java.lang.NullPointerException
            at com.gmail.calorious.start.Main.onCommand(Main.java:123) ~[?:?]
    What's line 123 in your Main class?
     
    • Agree Agree x 1
    • Useful Useful x 1
  4. I didn't.
     
  5. p.sendMessage(ChatColor.GREEN + "You have accepted the challenge.");
     
  6. How would I get around this with the two variables without messing up the rest of the code? (My brain almost got fried while doing this cuz I confused p and sender. Yep)

    and this is to answer "How in the world this is not giving me an error"
    upload_2020-1-27_19-8-27.png
     
  7. This is my new code I tried to modify:
    Code (Java):


    package com.gmail.calorious.start;

    import java.io.File;
    import java.io.IOException;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;

    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle.EnumTitleAction;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    import org.bukkit.block.Block;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
           @Override
           public void onEnable() {
               String argsString2 = null;
               String args = null;
               Player p = null;
               File config;
               try {
                config = File.createTempFile("config", ".yml");
                boolean exists = config.exists();
                if(exists == true) {
                    getLogger().info("Config found!");
                    getLogger().info("Saving Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");  
                } else {
                    getLogger().info("Creating Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");
                }
            } catch (IOException e) {
                getLogger().info(ChatColor.GOLD + "An exception has occurred while enabling the plugin" + getDescription().getName());
                e.printStackTrace();
            }
               
             
           }
           
           @Override
           public void onDisable() {
               getLogger().info(ChatColor.RED + "SafeHouse Plugin has been disabled.");
           }
           @SuppressWarnings({ "deprecation" })
        @Override
           public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
               Player p = null;
            if(cmd.getName().equalsIgnoreCase("safehouse")) {
                   if(args.length == 1 && args[0].equals("reload")) {
                        reloadConfig();
                        sender.sendMessage(ChatColor.GREEN + "Plugin has been reloaded!");
                   }
                  if(!(sender instanceof Player)) {
                     getLogger().info(ChatColor.RED + "You must be a player to run this command.");
               }
                  if(sender instanceof Player)   {
                     if(args.length == 0) {
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse - Base Command for SafeHouse Plugin");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse (player) - Challenge someone to SafeHouse");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse reload - Reload SafeHouse plugin");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                   }
                       
                                     }
                     if(args.length == 1 && !(args[0].equals("reload"))) {
                        String argsString = String.join("", args[0]);
                        p = Bukkit.getServer().getPlayer(argsString);
                        if(Bukkit.getPlayer(argsString) != null) { //Player is ONLINE!
                            TextComponent acceptmessage = new TextComponent("[Accept]");
                            TextComponent denymessage = new TextComponent("[Deny]");
                            TextComponent spacemessage = new TextComponent(" ");
                            acceptmessage.setColor(ChatColor.GREEN);
                            acceptmessage.setBold(true);
                            acceptmessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/accepted" )); //Insert command after ,
                            acceptmessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to accept.").create()));
                            denymessage.setColor(ChatColor.RED);
                            denymessage.setBold(true);
                            denymessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/declined" )); //Insert command after ,
                            denymessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to decline.").create()));
                            p.sendMessage(ChatColor.GOLD + "You have received a SafeHouse challenge from " + sender.getName());
                            p.sendMessage(ChatColor.GOLD + " Press the respective buttons to accept/deny.");
                            p.spigot().sendMessage(acceptmessage, spacemessage, denymessage);
                            //COMPLETE
                        } else if(Bukkit.getOfflinePlayer(argsString) != null) {
                            sender.sendMessage(ChatColor.RED + "This player is offline or does not exist!");
                        }
                         
                         
                     }

                     }
            if(cmd.getName().equalsIgnoreCase("accepted")) {
                if(!(sender instanceof Player)) {
                    getLogger().info(ChatColor.DARK_RED + "This command is not to be run alone, you are not a player.");
                }
                if(sender instanceof Player) {
                    Player issuer = Bukkit.getPlayer(sender.toString());
                    p = Bukkit.getPlayer(????);
                    sender.sendMessage(ChatColor.GREEN + "Your challenge to " + p.getName() + "has been accepted.");
                    Random random = new Random();
                    if (random.nextBoolean()) {
                         p = (Player) issuer; //issuer defends (Issuer is the person that sent challenge)
                         issuer = (Player) p; //p attacks (p is the person that receives challenge)
                    } else {
                        p = (Player) p;
                        issuer = (Player) sender;
                    }
                    sender.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                    p.sendMessage(ChatColor.GREEN + "You have accepted the challenge.");
                    p.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                    Location tplocattacker = p.getLocation();
                    Location tplocdefender = ((Player) sender).getLocation();
                    tplocattacker.setX(getConfig().getDouble("startloc.attacker.x"));
                    tplocattacker.setY(getConfig().getDouble("startloc.attacker.y"));
                    tplocattacker.setZ(getConfig().getDouble("startloc.attacker.z"));
                    tplocdefender.setX(getConfig().getDouble("startloc.defender.x"));
                    tplocdefender.setY(getConfig().getDouble("startloc.defender.y"));
                    tplocdefender.setZ(getConfig().getDouble("startloc.defender.z"));
                    p.teleport(tplocattacker);
                    ((Player) issuer).teleport(tplocdefender);
                    p.getInventory().clear();
                    PacketPlayOutTitle countdown3 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"3\",\"bold\":true,\"color\":\"green\"}]"));
                    PacketPlayOutTitle countdown2 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"2\",\"bold\":true,\"color\":\"gold\"}]"));
                    PacketPlayOutTitle countdown1 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"1\",\"bold\":true,\"color\":\"red\"}]"));
                    PacketPlayOutTitle countdownstart = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"Start!\",\"bold\":true,\"color\":\"green\"}]"));
                     
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown3);
                    ((CraftPlayer) issuer).getHandle().playerConnection.sendPacket(countdown3);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown2);
                    ((CraftPlayer) issuer).getHandle().playerConnection.sendPacket(countdown2);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown1);
                    ((CraftPlayer) issuer).getHandle().playerConnection.sendPacket(countdown1);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdownstart);
                    ((CraftPlayer) issuer).getHandle().playerConnection.sendPacket(countdownstart);
                    Location attackerdoor = new Location(Bukkit.getWorld("world"), getConfig().getDouble("blockloc.attacker.x"), getConfig().getDouble("blockloc.attacker.y"), getConfig().getDouble("blockloc.attacker.z"));
                    Block attackdoorblock = attackerdoor.getBlock();
                    attackdoorblock.setType(Material.AIR);
                    p.sendMessage(ChatColor.DARK_RED + "You are now able to leave the box, the box will be locked in 30 seconds.");
                    try {
                        Thread.sleep(30000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    attackdoorblock.setType(Material.OBSIDIAN);
                    p.sendMessage(ChatColor.GREEN + "The box has been locked.");
                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                    return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("declined")) {
                sender.sendMessage(ChatColor.RED + "Your challenge to " + p.getName() + " has been declined.");
                p.sendMessage(ChatColor.RED + "You declined the challenge.");
            }
            return true;
    }
    }
     
    However, I want p in the accepted command part to take on argsString from the safehouse command part. How do I do this?
    I tried different ways but all of them couldn't send and returned a NullPointerException or it said it couldn't convert from Player to String.
    Help?
     
  8. 1. It's null because you don't assign a value to p in your accepted command (but you do it correctly in your safehouse command).

    3. Your IDE doesn't give you an error because this is valid Java code and can sometimes make sense, so don't worry about that. (It just wouldn't compile if sender was final.) However, your logic is flawed. There's a 50% chance that this code block will be executed:
    Code (Text):
    p = (Player) p;
    sender = (Player) sender;
    This makes no sense (and is unnecessary code) because you assign the variables to themselves. p is already null, so it remains null and your exception is thrown. sender already has the value of sender, so this doesn't make sense either.
     
    • Useful Useful x 1
  9. Because I was planning to make a game like one defender and one attacker
    So for example p would be attacker and sender would be defender.
    I didn't know what to do so to make it the same, I just recopied the same variables.


    I don't know how to pass the variable onto accepted so that accepted has the same variable (p), I changed sender to issuer so it's now different and shouldn't intercept with 'sender#sendMessage'.
     
  10. Okay, suppose you have 2 players that you get from your command:

    Code (Java):
    Player attacker = (Player) sender;
    Player defender = Bukkit.getPlayer(args[0]);

    if (random.nextBoolean()) {
        Player tmp = defender;
        defender = attacker;
        attacker = tmp;
    }
    // else {
        // we don't need else because the attacker is already the right attacker
    // }
    Note that this is simplified and does not check of the sender is a player; args[0] exists and the Player can be found by name e.t.c.
     
    • Useful Useful x 1
  11. I don't seem to understand this.
    My aim is to make the randomized 50% if the 50% is on 1:
    p = defender
    issuer = attacker
    and if the 50% is on 2:
    p = attacker
    issuer = defender

    and I don't know how to do it and change my code accordingly?
     
  12. This code literally does what you want. The players (attacker) and (defender) are randomized
     
    • Useful Useful x 1
  13. Oh, okay thanks.



    My code seems to be messed up, do I put that code in accepted section? But it will show an ArrayIndexOutOfBoundsException, as I don't have any argument length there.
    If I put it in the base command (/safehouse), the variable won't pass on to my accepted command.
    Code (Java):

    package com.gmail.calorious.start;

    import java.io.File;
    import java.io.IOException;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;

    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle.EnumTitleAction;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    import org.bukkit.block.Block;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
           @Override
           public void onEnable() {
               String argsString2 = null;
               String args = null;
               Player p = null;
               File config;
               try {
                config = File.createTempFile("config", ".yml");
                boolean exists = config.exists();
                if(exists == true) {
                    getLogger().info("Config found!");
                    getLogger().info("Saving Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");  
                } else {
                    getLogger().info("Creating Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");
                }
            } catch (IOException e) {
                getLogger().info(ChatColor.GOLD + "An exception has occurred while enabling the plugin" + getDescription().getName());
                e.printStackTrace();
            }
               
             
           }
           
           @Override
           public void onDisable() {
               getLogger().info(ChatColor.RED + "SafeHouse Plugin has been disabled.");
           }
           @SuppressWarnings({ "deprecation" })
        @Override
           public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
               Player p = null;
            if(cmd.getName().equalsIgnoreCase("safehouse")) {
                   if(args.length == 1 && args[0].equals("reload")) {
                        reloadConfig();
                        sender.sendMessage(ChatColor.GREEN + "Plugin has been reloaded!");
                   }
                  if(!(sender instanceof Player)) {
                     getLogger().info(ChatColor.RED + "You must be a player to run this command.");
               }
                  if(sender instanceof Player)   {
                     if(args.length == 0) {
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse - Base Command for SafeHouse Plugin");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse (player) - Challenge someone to SafeHouse");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse reload - Reload SafeHouse plugin");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                   }
                       
                                     }
                     if(args.length == 1 && !(args[0].equals("reload"))) {
                        Player attacker = (Player) sender;            //Code wont pass to accepted command..
                        Player defender = Bukkit.getPlayer(args[0]);
                        String argsString = String.join("", args[0]);
                        p = Bukkit.getServer().getPlayer(argsString);
                        if(Bukkit.getPlayer(argsString) != null) { //Player is ONLINE!
                            TextComponent acceptmessage = new TextComponent("[Accept]");
                            TextComponent denymessage = new TextComponent("[Deny]");
                            TextComponent spacemessage = new TextComponent(" ");
                            acceptmessage.setColor(ChatColor.GREEN);
                            acceptmessage.setBold(true);
                            acceptmessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/accepted" )); //Insert command after ,
                            acceptmessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to accept.").create()));
                            denymessage.setColor(ChatColor.RED);
                            denymessage.setBold(true);
                            denymessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/declined" )); //Insert command after ,
                            denymessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to decline.").create()));
                            p.sendMessage(ChatColor.GOLD + "You have received a SafeHouse challenge from " + sender.getName());
                            p.sendMessage(ChatColor.GOLD + " Press the respective buttons to accept/deny.");
                            p.spigot().sendMessage(acceptmessage, spacemessage, denymessage);
                            //COMPLETE
                        } else if(Bukkit.getOfflinePlayer(argsString) != null) {
                            sender.sendMessage(ChatColor.RED + "This player is offline or does not exist!");
                        }
                         
                         
                     }

                     }
            if(cmd.getName().equalsIgnoreCase("accepted")) {
                if(!(sender instanceof Player)) {
                    getLogger().info(ChatColor.DARK_RED + "This command is not to be run alone, you are not a player.");
                }
                if(sender instanceof Player) {
                    Player attacker = (Player) sender;
                    Player defender = Bukkit.getPlayer(args[0]); //Won't work, no args, no args.length required.
                    sender.sendMessage(ChatColor.GREEN + "Your challenge to " + p.getName() + "has been accepted.");
                    Random random = new Random();
                    if (random.nextBoolean()) {
                            Player tmp = defender;
                            defender = attacker;
                            attacker = tmp;
                        }
                    sender.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                    p.sendMessage(ChatColor.GREEN + "You have accepted the challenge.");
                    p.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                    Location tplocattacker = p.getLocation();
                    Location tplocdefender = ((Player) sender).getLocation();
                    tplocattacker.setX(getConfig().getDouble("startloc.attacker.x"));
                    tplocattacker.setY(getConfig().getDouble("startloc.attacker.y"));
                    tplocattacker.setZ(getConfig().getDouble("startloc.attacker.z"));
                    tplocdefender.setX(getConfig().getDouble("startloc.defender.x"));
                    tplocdefender.setY(getConfig().getDouble("startloc.defender.y"));
                    tplocdefender.setZ(getConfig().getDouble("startloc.defender.z"));
                    p.teleport(tplocattacker);
                    ((Player) sender).teleport(tplocdefender);
                    p.getInventory().clear();
                    PacketPlayOutTitle countdown3 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"3\",\"bold\":true,\"color\":\"green\"}]"));
                    PacketPlayOutTitle countdown2 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"2\",\"bold\":true,\"color\":\"gold\"}]"));
                    PacketPlayOutTitle countdown1 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"1\",\"bold\":true,\"color\":\"red\"}]"));
                    PacketPlayOutTitle countdownstart = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"Start!\",\"bold\":true,\"color\":\"green\"}]"));
                     
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown3);
                    ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown3);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown2);
                    ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown2);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdown1);
                    ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown1);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(countdownstart);
                    ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdownstart);
                    Location attackerdoor = new Location(Bukkit.getWorld("world"), getConfig().getDouble("blockloc.attacker.x"), getConfig().getDouble("blockloc.attacker.y"), getConfig().getDouble("blockloc.attacker.z"));
                    Block attackdoorblock = attackerdoor.getBlock();
                    attackdoorblock.setType(Material.AIR);
                    p.sendMessage(ChatColor.DARK_RED + "You are now able to leave the box, the box will be locked in 30 seconds.");
                    try {
                        Thread.sleep(30000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    attackdoorblock.setType(Material.OBSIDIAN);
                    p.sendMessage(ChatColor.GREEN + "The box has been locked.");
                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                    p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                    return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("declined")) {
                sender.sendMessage(ChatColor.RED + "Your challenge to " + p.getName() + " has been declined.");
                p.sendMessage(ChatColor.RED + "You declined the challenge.");
            }
            return true;
    }
    }
     
     
    #13 Wilsoon, Jan 27, 2020
    Last edited: Jan 27, 2020
  14. After you get the player from Bukkit please check if it is null and react accordingly.
     
    • Useful Useful x 1
  15. I keep getting a NullPointerException at Line 119:
    attacker.sendMessage(ChatColor.GREEN + "Your challenge to " + defender.getName() + "has been accepted.");

    Code (Java):
    package com.gmail.calorious.start;

    import java.io.File;
    import java.io.IOException;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;

    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
    import net.minecraft.server.v1_8_R3.PacketPlayOutTitle.EnumTitleAction;
    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    import org.bukkit.block.Block;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
                    private static Main instance;
                    public static Main getInstance() { return instance; }
           @Override
           public void onEnable() {
               Player defender = null;
               File config;
               try {
                config = File.createTempFile("config", ".yml");
                boolean exists = config.exists();
                if(exists == true) {
                    getLogger().info("Config found!");
                    getLogger().info("Saving Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");  
                } else {
                    getLogger().info("Creating Config...");
                    saveDefaultConfig();
                    getLogger().info(ChatColor.GREEN + "SafeHouse Plugin has been enabled.");
                }
            } catch (IOException e) {
                getLogger().info(ChatColor.GOLD + "An exception has occurred while enabling the plugin" + getDescription().getName());
                e.printStackTrace();
            }
             
             
           }
         
           @Override
           public void onDisable() {
               getLogger().info(ChatColor.RED + "SafeHouse Plugin has been disabled.");
           }
           @SuppressWarnings({ "deprecation" })
        @Override
           public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("safehouse")) {

                   if(args.length == 1 && args[0].equals("reload")) {
                         reloadConfig();
                         sender.sendMessage(ChatColor.GREEN + "Plugin has been reloaded!");
                   }
                  if(!(sender instanceof Player)) {
                     getLogger().info(ChatColor.RED + "You must be a player to run this command.");
               }
                  if(sender instanceof Player)   {
                     if(args.length == 0) {
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse - Base Command for SafeHouse Plugin");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse (player) - Challenge someone to SafeHouse");
                         sender.sendMessage(ChatColor.GREEN + "    /safehouse reload - Reload SafeHouse plugin");
                         sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "-----------------------------------");
                         sender.sendMessage(ChatColor.GOLD + "                 SAFEHOUSE                       ");
                   }
                     
                                      }
                     if(args.length == 1 && !(args[0].equals("reload"))) {
                         Player defender = Bukkit.getPlayer(args[0]);
                         String defenderinstring = defender.getName();
                         if(Bukkit.getPlayer(defenderinstring) != null) { //Player is ONLINE!
                            TextComponent acceptmessage = new TextComponent("[Accept]");
                            TextComponent denymessage = new TextComponent("[Deny]");
                            TextComponent spacemessage = new TextComponent(" ");
                            acceptmessage.setColor(ChatColor.GREEN);
                            acceptmessage.setBold(true);
                            acceptmessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/accepted" )); //Insert command after ,
                            acceptmessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to accept.").create()));
                            denymessage.setColor(ChatColor.RED);
                            denymessage.setBold(true);
                            denymessage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/declined" )); //Insert command after ,
                            denymessage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to decline.").create()));
                            defender.sendMessage(ChatColor.GOLD + "You have received a SafeHouse challenge from " + sender.getName());
                            defender.sendMessage(ChatColor.GOLD + " Press the respective buttons to accept/deny.");
                            defender.spigot().sendMessage(acceptmessage, spacemessage, denymessage);
                            //COMPLETE
                         } else if(Bukkit.getOfflinePlayer(defenderinstring) != null) {
                            sender.sendMessage(ChatColor.RED + "This player is offline or does not exist!");
                         }
                       
                       
                     }

            }
            Player attacker = (Player) sender;            //Code wont pass to accepted command..
         Player defender = Bukkit.getPlayer(args.toString());
            if(cmd.getName().equalsIgnoreCase("accepted")) {
                 if(!(sender instanceof Player)) {
                     getLogger().info(ChatColor.DARK_RED + "This command is not to be run alone, you are not a player.");
                 }
               
                 if(sender instanceof Player) {
                     attacker.sendMessage(ChatColor.GREEN + "Your challenge to " + defender.getName() + "has been accepted.");
                     Random random = new Random();
                     if (random.nextBoolean()) {
                            Player tmp = defender;
                            defender = attacker;
                            attacker = tmp;
                        }
                     attacker.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                     defender.sendMessage(ChatColor.GREEN + "You have accepted the challenge.");
                     defender.sendMessage(ChatColor.GOLD + "Your inventory will be cleared as you enter the challenge.");
                     Location tplocattacker = defender.getLocation();
                     Location tplocdefender = ((Player) sender).getLocation();
                     tplocattacker.setX(getConfig().getDouble("startloc.attacker.x"));
                     tplocattacker.setY(getConfig().getDouble("startloc.attacker.y"));
                     tplocattacker.setZ(getConfig().getDouble("startloc.attacker.z"));
                     tplocdefender.setX(getConfig().getDouble("startloc.defender.x"));
                     tplocdefender.setY(getConfig().getDouble("startloc.defender.y"));
                     tplocdefender.setZ(getConfig().getDouble("startloc.defender.z"));
                     defender.teleport(tplocattacker);
                     ((Player) sender).teleport(tplocdefender);
                     defender.getInventory().clear();
                     PacketPlayOutTitle countdown3 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"3\",\"bold\":true,\"color\":\"green\"}]"));
                     PacketPlayOutTitle countdown2 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"2\",\"bold\":true,\"color\":\"gold\"}]"));
                     PacketPlayOutTitle countdown1 = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"1\",\"bold\":true,\"color\":\"red\"}]"));
                     PacketPlayOutTitle countdownstart = new PacketPlayOutTitle(EnumTitleAction.TITLE, ChatSerializer.a("[\"\",{\"text\":\"Start!\",\"bold\":true,\"color\":\"green\"}]"));
                   
                     ((CraftPlayer) defender).getHandle().playerConnection.sendPacket(countdown3);
                     ((CraftPlayer) attacker).getHandle().playerConnection.sendPacket(countdown3);
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     ((CraftPlayer) defender).getHandle().playerConnection.sendPacket(countdown2);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown2);
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     ((CraftPlayer) defender).getHandle().playerConnection.sendPacket(countdown1);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdown1);
                     try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     ((CraftPlayer) defender).getHandle().playerConnection.sendPacket(countdownstart);
                     ((CraftPlayer) sender).getHandle().playerConnection.sendPacket(countdownstart);
                     Location attackerdoor = new Location(Bukkit.getWorld("world"), getConfig().getDouble("blockloc.attacker.x"), getConfig().getDouble("blockloc.attacker.y"), getConfig().getDouble("blockloc.attacker.z"));
                     Block attackdoorblock = attackerdoor.getBlock();
                     attackdoorblock.setType(Material.AIR);
                     defender.sendMessage(ChatColor.DARK_RED + "You are now able to leave the box, the box will be locked in 30 seconds.");
                     try {
                        Thread.sleep(30000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     attackdoorblock.setType(Material.OBSIDIAN);
                     defender.sendMessage(ChatColor.GREEN + "The box has been locked.");
                     sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                     defender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "This plugin is no longer managing the game.");
                     return true;
                 }
             }
             if(cmd.getName().equalsIgnoreCase("declined")) {
                 sender.sendMessage(ChatColor.RED + "Your challenge to " + defender.getName() + " has been declined.");
                 defender.sendMessage(ChatColor.RED + "You declined the challenge.");
             }
        return true;
           }
    }
     
  16. You have written:
    Code (Text):
    Bukkit.getPlayer(args.toString());
    This does not what you expect it to do. Array#toString() returns a String-representation of that array. It's completely different from what you'd expect. Just try this code:
    Code (Java):
    String[] testArr = new String[]{"Hello", "I", "am", "a", "guest"};
    System.out.println(testArr);
    System.out.println(Arrays.toString(testArr));
    System.out.println(testArr[0]);
    args is just like test-array an array of Strings; so once you have understood the difference between Array#toString, Arrays#toString and ArrayElement(#toString), you can fix your error yourself :D
     
    • Friendly Friendly x 1
  17. Hmm, also you shouldn't be using Thread.sleep first of all. It freezes the whole server. Instead, use Bukkit schedulers :D
     
    #17 Ckblck, Jan 27, 2020
    Last edited: Jan 27, 2020
    • Agree Agree x 1
    • Useful Useful x 1
  18. I've never done any Schedulers before, may I know how I do a delay?
     
  19. Code (Java):
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
                // do delayed tasks here
            }, 20L);
     
    20L = 1 second
     
    • Friendly Friendly x 1
  20. Strahan

    Benefactor

    You should use "return", it would clean this up a bit. Like you check if the command is safehouse, and within that you check if args[0] is reload then you do a reload. At that point, you should "return true" because then you do not need to worry later if args[0] is return like when you do if(args.length == 1 && !(args[0].equals("reload"))) {. Same with the sender check. Once you've checked that sender is not a Player and sent the error, just return true then you will know moving forward that sender is a Player and will not need to do inverse checking. Also if you just do it very first thing, you can avoid the million instanceof checks you do in the code.

    You also aren't validating data right. For instance:
    Code (Text):
    Player defender = Bukkit.getPlayer(args[0]);
    String defenderinstring = defender.getName();
    if(Bukkit.getPlayer(defenderinstring) != null) { //Player is ONLINE!
    It's good you have the idea to check for null, but you are checking the wrong thing. If the player isn't online, defender will be null so when you try to getName() on it, the plugin will puke. You need to check if defender is null. Player#getName() cannot be null.

    In your code where you use nextBoolean to swap the attacker/defender, instead of making a tmp variable just pull from what you already have. I.E. you know attacker is the sender and defender is the args[0] name, so just do attacker = defender, defender = (Player)sender.

    Also you really should move the "meat" of the functionality to its own function. The command process should be clean and tight, not trying to do everything in onCommand. Read up on abstraction.
     
    • Useful Useful x 1
Thread Status:
Not open for further replies.