variable things..

Discussion in 'Spigot Plugin Development' started by nktfh100, Apr 16, 2017.

  1. ** i am new at java**


    so im trying to make a command that open custom inventory
    so i made 2 classes ( there main class... but leave it for now)
    1:
    Code (Text):
    package com.nktfh100.plugin;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;


    public class myCustomInventory {
        public static Inventory myInventory = Bukkit.createInventory(null, 9, "My custom Inventory!");
        // The first parameter, is the inventory owner. I make it null to let everyone use it.
        //The second parameter, is the slots in a inventory. Must be a multiple of 9. Can be up to 54.
        //The third parameter, is the inventory name. This will accept chat colors
        static {
            myInventory.setItem(0, new ItemStack(Material.DIAMOND, 1));
            myInventory.setItem(8, new ItemStack(Material.GOLD_INGOT, 1));
        }
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked(); // The player that clicked the item
        ItemStack clicked = event.getCurrentItem(); // The item that was clicked
        Inventory inventory = event.getInventory(); // The inventory that was clicked in
        if (inventory.getName().equals(myInventory.getName())) { // The inventory is our custom Inventory
        if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
        event.setCancelled(true); // Make it so the dirt is back in its original spot
        player.closeInventory(); // Closes there inventory
        player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
        }
        }
        }
       
       
           


    }



    2:
    Code (Text):
    package com.nktfh100.plugin;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    public class cmdinv implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
             if (!(sender instanceof Player)) {
                 sender.sendMessage(ChatColor.DARK_RED + "You're not a player!");
                 return false;
            }
             sender.openInventory(myInventory);
            return false;
        }
    }
     
    but in the sender.openInventory(myInventory); it says: myInventory cant resolove to a variable..

    i used this tutorial:
    https://bukkit.org/threads/tutorial-create-a-inventory-menu.173571/

    what i have done wrong?
    please help me.
    (sorry bad english)


     
  2. Change it to openInventory(myCustomInventory.myInventory)
     
  3. Mas

    Mas

    I really suggest learning Java before learning the Spigot API. You wouldn't try and learn to sprint without learning to walk first, would you?
    • Develop an understanding of OOP (object oriented programming) and proper usage of static (don't just put static in front of everything because it works)
    • Familiarise yourself with Java naming conventions (UpperCamcelCase for classes
    • Learn how objects inside classes work
    Also, your Listener class needs to implement Listener and be registered in your main class.
     
    #3 Mas, Apr 16, 2017
    Last edited: Apr 16, 2017
  4. You should use PascalCase for your class names (as it's the naming convention). Also, those variables shouldn't be static.
     
    • Like Like x 1
  5. As a side note, good job on commenting each line. It's a good thing to do if you're new.