Need help on getting a player removed from an Arraylist through a Listener

Discussion in 'Plugin Development' started by Once, Jan 3, 2022.

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

    Once

    Hey, so I am new to programming and I wanted to make a Plugin:
    So when u use the command kitpvp you get teleported to a position, get an inventory with a sword in it
    and you will get added to an Arraylist.
    Question
    I want to code that the player is removed from the arraylist fighting when he dies
    and made an EventHandler for that but I dont know how to remove the player from the arraylist
    cause I don't know how to reach it

    Code:
    package de.pancake.beginning.commands;
    
    
    import java.util.ArrayList;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class KitPvPCommand implements CommandExecutor, Listener {
        public ArrayList<String> getFighting() {
            return fighting;
        }
    
        public void setFighting(ArrayList<String> fighting) {
            this.fighting = fighting;
        }
    
        public ArrayList<String> fighting = new ArrayList<>();
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {;
      
        Player player = (Player)sender;
      
      
                if(sender instanceof Player)  {
                    if(args.length == 0) {
                      
                  
                        if(!fighting.contains(player.getName())) {
                   
                            player.teleport(new Location(Bukkit.getWorld("world"), 0, 69, 0));
                          
                            Inventory inventory = Bukkit.createInventory(null, 9*3, "§6§lKits");
                            ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
                               ItemMeta itemmeta = item.getItemMeta();
                               itemmeta.setDisplayName("§6Sword");
                               itemmeta.addEnchant(Enchantment.DAMAGE_ALL, 1, false);
                               item.setItemMeta(itemmeta);
                               inventory.addItem(item);
                            
                                                    player.openInventory(inventory);
                                                    fighting.add(player.getName());
      
                }else player.sendMessage("§cYou're already fighting!");
                  
                  
                
    }
              
        }
                return false;      
               }
        // Death
        @EventHandler
        public void onDeath(PlayerDeathEvent death) {
          
          
                }
    
           }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Once fighting.remove(player.getName()) ?
     
  3. Offline

    Once

    I've tried that but when I test that out ingame it doesn't work and it's still saying "You're already fighting!"
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Once Then show your full code.
     
  5. Offline

    Strahan

    Also checking if sender is a Player after casting it thus is kinda putting the cart before the horse, lol
     
  6. Offline

    Once

    Found the issue.
    I've had 2 instances of my kitpvp command of my arraylist in the main method:
    my fix:
    Code:
    KitPvPCommand kitpvpCommand = new KitPvPCommand();
    
    getCommand("kitpvp").setExecutor(kitpvpCommand);
    
    PluginManager pluginManager = Bukkit.getPluginManager();
        pluginManager.registerEvents(kitpvpCommand, this);
     
Thread Status:
Not open for further replies.

Share This Page