ExperienceBarWrapper.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.gmail.nossr50.util.experience;
  2. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  3. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  4. import com.gmail.nossr50.mcMMO;
  5. import com.gmail.nossr50.util.StringUtils;
  6. import com.gmail.nossr50.util.player.PlayerLevelTools;
  7. import org.bukkit.boss.BarColor;
  8. import org.bukkit.boss.BarStyle;
  9. import org.bukkit.boss.BossBar;
  10. import org.bukkit.entity.Player;
  11. import java.util.List;
  12. /**
  13. * A visual representation of a players skill level progress for a PrimarySkillType
  14. */
  15. public class ExperienceBarWrapper {
  16. protected final McMMOPlayer mcMMOPlayer;
  17. private final PrimarySkillType primarySkillType; //Primary Skill
  18. private BossBar bossBar;
  19. private int lastLevelUpdated;
  20. private final mcMMO pluginRef;
  21. /*
  22. * This is stored to help optimize updating the title
  23. */
  24. protected String niceSkillName;
  25. protected String title;
  26. public ExperienceBarWrapper(mcMMO pluginRef, PrimarySkillType primarySkillType, McMMOPlayer mcMMOPlayer) {
  27. this.pluginRef = pluginRef;
  28. this.mcMMOPlayer = mcMMOPlayer;
  29. this.primarySkillType = primarySkillType;
  30. title = "";
  31. lastLevelUpdated = 0;
  32. //These vars are stored to help reduce operations involving strings
  33. niceSkillName = StringUtils.getCapitalized(primarySkillType.toString());
  34. //Create the bar
  35. initBar();
  36. }
  37. private void initBar() {
  38. title = getTitleTemplate();
  39. createBossBar();
  40. }
  41. public void updateTitle() {
  42. title = getTitleTemplate();
  43. bossBar.setTitle(title);
  44. }
  45. private String getTitleTemplate() {
  46. //If they are using extra details
  47. if(pluginRef.getConfigManager().getConfigLeveling().getEarlyGameBoost().isEnableEarlyGameBoost() && pluginRef.getPlayerLevelTools().qualifiesForEarlyGameBoost(mcMMOPlayer, primarySkillType)) {
  48. return pluginRef.getLocaleManager().getString("XPBar.Template.EarlyGameBoost");
  49. } else if(pluginRef.getConfigManager().getConfigLeveling().getConfigExperienceBars().isMoreDetailedXPBars())
  50. return pluginRef.getLocaleManager().getString("XPBar.Complex.Template", pluginRef.getLocaleManager().getString("XPBar."+niceSkillName, getLevel()), getCurrentXP(), getMaxXP(), getPowerLevel(), getPercentageOfLevel());
  51. return pluginRef.getLocaleManager().getString("XPBar." + niceSkillName, getLevel(), getCurrentXP(), getMaxXP(), getPowerLevel(), getPercentageOfLevel());
  52. }
  53. private int getLevel() {
  54. return mcMMOPlayer.getSkillLevel(primarySkillType);
  55. }
  56. private int getCurrentXP() {
  57. return mcMMOPlayer.getSkillXpLevel(primarySkillType);
  58. }
  59. private int getMaxXP() {
  60. return mcMMOPlayer.getXpToLevel(primarySkillType);
  61. }
  62. private int getPowerLevel() {
  63. return mcMMOPlayer.getPowerLevel();
  64. }
  65. private int getPercentageOfLevel() {
  66. return (int) (mcMMOPlayer.getProgressInCurrentSkillLevel(primarySkillType) * 100);
  67. }
  68. public String getTitle() {
  69. return bossBar.getTitle();
  70. }
  71. public void setTitle(String s) {
  72. bossBar.setTitle(s);
  73. }
  74. public BarColor getColor() {
  75. return bossBar.getColor();
  76. }
  77. public void setColor(BarColor barColor) {
  78. bossBar.setColor(barColor);
  79. }
  80. public BarStyle getStyle() {
  81. return bossBar.getStyle();
  82. }
  83. public void setStyle(BarStyle barStyle) {
  84. bossBar.setStyle(barStyle);
  85. }
  86. public double getProgress() {
  87. return bossBar.getProgress();
  88. }
  89. public void setProgress(double v) {
  90. //Clamp Values
  91. if (v < 0)
  92. bossBar.setProgress(0.0D);
  93. else if (v > 1)
  94. bossBar.setProgress(1.0D);
  95. else
  96. bossBar.setProgress(v);
  97. //Check player level
  98. if(pluginRef.getConfigManager().getConfigLeveling().getEarlyGameBoost().isEnableEarlyGameBoost() && pluginRef.getPlayerLevelTools().qualifiesForEarlyGameBoost(mcMMOPlayer, primarySkillType)) {
  99. setColor(BarColor.YELLOW);
  100. } else {
  101. setColor(pluginRef.getConfigManager().getConfigLeveling().getConfigExperienceBars().getXPBarColor(primarySkillType));
  102. }
  103. //Every time progress updates we need to check for a title update
  104. if (getLevel() != lastLevelUpdated || pluginRef.getConfigManager().getConfigLeveling().isMoreDetailedXPBars()) {
  105. updateTitle();
  106. lastLevelUpdated = getLevel();
  107. }
  108. }
  109. public List<Player> getPlayers() {
  110. return bossBar.getPlayers();
  111. }
  112. public boolean isVisible() {
  113. return bossBar.isVisible();
  114. }
  115. public void hideExperienceBar() {
  116. bossBar.setVisible(false);
  117. }
  118. public void showExperienceBar() {
  119. bossBar.setVisible(true);
  120. }
  121. /*public NamespacedKey getKey()
  122. {
  123. return bossBar
  124. }*/
  125. private void createBossBar() {
  126. bossBar = mcMMOPlayer.getPlayer().getServer().createBossBar(title,
  127. pluginRef.getConfigManager().getConfigLeveling().getXPBarColor(primarySkillType),
  128. pluginRef.getConfigManager().getConfigLeveling().getXPBarStyle(primarySkillType));
  129. bossBar.addPlayer(mcMMOPlayer.getPlayer());
  130. }
  131. }