Need help with an ArrayList

Discussion in 'Plugin Development' started by TJM4, Sep 2, 2015.

Thread Status:
Not open for further replies.
  1. Fist of all this is my first post here so don't get mad if I mess up :mad:

    So I am fairly new to Java and bukkit and have just got into using multiple classes. If for whatever reason you need to know, this is not in the main class.

    So at the top of the code I create an array list called 'NewJoinPlayers':

    Code:
    ArrayList<String> NewJoinPlayers = new ArrayList<String>();
    Then I add them to the array list when they join (here is a section on the PlayerJoinEvent):

    Code:
            if ((p.hasPlayedBefore())) { //TODO Set to not played
                //TODO Add teleport p.teleport()
                NewJoinPlayers.add(p.getName());
    Later on in the code, when a command is run it tests if the player is in the array list:

    Code:
            if(cmd.getName().equalsIgnoreCase("answerQuestionFalse")) {
                if (NewJoinPlayers.contains(p.getName())) {
                    TitleAPI.sendFullTitle(p, 20, 100, 20, ChatColor.GOLD + "Taking you to your village", "");
                    create.CreateVillage(p);
                    NewJoinPlayers.remove(p.getName());
                    return true;
                }else {
                    p.sendMessage(ChatColor.RED + "That command is for the tutorial only");
                    return true;
                }
            }
    For some reason when I test this in game I get the error "That command is for the tutorial only".

    Throughout the code I added
    Code:
    p.sendMessage(NewJoinPlayers.toString());
    and TJM4 (my in game name) show up throughout the code in the onPlayerJoinEvent but as soon as it reaches the commands my name gets removed.

    Here is the code for the whole class:

    Code:
    package me.TJM4.ClashOfCraft;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.connorlinfoot.titleapi.TitleAPI;
    
    import mkremins.fanciful.FancyMessage;
    
    public class FirstJoin implements Listener, CommandExecutor{
       
        Main plugin;
        
        public void ConfigListener(Main instance) {
        plugin = instance;
        }
       
        ArrayList<String> NewJoinPlayers = new ArrayList<String>();
        VillageCreation create = new VillageCreation();
       
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("Test")) {
                p.sendMessage(NewJoinPlayers.toString());
            }
           
            if(cmd.getName().equalsIgnoreCase("answerQuestionFalse")) {
                p.sendMessage(NewJoinPlayers.toString());
                if (NewJoinPlayers.contains(p.getName())) {
                    TitleAPI.sendFullTitle(p, 20, 100, 20, ChatColor.GOLD + "Taking you to your village", "");
                    create.CreateVillage(p);
                    NewJoinPlayers.remove(p.getName());
                    return true;
                }else {
                    p.sendMessage(ChatColor.RED + "That command is for the tutorial only");
                    return true;
                }
            }
            if (cmd.getName().equalsIgnoreCase("answerQuestionTrue")) {
                TitleAPI.sendFullTitle(p, 20, 100, 20, ChatColor.GOLD + "Question:", ChatColor.YELLOW + "How did you find out about us?");
                p.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "How did you find put about this server?");
                new FancyMessage("Planet Minecraft")
                .color(ChatColor.YELLOW)
                .tooltip("You found this server on Planet Minecraft")
                .command("/FoundFromPMC")
                .send(p);
               
                new FancyMessage("Youtube")
                .color(ChatColor.YELLOW)
                .tooltip("You found this server on Youtube")
                .command("/FoundFromYoutube")
                .send(p);
               
                new FancyMessage("Website")
                .color(ChatColor.YELLOW)
                .tooltip("You found this server on website")
                .command("/FoundFromWebsite")
                .send(p);
               
               
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("FoundFromPMC")) {
                if (NewJoinPlayers.contains(p.getName())) {
                    plugin.getConfig().set("PeopleComingDestinationsVoting.PlanetMinecraft", (plugin.getConfig().getInt("PeopleComingDestinationsVoting.PlanetMinecraft") + 1));
                    TitleAPI.sendFullTitle(p, 20, 100, 20, ChatColor.GOLD + "Taking you to your village", ChatColor.GREEN + "Your gems have been added");
                    create.CreateVillage(p);
                    NewJoinPlayers.remove(p);
                    //TODO Add gems
                    return true;
                }else {
                    p.sendMessage(ChatColor.RED + "That command is for the tutorial only");
                    return true;
                }
               
            }
            if (cmd.getName().equalsIgnoreCase("FoundFromWebsite")) {
                if (NewJoinPlayers.contains(p.getName())) {
                    plugin.getConfig().set("PeopleComingDestinationsVoting.Website", (plugin.getConfig().getInt("PeopleComingDestinationsVoting.PlanetMinecraft") + 1));
                    TitleAPI.sendFullTitle(p, 20, 100, 20, ChatColor.GOLD + "Taking you to your village", ChatColor.GREEN + "Your gems have been added");
                    create.CreateVillage(p);
                    NewJoinPlayers.remove(p);
                    //TODO Add gems
                    return true;
                }else {
                    p.sendMessage(ChatColor.RED + "That command is for the tutorial only");
                    return true;
                }
               
            }
            if (cmd.getName().equalsIgnoreCase("FoundFromYoutube")) {
                if (NewJoinPlayers.contains(p.getName())) {
                    plugin.getConfig().set("PeopleComingDestinationsVoting.Youtube", (plugin.getConfig().getInt("PeopleComingDestinationsVoting.PlanetMinecraft") + 1));
                    TitleAPI.sendFullTitle(p, 20, 100, 20, ChatColor.GOLD + "Taking you to your village", ChatColor.GREEN + "Your gems have been added");
                    create.CreateVillage(p);
                    NewJoinPlayers.remove(p);
                    //TODO Add gems
                    return true;
                }else {
                    p.sendMessage(ChatColor.RED + "That command is for the tutorial only");
                    return true;
                }
               
            }
           
            return false;
        }
       
    
        @SuppressWarnings("deprecation")
        @EventHandler
       
        public void playerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
           
            if ((p.hasPlayedBefore())) { //TODO Set to not played
                //TODO Add teleport p.teleport()
                NewJoinPlayers.add(p.getName());
                p.sendMessage(NewJoinPlayers.toString());
                TitleAPI.sendFullTitle(p, 20, 100, 20, ChatColor.GOLD + "Welcome " + p.getDisplayName() + "!", ChatColor.YELLOW + "Please wait around 5 seconds so we can check your resource pack");
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(JavaPlugin.getPlugin(Main.class), new Runnable() {
                   
                    @Override
                    public void run() {
                       
                        TitleAPI.sendFullTitle(p, 20, 200, 20, ChatColor.GOLD + "Quick question?", ChatColor.YELLOW + "Would you like to get an extra 30 gems by answering a quick question?");
                        p.sendMessage(ChatColor.YELLOW + "" + ChatColor.UNDERLINE + "Do you want to answer the question and get 30 gems?");
                        p.sendMessage(NewJoinPlayers.toString());
                        new FancyMessage("[")
                        .color(ChatColor.GOLD)
                        .style(ChatColor.BOLD)
                        .then("Yes")
                        .color(ChatColor.GREEN)
                        .command("/answerQuestionTrue")
                        .tooltip("Click here to answer the question")
                        .then("]   ")
                        .color(ChatColor.GOLD)
                        .style(ChatColor.BOLD)
                        .then("[")
                        .color(ChatColor.GOLD)
                        .style(ChatColor.BOLD)
                        .then("No")
                        .color(ChatColor.RED)
                        .command("/answerQuestionFalse")
                        .tooltip("Click here to start playing")
                        .then("]")
                        .color(ChatColor.GOLD)
                        .style(ChatColor.BOLD)
                        .send(p);
                    }
                }, 120);
               
               
            }
                   
        }
    
       
    
       
       
    
    }
    
    I hope someone can help.

    Thanks,

    TJM4
     
  2. Offline

    tkuiyeager1

    Hi,
    i am also new to java but i think you need to change the type of the arraylist from <String> to <Player> and change the line when you add a player to ArrayListName.add(Player) without p.getname()
    GoodLuck
     
  3. Offline

    Signatured

    Are you registering events in your onEnable method?
     
  4. Yes I have -
    Code:
    registerEvents(this, new FirstJoin());
    I have already tried that but will try again, thanks

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>

    No, still getting the 'That command is for the tutorial only' error :'(

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Sep 2, 2015
  5. Offline

    Signatured

    I may be mistaken since I'm not on my computer, but doesn't you register events with the listener THEN the plugin instance? You may have it backwards.
     
  6. The event is working - its the array list that's broken. All the code in the event it working fine :D
     
  7. Offline

    Xerox262

    You shouldn't store players in an array list, it causes memory leaks.

    He's right, it is PluginManager#registerEvents(Listener, Plugin); not (Plugin, Listener);
     

  8. Ermmm..... view image.
     
  9. Offline

    Signatured

    Can I see your registerEvents method? You don't appear to be using the PluginManager.
     
  10. Offline

    Xerox262

    If it's a method within your JavaPlugin class then why are you passing it a this? Can't you just assume that all events registered by it will use that plugin
     
  11. Oh wow, I feel dumb....

    Changed it to:
    Code:
    Bukkit.getServer().getPluginManager().registerEvents(new FirstJoin(), this);
    and testing plugin now

    Edit:

    Array list still now working :(
     
  12. Offline

    teej107

    Which is it?

    What do you need the ArrayList for?

    No it doesn't. Failing to handle the Players in the Collection after they leave do.
     

  13. Array list: NewJoinPlayers

    I need it to track people who are in the tutorial
     
  14. Offline

    teej107

    @TJM4
    1. You don't need an Arraylist
    2. You should use a Set.
    3. If you are going to store the players directly in a collection, remove them from the collection when they leave or use a "Weak HashSet"
     
Thread Status:
Not open for further replies.

Share This Page