Solved Take the name of player for another GUI..

Discussion in 'Spigot Plugin Development' started by Giorgino, Feb 21, 2020.

  1. Hi everyone, by chance someone knows how to take the name of an online player present in the GUI as a title, then in another GUI perform actions only for that player selected in the first GUI.
     
  2. Please be more specific. What exactly are you trying to do?
     
    • Useful Useful x 1
    • Useful Useful x 1
  3. Take the player's name from a GUI item, then perform an action for that person only.
     
  4. Something you could try:

    1) Create an inventory. Use a for loop to get all the online players and create items that have the player's name set as the display name.
    2) Register an InventoryClickEvent and listen for the display name of the item that the user clicks.
    3) Since the display name SHOULD be the name of an online player, you could probably get the desired player using Bukkit.getPlayer(String name) and use the display name as the variable.
    4) Then you could open a new GUI (making sure you have that player variable) and make a list of items in that GUI that perform actions when clicked.

    My description might be a little bit confusing but if this is along the lines of what you're trying to do, let me know and I can help you with more detail.
     
    • Useful Useful x 1
  5. That's exactly what I did for the plugin I mentioned above, except instead of just doing

    Code (Java):
    Bukkit#getPlayer()
    I did something like:
    Code (Text):
    for(Player p : bukkit.getOnlinePlayers()){

    If(p.getDisplayName().equals(Item display name){
    Player actionPlayer = p;
    }
    }
     
    • Useful Useful x 1
  6. Can you send the entire method that you're using to do this?
     
    • Useful Useful x 1
  7. What method? The method for changing people?
     
  8. Yeah. I want to see what you tried to do to so far
     
    • Useful Useful x 1
  9. What I did is put the player on the list and in the AsyncPlayerChatEvent event check if the clicked player is present in the list. If the event is present, cancel the event.
     
  10. What did you do to put the player in the list?
     
    • Useful Useful x 1
  11. You could try putting the player name as a lore line and then just getting the lore line

    you could also use String.split on your item name and check all the strings for your player name, if you have colors you can use something like ChatColor.stripColor (on mobile so I can’t really look it up)
     
    • Useful Useful x 1
  12. But I put the player's name in the title ...
     
  13. Yeah that’s what I’m saying
    Say you have a title that is &b(player name)’s &6pickaxe
    If you do String.split(“ “) on that, it gives you an array of strings (String[]) in this example it would be { “&b(player name)’s”, “&6pickaxe” }

    so using this you could use a forEach to find the player name in this display name array
    Code (Java):

    (title).split(“ “).stream().forEach(s -> {
    // your code for finding your player name
    // make sure you strip color if you have it!
    });
     
     
    • Useful Useful x 1
  14. Then I get uncomfortable .. Isn't there another method?
     
  15. You could try Arrays.asList((title).spit(“ “)).contains()
    Edit:
    But if you want to check if there’s a player in the array and you don’t have a specific player name my previous method is the only easy way I know of
     
    • Useful Useful x 1
  16. I'm working on some code right now, I think it will help make this a lot easier on yourself.

    Also, your end goal is to make a player specific options GUI, correct? (eg. you want to be able to do actions such as mute a player using a gui, right?)
     
    • Useful Useful x 1
  17. No, my intent is to mute the player with a click in the GUI .. (Thank you)
     
  18. The spoiler below is an inventory that contains items for all the players that are online as well as a listener to handle the InventoryClickEvent. To mute the player, use the listener I provided and mute the player after the line
    Code (Java):
    Player p = Bukkit.getPlayer(im.getDisplayName());
    View the spoiler to see the methods.

    Code (Java):
    @SuppressWarnings("deprecation")
        public void openAllPlayersGUI(Player player) {
            /*
             * Here, we create the inventory
             * Note: You can change all players to whatever you want.
             */

            Inventory i = Bukkit.createInventory(null, 56, ChatColor.translateAlternateColorCodes('&', "&0Mute Players"));
            // Getting each online player
            for (Player p : Bukkit.getServer().getOnlinePlayers()) {
             
                /* Creating an ItemStack for each player. The display name of the item is the name of the player.
                 * Also, the skull material will be set to the player's skull.
                 */

                ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                SkullMeta meta = (SkullMeta) item.getItemMeta();
                meta.setOwner(p.getName());
                meta.setDisplayName(p.getName());
                item.setItemMeta(meta);
                // Adds each item to the inventory.
                i.addItem(item);
            }
         
            // Opens the inventory for the player.
            player.openInventory(i);
        }
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getInventory() != null) {
                // If you changed the inventory name in the method openAllPlayersGUI, make sure to change the name here as well.
                if (e.getInventory().getTitle().equals(ChatColor.translateAlternateColorCodes('&', "&0Mute Players"))) {
                    e.setCancelled(true);
                    if (e.getCurrentItem().hasItemMeta()) {
                        ItemMeta im = e.getCurrentItem().getItemMeta();
                        if (im.hasDisplayName()) {
                            /*
                             * Since the display name of the item is a player's name, we can use Bukkit.getPlayer(String s) to get the player.
                             */

                            Player p = Bukkit.getPlayer(im.getDisplayName());
                            // Now that we have the player, we can mute the player.
                         
                        }
                    }
                }
            }
         
        }

    If you have any more questions, feel free to let me know :)
     
    • Winner Winner x 1