Solved My code seems to have problems running. (Be ready for your brain to hurt when reading this thread)

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

Thread Status:
Not open for further replies.
  1. Code (Java):


    if(cmd.getName().equalsIgnoreCase("safehouse")) {
                   if(args.length == 1 && args.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) {
                        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);
                            //Wait for accept or decline
                            // Start Game
                     

     
    This is one part of my onCommand(), the other part is where my /accepted and /declined are at. I wanted to make it so a reload command is available because it's more better than constantly restarting.

    However, wherever I put my if(args.length == 1 && args.equals("reload"), it always runs

    Code (Java):
    else if(Bukkit.getOfflinePlayer(argsString) != null) {
       sender.sendMessage(ChatColor.RED + "This player is offline or does not exist!");
    }
    instead of my reload part
     
  2. For your check on if they typed "reload" doesn't know what to refer to since it is an array of strings (You can find plenty of resources on arrays)

    Since arrays are 0 based, we want to check the first argument regardless if the length is 1 or not,
    So try this and let me know if the error keeps happening
    Code (Java):
     if(args.length == 1 && args[0].equals("reload"))
     
    #2 DatSquidFTW, Jan 27, 2020
    Last edited: Jan 27, 2020
  3. Oh, it works now. That's what causing the unlikely-arg-type warning, thank you!

    Other than this, is Java able to make changes to blocks in the world with it's coordinates (Like a /fill or /setblock in Java)?
    What's the code to do it?
     
  4. First you need to create an instance of Location, then you can change the material of the block at that location.
    Code (Java):
    Location loc = new Location(world, x, y, z);
    Block b = loc.getBlock();
    b.setType(Material.BEDROCK);
    The code above can also be shortened to something smaller like
    Code (Java):
    Bukkit.getWorld("worldname").getBlockAt(x, y, z).setType(Material.BEDROCK);
     
    #4 kixmc, Jan 27, 2020
    Last edited: Jan 27, 2020
  5. FrostedSnowman

    Resource Staff

    Bukkit::getOfflinePlayer will never return null, so that's not the way to check if a player is online on the server.

    To check if a player is online or not, use:

    Code (Java):
    if (Bukkit.getPlayer(playerName) == null) {
         //player is offline
    }
     
  6. Thank you!
     
Thread Status:
Not open for further replies.