Search code examples
javaarraylistminecraft

How to extract a string from an ArrayList in Java?


So I am making a plugin for a Minecraft server and I had to get my command command argument from one class to another and After trying multiple things, I started using an ArrayList, It worked for the most part but it put brackets around the argument.

ArrayList<String> target = Main.target;
      
@EventHandler
public void invClickEvent(InventoryClickEvent e) {
  
  Inventory inv = e.getInventory();
  Player player = (Player) e.getWhoClicked();
  
  String name = inv.getName();
  if ( name.equals(ChatColor.RED + "Ban Menu")) {
  
    e.setCancelled(true);
    int slot = e.getSlot();
    if (slot < 0) return;
    if (slot == 0) {
      player.performCommand("kill " + target);
      player.closeInventory();
      return;
    }
  }
}

That is the code that is giving me troubles. It is pulling the data from my main class. It the ArrayList is pulling text from an argument.

public static ArrayList<String> target = new ArrayList<String>();
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

if (sender instanceof Player && args.length == 1) {
  Player p = (Player) sender;
  if (args.length > 0) {
    switch (args.length) {
      case 1: 
        Player player = Bukkit.getPlayer(args[0]);
        if (player == null) {
          sender.sendMessage("That player is not online");
          
        } else if (player.isOnline()) {
      
          Menus.openMenu(p);
          target.add(player.getPlayerListName());
          
        }
      }
    }
  }
}

That is where the array is pulling the argument. The output would be something like player executed command: /kill [HoloPanio] and I want it to output player executed command: /kill HoloPanio

If there is another method to import the arguments please let me know, if not then please tell me how to fix this issue.

Thanks!

Clarification

To clarify on what I originally meant with this question was that I was trying to store a string inside of an ArrayList in Java, and when I went to retrieve the value I was expecting nothing but a plain string.

I was in fact using the incorrect type for the variable that I was assigning the string to. I should've been using type String as pointed out by several people. so the correct way of doing this would look like:

public String target;
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

  if (sender instanceof Player && args.length == 1) {
      Player p = (Player) sender;
      if (args.length > 0) {
          switch (args.length) {
          case 1: 
            Player player = Bukkit.getPlayer(args[0]);
            if (player == null) {
              sender.sendMessage("That player is not online");
              
            } else if (player.isOnline()) {
          
              Menus.openMenu(p);
              target = player.getPlayerListName();
              
            }
          }
      }
  }
}

In the above code block I am declaring the target variable as a string then setting it further down with the line target = player.getPlayerListName();. This is a much more efficient way defining the value. The only change that would need to be made to the first block is changing:

ArrayList<String> target = Main.target;

to

String target = Main.target;

Final Note

This post was originally written when I was still learning java and would like to add that I would not use an ArrayList for a string nor would I define it statically as I had done when this post was originally written.


Solution

  • Just expanding on what Titus has said in their comment.

    Instead of using ArrayList, try using a String as the type for target. This will allow the output to be player executed command: /kill HoloPanio.

    String target = Main.target;
    
    @EventHandler
    public void invClickEvent(InventoryClickEvent e) {
    
       Inventory inv = e.getInventory();
       Player player = (Player) e.getWhoClicked();
    
       String name = inv.getName();
       if ( name.equals(ChatColor.RED + "Ban Menu")) {
    
           e.setCancelled(true);
           int slot = e.getSlot();
           if (slot < 0)
           {
               return;
           }
           if (slot == 0)
           {
               player.performCommand("kill " + target);
               player.closeInventory();
               return;
           }
    

    The below would be updated to initial target as an empty String. Note that a static variable will be updated for every user of this class. This means that any other code accessing this class will have the same target. And, any other code accessing this class can change the target.

    public static String target = "";
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    
    
        if (sender instanceof Player && args.length == 1)
        {
            Player p = (Player) sender;
            if (args.length > 0) {
                switch (args.length)
                {
                case 1: 
                  Player player = Bukkit.getPlayer(args[0]);
                  if (player == null) {
                    sender.sendMessage("That player is not online");
    
                  } else if (player.isOnline()) {
    
                      Menus.openMenu(p);
                      target = player.getPlayerListName();
    
                  }
    

    Also as Titus said, this is not the best solution because, as a static variable, the target will likely change when you don't want it to (or will end up being the same when you want it to change). But, this should solve the command issue.