Public void run() will not run when more then 2 players online

Discussion in 'Plugin Development' started by havejack, Oct 20, 2012.

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


    Title helps explain a little but heres the code snippet

    package me.havejack.CatchCreative;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    public class CatchCreative extends JavaPlugin implements Listener {
        static final Logger log = Logger.getLogger("Minecraft");
        public final List<Player> creativePlayers = new LinkedList<>();
        public void onDisable() {
  "CatchCreative disabled ! v0.7");
        public void onEnable() {
  "CatchCreative Enabled ! v0.7");
            getServer().getPluginManager().registerEvents(this, this);
    public boolean onCommand(final CommandSender sender, Command command, String commandLabel, String[] args){
        if ((sender.hasPermission("catch.creative")) || (sender.isOp())) {
            if (command.getName().equalsIgnoreCase("cc")) {
                String color = this.getConfig().getString("color.playername");
                String color2 = this.getConfig().getString("color.desc");
                String color3 = this.getConfig().getString("color.main");
                String color7 = this.getConfig().getString("color.noplayers");
                String color9 = this.getConfig().getString("color.denied");
                final String color4 = ChatColor.translateAlternateColorCodes('&', color);
                final String color5 = ChatColor.translateAlternateColorCodes('&', color2);
                String color6 = ChatColor.translateAlternateColorCodes('&', color3);
                String color8 = ChatColor.translateAlternateColorCodes('&', color7);
                String color10 = ChatColor.translateAlternateColorCodes('&', color9);
                Server server = sender.getServer();
                Player[] players = server.getOnlinePlayers();
                if (players.length <= 0) {
                    sender.sendMessage(color8 + "No Online players to check");
                    return true;           
                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    for(Player player : new ArrayList<Player>(creativePlayers)){
                        player.sendMessage(color5 + "You are not allowed Creative, your gamemode is changed");
                        log.log(Level.INFO, "{0} Was Changed Gamemode, since /cc was run  by" + sender.getName(), player.getName());
                        sender.sendMessage(color4 + player.getName() + color4 + "'s Gamemode was changed");
                }, 40L);
                sender.sendMessage(color6 + "Players in Creative:");
                for (Player player : players) {               
                    if (player.getGameMode().equals(GameMode.CREATIVE)) {
                        log.log(Level.INFO, "{0} Has Creative.", player.getName());
                        sender.sendMessage(color4 + player.getName() + " " + color5 + "has Creative.");
                return true;
        } else {
            log.log(Level.INFO, "{0} Was denied access to /cc", sender.getName());
            String color9 = this.getConfig().getString("color.denied");
            String color10 = ChatColor.translateAlternateColorCodes('&', color9);
            sender.sendMessage(color10 + "No Permission");
            return true;
      return true;
  2. Offline


    creativePlayers has no elements when you do new ArrayList<Player>(creativePlayers). This creates a new ArrayList and adds elements from
    creativePlayers(no elements)

    Later when you add elements, you add it to creativePlayers, not to the new array list you created.
    This should get fixed when you run the command the second time. But why not just add creative players before run()?
    kroltan likes this.
Thread Status:
Not open for further replies.

Share This Page