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

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