Spectate Plugin not working properly.

Discussion in 'Plugin Development' started by Luflexed, Dec 14, 2014.

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

    Luflexed

    I've been coding a spectate plugin, it used to work, but now every time someone uses the command /spec on, it says an internal error has occured while attempting to perform this command. Heres my code.
    Code:
    public static ArrayList<String> players = new ArrayList<>();
        public static ArrayList<String> spectators = new ArrayList<>();
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("spec")){
                if(!sender.hasPermission("donor.spectate")){
                    sender.sendMessage("§8[§eSGFFA§8] §cYou must be a donor to do that, http://store.sgffa.us");
                    return true;
                }
               
                if(args.length == 0 || args.length < 1){
                    sender.sendMessage("§8[§6SGFFA§8] §fYou can either /spec on, or /spec off.");
                }
               
                if(args[0].equalsIgnoreCase("on") || args[0].equalsIgnoreCase("off")){
                if(args[0].equalsIgnoreCase("on") && args.length == 1){
                    Player p = (Player) sender;
                    spectators.add(p.getName());
                    players.remove(p.getName());
                    p.setFlying(true);
                    p.setAllowFlight(true);
                    for(Player pall : Bukkit.getOnlinePlayers()){
                        pall.hidePlayer(p);
                        p.getInventory().clear();
                        p.getInventory().setHelmet(new ItemStack(Material.AIR));
                        p.getInventory().setChestplate(new ItemStack(Material.AIR));
                        p.getInventory().setLeggings(new ItemStack(Material.AIR));
                        p.getInventory().setBoots(new ItemStack(Material.AIR));
                        p.sendMessage("§8[§6SGFFA§8] §fSpectate mode §aenabled§f.");
                        return true;
                        }
                    }
               
                if(args[0].equalsIgnoreCase("off") && args.length == 1){
                    Player p = (Player) sender;
                    spectators.remove(p.getName());
                    players.add(p.getName());
                    p.setFlying(false);
                    p.setAllowFlight(false);
                    for(Player pall : Bukkit.getOnlinePlayers()){
                        pall.showPlayer(p);
                        p.kickPlayer("§8[§6SGFFA§8] §fThis will be fixed soon.");
                        return true;
                    }
                }
            }
        }
            return true;
    }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            players.add(p.getName());
            spectators.remove(p.getName());
            p.setFlying(false);
            p.setAllowFlight(false);
        }
       
        @EventHandler
        public void onHit(EntityDamageByEntityEvent e){
            if(e.getDamager() instanceof Player){
                Player p = (Player) e.getDamager();
                if(e.getEntity() instanceof LivingEntity){
                    if(spectators.contains(p.getName())){
                        e.setCancelled(true);
                    }
                }
            }
        }
       
        @EventHandler
        public void onLeave(PlayerQuitEvent e){
            Player p = e.getPlayer();
            players.add(p.getName());
            spectators.remove(p.getName());
            p.setFlying(false);
            p.setAllowFlight(false);
        }
       
        @EventHandler
        public void onDeath(PlayerDeathEvent e){
            Player p = e.getEntity();
            spectators.remove(p.getName());
            players.add(p.getName());
        }
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Techy4198

    @Luflexed you also need to check if the command sender is a player. This will throw an error if the console or a command block executes /spec.
     
  4. Offline

    Luflexed

    @Techy4198 it does not bother me if console throws an error. It's the plugin that's not working.
     
  5. Offline

    drpk

Thread Status:
Not open for further replies.

Share This Page