Can't Use A Method From Another Class in Main

Discussion in 'Plugin Development' started by LoyalViper, Sep 2, 2020.

  1. Offline

    LoyalViper

    So hello everyone as the title says i have a issue. I'm trying to add a GUI to my plugin.
    Code:
    package me.loyalviper.commandplugin;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
       
        static ChangeTeam change = new ChangeTeam();
       
        @Override
        public void onEnable() {
           
            change.createInv();
           
           
            this.getServer().getPluginManager().registerEvents(new poseidon(), this);
            this.getServer().getPluginManager().registerEvents(new ChangeTeam(), this);
            this.getServer().getPluginManager().registerEvents(new autoFeed(), this);
            this.getServer().getPluginManager().registerEvents(this, this);
           
            // Registers the command executors
            getCommand("hello").setExecutor(new hello());
            getCommand("heal").setExecutor(new heal());
            getCommand("suicide").setExecutor(new suicide());
            getCommand("launch").setExecutor(new launch());
            getCommand("doctor").setExecutor(new doctor());
            getCommand("poseidon").setExecutor(new poseidon());
            getCommand("ChangeTeam").setExecutor(new ChangeTeam());
            getCommand("autoFeed").setExecutor(new autoFeed());
           
          
        }
        public void onDisable() {
           
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
    
            if(label.equalsIgnoreCase("godboots")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage("Console can't execute this command");
                   
                return true;
                }
                Player player = (Player) sender;
                if(player.getInventory().firstEmpty() == -1) {
                    Location loc = player.getLocation();
                    World world = player.getWorld();
                   
                    world.dropItemNaturally(loc, getItem());
                    player.sendMessage(ChatColor.GOLD+"Minecraft legends dropped a item near you.");
                    return true;
                }
                player.getInventory().addItem(getItem());
                player.sendMessage(ChatColor.GOLD+"The Minecraft Legends gave you this boot.");
                return true;
            }
           
           
            return false;
        }
       
       
            public ItemStack getItem() {
           
            ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);
            ItemMeta meta = boots.getItemMeta();
           
            meta.setDisplayName(ChatColor.BLUE+"Boots of Leaping");
            List<String> lore = new ArrayList<String>();
            lore.add("");
            lore.add(ChatColor.GOLD+""+ChatColor.ITALIC+"Boots made for the Minecraft Gods");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.PROTECTION_FALL, 1, true);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            meta.setUnbreakable(true);
           
            boots.setItemMeta(meta);
           
           
            return boots;
      
        }
           
            @EventHandler()
            public void onJump(PlayerMoveEvent event ) {
               
                Player player = (Player) event.getPlayer();
                if(player.getInventory().getBoots()!= null);
                    if(player.getInventory().getBoots().getItemMeta().getDisplayName().contains("Boots of Leaping"))
                        if(player.getInventory().getBoots().getItemMeta().hasLore())
                            if(event.getFrom().getY() < event.getTo().getY() &&
                                    player.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) {
                                player.setVelocity(player.getLocation().getDirection().multiply(2).setY(2));
                               
                               
                            }
       
       
            }
           
            @EventHandler()
            public void onFall(EntityDamageEvent event) {
               
                if(event.getEntity() instanceof Player) {
                    Player player = (Player) event.getEntity();
                    if(event.getCause() == DamageCause.FALL) {
                        if(player.getInventory().getBoots() != null);
                          if(player.getInventory().getBoots().getItemMeta().getDisplayName().contains("Boots of Leaping"))
                            if(player.getInventory().getBoots().getItemMeta().hasLore()) {
                                event.setCancelled(true);
                            }
                       
                    }
                }
            }
           
       
      @EventHandler()
      public void onJoin(PlayerJoinEvent event) {
          Player player = (Player) event.getPlayer();
          player.sendMessage(ChatColor.GREEN +"Welcome to our server" + " " + player.getName() + " !");
         
         
         
      }
    }

    This is my Main class
    Code:
    package me.loyalviper.commandplugin;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event.Result;
    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.inventory.meta.LeatherArmorMeta;
    
    public class ChangeTeam  implements CommandExecutor, Listener{
       
        Inventory inv;
       
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(label.equalsIgnoreCase("changeteam")){
                if(!(sender instanceof Player)) {
                   
                    sender.sendMessage("You can't execute this command");
                    return true;
                }
                Player player = (Player) sender;
           
                player.openInventory(inv);
                return true;
           
            }
           
           
           
            return false;
        }
       
    public void createInv() {
           
            inv = Bukkit.createInventory(null, 9,ChatColor.GOLD + "Select Your Team !");
           
            ItemStack item = new ItemStack(Material.BLUE_CONCRETE);
            ItemMeta meta = item.getItemMeta();
           
            //Blue Team
            meta.setDisplayName(ChatColor.DARK_BLUE+"BLUE TEAM");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY+"Click to join team!");
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(0, item);
           
            // Red Team
            item.setType(Material.RED_CONCRETE);
            meta.setDisplayName(ChatColor.DARK_RED+"RED TEAM");
            item.setItemMeta(meta);
            inv.setItem(1,item);
    
            // Green Team
            item.setType(Material.LIME_CONCRETE);
            meta.setDisplayName(ChatColor.GREEN+"Green TEAM");
            item.setItemMeta(meta);
            inv.setItem(2,item);
            // Orange Team
            item.setType(Material.ORANGE_CONCRETE);
            meta.setDisplayName(ChatColor.GOLD+"Orange TEAM");
            item.setItemMeta(meta);
            inv.setItem(3,item);
            // Purple Team
            item.setType(Material.PURPLE_CONCRETE);
            meta.setDisplayName(ChatColor.DARK_PURPLE + " PURPLE TEAM");
            item.setItemMeta(meta);
            inv.setItem(4,item);
            // Cyan Team
            item.setType(Material.CYAN_CONCRETE);
            meta.setDisplayName(ChatColor.BLUE+"CYAN TEAM");
            item.setItemMeta(meta);
            inv.setItem(5,item);
            // BLACK Team
            item.setType(Material.BLACK_CONCRETE);
            meta.setDisplayName(ChatColor.DARK_GRAY+"BLACK TEAM");
            item.setItemMeta(meta);
            inv.setItem(6,item);
           
            //close button
            item.setType(Material.BARRIER);
            meta.setDisplayName(ChatColor.RED+""+ ChatColor.BOLD+"Click to close menu");
            lore.clear();
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(8, item);
           
           
           
    
           
           
           
        }
       
       
        @EventHandler()
        public void onClick(InventoryClickEvent event) {
           
            if(!event.getInventory().equals(inv))
                return;
            if(event.getCurrentItem() == null) return;
            if(event.getCurrentItem().getItemMeta() == null) return;
            if(event.getCurrentItem().getItemMeta().getDisplayName()== null) return;
           
            event.setResult(Result.DENY);
    
            Player player = (Player) event.getWhoClicked();
           
            if(event.getSlot() == 0) {
               
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.BLUE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD+ "You changed your team!");
            }
            if(event.getSlot() == 1) {
               
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.RED);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD+ "You changed your team!");
            }
            if(event.getSlot() == 2) {
               
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.GREEN);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD+ "You changed your team!");
            }
            if(event.getSlot() == 3) {
               
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.ORANGE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD+ "You changed your team!");
            }
            if(event.getSlot() == 4) {
               
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.PURPLE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD+ "You changed your team!");
            }
            if(event.getSlot() == 5) {
               
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.BLUE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD+ "You changed your team!");
            }
            if(event.getSlot() == 6) {
               
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.BLACK);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD+ "You changed your team!");
            }
            if(event.getSlot() == 8) {
               
                player.closeInventory();
               
            }
           
            return;
           
        }
       
       public ItemStack[] changeColor(ItemStack[] a, Color color) {
          
           for(ItemStack item : a) {
              
               try {
                   if(item.getType() == Material.LEATHER_BOOTS || item.getType() == Material.LEATHER_LEGGINGS || item.getType() == Material.LEATHER_CHESTPLATE ||
                              item.getType() == Material.LEATHER_HELMET) {
                         
                          LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
                         
                          meta.setColor(color);
                          item.setItemMeta(meta);
                         
                         
                      }
                  
               }
               catch(Exception e) {
                  
                   //do nothing
               }
              
           
              
           }
           return a;
       }
       
    }
    
    And this is the other class. When i try this it doesn't work. Can anyone help?
     
    Last edited by a moderator: Sep 2, 2020
  2. Offline

    KarimAKL

    @LoyalViper What isn’t working? We need more information.
     
  3. Offline

    LoyalViper

    I
    When I type the command the inventory doesn't show up. Instead, I get an error message as an internal error occurred. I tried moving the createInv method to the main class and I used plugin.inv in my second class and then it worked. But to prevent further confusion in my code i'd like to move createInv method as well to the second class.
     
  4. Online

    timtower Moderator Moderator

  5. Offline

    ChiefMoneyBags

    inv in your ChangeTeam class is null when you run the command because you are creating 2 seperate instances of ChangeTeam. The first one you create in the main class upon initialization of the class, this is the one where you call createInv() in onEnable(). Then you create a second instance when you are registering your CommandExecutor. This second instance never had its method createInv() invoked. Try these changes instead.
    Code:
    getCommand("ChangeTeam").setExecutor(change);
    this.getServer().getPluginManager().registerEvents(change, this);
    
     

Share This Page