I need help with my ArrayList.

Discussion in 'Spigot Plugin Development' started by VinexAx789, Jul 10, 2015.

  1. My Issue:
    Hello, I'm having trouble setting the kit that I've forced through the array list as you can see in my code below in my command I'm adding them to the array list "plugin.forcekitplayer.add(onlineplayers.getUniqueId());" the reason why I'm doing this is because I had a slight problem when a player left the game and joined back they would receive the kit selector again which is bad because when I forced a kit and they left they could just pick there own kit so what I did was in the public void joinplayer I checked if they were in the array list when it was force and it would add the IRON_FENCE that had a display name called "A kit has been forced!" now the problem starts here when the game starts they didn't get a kit so how the heck can I some how figure out how to set the kit that I forced through the command let's use marine for an example. My code will be below as said above if you can help me it will be much appreciated thank you.

    Here's my code for my command that works:

    Code (Java):
    @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            plugin.forcekit = new ItemStack(Material.IRON_FENCE, 1);
            ItemMeta forcedmeta = plugin.forcekit.getItemMeta();
            forcedmeta.setDisplayName("§c§lA kit has been forced!");
            plugin.forcekit.setItemMeta(forcedmeta);

            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED
                        + "This plugin is for players only!");
                return true;
            }

            Player p = (Player) sender;
            if (!sender.hasPermission("Admin.forcekit")) {
                sender.sendMessage("§cYou do not have the permission to do this command!");
                return true;
            }
            if (args.length == 0) {
                sender.sendMessage("§cIncorrect usage! §aCorrect usage: §9/forcekit <kitname>");
                return true;
            }
            if (args[0].equalsIgnoreCase("rewind")) {
                Bukkit.getServer().broadcastMessage(
                        "§a" + p.getPlayer().getName()
                                + " §9has forced kit rewind!");
                for (Player onlineplayers : Bukkit.getOnlinePlayers()) {
                    plugin.forcekitplayer.add(onlineplayers.getUniqueId());
                    plugin.kitselected.put(onlineplayers, "rewind");
                    if (onlineplayers.getInventory().contains(Material.CHEST)) {
                        onlineplayers.getInventory().setItem(1, plugin.forcekit);
                    }
                }
            }

            if (args[0].equalsIgnoreCase("trooper")) {
                Bukkit.getServer().broadcastMessage(
                        "§a" + p.getPlayer().getName()
                                + " §9has forced kit trooper!");
                for (Player onlineplayers : Bukkit.getOnlinePlayers()) {
                    plugin.forcekitplayer.add(onlineplayers.getUniqueId());
                    plugin.kitselected.put(onlineplayers, "trooper");
                    if (onlineplayers.getInventory().contains(Material.CHEST)) {
                        onlineplayers.getInventory().setItem(1, plugin.forcekit);
                    }
                }
            }
            if (args[0].equalsIgnoreCase("marine")) {
                Bukkit.getServer().broadcastMessage(
                        "§a" + p.getPlayer().getName()
                                + " §9has forced kit trooper!");
                for (Player onlineplayers : Bukkit.getOnlinePlayers()) {
                    plugin.forcekitplayer.add(onlineplayers.getUniqueId());
                    plugin.kitselected.put(onlineplayers, "trooper");
                    if (onlineplayers.getInventory().contains(Material.CHEST)) {
                        onlineplayers.getInventory().setItem(1, plugin.forcekit);
                    }
                }
            }

    Here's my code for my joinplayer:

    Code (Java):
        public void joinplayer(Player p) {
            p.setGameMode(GameMode.SURVIVAL);
            for (PotionEffect ef : p.getActivePotionEffects()) {
                p.removePotionEffect(ef.getType());
            }
            players.add(p);
            p.setFoodLevel(20);
            p.setExp(0);
            p.setLevel(0);
            p.setMaxHealth(20.0);
            p.setHealth(((Damageable) p).getMaxHealth());
            p.setMaxHealth(20.0);
            p.getInventory().clear();
            p.getInventory().setHelmet(new ItemStack(Material.AIR));
            p.getInventory().setBoots(new ItemStack(Material.AIR));
            p.getInventory().setChestplate(new ItemStack(Material.AIR));
            p.getInventory().setLeggings(new ItemStack(Material.AIR));
            p.teleport(lobby);
            // p.getInventory().addItem(menu);
            if (forcekitplayer.contains(p.getUniqueId())) {
                p.getInventory().setItem(0,teamselector);
                p.getInventory().setItem(1, forcekit);
                //I need to some how set the kit that was force through the command.
                return;
            }
            p.getInventory().addItem(teamselector);
            p.getInventory().addItem(kitselector);
            p.getInventory().setItem(8, hubleave);
            p.getPlayer().setAllowFlight(true);
            p.getPlayer().getAllowFlight();
            p.getPlayer().setFlying(true);
            kitselected.put(p, "trooper");
            Bukkit.broadcastMessage("§f" + p.getName() + " §ejoined the game "
                    + players.size() + "/" + maxplayers);
     
  2. From what I see, you haven't registered an actual event for when the player joins the server.

    What you're doing:
    Code (Text):
    public void joinplayer(Player p)
    You're constructing a method without actually defining an event.


    Try this instead:
    Code (Text):
    public void joinplayer(PlayerJoinEvent event) {
    Player p = event.getPlayer()
    Tell me if you have any more problems.
     
    #2 SammySpicer, Jul 10, 2015
    Last edited: Jul 10, 2015
  3. Okay thank you for letting me know that but how is this going to solve my problem.
     
  4. Agreed.
    I strongly suggest using a PlayerJoinEvent, and, if required, a PlayerQuitEvent for this problem. Please keep in mind that you'll have to register the class your listeners are in.
     
  5. You are not helping me in anyway now I already know about that.
     
  6. You claim you know, yet you're using methods instead of Listeners. Look, I'm not trying to start a flame war or anything, I'm just giving out suggestions.
     
    • Agree Agree x 1
  7. I understand but the guy above already said it. Listen we are getting of topic please just no more suggestions unless they are to the problem I'm having.
     
  8. Is this even possible to do?
     
  9. He might have no registered that method as a listener but whats saying he dosnt have a join listener and putting that method in there
     
    • Agree Agree x 1