Need help with mana regeneration

Discussion in 'Plugin Development' started by justin_393, Sep 13, 2014.

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

    justin_393

    Ok, so I'm creating a plugin that uses mana to cast skills, but I'm having issues with mana regeneration. Here's my class for the timed event
    Code:java
    1. package jjbat_000.legendaryrealms;
    2.  
    3. import java.util.HashMap;
    4. import java.util.UUID;
    5.  
    6. import jjbat_000.legendaryrealms.spells.FireballSpell;
    7. import jjbat_000.legendaryrealms.spells.Heal;
    8. import jjbat_000.legendaryrealms.spells.LightningBolt;
    9. import jjbat_000.legendaryrealms.spells.Teleport;
    10.  
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.scheduler.BukkitRunnable;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.Bukkit;
    19.  
    20. public class Core extends JavaPlugin implements Listener {
    21. public HashMap<UUID, LRPlayer> lrPlayer = new HashMap<>();
    22.  
    23. public LRPlayer getLRPlayer(UUID uuid) {
    24. return lrPlayer.get(uuid);
    25. }
    26.  
    27. public void onEnable() {
    28. saveDefaultConfig();
    29. Bukkit.getServer().getPluginManager()
    30. .registerEvents(new PlayerListener(this), this);
    31. Bukkit.getServer().getPluginManager()
    32. .registerEvents(new FireballSpell(this), this);
    33. Bukkit.getServer().getPluginManager()
    34. .registerEvents(new LightningBolt(this), this);
    35. Bukkit.getServer().getPluginManager()
    36. .registerEvents(new Heal(this), this);
    37. Bukkit.getServer().getPluginManager()
    38. .registerEvents(new Teleport(this), this);
    39. new BukkitRunnable() {
    40. public void run() {
    41. for (Player player : Bukkit.getOnlinePlayers()) {
    42. LRPlayer lrPlayer = getLRPlayer(player.getUniqueId());
    43. if (lrPlayer.getMana() != getConfig().getInt("Max-Mana")) {
    44. lrPlayer.addMana(getConfig().getInt("Mana-To-Add"));
    45. }
    46.  
    47. }
    48. }
    49. }.runTaskTimer(this, 20, getConfig().getInt("Mana-Delay"));
    50. }
    51.  
    52. public void onDisable() {
    53. saveConfig();
    54.  
    55.  
    56. }
    57.  
    58. @Override
    59. public boolean onCommand(CommandSender sender, Command cmd, String label,
    60. String[] args) {
    61. if (cmd.getName().equalsIgnoreCase("lreload")) {
    62. if (sender.hasPermission("legendaryrealms.reload")) {
    63. saveConfig();
    64. reloadConfig();
    65. sender.sendMessage(ChatColor.GREEN
    66. + "Successfully reloaded configuration file");
    67. } else {
    68. sender.sendMessage(ChatColor.RED
    69. + "You do not have permission to use this command!");
    70. }
    71.  
    72. return true;
    73. }
    74.  
    75. return false;
    76.  
    77. }
    78. }


    and here's the class with the mana methods.
    Code:java
    1. package jjbat_000.legendaryrealms;
    2.  
    3. import org.bukkit.event.Listener;
    4. import org.bukkit.entity.Player;
    5.  
    6. import jjbat_000.legendaryrealms.Core;
    7. import jjbat_000.legendaryrealms.LRPlayer;
    8.  
    9. public class LRPlayer implements Listener {
    10.  
    11. private final Core core;
    12. private Player player;
    13. private int mana;
    14. private int MaxMana;
    15.  
    16. public LRPlayer(Core core, Player player) {
    17. this.core = core;
    18. this.player = player;
    19. }
    20.  
    21. public void setFoodLevel(int amount) {
    22. player.setFoodLevel(core.getConfig().getInt("Max-Mana"));
    23. }
    24.  
    25. public Player getPlayer() {
    26. return this.player;
    27. }
    28.  
    29. public int getMana() {
    30. return mana;
    31. }
    32.  
    33. public int getMaxMana() {
    34. MaxMana = core.getConfig().getInt("Max-Mana");
    35. return MaxMana;
    36. }
    37.  
    38. public int setMana(int mana) {
    39. if (getMana() != MaxMana) {
    40. this.mana = mana;
    41. return mana;
    42. }
    43. return -1;
    44.  
    45. }
    46.  
    47. public int addMana(int amount) {
    48. if (getMana() != core.getConfig().getInt("Max-Mana")) {
    49. setMana(this.mana + amount);
    50. player.setFoodLevel(getMana());
    51. return mana;
    52. }
    53. return -1;
    54.  
    55. }
    56.  
    57. public int removeMana(int amount) {
    58. if (getMana() <= 0) {
    59. setMana(this.mana - amount);
    60. player.setFoodLevel(getMana());
    61. return mana;
    62. }
    63.  
    64. return -1;
    65. }
    66.  
    67. }
     
  2. Offline

    CubieX

    In your addMana() method you should check if current mana + amount <= maxMana
    To make sure you do not exceed the max mana level.

    In your removeMana() method, you should check if current mana - amount >= 0 to make sure you do not go negative.

    If you have further problems, you should explain them to us in detail.
     
Thread Status:
Not open for further replies.

Share This Page