Open an Inventory in 1.11.2

Discussion in 'Plugin Development' started by FriedrichLP, Sep 11, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    FriedrichLP

    Hi
    I‘ve searched now for a LOOOONG time how to open an inventory and I found many ways to do it but it never worked for me. Some Threads were tooo old and the other were tooo new xD. Could anyone help me to open an Inventory in Minecraft 1.11.2 and maybe send me the code? Thanks for all help.
    I‘ve tried Bukkit.createInventory and plugin.createInventory.
     
  2. Offline

    travja

    Code:
    Inventory inv = Bukkit.createInventory(null, 36, "§b§lTesting Inventory"); //All this does is create the inventory.
    //The first argument in createInventory is a player object, but it can be null, and commonly is.
    
    //In order to have the player open the inventory, you need to run:
    player.openInventory(inv);
     
  3. Offline

    FriedrichLP

    Could you send me your imports please? Because it still sends me an error message!
    Here is my error message (maybe you can use it):
    Code:
    [15:41:27 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tban' in plugin UniversalBukkitPlugin v0.4.8
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:629) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1295) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1155) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_141]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_141]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:695) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:360) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:650) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_141]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at com.FriedrichLP.youtube.UniversalBukkitPlugin.UniversalBukkitPlugin.onCommand(UniversalBukkitPlugin.java:41) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.11.2.jar:git-Bukkit-6e3cec8]
            ... 15 more

    And here is my code:
    Code:
    package com.FriedrichLP.youtube.UniversalBukkitPlugin;
    
    import java.awt.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class UniversalBukkitPlugin extends JavaPlugin{
      
        @Override
        public void onEnable() {
            super.onEnable();
          
            String pluginname = this.getDescription().getName();
            String pluginversion = this.getDescription().getVersion();
          
            new UniversalEventListener(this);
          
            System.out.println(ChatColor.YELLOW + "Plugin Started");
            System.out.println(ChatColor.YELLOW + pluginname + ChatColor.RED + " version " + pluginversion + ChatColor.YELLOW +  " geladen");
          
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            //falls sender ein player ist dann wird der sender zum player
            Player player = null;
            if(sender instanceof Player) {
                player = (Player) sender;  
            }
            //kommando "tban"
            if(command.getName().equalsIgnoreCase("tban")) {
                    Inventory BanInv = Bukkit.createInventory(null, 36, ChatColor.RED + args[0] + " bannen.");
                    player.openInventory(BanInv);
                    BanInv.setItem(0, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(1, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(2, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(3, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(4, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(5, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(6, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(7, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    BanInv.setItem(8, new ItemStack(Material.STAINED_GLASS_PANE, 1, (short)14));
                    return true;
    After this is more code but everything else works so I´m very confused. Thx for any help
     
    Last edited by a moderator: Sep 12, 2017
  4. Online

    timtower Administrator Administrator Moderator

    @FriedrichLP You are using args[0] without checking if it exists.
    Don't log your own plugins.
    Add the items to the inventory, THEN open it
     
  5. Offline

    FriedrichLP

    One more question.
    I now have an Item thats called "Cancel" and I want it so that if I click on the item the event gets cancelled and the Inventory closes. I made an InventoryClickEvent but it doesn´t even activates if I click the item.
     
  6. Offline

    Caderape2

    @FriedrichLP Show what you did and where you register the event
     
  7. Offline

    FriedrichLP

    Code:
        @EventHandler
        public void InventoryClick(InventoryClickEvent event) {
            Inventory open = event.getInventory();
            ItemStack item = (ItemStack) event.getCurrentItem();
            Player player = (Player) event.getWhoClicked();
            if(open.getName().equalsIgnoreCase("BanInv")) {
                event.setCancelled(true);
                if(item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Cancel")) {
                    player.closeInventory();
                }
            }
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            Player player = null;
            if(sender instanceof Player) {
                player = (Player) sender;   
            }
            if(command.getName().equalsIgnoreCase("tban")) {
                if(args.length == 1) {
                    Inventory BanInv = Bukkit.createInventory(null, 36, "Ban" + args[0]);
                    ItemStack redglass = nameItem(Material.STAINED_GLASS_PANE, ChatColor.RED + "Cancel", 1, (short)14);
                    BanInv.setItem(0, new ItemStack(redglass));
                    BanInv.setItem(1, new ItemStack(redglass));
                    BanInv.setItem(2, new ItemStack(redglass));
                    BanInv.setItem(3, new ItemStack(redglass));
                    BanInv.setItem(4, new ItemStack(redglass));
                    BanInv.setItem(5, new ItemStack(redglass));
                    BanInv.setItem(6, new ItemStack(redglass));
                    BanInv.setItem(7, new ItemStack(redglass));
                    BanInv.setItem(8, new ItemStack(redglass));
                    BanInv.setItem(27, new ItemStack(redglass));
                    BanInv.setItem(28, new ItemStack(redglass));
                    BanInv.setItem(29, new ItemStack(redglass));
                    BanInv.setItem(30, new ItemStack(redglass));
                    BanInv.setItem(31, new ItemStack(redglass));
                    BanInv.setItem(32, new ItemStack(redglass));
                    BanInv.setItem(33, new ItemStack(redglass));
                    BanInv.setItem(34, new ItemStack(redglass));
                    BanInv.setItem(35, new ItemStack(redglass));
                    player.openInventory(BanInv);
                    return true;
                   
                }
     
  8. Offline

    Horsey

    1. Reuse the same inventory rater than creating it over and over again.
    2. Your code will produce NPEs if executed by console
    3. Your problem: You create the inventory with the name 'Ban', however our check if the name is BanInv' in your listener
     
  9. Offline

    FriedrichLP

    But how can i reuse it? I´ve tried to put it in onEnable but then it doesn´t work in the onCommand.
     
  10. Online

    timtower Administrator Administrator Moderator

    @FriedrichLP You declare it in the class as field, you initialize it in the onEnable
     
  11. Offline

    FriedrichLP

    pls send me an example. I have no idea how to do this xD
    And how can I make the name of the player from args[0] private/public so i can use it to get the Inventory name?
     
  12. Offline

    bennie3211

    @FriedrichLP I think you need to learn the basics and start reading the javadocs for all the functions and use google instead of asking for code.
     
  13. Offline

    travja

    Check if the Inventory title starts with "Ban" by using title.startsWith. No need to save the inventory either, as the inventory will have to change for every player you go to ban.
     
  14. Offline

    Minesuchtiiii

    1. Create inventory
    Code:
    Inventory inv = Bukkit.createInventory(null, 9, "§cExample inventory");
    2. Create ItemStacks, example:
    Code:
          ItemStack sky = new ItemStack(Material.GLASS);
          ItemMeta skymeta = sky.getItemMeta();
          skymeta.setDisplayName("§eSky");
          ArrayList<String> skymetalore = new ArrayList<String>();
          skymetalore.add("§7To teleport a player to the sky.");
          skymeta.setLore(skymetalore);
          sky.setItemMeta(skymeta);
    3. Add the item to the inventory
    Code:
    inv.setItem(5, sky);
    4. Open the inventory for the player
    Code:
    p.openInventory(inv);
     
Thread Status:
Not open for further replies.

Share This Page