Getting variables from different class

Discussion in 'Plugin Development' started by plisov, Jan 17, 2017.

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

    plisov

    So I'm trying to get some variables I set in a class from a different class. The class with the variables looks like this

    Code:
    package me.plisov.shards.items;
    
    import java.io.IOException;
    import java.util.Arrays;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Sword implements Listener {
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) throws IOException {
            Player player = (Player) e.getWhoClicked();
    
            ItemStack ss1 = new ItemStack(Material.PRISMARINE_SHARD);
            ItemMeta ss1meta = ss1.getItemMeta();
            ss1meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Sword Shard");
            ss1meta.setLore(Arrays.asList(ChatColor.GREEN + "Pommel", ChatColor.GRAY + "1/5"));
            ss1.setItemMeta(ss1meta);
    
            ItemStack ss2 = new ItemStack(Material.PRISMARINE_SHARD);
            ItemMeta ss2meta = ss2.getItemMeta();
            ss2meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Sword Shard");
            ss2meta.setLore(Arrays.asList(ChatColor.GREEN + "Grip", ChatColor.GRAY + "2/5"));
            ss2.setItemMeta(ss2meta);
    
            ItemStack ss3 = new ItemStack(Material.PRISMARINE_SHARD);
            ItemMeta ss3meta = ss3.getItemMeta();
            ss3meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Sword Shard");
            ss3meta.setLore(Arrays.asList(ChatColor.GREEN + "Guard", ChatColor.GRAY + "3/5"));
            ss3.setItemMeta(ss3meta);
    
            ItemStack ss4 = new ItemStack(Material.PRISMARINE_SHARD);
            ItemMeta ss4meta = ss4.getItemMeta();
            ss4meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Sword Shard");
            ss4meta.setLore(Arrays.asList(ChatColor.GREEN + "Blade", ChatColor.GRAY + "4/5"));
            ss4.setItemMeta(ss4meta);
    
            ItemStack ss5 = new ItemStack(Material.PRISMARINE_SHARD);
            ItemMeta ss5meta = ss5.getItemMeta();
            ss5meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Sword Shard");
            ss5meta.setLore(Arrays.asList(ChatColor.GREEN + "Sheath", ChatColor.GRAY + "5/5"));
            ss5.setItemMeta(ss5meta);
        }
    }
    
    In the class from where I'm trying to call the variables from I type
    Sword sword = new Sword();

    How would I do it so that later on in the class I could do something like sword.ss1?
     
  2. Offline

    Zombie_Striker

    @plisov
    Create fields. Store those instances in those fields.

    BTW: The only thing that changes is the number. Make a for loop so you can reduce 20 lines.
     
  3. Offline

    plisov

    The line and the lore changes. Each item has a different lore.
    Pommel, Grip etc. And thanks. I'll try the field thing.

    EDIT:
    This is how I'm trying to make the field. Is this correct?

    public Sword sword;

    When I go down and type sword.ss1, ss1 becomes red.
     
  4. Offline

    Zombie_Striker

    @plisov
    1. Yes, each one has different lore, but the only thing different is a single number. That is why a for loop would be good.
    2. Yes, that is a field, but no, that is not what you need to do. You need to make the itemstacks fields, not the class itself.
     
  5. Offline

    plisov

    Ah Okay. So
    public ItemStack sword;?
    How would I get the variables from the other class?
    sword = new Sword();?
     
  6. Offline

    Zombie_Striker

    @plisov
    Don't create new instances if you don't have to. Only create the class once.

    After having the sword instance stored somewhere, use sword.sword.
     
  7. Offline

    plisov

    So something like this?
    public ItemStack sword;
    Sword sword;
    ?
     
Thread Status:
Not open for further replies.

Share This Page