1.16.5 Can Someone Help Me with GUI?

Discussion in 'Spigot Plugin Development' started by BandaiBoys, Jan 12, 2021.

  1. So, I'm trying to make a GUI plugin and I tried following tutorials but nothing worked.

    Here is the code:

    I don't know what the problem is here.. Can someone tell me what I did wrong?

    Code (Java):
    Main.java:

    package me.marketplugin.marketplugin;

    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
       
            private static Main instance;
       
        public void onEnable() {
            instance = this;
                new guiCMD(plugin: this);
            }
           
            public static Main  getInstance() {
                return instance;
               
            }

    }
     
    Code (Java):
    guiCMD:

    package me.marketplugin.marketplugin;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class guiCMD implements CommandExecutor {
       
        private Main plugin;
       
        public guiCMD(Main plugin) {
           
            plugin.getCommand(name:"gui").setExecutor(this);
        }
       
        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
            if (!(commandSender instanceof Player)) {
                return false;
            }
           
            Player p = (Player) commandSender;
            GUIinventory gui = new GUIinventory(p, name:"Test", slots: 9);
            gui.setItems();
            gui.open();
            return true;
        }
    }
     
    Code (Java):
    package me.marketplugin.marketplugin;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryAction;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class GUIinventory implements Listener {
       
        private Player p;
        private  String name;
        private Inventory inv;
        private ItemStack healIs;
       
        public void guiInventory(Player p, String name, int slots) {
            this.p = p;
            this.name = name;
            inv = Bukkit.createInventory(p, slots, Color(name));
            Main.getInstance().getServer().getPluginManager().registerEvents(listener: this, Main.getInstance());
        }
       
        public void open() {
            p.openInventory(inv);
        }
       
        public void setItems() {
            healIs = new ItemStack(Material.EMERALD);
            ItemMeta im = HealIs.getItemMeta();
            im.SetDisplayName(Color(s:"Create Market Item"));
            healIs.setItemMeta(im);
            inv.setItem(i: 4, healIs);  
        }
       
       
        @EventHandler
        public void OnClick(InventoryClickEvent e) {
            Inventory clicked = e.getClickedInventory();
            if (clicked == null) {
                return;
            }
            if (le.getWhoClicked().equals(p)) {
                return;
            }
            if (clicked.equals(inv)) {
                return;
            }
            e.setCancelled(true);
            if (e.getAction().equals(InventoryAction.HOTBAR_SWAP)) {
                return;
            }
            if (clicked.equals(p.getInventory())) {
                return;  
            }
            ItemStack is = e.getCurrentItem();
            if (is == null || is.getType().equals(Material.AIR)) {
            return;  
            }
           
            if (is.equals(healIs)) {
               
            }
        }
       
        private String Color(String s) {
            return ChatColor.translateAlternateColorCodes(altColorChar:'&', s)
        }
    }
     
     
  2. thats not a constructor
     
    • Agree Agree x 1
  3. The constructor for guiInventory is incorrect, whatever tutorial you followed is wrong :p

    this.p and this.name are not necessary at all, and it probably messes things up lol, this is what you want inside of your guiInventory void:
    Code (Java):
    inv = Bukkit.createInventory(p, slots, name);
    inv.setContents(///whatever itemstack(s) in here///)
    then in your command class, you want to open the inventory with:
    Code (Java):
    player.openInventory(inv);