ExperienceBarWrapper.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.gmail.nossr50.util.experience;
  2. import com.gmail.nossr50.config.experience.ExperienceConfig;
  3. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  4. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  5. import com.gmail.nossr50.locale.LocaleLoader;
  6. import com.gmail.nossr50.util.StringUtils;
  7. import org.bukkit.Server;
  8. import org.bukkit.boss.*;
  9. import org.bukkit.entity.Player;
  10. import java.util.List;
  11. /**
  12. * A visual representation of a players skill level progress for a PrimarySkillType
  13. */
  14. public class ExperienceBarWrapper {
  15. private final PrimarySkillType primarySkillType; //Primary Skill
  16. private BossBar bossBar;
  17. private final Server server;
  18. protected final McMMOPlayer mcMMOPlayer;
  19. private int lastLevelUpdated;
  20. /*
  21. * This is stored to help optimize updating the title
  22. */
  23. protected String niceSkillName;
  24. protected String title;
  25. public ExperienceBarWrapper(PrimarySkillType primarySkillType, McMMOPlayer mcMMOPlayer)
  26. {
  27. this.mcMMOPlayer = mcMMOPlayer;
  28. this.server = mcMMOPlayer.getPlayer().getServer(); //Might not be good for bungee to do this
  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. {
  39. title = getTitleTemplate();
  40. createBossBar();
  41. }
  42. public void updateTitle() {
  43. title = getTitleTemplate();
  44. bossBar.setTitle(title);
  45. }
  46. private String getTitleTemplate() {
  47. return LocaleLoader.getString("XPBar."+niceSkillName, getLevel(), getCurrentXP(), getMaxXP(), getPowerLevel(), getPercentageOfLevel());
  48. }
  49. private int getLevel() {
  50. return mcMMOPlayer.getSkillLevel(primarySkillType);
  51. }
  52. private int getCurrentXP() { return mcMMOPlayer.getSkillXpLevel(primarySkillType); }
  53. private int getMaxXP() { return mcMMOPlayer.getXpToLevel(primarySkillType); }
  54. private int getPowerLevel() { return mcMMOPlayer.getPowerLevel(); }
  55. private int getPercentageOfLevel() { return (int) (mcMMOPlayer.getProgressInCurrentSkillLevel(primarySkillType) * 100); }
  56. public String getTitle() {
  57. return bossBar.getTitle();
  58. }
  59. public void setTitle(String s) {
  60. bossBar.setTitle(s);
  61. }
  62. public BarColor getColor() {
  63. return bossBar.getColor();
  64. }
  65. public void setColor(BarColor barColor) {
  66. bossBar.setColor(barColor);
  67. }
  68. public BarStyle getStyle() {
  69. return bossBar.getStyle();
  70. }
  71. public void setStyle(BarStyle barStyle) {
  72. bossBar.setStyle(barStyle);
  73. }
  74. public void setProgress(double v) {
  75. //Clamp Values
  76. if(v < 0)
  77. bossBar.setProgress(0.0D);
  78. else if(v > 1)
  79. bossBar.setProgress(1.0D);
  80. else
  81. bossBar.setProgress(v);
  82. //Every time progress updates we need to check for a title update
  83. if(getLevel() != lastLevelUpdated || ExperienceConfig.getInstance().getDoExperienceBarsAlwaysUpdateTitle())
  84. {
  85. updateTitle();
  86. lastLevelUpdated = getLevel();
  87. }
  88. }
  89. public double getProgress() {
  90. return bossBar.getProgress();
  91. }
  92. public List<Player> getPlayers() {
  93. return bossBar.getPlayers();
  94. }
  95. public boolean isVisible() {
  96. return bossBar.isVisible();
  97. }
  98. public void hideExperienceBar()
  99. {
  100. bossBar.setVisible(false);
  101. }
  102. public void showExperienceBar()
  103. {
  104. bossBar.setVisible(true);
  105. }
  106. /*public NamespacedKey getKey()
  107. {
  108. return bossBar
  109. }*/
  110. private void createBossBar()
  111. {
  112. bossBar = mcMMOPlayer.getPlayer().getServer().createBossBar(title, ExperienceConfig.getInstance().getExperienceBarColor(primarySkillType), ExperienceConfig.getInstance().getExperienceBarStyle(primarySkillType));
  113. bossBar.addPlayer(mcMMOPlayer.getPlayer());
  114. }
  115. }