Staff Chat error

Discussion in 'Plugin Development' started by GodzillaFlame42, Aug 10, 2016.

Thread Status:
Not open for further replies.
  1. Hi, i do not know how to fix this error. The error was so i coded
    for(Player p : Bukkit.getOnlinePlayers()); then everything else i did with like p.getName(); for example it has an error by p.

    Here is the code:
    Code:
    package me.godzilla;
    
    import java.util.ArrayList;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
        private FileConfiguration config = getConfig();
        private ArrayList<String> staff = new ArrayList<String>();
        private String prefix = config.getString("Prefix");
     
        @EventHandler
        public void onChat (AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            if(staff.contains(player.getName())){
                e.setCancelled(true);
                for(Player p : Bukkit.getOnlinePlayers());
                if(staff.contains(p.getName())){
                    String format = config.getString("Chat Format");
                    format = format.replaceAll("%player%", player.getName());
                    format = format.replaceAll("%displayname%", player.getDisplayName());
                    format = format.replaceAll("%message%", e.getMessage());
                    format = format.replaceAll("%prefix%", prefix);
                    p.sendMessage(ChatColor.AQUA + ChatColor.BOLD.toString() + "Staff Chat" + ChatColor.DARK_GRAY + " >> " + ChatColor.GRAY + player.getName() + ChatColor.DARK_GRAY + " > " + ChatColor.GRAY + e.getMessage());
                }
            }
        }
    
    @Override
    public void onDisable() {
     
    }
    
    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(this, this);
        saveDefaultConfig();
     
    }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(label.equalsIgnoreCase("sc")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "You must be a player in-game to use this command.");
                    return true;
                }
             
                Player player = (Player) sender;
             
                if (args.length == 0) {
                    player.sendMessage(ChatColor.DARK_GRAY + "--------------------------");
                    player.sendMessage(ChatColor.BLUE + "/sc " + ChatColor.GRAY + "to toggle Staff Chat.");
                    player.sendMessage(ChatColor.DARK_GRAY + "--------------------------");
                }
                if(args[0].equalsIgnoreCase("toggle"));
                    if(!player.hasPermission("staffchat.toggle")){
                        String noPerm = config.getString("No Permission");
                        player.sendMessage(noPerm);
                        return true;
                    }
                    if(!staff.contains(player.getName())){
                        staff.add(player.getName());
                        player.sendMessage(ChatColor.BLUE + "Staff Chat On!");
                        return true;
                    }
                    staff.remove(player.getName());
                    player.sendMessage(ChatColor.BLUE + "Staff Chat Off!");
                    return true;
            }
            return false;
        }
    }
     
    Last edited: Aug 10, 2016
  2. Offline

    Zombie_Striker

    Your problem is caused by that semi-colon at the end. Since you forgot to add brackets to the end of this line, it will read everything up untill it reaches a semicolon, which happens instantly. All this does currently is loop through all the players online and do nothing. Replace the semicolon with an open bracket and put an end bracket with the rest of the end brackets to fix your problem.

    Also, since this method doe3s nothing, you can delete it.
     
Thread Status:
Not open for further replies.

Share This Page