2013-09-07 04:44:31 +02:00
|
|
|
package com.pahimar.ee3.emc;
|
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2013-09-07 04:44:31 +02:00
|
|
|
import java.util.List;
|
2013-10-06 04:46:29 +02:00
|
|
|
import java.util.Map;
|
2013-10-16 21:55:14 +02:00
|
|
|
import java.util.SortedMap;
|
2013-10-06 04:46:29 +02:00
|
|
|
import java.util.TreeMap;
|
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
import com.google.common.collect.ImmutableSortedMap;
|
2013-09-07 04:44:31 +02:00
|
|
|
import com.pahimar.ee3.item.CustomWrappedStack;
|
|
|
|
|
|
|
|
public class EmcRegistry {
|
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
private static EmcRegistry emcRegistry = null;
|
2013-10-06 04:46:29 +02:00
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
private ImmutableSortedMap<CustomWrappedStack, EmcValue> stackMappings;
|
|
|
|
private ImmutableSortedMap<EmcValue, List<CustomWrappedStack>> valueMappings;
|
2013-09-07 04:44:31 +02:00
|
|
|
|
2013-10-16 21:55:14 +02:00
|
|
|
private static void lazyInit() {
|
2013-09-07 04:44:31 +02:00
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
if (emcRegistry == null) {
|
|
|
|
emcRegistry = new EmcRegistry();
|
|
|
|
emcRegistry.init();
|
|
|
|
}
|
|
|
|
}
|
2013-09-07 04:44:31 +02:00
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
private void init() {
|
2013-10-17 04:27:07 +02:00
|
|
|
|
|
|
|
ImmutableSortedMap.Builder<CustomWrappedStack, EmcValue> stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
|
|
|
|
ImmutableSortedMap.Builder<EmcValue, List<CustomWrappedStack>> valueMappingsBuilder = ImmutableSortedMap.naturalOrder();
|
|
|
|
|
|
|
|
Map<CustomWrappedStack, EmcValue> defaultValues = EmcDefaultValues.getDefaultValueMap();
|
|
|
|
|
|
|
|
// Handle the stack mappings
|
|
|
|
stackMappingsBuilder.putAll(defaultValues);
|
|
|
|
stackMappings = stackMappingsBuilder.build();
|
|
|
|
|
|
|
|
// Handle the value mappings
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> tempValueMappings = new TreeMap<EmcValue, List<CustomWrappedStack>>();
|
|
|
|
|
2013-10-17 16:11:16 +02:00
|
|
|
for (CustomWrappedStack stack : defaultValues.keySet()) {
|
2013-10-17 04:27:07 +02:00
|
|
|
EmcValue value = defaultValues.get(stack);
|
|
|
|
|
|
|
|
if (tempValueMappings.containsKey(value)) {
|
|
|
|
if (!(tempValueMappings.get(value).contains(stack))) {
|
|
|
|
tempValueMappings.get(value).add(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tempValueMappings.put(value, Arrays.asList(stack));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
valueMappingsBuilder.putAll(tempValueMappings);
|
|
|
|
valueMappings = valueMappingsBuilder.build();
|
2013-09-10 03:48:34 +02:00
|
|
|
}
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-18 02:40:45 +02:00
|
|
|
public static boolean hasEmcValue(Object object) {
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-16 21:55:14 +02:00
|
|
|
lazyInit();
|
2013-10-18 02:40:45 +02:00
|
|
|
|
|
|
|
if (CustomWrappedStack.canBeWrapped(object)) {
|
|
|
|
CustomWrappedStack stack = new CustomWrappedStack(object);
|
|
|
|
return emcRegistry.stackMappings.containsKey(new CustomWrappedStack(stack.getWrappedStack()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-07 04:47:12 +02:00
|
|
|
}
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-18 02:40:45 +02:00
|
|
|
public static EmcValue getEmcValue(Object object) {
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-16 21:55:14 +02:00
|
|
|
lazyInit();
|
2013-10-18 02:40:45 +02:00
|
|
|
|
|
|
|
if (CustomWrappedStack.canBeWrapped(object)) {
|
|
|
|
CustomWrappedStack stack = new CustomWrappedStack(object);
|
|
|
|
return emcRegistry.stackMappings.get(new CustomWrappedStack(stack.getWrappedStack()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<CustomWrappedStack> getStacksInRange(int start, int finish) {
|
|
|
|
|
|
|
|
return getStacksInRange(new EmcValue(start), new EmcValue(finish));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<CustomWrappedStack> getStacksInRange(float start, float finish) {
|
|
|
|
|
|
|
|
return getStacksInRange(new EmcValue(start), new EmcValue(finish));
|
2013-10-06 19:53:08 +02:00
|
|
|
}
|
2013-10-17 04:27:07 +02:00
|
|
|
|
|
|
|
public static List<CustomWrappedStack> getStacksInRange(EmcValue start, EmcValue finish) {
|
|
|
|
|
|
|
|
lazyInit();
|
|
|
|
|
|
|
|
List<CustomWrappedStack> stacksInRange = new ArrayList<CustomWrappedStack>();
|
|
|
|
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> tailMap = emcRegistry.valueMappings.tailMap(start);
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> headMap = emcRegistry.valueMappings.headMap(finish);
|
|
|
|
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> smallerMap;
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> biggerMap;
|
|
|
|
|
|
|
|
if (!tailMap.isEmpty() && !headMap.isEmpty()) {
|
|
|
|
|
|
|
|
if (tailMap.size() <= headMap.size()) {
|
|
|
|
smallerMap = tailMap;
|
|
|
|
biggerMap = headMap;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
smallerMap = headMap;
|
|
|
|
biggerMap = tailMap;
|
|
|
|
}
|
|
|
|
|
2013-10-17 16:11:16 +02:00
|
|
|
for (EmcValue value : smallerMap.keySet()) {
|
2013-10-17 04:27:07 +02:00
|
|
|
if (biggerMap.containsKey(value)) {
|
|
|
|
stacksInRange.addAll(emcRegistry.valueMappings.get(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stacksInRange;
|
|
|
|
}
|
2013-09-07 04:44:31 +02:00
|
|
|
}
|