Solved Updating balance variable and calling it in a string

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

  1. Offline

    ReflectionCraft

    I have a plugin where so far I can do "/getdust" to get a "Magic Dust" which atm is just a glowstone dust with NBT data. When you right click the dust it gets added to a balance and disappears from your inventory.

    I have a second command "/dbal" to view your balance of dust. The problem is when I perform the /dbal command in the game no matter how much dust is consumed it always sends as if the value of my balance is zero.

    I'm, sure I'm just making some mistake of where I'm defining my variable or maybe I need to redefine my balance variable before I call it, but I'm unsure so I'm asking for help. I've attached the code below, Thanks.

    MAIN: where I make the commands and such
    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;
       
        }
    
    }

    EVENTS: Where I update the balance on right click event
    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);
        }
        }
     
    }

    plugin.yml (open)

    Code:
    name: MagicDust
    author: SnappSeal
    version: 1.0
    main: me.alton.MagicDust.Main
    description: Magic Dust
    
    commands:
      getdust:
        description: get dust
        default: false
        permission: dust.get
    
      dbal:
        description: check balance
        default: false
        permission: dust.bal
         
    permissions:
      dust.get:
        description: for /getdust
        default: op
    
      dust.bal:
        description: for /dbal
        default: op
          
     
    Last edited: Jul 5, 2019

Share This Page