summaryrefslogtreecommitdiff
path: root/src/main/java/lv/enes/mc/eris_alchemy/EMC.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/EMC.java')
-rw-r--r--src/main/java/lv/enes/mc/eris_alchemy/EMC.java36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/EMC.java b/src/main/java/lv/enes/mc/eris_alchemy/EMC.java
index e34d28f..3ff33ca 100644
--- a/src/main/java/lv/enes/mc/eris_alchemy/EMC.java
+++ b/src/main/java/lv/enes/mc/eris_alchemy/EMC.java
@@ -16,6 +16,7 @@ import net.minecraft.world.level.Level;
16import net.minecraft.world.level.block.Block; 16import net.minecraft.world.level.block.Block;
17import vazkii.patchouli.common.item.PatchouliItems; 17import vazkii.patchouli.common.item.PatchouliItems;
18 18
19import java.text.DecimalFormat;
19import java.util.*; 20import java.util.*;
20import java.util.stream.Stream; 21import java.util.stream.Stream;
21 22
@@ -62,6 +63,11 @@ public class EMC {
62 private static final EMC defaultInstance = new EMC(null); 63 private static final EMC defaultInstance = new EMC(null);
63 private static final Map<Level, EMC> instances = Collections.synchronizedMap(new WeakHashMap<>()); 64 private static final Map<Level, EMC> instances = Collections.synchronizedMap(new WeakHashMap<>());
64 65
66 private static final DecimalFormat formatter = new DecimalFormat("0");
67 static {
68 formatter.setMaximumFractionDigits(1);
69 }
70
65 public static EMC getInstance(Level world) { 71 public static EMC getInstance(Level world) {
66 if (world == null) { 72 if (world == null) {
67 return defaultInstance; 73 return defaultInstance;
@@ -76,6 +82,10 @@ public class EMC {
76 return instance; 82 return instance;
77 } 83 }
78 84
85 public static String formatEmc(double value) {
86 return formatter.format(value);
87 }
88
79 private final Map<Item, Double> data; 89 private final Map<Item, Double> data;
80 90
81 private EMC(@Nullable Level world) { 91 private EMC(@Nullable Level world) {
@@ -91,10 +101,9 @@ public class EMC {
91 .forEach(holder -> data.putIfAbsent(holder.value().asItem(), emcValue)) 101 .forEach(holder -> data.putIfAbsent(holder.value().asItem(), emcValue))
92 ); 102 );
93 103
94 ErisAlchemy.LOGGER.info("Getting recipes..."); 104 ErisAlchemy.LOGGER.info("Calculating EMC values...");
95 var recipes = getRecipes(world); 105 var recipes = getRecipes(world);
96 var configured = new HashSet<>(data.keySet()); 106 var configured = new HashSet<>(data.keySet());
97 ErisAlchemy.LOGGER.info("Calculating EMC values...");
98 BuiltInRegistries.ITEM.forEach(item -> { 107 BuiltInRegistries.ITEM.forEach(item -> {
99 configEmc(recipes, configured, item); 108 configEmc(recipes, configured, item);
100 if (world != null && !data.containsKey(item)) { 109 if (world != null && !data.containsKey(item)) {
@@ -103,11 +112,30 @@ public class EMC {
103 }); 112 });
104 } 113 }
105 114
106 public OptionalDouble get(Item item) { 115 public OptionalDouble get(ItemStack stack) {
116 if (stack.isEmpty()) {
117 return OptionalDouble.empty();
118 }
119
120 var item = stack.getItem();
107 var value = data.get(item); 121 var value = data.get(item);
108 if (value == null || value <= 0) { 122 if (value == null || value <= 0) {
109 return OptionalDouble.empty(); 123 return OptionalDouble.empty();
110 } 124 }
125
126 EmcStorage storage = null;
127 if (item instanceof EmcStorage emcStorage) {
128 storage = emcStorage;
129 } else if (item instanceof BlockItem blockItem) {
130 if (blockItem.getBlock() instanceof EmcStorage emcStorage) {
131 storage = emcStorage;
132 }
133 }
134
135 if (storage != null) {
136 value += storage.getStoredEmc(stack);
137 }
138
111 return OptionalDouble.of(value); 139 return OptionalDouble.of(value);
112 } 140 }
113 141
@@ -134,7 +162,7 @@ public class EMC {
134 } 162 }
135 163
136 private OptionalDouble configEmc(List<SimplifiedRecipe> recipes, Set<Item> configured, Item item) { 164 private OptionalDouble configEmc(List<SimplifiedRecipe> recipes, Set<Item> configured, Item item) {
137 var res = get(item); 165 var res = get(item.getDefaultInstance());
138 if (res.isPresent() || configured.contains(item)) { 166 if (res.isPresent() || configured.contains(item)) {
139 return res; 167 return res;
140 } 168 }