Plugin not working

Discussion in 'Plugin Development' started by MaxBans, Nov 9, 2020.

  1. Offline

    MaxBans

    Hello, im new to programming in java and i just made a GUI plugin but when i type /changeteam in game nothing happens. Can someone please help me? Plugin is not showing any errors in console.

    CODE:

    Code:
    package me.MaxBans.Minihra;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.Material;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    import com.mojang.brigadier.Command;
    
    import io.netty.buffer.ByteBuf;
    
    import io.netty.handler.codec.http2.Http2Stream;
    
    
    public class Main extends JavaPlugin implements Listener {
    public Inventory inv;
        @Override
        public void onEnable()
        {
            this.getServer().getPluginManager().registerEvents((org.bukkit.event.Listener) this, this);
            createInv();
          
        }
      
        public void onDisable()
        {
          
          
        }
      
        public  boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(label.equalsIgnoreCase("changeteam")) {
                if(!(sender instanceof Player))
                {
                    sender.sendMessage(ChatColor.RED + "You cannot do this!");
                    return true;
                }
              
                Player player = (Player) sender;
                //open the GUI
                player.openInventory(inv);
                return true;
              
            }
            return false;
        }
      
        @EventHandler()
        public void onClick(InventoryClickEvent event) {
          
            if(!event.getInventory().equals(inv))
                return;
          
            if(event.getCurrentItem() == null) return;
            if(event.getCurrentItem().getItemMeta() == null) return;
            if(event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
          
            event.setCancelled(true);
            Player player = (Player) event.getWhoClicked();
            if(event.getSlot() == 0) {
            //BLUE TEAM
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.BLUE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "You changed your team!");
            }
          
            if(event.getSlot() == 1) {
                //RED TEAM
                    ItemStack[] armor = player.getEquipment().getArmorContents();
                    armor = changeColor(armor, Color.RED);
                    player.getEquipment().setArmorContents(armor);
                    player.sendMessage(ChatColor.GOLD + "You changed your team!");
                }
          
          
            if(event.getSlot() == 2) {
                //YELLOW TEAM
                    ItemStack[] armor = player.getEquipment().getArmorContents();
                    armor = changeColor(armor, Color.YELLOW);
                    player.getEquipment().setArmorContents(armor);
                    player.sendMessage(ChatColor.GOLD + "You changed your team!");
                }
          
          
            if(event.getSlot() == 3) {
                //GREEN TEAM
                    ItemStack[] armor = player.getEquipment().getArmorContents();
                    armor = changeColor(armor, Color.GREEN);
                    player.getEquipment().setArmorContents(armor);
                    player.sendMessage(ChatColor.GOLD + "You changed your team!");
                }
          
            //CLOSE
            if(event.getSlot() == 8) {
                player.closeInventory();
                }
          
            return;
        }
      
        public ItemStack[] changeColor(ItemStack [] a, Color color)
        {
            for(ItemStack item : a)
            {
                try {
                    if(item.getType() == Material.LEATHER_BOOTS || item.getType() == Material.LEATHER_CHESTPLATE || item.getType() == Material.LEATHER_LEGGINGS || item.getType() == Material.LEATHER_HELMET) {
                        LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
                        meta.setColor(color);
                        item.setItemMeta(meta);
                  
                  
                }
                  
                } catch (Exception e) {
                    //do nothing
                  
                  
                }
              
              
            }
          
            return a;
        }
     
        public void createInv() {
        inv =  Bukkit.createInventory(null, 9, ChatColor.GRAY + "Select Team");
        ItemStack item = new ItemStack(Material.BLUE_CONCRETE);
        ItemMeta meta = item.getItemMeta();
      
        //BLUE TEAM
        meta.setDisplayName(ChatColor.DARK_BLUE + "BLUE TEAM");
        List<String> lore = new ArrayList<String>();
        lore.add(ChatColor.GRAY + "Click to join team!");
        meta.setLore(lore);
        item.setItemMeta(meta);
        inv.setItem(0, item);
      
        //RED TEAM
        item.setType(Material.RED_CONCRETE);
        meta.setDisplayName(ChatColor.DARK_RED + "RED TEAM");
        item.setItemMeta(meta);
        inv.setItem(1, item);
      
      
        //YELLOW TEAM
        item.setType(Material.YELLOW_CONCRETE);
        meta.setDisplayName(ChatColor.DARK_RED + "YELLOW TEAM");
        item.setItemMeta(meta);
        inv.setItem(2, item);
          
          
        //GREEN TEAM
        item.setType(Material.GREEN_CONCRETE);
        meta.setDisplayName(ChatColor.DARK_RED + "GREEN TEAM");
        item.setItemMeta(meta);
        inv.setItem(3, item);
      
        //close button
        item.setType(Material.BARRIER);
        meta.setDisplayName(ChatColor.RED + "Close menu");
        lore.clear();
        meta.setLore(lore);
        item.setItemMeta(meta);
        inv.setItem(8, item);
    
          
        }
    
        public void onGoAwayReceived(int arg0, long arg1, ByteBuf arg2) {
          
        }
    
        public void onGoAwaySent(int arg0, long arg1, ByteBuf arg2) {
            // TODO Auto-generated method stub
          
        }
    
        public void onStreamActive(Http2Stream arg0) {
          
        }
    
        public void onStreamAdded(Http2Stream arg0) {
          
        }
    
        public void onStreamClosed(Http2Stream arg0) {
            // TODO Auto-generated method stub
          
        }
    
        public void onStreamHalfClosed(Http2Stream arg0) {
            // TODO Auto-generated method stub
          
        }
    
        public void onStreamRemoved(Http2Stream arg0) {
            // TODO Auto-generated method stub
          
        }
    }
     
    Last edited by a moderator: Nov 9, 2020
  2. Offline

    timtower Moderator Moderator

    @MaxBans Have you added print statements?
    And don't do full caps titles, is not appreciated anywhere.
     
  3. Offline

    MaxBans

    I dont know, how do i do that?

    Oh wait, i think i fixed it i had a bad import there :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 9, 2020

Share This Page