Calling variables in other classes.

Discussion in 'Plugin Development' started by O_o_o_o_o, Sep 23, 2019.

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

    O_o_o_o_o

    Hey, I'm making a plugin that has an inventory GUI and when you click the glass it gives you an item in a certain slot. However, The way I'm doing it is having the game test if a certain variable is a certain number and if it returns true then it switches out whatever is in you're first slot but I don't know how to go about doing this. The code as of now works perfectly, the special inventory opens up upon usage of the command /abilities, and I tested the click event with healing and it also works. I just don't know how to call the variable
    Code:
    public class A
    
    public static boolean hActive = false;
    
    
        public static int Slot1 = 0;
        public static int Slot2 = 0;
        public static int Slot3 = 0;
        @EventHandler
        public void activateH(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (p.getInventory().getHeldItemSlot() == 8 && HUser == true ) {
                HActive = true;
                p.sendMessage("what.");
              
                if (Slot1 == 0 && Slot2 == 0 && Slot3 == 0) {
                ItemStack SLO = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
                ItemMeta SLOMeta =  SLO.getItemMeta();
                SLOMeta.setDisplayName(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "No ability assigned!");
                ArrayList<String> SLOlore = new ArrayList<>();
                SLOlore.add(ChatColor.GRAY + "To view your abilities do /abillity!");
                SLOMeta.setLore(SLOlore);
                SLO.setItemMeta(SLOMeta);
                p.getInventory().setItem(1, SLO);
                p.getInventory().setItem(2, SLO);
                p.getInventory().setItem(3, SLO);
                }
                else {
                    if (Slot1 == 1 && Slot2 == 0 && Slot3 == 0) {
                        ItemStack SLO = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
                        ItemMeta SLOMeta =  SLO.getItemMeta();
                        SLOMeta.setDisplayName(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "No ability assigned!");
                        ArrayList<String> SLOlore = new ArrayList<>();
                        SLOlore.add(ChatColor.GRAY + "To view your abilities do /abillity!");
                      
                        ItemStack SuperO= new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 1);
                        ItemMeta SuperO =  SuperO.getItemMeta();
                        SuperOMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Super O");
                        ArrayList<String> SuperOlore = new ArrayList<>();
                        SuperOlore.add(ChatColor.YELLOW + "Let energy flow through your very being to unleash a powerful punch!");
                        SunlightOverdrivelore.add(ChatColor.YELLOW + "" + ChatColor.ITALIC + "Use /stats to upgrade your hamon potential.");
                        SuperOMeta.setLore(SunlightOverdrivelore);
                        SuperO.setItemMeta(SuperO);
                      
                        SLOMeta.setLore(SLOlore);
                        SLO.setItemMeta(SLOMeta);
                        p.getInventory().setItem(1, SuperO);
                        p.getInventory().setItem(2, SLO);
                        p.getInventory().setItem(3, SLO);
                    }
                }
                ItemStack exitHamon = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 14);
                ItemMeta exitHMeta =  exitH.getItemMeta();
                exitHMeta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Deactivate");
                ArrayList<String> exitHlore = new ArrayList<>();
                exitHlore.add(ChatColor.GRAY + "Do you need a description? The item name is deactivate.");
                exitHMeta.setLore(exitHlore);
                exitH.setItemMeta(exitHMeta);
                p.getInventory().setItem(0, exitH);
              
              
              
            }
        }
    
    
    
    public class HUI implements Listener {
        private Plugin plugin = Main.getPlugin(Main.class);
    
        public void newInventory(Player player) {
            Inventory i = plugin.getServer().createInventory(null, 18, "H Abilities");
            ItemStack SunlightOverdrive = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 1);
            ItemMeta SuperOMeta =  SuperO.getItemMeta();
            SuperOMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "SuperO");
            ArrayList<String> SuperOlore = new ArrayList<>();
            SuperOlore.add(ChatColor.YELLOW + "Let energy flow through your very being to unleash a powerful punch!");
            SuperOlore.add(ChatColor.YELLOW + "" + ChatColor.ITALIC + "Use /stats to upgrade your  potential.");
            SuperOMeta.setLore(SuperOlore);
            SuperO.setItemMeta(SuperO);
            i.setItem(0, SuperO);
          
            player.openInventory(i);
        }
      
    }
    
    
    public class EventsClass implements Listener {
        public String prefix = ("TUTORIAL");
      
        @EventHandler
        public void InvenClick(InventoryClickEvent event) {
          
            Player player = (Player) event.getWhoClicked();
            Inventory open = event.getClickedInventory();
            ItemStack item = event.getCurrentItem();
            if(open == null) {
                return;
            }
            if(open.getName().equals("H Abilities")) {
                if(item.equals(null) || !item.hasItemMeta()) {
                    return;
                }
                if(item.getItemMeta().getDisplayName().equals(ChatColor.GOLD + "" + ChatColor.BOLD + "SuperO")){
                    Slot1 = 1;
                }
              
            }
        }
      
    }
     
  2. Offline

    Sir_Jonn

    What do you mean by calling a variable? From another class?
     
  3. Offline

    O_o_o_o_o

    I mean like call the variable Slot1 in the class EventClass so I can change its value.
     
  4. Since these variables are static (for whatever reason) you can access them using A.Slot1 (ClassName.Variable). A way to walk around static variables would be to save the instance of A when you register the listener in your main and call the variable via that instance. But that's your choice
     
Thread Status:
Not open for further replies.

Share This Page