Change block id onClickEvent

Discussion in 'Plugin Development' started by cyaeu, Aug 27, 2021.

  1. Offline

    cyaeu

    Hi, i learing java and trying create PlayerSettings, but i dont know how i can change block id from LIME_DYE, to GRAY_DYE.. Like status: hidden or displayed (hide / show player), changing every click..

    InventorySetupScreenEvents.java
    Code:
    package me.cyaeu.playersettings;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    
    public class InventorySetupScreenEvents implements Listener{
      
        @EventHandler
        public void onClick(InventoryClickEvent e) {
            if(e.getClickedInventory() == null) {return;}
            if(e.getClickedInventory().getHolder() instanceof InventorySetupScreen) {
                e.setCancelled(true);
                Player p = (Player) e.getWhoClicked();
                if(e.getCurrentItem() == null) {return;}
              
                if(e.getCurrentItem().getType() == Material.NAME_TAG) {
                  
                    p.sendMessage("§aAll players has been hidden");
    
                  
                }
              
                if(e.getCurrentItem().getType() == Material.BARRIER) {
                    p.closeInventory();
                }
              
            }
        }
    
    }
    
    InventorySetupScreen.java
    Code:
    package me.cyaeu.playersettings;
    
    import java.util.Collections;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryHolder;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    
    
    public class InventorySetupScreen implements InventoryHolder{
      
        private Inventory inv;
      
        public InventorySetupScreen() {
            inv = Bukkit.createInventory(this, 36, "PlayerSettings » Main menu");
            init();
        }
      
      
        private void init() {
            ItemStack item;
            //top
            for(int i = 0; i < 9; i++) {
                item = createItem("§7", Material.BLACK_STAINED_GLASS_PANE, Collections.singletonList("§7"));
                inv.setItem(i, item);
            }
          
            //left side 1,2 + right side 1,2
            item = createItem("§7", Material.BLACK_STAINED_GLASS_PANE, Collections.singletonList("§7"));
            inv.setItem(9, item);
            inv.setItem(17, item);
            inv.setItem(18, item);
            inv.setItem(26, item);
          
            //bottom side
            for(int i = 27; i < 36; i++) {
                item = createItem("§7", Material.BLACK_STAINED_GLASS_PANE, Collections.singletonList("§7"));
                inv.setItem(i, item);
            }
          
          
          
            //show / hide player
            item = createItem("§a§l» §aHide Players §a§l«", Material.NAME_TAG, Collections.singletonList("§7Click to hide players"));
            inv.setItem(10, item);
            //status
            item = createItem("§7", Material.LIME_DYE, Collections.singletonList(""));
            inv.setItem(19, item);
          
          
            //close
            item = createItem("§cClose menu", Material.BARRIER, Collections.singletonList("§7To close menu"));
            inv.setItem(31, item);
                  
        }
      
        private ItemStack createItem(String name, Material mat, List<String> lore) {
            ItemStack item = new ItemStack(mat, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            meta.setLore(lore);
            item.setItemMeta(meta);
            return item;
        }
      
        @Override
        public Inventory getInventory() {
            return inv;
        }
    
    }
    
    
    [​IMG]
    Can someone help me or try explain, how to do this? <3
     
  2. Offline

    Tim_M

    Check if the player clicked on a green dye, if they did set to a red dye and hide/unhide players, if they clicked on a red dye hide/unhide players and set the item to red dye. I'm pretty sure it's Inventory.setItem(int slot, ItemStack item);
     
  3. Offline

    cyaeu

    green dye is only like status: hide - green /show - red and players need to click to name tag and on click change meta name from "Hide players" to "Show players" so i make check in InventorySetupScreenEvents.java? Because i cant do inv.setItem here..
     
  4. Offline

    xpaintall

    I'd recommend making an inventory when the player executes a command (or an event or similar) and then checking if the player has set the players to be hidden or not. Because the inventory is same to everyone, you should make it in an event or a command. I would also recommend that you should create arraylists and see if the player belongs in one, since that would be very useful.


    I'll show you what I mean, I did this before:

    Code:
    public class HidePlayersCommand implements CommandExecutor, Listener {
    
        //I made a private inventory (you can make it public if you want)
        private Inventory hideInventory;
     
        //the arraylist which stores players that have the option to hide players
        ArrayList<UUID> hidePlayers = new ArrayList<>();
    
        public HidePlayersCommand(ArrayList<UUID> hidePlayers) {
            this.hidePlayers = hidePlayers;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if(!(sender instanceof Player)) return false;
    
            Player player = (Player) sender;
    
            if(cmd.getName().equalsIgnoreCase("hideplayersinventory")) {
                //Here we create the inventory inside of the command (make sure the owner is null)
                hideInventory = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Hide player GUI");
    
                //here I set the items
                ItemStack item = new ItemStack(Material.NAME_TAG, 1);
                ItemMeta meta = item.getItemMeta();
                meta.setDisplayName("Click to change if you want to hide players or not!");
                item.setItemMeta(meta);
                hideInventory.setItem(0, item);
    
                if (hidePlayers.contains(player.getUniqueId())) {
                    ItemStack hide = new ItemStack(Material.DIAMOND, 1);
                    ItemMeta hideMeta = hide.getItemMeta();
                    hideMeta.setDisplayName(ChatColor.GREEN + "Players are showing!");
                    hide.setItemMeta(hideMeta);
                    hideInventory.setItem(1, hide);
                } else {
                    ItemStack hide = new ItemStack(Material.REDSTONE, 1);
                    ItemMeta hideMeta = hide.getItemMeta();
                    hideMeta.setDisplayName(ChatColor.RED + "Players aren't showing!");
                    hide.setItemMeta(hideMeta);
                    hideInventory.setItem(1, hide);
                }
                player.openInventory(hideInventory);
             
                return true;
    
            }
    
    
    
            return false;
        }
     
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            if(event.getInventory() == null) return;
            if(event.getClickedInventory().getHolder() == hideInventory.getHolder()) {
                if(event.getCurrentItem() == null) return;
                if(event.getSlot() == 0) {
                 
                    //check if the arraylist contains the player, if it doesn't it will add them to the arraylist. If it does, it will remove them from the arraylist.
                    // Then, you just want to execute the command again to see the differences
                    if(!hidePlayers.contains(player.getUniqueId())) {
                        hidePlayers.add(player.getUniqueId());
                        Bukkit.dispatchCommand(player, "hideplayersinventory");
                    } else {
                        hidePlayers.remove(player.getUniqueId());
                        Bukkit.dispatchCommand(player, "hideplayersinventory");
                    }
                }
            }
        }
     
     
     
     
    }
    Now in your main class you to do this, because if you don't when you add the player in the arraylist it will only add it in the void.

    Code:
    public class TutMain extends JavaPlugin {
    
        ArrayList<UUID> hidePlayers = new ArrayList<>();
    
        @Override
        public void onEnable() {
            getCommand("hidePlayersinventory").setExecutor(new HidePlayersCommand(hidePlayers));
            getServer().getPluginManager().registerEvents(new HidePlayersCommand(hidePlayers), this);
        }
    
        @Override
        public void onDisable() {
        }
    
    
    
    }
    I may be very wrong, but don't hesitate to ask me questions if you don't understand what's going on. Just please, use the ChatColor class instead of the color code letter, it just hurts my eyes to see that.
     
    Last edited: Aug 28, 2021
  5. Offline

    cyaeu

    Thank you so mutch man!
     

Share This Page