How to set chatcolor when clicked an item in a GUI

Discussion in 'Spigot Plugin Development' started by Stellrow, Jul 5, 2018.

  1. So im trying to make a simple plugin when someone types /chat and click on of the items(colors) to change theyr chatcolor to that
    Executor
    Code (Text):
    package main;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import net.md_5.bungee.api.ChatColor;

    public class MyPluginCommandExecutor implements CommandExecutor {
        private final MyPlugin plugin;
        public MyPluginCommandExecutor(MyPlugin plugin) {
            this.plugin = plugin;
        }
        @Override
        public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] arg3) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                Inventory i = plugin.getServer().createInventory(null, 9, ChatColor.DARK_GREEN + "CuloareChat");
             
                ItemStack empty = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15);
                ItemMeta emptyMeta = empty.getItemMeta();
                emptyMeta.setDisplayName("-");
                empty.setItemMeta(emptyMeta);
             
             
                ItemStack title1 = new ItemStack(Material.PAPER);
                ItemMeta title1m = title1.getItemMeta();
                title1m.setDisplayName(ChatColor.RED + "1V1IRL");
                title1.setItemMeta(title1m);
             
                ItemStack albastru = new ItemStack(Material.WOOL, 1, (short) 11);
                ItemMeta albastrum = albastru.getItemMeta();
                albastrum.setDisplayName(ChatColor.BLUE + "Albastru");
                albastru.setItemMeta(albastrum);
             
                ItemStack rosu = new ItemStack(Material.WOOL, 1, (short) 14);
                ItemMeta rosum = rosu.getItemMeta();
                rosum.setDisplayName(ChatColor.RED + "Rosu");
                rosu.setItemMeta(rosum);
             
                ItemStack galben = new ItemStack(Material.WOOL, 1, (short) 4);
                ItemMeta galbenm = galben.getItemMeta();
                galbenm.setDisplayName(ChatColor.YELLOW + "Galben");
                galben.setItemMeta(galbenm);
             
                ItemStack auriu = new ItemStack(Material.WOOL, 1, (short) 1);
                ItemMeta aurium = auriu.getItemMeta();
                aurium.setDisplayName(ChatColor.GOLD + "Auriu");
                auriu.setItemMeta(aurium);
             
                ItemStack mov = new ItemStack(Material.WOOL, 1, (short) 10);
                ItemMeta movm = mov.getItemMeta();
                movm.setDisplayName(ChatColor.DARK_PURPLE + "Mov");
                mov.setItemMeta(movm);
             
                ItemStack verde = new ItemStack(Material.WOOL, 1, (short) 13);
                ItemMeta verdem = verde.getItemMeta();
                verdem.setDisplayName(ChatColor.GREEN + "Verde");
                verde.setItemMeta(verdem);
             
                ItemStack aqua = new ItemStack(Material.WOOL, 1, (short) 9);
                ItemMeta aquam = aqua.getItemMeta();
                aquam.setDisplayName(ChatColor.AQUA + "Aqua");
                aqua.setItemMeta(aquam);
             
                ItemStack negru = new ItemStack(Material.WOOL, 1, (short) 15);
                ItemMeta negrum = negru.getItemMeta();
                negrum.setDisplayName(ChatColor.BLACK + "Negru");
                negru.setItemMeta(negrum);

             
                ItemStack alb = new ItemStack(Material.WOOL);
                ItemMeta albm = alb.getItemMeta();
                albm.setDisplayName(ChatColor.WHITE + "Alb");
                alb.setItemMeta(albm);
             
                i.setItem(0, albastru);
                i.setItem(1, rosu);
                i.setItem(2, galben);
                i.setItem(3, auriu);
                i.setItem(4, mov);
                i.setItem(5, verde);
                i.setItem(6, aqua);
                i.setItem(7, negru);
                i.setItem(8, alb);
             
             
                player.openInventory(i);
            }else {
                sender.sendMessage(ChatColor.RED + "You must be a player");
            }
            return false;
        }

    }
    The EventsClass
    Code (Text):
    package main;

    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;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import net.md_5.bungee.api.ChatColor;



    public class EventsClass implements Listener {


        @EventHandler
        public void  InvenClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
         
            Inventory open = event.getClickedInventory();
            ItemStack item = event.getCurrentItem();
            if (item == null)
                   return;

               if (event.isCancelled())
                   event.setCancelled(true);


               if (!item.hasItemMeta())
                   return; // ItemMeta check
               ItemMeta meta = item.getItemMeta();

               if (!meta.hasDisplayName())
                   return; // displayname checlk
               String name = meta.getDisplayName();

         
           
           
           
             if (ChatColor.stripColor(name).equalsIgnoreCase("Health"))
                   player.setHealth(20);
         
            if(open.getName().equals(ChatColor.DARK_GREEN + "CuloareChat")) {
                 }

            if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Albastru")) {
                ///////////////////////<--------Want to add it here
             
            }

                event.setCancelled(true);
             
                if(item.equals(null)) {
                    return;
                }
                if(item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Health")) {
                    player.setHealth(20);
                }
            }
        }

     
    What im trying to make
    Code (Text):
    package main;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;


    public class ChatColor implements Listener{
        public ChatColor(Player player, String color) {
            @EventHandler
            public Chat(PlayerChatEvent event) {
                event.getMessage();
                event.setMessage(ChatColor.color + event.getMessage());
            }
         
         
        }
     
    }
     
    I want to make it so it switches the chatcolor when they clicked one of the options but i dont know how to connect them
     
  2. Have a map that stores a UUID as the key and a ChatColor as the value and save it to a file so that it persists over reloads.
     
  3. ATM i want it to simply work,i cant figure out how to change the chatcolor when clicked
     
  4. Just so you know you’re importing the wrong chatcolour. The way I’d go about it without thinking mass optimisation and stuff like that would be to on click get the colour of the block and relate it to a ChatColor.
    On chat add the chat colour before the message
     
  5. @xMrPoi has the right idea here.

    Create a simple map with for UUIDs and ChatColor
    Code (Text):
    Map<UUID,org.bukkit.ChatColor> playerColors = new HashMap<>();
    When you click on an item in the GUI, access the map and do
    Code (Text):
    playerColors.put(player.getUniqueId(), org.bukkit.ChatColor.Blue);
    And when you run the event
    Code (Text):
    playerColors.get(player.getUniqueId()]);
     
  6. package main;
    ???
     
    • Agree Agree x 1