Why isnt this for loop working.

Discussion in 'Plugin Development' started by Hyperenci, Sep 13, 2016.

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

    Hyperenci

    So ive debugged to the point that the for loop isnt working in this class, i registered the event and commands, he is my code.
    package hk.dcustoms.events;



    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;

    import net.md_5.bungee.api.ChatColor;

    public class CommandSpy implements Listener, CommandExecutor
    {
    List<Player>currentSpys = new ArrayList<Player>();
    @EventHandler
    public void PlayerCommand(PlayerCommandPreprocessEvent event)
    {
    String commandExecuted = event.getMessage();
    Player whoExecuted = event.getPlayer();
    String whoExecutedName = whoExecuted.getName();
    int spySize = currentSpys.size();
    for (int i = 0; i < spySize; i++)
    {
    currentSpys.get(i).sendMessage("Test!");
    }
    }
    public boolean onCommand (CommandSender sender, Command cmd, String commandAlias, String[] args)
    {
    if(cmd.getName().equalsIgnoreCase("dcspy"))
    {
    Player addSpy = (Player) sender;
    if(currentSpys.contains(addSpy))
    {
    currentSpys.remove(addSpy);
    addSpy.sendMessage("Removed from the spy list!");
    }
    else
    {
    currentSpys.add(addSpy);
    addSpy.sendMessage("Added to the spy list");
    }
    }
    return true;
    }
    }
    If you perfer hastebin
    http://hastebin.com/wawavigadu.swift
    Thanks for all the help.
     
  2. Offline

    Zombie_Striker

    @Hyperenci
    Please use the [code][/code]tags. It makes your code easier to read. Click Insert > Code and copy-and-paste your code there.

    Has your class been registered? If so, post the class where you register it. What is "spySize" equal to? If it is equal to 0, then the for loop would never run.
     
  3. Offline

    Hyperenci

    The class has been registered and spySize is equal to the size of the list, and i already added myself to the list. And here is the class where i register it.
    Code:
    package hk.dcustoms.core;
    
    import java.io.File;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import hk.dcustoms.commands.ConfigReload;
    import hk.dcustoms.commands.ConvertBases;
    import hk.dcustoms.commands.Facepalm;
    import hk.dcustoms.commands.GetUUID;
    import hk.dcustoms.commands.PlayerChatClear;
    import hk.dcustoms.commands.WarnLagP;
    import hk.dcustoms.events.CommandSpy;
    
    public class DcCore extends JavaPlugin
    {
        private static DcCore plugin;
    
        //Enables the plugin
        public void onEnable() 
        {
            if (!new File(getDataFolder(), "config.yml").exists()) {
                saveDefaultConfig();
            }
            DcCore.plugin = this;
            getCommand("warnp").setExecutor(new WarnLagP());
            getCommand("dcreload").setExecutor(new ConfigReload());
            getCommand("facepalm").setExecutor(new Facepalm());
            getCommand("getUuid").setExecutor(new GetUUID());
            getCommand("pcc").setExecutor(new PlayerChatClear());
            getCommand("dcspy").setExecutor(new CommandSpy());
            getCommand("cvt").setExecutor(new ConvertBases());
            Bukkit.getServer().getPluginManager().registerEvents(new CommandSpy(), this);
        }
       
        public static DcCore getPlugin() {
            return plugin;
        }
    
        public static void setPlugin(DcCore plugin) {
            DcCore.plugin = plugin;
        }
    }
    
    
     
  4. Offline

    Zombie_Striker

    Your issue is that you have multiple instances. You have one class that has the command, and a separate instance that does not interact AT ALL with the first instance that has the events. When you issue the command to that puts you into the array, you are only put into the array of the instance that deals with the command. The instance that deals with the events is not affected by the command.

    To fix this, create a variable for your command and register that one instance. It should look like
    Code:
    CommandSpy commandSpy = new CommandSpy();
    
    getCommand("dcspy").setExecutor(commandSpy);
    Bukkit.getServer().getPluginManager().registerEvents(commandSpy, this);
    
    
     
  5. Offline

    Hyperenci

    So
    Nvm Let me try this
    Eh,well i created a variable for my command but now i cant use it and it's saying it doesnt exist.
     
  6. Offline

    Zombie_Striker

  7. Offline

    InstanceofDeath

    You imported the nms ChatColor enum?
     
Thread Status:
Not open for further replies.

Share This Page