Spawn given particle with command

Discussion in 'BungeeCord Plugin Development' started by JonasArgent, Sep 16, 2020.

  1. Hey!

    I would like to be able to cast and argument from a command into a particle I can spawn(e.g /spawn heart <count>)

    This is what I have so far:

    Code (Java):
                        if (args[0].equalsIgnoreCase("spawn")) {
                            if (isParticle(args[0])){
                                Particle particle = Particle.valueOf(args[1].toUpperCase());
                            int count = 0;
                            if (StringUtils.isNumeric(args[2])) {
                                count = Integer.parseInt(args[2]);
                            } else {
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Config.getString("invalid-usage-message")));
                            }
                            Block target = p.getTargetBlockExact(Config.getInt("max-block-distance"));
                            if (target != null) {
                                Location loc = target.getLocation();
                                p.getWorld().spawnParticle(particle, loc, count);
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', Config.getString("prefix")) +
                                        "Spawned " + count + " particles at " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ() + "!");
                            }else{p.sendMessage("null");}
                            }else{p.sendMessage(ChatColor.translateAlternateColorCodes('&', Config.getString("prefix"))+args[1]+" is not a particle!");}
                        }
    Code (Java):
        private static boolean isParticle(String particle) {
            for (Particle par : Particle.values()) {
                if (par.name().equals(particle)) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }
     
  2. SteelPhoenix

    Moderator

    You can use Particle#valueOf(String).

    Also,

    Code (Java):
    if (StringUtils.isNumeric(args[2])) {
        count = Integer.parseInt(args[2]);
    }
    does not make any sense because
    "9999999999" is numeric, but cannot be parsed into an integer.
     
  3. That's what I've tried in this code, as seen here:

    Code (Java):
    if (isParticle(args[0])){
                                Particle particle = Particle.valueOf(args[1].toUpperCase());
     
  4. SteelPhoenix

    Moderator

    First you look at args[0] and then at args[1], also there's no point in using your isParticle() method.
     
  5. Thank you! I figured it out