Making command accesable by command blocks

Discussion in 'Plugin Development' started by NiekZndt, May 5, 2020.

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

    NiekZndt

    Hi everyone,
    I am making a plugin with commands that when triggerd spawn zombies or other mobs above terrecotta blocks. The only problem being that I need the command to work in a command block or some different alternetive that can be triggered. The code for my command is:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
            Player player = (Player) sender;
            if(lbl.equalsIgnoreCase("spawnAll")) {
                if(args.length == 3) {
                    if(args[0].matches("[0-9]+") && args[1].matches("[0-9]+") && args[2].matches("[0-9]+")) {
                        for(int x=0; x<=Double.parseDouble(args[0]) * 2 - 1; x++) {
                            for(int y=0; y<=Double.parseDouble(args[1]) * 2 - 1; y++) {
                                for(int z=0; z<=Double.parseDouble(args[2]) * 2 - 1; z++) {
                                    if(player.getLocation().add(x, y, z).getBlock().getType() == Material.WHITE_GLAZED_TERRACOTTA)
                                        player.getWorld().spawnEntity(player.getLocation().add(x, y + 2, z), EntityType.ZOMBIE);
                                }
                            }
                        }
                        return true;
                    }
                    else
                        player.sendMessage("The radius must be a number");
                }
                else
                    player.sendMessage("Pleas enter a radius like /spawnAll [xRad] [yRad] [zRad]");
                   
                return true;
            }
            return false;
    Now I do realise that store everything in a player object but have no idea how to do it in a different way...
     
  2. Offline

    timtower Administrator Administrator Moderator

    @NiekZndt Don't cast to Player before checking if it is one.
    And you could add a command where you need to input world, x, y, z, xRad, yRad, zRad
     
  3. Offline

    NiekZndt

    Thank you for your quick reply, I have correct the mistakes you pointed out but the command can still not be executed by a command block
     
  4. Offline

    timtower Administrator Administrator Moderator

    Please post your updated code.
     
  5. Offline

    NiekZndt

    My new codes containts a new really weird bug, but nevertheless that shouldn't have any influence on whom is executing it so
    Code:
    if(args.length == 7) {
                if(args[0].matches("[0-9]+") && args[1].matches("[0-9]+") && args[2].matches("[0-9]+") && args[3].matches("[0-9]+") && args[4].matches("[0-9]+") && args[5].matches("[0-9]+")) {
                    Location cornerLoc = new Location(Bukkit.getServer().getWorld(args[6]), Double.parseDouble(args[0]) - Double.parseDouble(args[3]), Double.parseDouble(args[1]) - Double.parseDouble(args[4]), Double.parseDouble(args[2]) - Double.parseDouble(args[5]));
                    Location checkLoc = cornerLoc;
                    for(int x=0; x<=Double.parseDouble(args[3]) * 2 - 1; x++) {
                        for(int y=0; y<=Double.parseDouble(args[4]) * 2 - 1; y++) {
                            for(int z=0; z<=Double.parseDouble(args[5]) * 2 - 1; z++) {
                                checkLoc.add(x, y, z);
                                if(checkLoc.getBlock().getType() == Material.WHITE_GLAZED_TERRACOTTA)
                                    Bukkit.getServer().getWorld(args[6]).spawnEntity(checkLoc.add(0, 2, 0), EntityType.ZOMBIE);
                                checkLoc = cornerLoc;
                            }
                        }
                    }
                        return true;
                }
                else
                    Bukkit.getServer().broadcastMessage("All of the arguments must be numbers");
            }
            else
                Bukkit.getServer().broadcastMessage("Pleas enter arguments like /spawnall [XPos] [YPos] [ZPos] [XRad] [YRad] [ZRad] [worldName]");
           
                   
            return false;
    Thank you in advanced for helping me :D

    I fixed the small bug and it actually works, can you explain to me what I did wrong so I don't run into the same problem in the future? This is my code right now:
    Code:
    if(args.length == 7) {
                if(args[0].matches("[0-9]+") && args[1].matches("[0-9]+") && args[2].matches("[0-9]+") && args[3].matches("[0-9]+") && args[4].matches("[0-9]+") && args[5].matches("[0-9]+")) {
                    Location checkLoc = new Location(Bukkit.getServer().getWorld(args[6]), Double.parseDouble(args[0]) - Double.parseDouble(args[3]), Double.parseDouble(args[1]) - Double.parseDouble(args[4]), Double.parseDouble(args[2]) - Double.parseDouble(args[5]));
                    for(int x=0; x<=Double.parseDouble(args[3]) * 2 - 1; x++) {
                        for(int y=0; y<=Double.parseDouble(args[4]) * 2 - 1; y++) {
                            for(int z=0; z<=Double.parseDouble(args[5]) * 2 - 1; z++) {
                                if(checkLoc.clone().add(x, y, z).getBlock().getType() == Material.WHITE_GLAZED_TERRACOTTA)
                                    Bukkit.getServer().getWorld(args[6]).spawnEntity(checkLoc.clone().add(x, y+2, z), EntityType.ZOMBIE);
                            }
                        }
                    }
                        return true;
                }
                else
                    Bukkit.getServer().broadcastMessage("All of the arguments must be numbers");
            }
            else
                Bukkit.getServer().broadcastMessage("Pleas enter arguments like /spawnall [XPos] [YPos] [ZPos] [XRad] [YRad] [ZRad] [worldName]");
           
                   
            return false;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 6, 2020
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    NiekZndt

    The variable cornerLoc was changing without that being the intention
     
Thread Status:
Not open for further replies.

Share This Page