/newcode/src/com/prupe/mcpatcher/cit/EnchantmentList.java

https://bitbucket.org/Freso/mcpatcher · Java · 161 lines · 141 code · 20 blank · 0 comment · 28 complexity · cddedffb99bd45c95600f2e84ed5c5d5 MD5 · raw file

  1. package com.prupe.mcpatcher.cit;
  2. import com.prupe.mcpatcher.MCLogger;
  3. import com.prupe.mcpatcher.MCPatcherUtils;
  4. import net.minecraft.src.ItemStack;
  5. import java.util.*;
  6. final class EnchantmentList {
  7. private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.CUSTOM_ITEM_TEXTURES, "CIT");
  8. private static final float PI = (float) Math.PI;
  9. private static LayerMethod applyMethod;
  10. private static int limit;
  11. private static float fade;
  12. private final List<Layer> layers = new ArrayList<Layer>();
  13. static void setProperties(Properties properties) {
  14. applyMethod = new Average();
  15. limit = 99;
  16. fade = 0.5f;
  17. if (properties != null) {
  18. String value = MCPatcherUtils.getStringProperty(properties, "method", "average").toLowerCase();
  19. if (value.equals("layered")) {
  20. applyMethod = new Layered();
  21. } else if (value.equals("cycle")) {
  22. applyMethod = new Cycle();
  23. } else if (!value.equals("average")) {
  24. logger.warning("%s: unknown enchantment layering method '%s'", CITUtils.CIT_PROPERTIES, value);
  25. }
  26. limit = Math.max(MCPatcherUtils.getIntProperty(properties, "cap", limit), 0);
  27. fade = Math.max(MCPatcherUtils.getFloatProperty(properties, "fade", fade), 0.0f);
  28. }
  29. }
  30. EnchantmentList(Enchantment[][] enchantments, ItemStack itemStack) {
  31. BitSet layersPresent = new BitSet();
  32. Map<Integer, Layer> tmpLayers = new HashMap<Integer, Layer>();
  33. int itemID = itemStack.itemID;
  34. int[] enchantmentLevels = CITUtils.getEnchantmentLevels(itemID, itemStack.stackTagCompound);
  35. boolean hasEffect = itemStack.hasEffect();
  36. if (itemID >= 0 && itemID < enchantments.length && enchantments[itemID] != null) {
  37. for (Enchantment enchantment : enchantments[itemID]) {
  38. if (enchantment.match(itemStack, enchantmentLevels, hasEffect)) {
  39. int level = Math.max(enchantment.lastEnchantmentLevel, 1);
  40. int layer = enchantment.layer;
  41. if (!layersPresent.get(layer)) {
  42. Layer newLayer = new Layer(enchantment, level);
  43. tmpLayers.put(layer, newLayer);
  44. layersPresent.set(layer);
  45. }
  46. }
  47. }
  48. }
  49. if (layersPresent.isEmpty()) {
  50. return;
  51. }
  52. while (layersPresent.cardinality() > limit) {
  53. int layer = layersPresent.nextSetBit(0);
  54. layersPresent.clear(layer);
  55. tmpLayers.remove(layer);
  56. }
  57. for (int i = layersPresent.nextSetBit(0); i >= 0; i = layersPresent.nextSetBit(i + 1)) {
  58. layers.add(tmpLayers.get(i));
  59. }
  60. applyMethod.computeIntensities(this);
  61. }
  62. boolean isEmpty() {
  63. return layers.isEmpty();
  64. }
  65. int size() {
  66. return layers.size();
  67. }
  68. Enchantment getEnchantment(int index) {
  69. return layers.get(index).enchantment;
  70. }
  71. float getIntensity(int index) {
  72. return layers.get(index).intensity;
  73. }
  74. private static final class Layer {
  75. final Enchantment enchantment;
  76. final int level;
  77. float intensity;
  78. Layer(Enchantment enchantment, int level) {
  79. this.enchantment = enchantment;
  80. this.level = level;
  81. }
  82. float getEffectiveDuration() {
  83. return enchantment.duration + 2.0f * fade;
  84. }
  85. }
  86. abstract private static class LayerMethod {
  87. abstract void computeIntensities(EnchantmentList enchantments);
  88. protected void scaleIntensities(EnchantmentList enchantments, int denominator) {
  89. if (denominator > 0) {
  90. for (Layer layer : enchantments.layers) {
  91. layer.intensity = (float) layer.level / (float) denominator;
  92. }
  93. } else {
  94. for (Layer layer : enchantments.layers) {
  95. layer.intensity = layer.level > 0 ? 1.0f : 0.0f;
  96. }
  97. }
  98. }
  99. }
  100. private static final class Average extends LayerMethod {
  101. @Override
  102. void computeIntensities(EnchantmentList enchantments) {
  103. int total = 0;
  104. for (Layer layer : enchantments.layers) {
  105. total += layer.level;
  106. }
  107. scaleIntensities(enchantments, total);
  108. }
  109. }
  110. private static final class Layered extends LayerMethod {
  111. @Override
  112. void computeIntensities(EnchantmentList enchantments) {
  113. int max = 0;
  114. for (Layer layer : enchantments.layers) {
  115. Math.max(max, layer.level);
  116. }
  117. scaleIntensities(enchantments, max);
  118. }
  119. }
  120. private static final class Cycle extends LayerMethod {
  121. @Override
  122. void computeIntensities(EnchantmentList enchantments) {
  123. float total = 0.0f;
  124. for (Layer layer : enchantments.layers) {
  125. total += layer.getEffectiveDuration();
  126. }
  127. float timestamp = (float) ((System.currentTimeMillis() / 1000.0) % total);
  128. for (Layer layer : enchantments.layers) {
  129. if (timestamp <= 0.0f) {
  130. break;
  131. }
  132. float duration = layer.getEffectiveDuration();
  133. if (timestamp < duration) {
  134. float denominator = (float) Math.sin(PI * fade / duration);
  135. layer.intensity = (float) (Math.sin(PI * timestamp / duration) / (denominator == 0.0f ? 1.0f : denominator));
  136. }
  137. timestamp -= duration;
  138. }
  139. }
  140. }
  141. }