Making Bukkit GUI

Discussion in 'Spigot Plugin Development' started by KingTux, Jun 11, 2016.

  1. How do I make a Bukkit GUI?
    Also how do I open it on Command.
    I do not care how to Make it interactive Just.
     
  2. Bukkit.createInventory
     
  3. Code (Text):

    package me.KingLinux01;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.minecraft.server.v1_9_R2.ItemStack;

    public class Main extends JavaPlugin implements Listener {
       public Inventory inv;
       public void CreateINV() {
          inv = Bukkit.createInventory(null, 9, "HEy");
         org.bukkit.inventory.ItemStack stuff = new org.bukkit.inventory.ItemStack(Material.BEDROCK);
         inv.addItem(stuff);
         
       }

       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         Player player = (Player) sender;
         if (cmd.getName().equalsIgnoreCase("test")) {
    player.openInventory(inv);
         }
         return false;

       }

       

    }
     
     
  4. Why have you imported net.minecraft.server.v1_9_R2.ItemStack? That is the NMS ItemStack.

    Player#openInventory
     
  5. The import was a mistake.
     
  6. DO NOT IMPORT net.minecraft.server.v1_9_R2.ItemStack AS IT WILL GIVE YOU ERRORS


    Code (Text):
    import org.bukkit.Bukkit;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    public class MenuInv extends JavaPlugin implements Listener {
            private Menu menu;
         
            public void onEnable() {
                    menu = new Menu(this);
                    Bukkit.getServer().getPluginManager().registerEvents(this, this);
            }
         
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                    if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
                    menu.show(e.getPlayer());
            }
    }


    Code (Text):
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    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.material.Wool;
    import org.bukkit.plugin.Plugin;
    public class Menu implements Listener {
            private Inventory inv;
            private ItemStack c, s, a;
         
            public Menu(Plugin p) {
                    inv = Bukkit.getServer().createInventory(null, 9, "Gamemode Chooser");
                 
                    c = createItem(DyeColor.GREEN, ChatColor.GREEN + "Creative");
                    s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "Survival");
                    a = createItem(DyeColor.RED, ChatColor.RED + "Adventure");
                 
                    inv.setItem(2, c);
                    inv.setItem(4, s);
                    inv.setItem(6, a);
                 
                    Bukkit.getServer().getPluginManager().registerEvents(this, p);
            }
         
            private ItemStack createItem(DyeColor dc, String name) {
                    ItemStack i = new Wool(dc).toItemStack(1);
                    ItemMeta im = i.getItemMeta();
                    im.setDisplayName(name);
                    im.setLore(Arrays.asList("Set your gamemode", "to " + name.toLowerCase() + " mode"));
                    i.setItemMeta(im);
                    return i;
            }
         
            public void show(Player p) {
                    p.openInventory(inv);
            }
         
            @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
                    if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                    if (e.getCurrentItem().getItemMeta() == null) return;
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Creative")) {
                            e.setCancelled(true);
                            e.getWhoClicked().setGameMode(GameMode.CREATIVE);
                            e.getWhoClicked().closeInventory();
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival")) {
                            e.setCancelled(true);
                            e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
                            e.getWhoClicked().closeInventory();
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure")) {
                            e.setCancelled(true);
                            e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
                            e.getWhoClicked().closeInventory();
                    }
            }
    }

    You can thank PogoStick29 for this. And yes, you will need multiple classes. But your final output should be the same if they are all within the same package.
     
  7. I seriously advice learning Java then the Bukkit API. Don't just expect people to spoon-feed you very basic code.
     
  8. ..?
     
  9. It will give you errors in your IDE, not in the actual game. It does not function the same way that org.bukkit.inventory.ItemStack does.
     
  10. Code (Text):

    ([world]272.4587854997904, 70.9875322518619, 602.035024387402)
    [17:39:23 INFO]: KingLinux01 issued server command: /test
    [17:39:23 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin PluginTest v1.4
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
       at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91]
       at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
       at org.bukkit.craftbukkit.v1_9_R2.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:183) ~[SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       at me.KingLinux01.Main.onCommand(Main.java:44) ~[?:?]
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[SpigotAPI.jar:git-Spigot-8a048fe-042911f]
       ... 15 more
    >

     
     
  11. at me.KingLinux01.Main.onCommand(Main.java:44) ~[?:?]
    At least share the code it tells you there's a problem in...
     
  12. player.openInventory(inv);
     
  13. NullPointerException... inv or player will be null. Most likely inv.
     
  14. Code (Text):

      public void CreateINV() {
      inv = Bukkit.getServer().createInventory(null, 9, "gggg");    
      org.bukkit.inventory.ItemStack stuff = new org.bukkit.inventory.ItemStack(Material.BEDROCK);
         inv.addItem(stuff);
     

       }
     
     
  15.  
  16. Just share you entire main class
     
  17. Code (Text):

    package me.KingLinux01;
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
       public Inventory inv;
       public void CreateINV() {
      inv = Bukkit.getServer().createInventory(null, 9, "gggg");    
      org.bukkit.inventory.ItemStack stuff = new org.bukkit.inventory.ItemStack(Material.BEDROCK);
         inv.addItem(stuff);
     

       }
       

       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         Player player = (Player) sender;
         if (cmd.getName().equalsIgnoreCase("test")) {
           player.openInventory(inv);
           return true;
         }
         return false;

       }

    }
     
     
  18. Wow... please follow my advice. Do you expect this plugin to just run magically...
     
  19. Do you ever call CreateINV() at all?