Variables from other class.

Discussion in 'Plugin Development' started by placerwiz, May 21, 2016.

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

    placerwiz

    I'm very new with Java and was introduced to it by creating Minecraft plugins. I am currently using Spigot and want a variable to be accessed through another class. In this plugin, I want players to be able to create a Hero that has certain abilities. The two classes that I use are below.

    Code:
    package me.placerwiz;
    
    import org.bukkit.command.Command;
    
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.plugin.java.JavaPlugin;publicclassMobaextendsJavaPlugin{
    
    StompCooldown a;
    
    @Overridepublicvoid onEnable(){
    getServer().getPluginManager().registerEvents(newMenuClick(this),this);newPlayerListener(this);newStompAbility(this);
    getLogger().info("This plugin has been enabled!");
    a =newStompCooldown(this);
    a.runTaskTimer(this,20,20);
    getCommand("pearl").setExecutor(newWarpAbility());
    getCommand("menu").setExecutor(this);}
    
    @Overridepublicvoid onDisable(){
    
    }
    
    publicboolean onCommand(CommandSender sender,Command cmd,String label,String[] args){
    
    if(cmd.getName().equalsIgnoreCase("Menu")&& sender instanceofPlayer){
    
    Player player =(Player) sender;
    player.openInventory(Menu.getMenu());
    
    returntrue;
    
    }
    
    returnfalse;}
    
    publicstaticvoid sircunningham1_1(String args[]){
    
    SirCunningham_1_1 getLoadout =newSirCunningham_1_1();
    getLoadout.heroChosen();
    
    }
    
    publicstaticvoid sircunningham2_1(String args[]){
    
    SirCunningham_2_1 getLoadout =newSirCunningham_2_1();
    getLoadout.heroChosen();
    
    }
    
    publicvoid gotHero(String heroChoice){if(heroChoice ==""){
    
    }}
    
    publicboolean heroTest(CommandSender sender,Command cmd,String label,String[] args){
    
    if(cmd.getName().equalsIgnoreCase("hero")&& sender instanceofPlayer){
    
    Player player =(Player) sender;
    player.openInventory(Menu.getMenu());
    
    returntrue;
    
    }
    
    returnfalse;}}
    The code above is my main class, Moba. In this code, a variable called heroChoice is received from the other class. The only problem from this is that I want the code to get what the player has selected as the hero. When it gets the hero, I want it to get the hero that the player has selected. Is there anyway I can get a variable sent to the Moba class after the player clicks on the final inventory item. It might need to use this class where the player selects the final ability for the hero "Sir Cunningham". (See code below)

    Code:
    package me.placerwiz;
    
    import java.util.ArrayList;import java.util.List;import java.util.Scanner;
    
    import org.bukkit.Bukkit;import org.bukkit.ChatColor;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;import org.bukkit.inventory.meta.ItemMeta;
    
    publicclassSirCunningham_2_1{staticString hero;publicstaticInventory getMenu(){
    
    Inventory inv =Bukkit.createInventory(null,18,ChatColor.GREEN +ChatColor.BOLD.toString()+"Choose ultimate ability!");
    
    ItemStack item =newItemStack(Material.IRON_BOOTS);ItemMeta meta = item.getItemMeta();List<String> lore =newArrayList<String>();
    lore.add(" ");
    lore.add(ChatColor.YELLOW +"Thoughts of glory inspire your team to");
    lore.add(ChatColor.YELLOW +" win this battle! Everyone on your team");
    lore.add(ChatColor.YELLOW +" gains a buff!");
    meta.setLore(lore);
    meta.setDisplayName(ChatColor.GOLD +ChatColor.BOLD.toString()+"Glory");
    item.setItemMeta(meta);
    inv.addItem(item);
    
    return inv;
    
    }
    
    @EventHandlerpublicstaticvoid onClick(InventoryClickEvent event){if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Choose ultimate ability!"))return;
    
    Player player =(Player) event.getWhoClicked();
    event.setCancelled(true);
    
    if(event.getCurrentItem()==null|| event.getCurrentItem().getType()==Material.AIR ||!event.getCurrentItem().hasItemMeta()){
    player.closeInventory();return;}if(event.getCurrentItem().getType()==Material.IRON_BOOTS){
    player.closeInventory();String hero ="SirCunnigham_2_1";
    player.openInventory(Customizer.getMenu());}else{
    player.sendMessage(ChatColor.GREEN +"["+ChatColor.YELLOW +"MOBA"+ChatColor.GREEN +"]"+ChatColor.GOLD +"-Under Construction-");
    player.closeInventory();}
    
    }publicstaticvoid heroChosen(){String heroChoice = hero;Moba sendLoadout =newMoba();
    
    sendLoadout.gotHero(heroChoice);}}
    All I need to get this to work is to have the String hero (from the if event above) to equal the String heroChoice. Thanks for reading this far and I hope this will get solved. It means a lot to me!
     
  2. Offline

    teej107

    I suggest you take a break from making plugins and finish getting a comfortable understanding of Java first. This has nothing to do with Bukkit and is a plain Java problem. That being said, your answer can be solved by passing information through parameters and possibly using getter methods.
    https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
    http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work
     
    Zombie_Striker and MadMaxCookie like this.
Thread Status:
Not open for further replies.

Share This Page