Solved keep track of a custom balance

Discussion in 'Plugin Development' started by ReflectionCraft, Jul 6, 2019.

  1. Offline

    ReflectionCraft

    I have a plugin where I've created an item called "Dust" when you right-click the dust it gets added to a balance.
    Currently, My problem is that in my Main class I create a new EventsClass (EventsClass ec = new EventsClass();) in order to have access to my balance variable that I created in the events class.

    * I need help figuring out either how to tie my balance variable to the player so I could call it like player.dustBal which could call the dust balance for the player or how to call over the variable to my class without making a whole new class every time the method is called.

    (Keep in mind I'm new to java development I simply do it for fun)

    Main.java (open)

    Code:
    package me.alton.MagicDust;
    
    
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.alton.MagicDust.Events.EventsClass;
    
    public class Main extends JavaPlugin
    {
        public void onEnable()
        {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\n[MagicDust] Plugin Enabled\n\n"); //sends message to console saying plugin enabled
            getServer().getPluginManager().registerEvents(new EventsClass(), this);
            loadConfig();
        }
        public void onDisable()
        {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "\n\n[MagicDust] Plugin Disabled\n\n"); //sends message to console saying plugin disabled
        }
        public void loadConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            Player player = (Player)sender;
            EventsClass ec = new  EventsClass();
           
            if (cmd.getName().equalsIgnoreCase("getdust") && player.hasPermission("dust.get")) //permission node is "dust.get"
            {
                ItemStack dust = new ItemStack(Material.valueOf(Main.getPlugin(Main.class).getConfig().getString("magicDustType")));
                ItemMeta dustMeta = dust.getItemMeta();
                dustMeta.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + "Magic Dust");
                dustMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                dustMeta.addEnchant(Enchantment.MENDING, 1, true);
                ArrayList<String> dustlore = new ArrayList<String>();
                dustlore.add(ChatColor.WHITE + "" + ChatColor.ITALIC + "Right Click To Consume To Balance");
                dustMeta.setLore(dustlore);
                dust.setItemMeta(dustMeta); 
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin(Main.class).getConfig().getString("dustGetMessage"))); //sends message defines in config
                player.getInventory().addItem(dust);
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("dbal") && player.hasPermission("dust.bal")) //permission node is "dust.bal"
            {
                player.sendMessage("You have " + ec.dustBal + " Magic Dust");   
                return true;
            }
            return true;
         
        }
    
    }
    
    


    EventsClass.java (open)

    Code:
    package me.alton.MagicDust.Events;
    
    
    
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    public class EventsClass implements Listener {
    
    
    public int dustBal = 0;
    
    
    
       
        @EventHandler //consume dust to balance
        public void onRightClick(PlayerInteractEvent event) // method for event PlayerInteract
        {
        Player player = event.getPlayer();
        ItemStack handItem = player.getInventory().getItemInMainHand();
       
         if(event.getAction().equals(Action.RIGHT_CLICK_AIR) && handItem.getItemMeta().getItemFlags().contains(ItemFlag.HIDE_ENCHANTS))
         {
             int amount = handItem.getAmount();
             player.getInventory().removeItem(handItem);
             player.sendMessage(ChatColor.DARK_AQUA + "You have consumed " + ChatColor.WHITE + amount + ChatColor.DARK_AQUA + " Magic Dust");
             dustBal = dustBal + amount;
             player.sendMessage("testing " + dustBal);
         }
        }
       
    }
    
       
    
     
  2. Online

    timtower Ninja on the waves Moderator

  3. Offline

    ReflectionCraft

    Would this be done like
    Code:
    public void getBalance(player)
    {
    //code
    }
     
  4. Online

    timtower Ninja on the waves Moderator

    @ReflectionCraft Well, it is a getBalance, so you probably want to have it return a balance number.
     
  5. Offline

    ReflectionCraft

    Could you possibly go more in deapth
     
  6. Online

    timtower Ninja on the waves Moderator

    What you want (adding a variable to the Player class) is just not possible.
    You need a HashMap to store the balance of each player.
    You need to have methods to get and modify that.
     
  7. Offline

    ReflectionCraft

    Do you have any good examples of this that I can look at and learn from
     
  8. Online

    timtower Ninja on the waves Moderator

    I do not.
     

Share This Page