Remove the concept of EnergyComponents and set it so that an EnergyValue is only of one EnergyType (simplifies a lot of things)

This commit is contained in:
Pahimar 2014-07-10 16:05:14 -04:00
parent 32bbca53cb
commit 0a38400559
6 changed files with 52 additions and 322 deletions

View file

@ -35,12 +35,12 @@ public class ItemTooltipEventHandler
EnergyValue energyValue = EnergyValueRegistry.getInstance().getEnergyValue(stack);
if (stack.getStackSize() > 1)
{
event.toolTip.add("Exchange Energy (Item): " + String.format("%s", energyValueDecimalFormat.format(energyValue.getValue())));
event.toolTip.add("Exchange Energy (Stack): " + String.format("%s", energyValueDecimalFormat.format(stack.getStackSize() * energyValue.getValue())));
event.toolTip.add("Exchange Energy (Item): " + String.format("%s", energyValueDecimalFormat.format(energyValue.getEnergyValue())));
event.toolTip.add("Exchange Energy (Stack): " + String.format("%s", energyValueDecimalFormat.format(stack.getStackSize() * energyValue.getEnergyValue())));
}
else
{
event.toolTip.add("Exchange Energy: " + String.format("%s", energyValueDecimalFormat.format(stack.getStackSize() * energyValue.getValue())));
event.toolTip.add("Exchange Energy: " + String.format("%s", energyValueDecimalFormat.format(stack.getStackSize() * energyValue.getEnergyValue())));
}
}
else

View file

@ -1,65 +0,0 @@
package com.pahimar.ee3.exchange;
public class EnergyComponent implements Comparable<EnergyComponent>
{
public final EnergyType type;
public final int weight;
public EnergyComponent(EnergyType type)
{
this(type, 1);
}
public EnergyComponent(EnergyType type, int weight)
{
this.type = type;
if (weight > 0)
{
this.weight = weight;
}
else
{
this.weight = -1;
}
}
@Override
public boolean equals(Object object)
{
if (!(object instanceof EnergyComponent))
{
return false;
}
EnergyComponent energyComponent = (EnergyComponent) object;
return ((this.type == energyComponent.type) && (this.weight == energyComponent.weight));
}
@Override
public String toString()
{
return String.format("<Energy Type: %s, Weight: %s>", type, weight);
}
@Override
public int compareTo(EnergyComponent energyComponent)
{
if (energyComponent != null)
{
if (this.type == energyComponent.type)
{
return (this.weight - energyComponent.weight);
}
else
{
return this.type.compareTo(energyComponent.type);
}
}
else
{
return 1;
}
}
}

View file

@ -1,10 +0,0 @@
package com.pahimar.ee3.exchange;
public enum EnergyType
{
OMNI, CORPOREAL, KINETIC, TEMPORAL, ESSENTIA, AMORPHOUS, VOID;
public static final EnergyType[] TYPES = EnergyType.values();
public static final EnergyType DEFAULT = EnergyType.CORPOREAL;
}

View file

@ -1,166 +1,29 @@
package com.pahimar.ee3.exchange;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class EnergyValue implements Comparable<EnergyValue>
{
private static final int PRECISION = 4;
private final float energyValue;
private final EnergyType energyType;
public final float[] components;
public EnergyValue()
public EnergyValue(int energyValue)
{
this(new float[EnergyType.TYPES.length]);
this((float) energyValue);
}
public EnergyValue(float[] components)
public EnergyValue(float energyValue)
{
if (components.length == EnergyType.TYPES.length)
{
this.components = new float[components.length];
for (int i = 0; i < this.components.length; i++)
{
BigDecimal bigComponent = BigDecimal.valueOf(components[i]).setScale(PRECISION, BigDecimal.ROUND_HALF_DOWN);
this.components[i] = bigComponent.floatValue();
}
}
else
{
this.components = null;
}
this(energyValue, EnergyType.DEFAULT);
}
public EnergyValue(int value)
public EnergyValue(float energyValue, EnergyType energyType)
{
this((float) value);
this.energyValue = energyValue;
this.energyType = energyType;
}
public EnergyValue(float value)
public EnergyValue(int energyValue, EnergyType energyType)
{
this(value, EnergyType.DEFAULT);
}
public EnergyValue(float value, EnergyType energyType)
{
this(value, Arrays.asList(new EnergyComponent(energyType)));
}
public EnergyValue(float value, List<EnergyComponent> componentList)
{
this.components = new float[EnergyType.TYPES.length];
List<EnergyComponent> collatedComponents = collateComponents(componentList);
int totalComponents = 0;
for (EnergyComponent component : collatedComponents)
{
if (component.weight > 0)
{
totalComponents += component.weight;
}
}
if (totalComponents > 0)
{
for (EnergyComponent component : collatedComponents)
{
if (component.weight > 0)
{
this.components[component.type.ordinal()] = value * (component.weight * 1F / totalComponents);
}
}
}
else
{
this.components[EnergyType.DEFAULT.ordinal()] = value;
}
for (int i = 0; i < this.components.length; i++)
{
BigDecimal bigComponent = BigDecimal.valueOf(this.components[i]).setScale(PRECISION, BigDecimal.ROUND_HALF_DOWN);
this.components[i] = bigComponent.floatValue();
}
}
private static List<EnergyComponent> collateComponents(List<EnergyComponent> uncollatedComponents)
{
Integer[] componentCount = new Integer[EnergyType.TYPES.length];
for (EnergyComponent energyComponent : uncollatedComponents)
{
if (componentCount[energyComponent.type.ordinal()] == null)
{
componentCount[energyComponent.type.ordinal()] = 0;
}
if (energyComponent.weight >= 0)
{
componentCount[energyComponent.type.ordinal()] = componentCount[energyComponent.type.ordinal()] + energyComponent.weight;
}
}
List<EnergyComponent> collatedComponents = new ArrayList<EnergyComponent>();
for (int i = 0; i < EnergyType.TYPES.length; i++)
{
if (componentCount[i] != null)
{
collatedComponents.add(new EnergyComponent(EnergyType.TYPES[i], componentCount[i]));
}
}
Collections.sort(collatedComponents);
return collatedComponents;
}
public EnergyValue(float value, EnergyComponent component)
{
this(value, component.type);
}
public EnergyValue(int value, EnergyType energyType)
{
this((float) value, energyType);
}
public EnergyValue(int value, List<EnergyComponent> componentList)
{
this((float) value, componentList);
}
@Override
public int hashCode()
{
int hashCode = 1;
hashCode = 37 * hashCode + Float.floatToIntBits(getValue());
for (float subValue : components)
{
hashCode = 37 * hashCode + Float.floatToIntBits(subValue);
}
return hashCode;
}
public float getValue()
{
float sumSubValues = 0;
for (float subValue : this.components)
{
if (subValue > 0)
{
sumSubValues += subValue;
}
}
return sumSubValues;
this((float) energyValue, energyType);
}
@Override
@ -172,27 +35,22 @@ public class EnergyValue implements Comparable<EnergyValue>
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (EnergyType energyType : EnergyType.TYPES)
{
if (components[energyType.ordinal()] > 0)
{
stringBuilder.append(String.format(" %s:%s ", energyType, components[energyType.ordinal()]));
}
}
stringBuilder.append("]");
return stringBuilder.toString();
return String.format(" %s@%s ", energyValue, energyType);
}
@Override
public int compareTo(EnergyValue exchangeEnergyValue)
public int compareTo(EnergyValue energyValue)
{
if (exchangeEnergyValue != null)
if (energyValue != null)
{
return compareComponents(this.components, exchangeEnergyValue.components);
if (this.energyType == energyValue.getEnergyType())
{
return Float.compare(this.energyValue, energyValue.getEnergyValue());
}
else
{
return (this.energyType.ordinal() - energyValue.getEnergyType().ordinal());
}
}
else
{
@ -200,24 +58,22 @@ public class EnergyValue implements Comparable<EnergyValue>
}
}
private static int compareComponents(float[] first, float[] second)
public EnergyType getEnergyType()
{
if (first.length == EnergyType.TYPES.length && second.length == EnergyType.TYPES.length)
{
return this.energyType;
}
for (EnergyType energyType : EnergyType.TYPES)
{
if (Float.compare(first[energyType.ordinal()], second[energyType.ordinal()]) != 0)
{
return Float.compare(first[energyType.ordinal()], second[energyType.ordinal()]);
}
}
public float getEnergyValue()
{
return this.energyValue;
}
return 0;
}
else
{
throw new ArrayIndexOutOfBoundsException();
}
public static enum EnergyType
{
OMNI, CORPOREAL, KINETIC, TEMPORAL, ESSENTIA, AMORPHOUS, VOID;
public static final EnergyType[] TYPES = EnergyType.values();
public static final EnergyType DEFAULT = EnergyType.CORPOREAL;
}
}

View file

@ -48,7 +48,7 @@ public class EnergyValueRegistry
if (keyStack != null && keyStack.getWrappedStack() != null && keyStack.getStackSize() > 0)
{
if (defaultValuesMap.get(keyStack) != null && Float.compare(defaultValuesMap.get(keyStack).getValue(), 0f) > 0)
if (defaultValuesMap.get(keyStack) != null && Float.compare(defaultValuesMap.get(keyStack).getEnergyValue(), 0f) > 0)
{
factoredExchangeEnergyValue = EnergyValueHelper.factorEnergyValue(defaultValuesMap.get(keyStack), keyStack.getStackSize());
factoredKeyStack = new WrappedStack(keyStack, 1);
@ -136,7 +136,7 @@ public class EnergyValueRegistry
if (keyStack != null && keyStack.getWrappedStack() != null && keyStack.getStackSize() > 0)
{
if (computedStackValues.get(keyStack) != null && Float.compare(computedStackValues.get(keyStack).getValue(), 0f) > 0)
if (computedStackValues.get(keyStack) != null && Float.compare(computedStackValues.get(keyStack).getEnergyValue(), 0f) > 0)
{
factoredExchangeEnergyValue = EnergyValueHelper.factorEnergyValue(computedStackValues.get(keyStack), keyStack.getStackSize());
factoredKeyStack = new WrappedStack(keyStack, 1);
@ -247,7 +247,7 @@ public class EnergyValueRegistry
}
}
if ((lowestValue != null) && (lowestValue.getValue() > 0f))
if ((lowestValue != null) && (lowestValue.getEnergyValue() > 0f))
{
computedStackMap.put(new WrappedStack(recipeOutput.getWrappedStack()), lowestValue);
}
@ -421,7 +421,7 @@ public class EnergyValueRegistry
}
else if (wrappedItemStack.getItem().isDamageable() && wrappedItemStack.isItemDamaged())
{
EnergyValue stackValue = new EnergyValue(energyValueRegistry.stackMappings.get(valuedStack).getValue() * (1 - (wrappedItemStack.getItemDamage() * 1.0F / wrappedItemStack.getMaxDamage())));
EnergyValue stackValue = new EnergyValue(energyValueRegistry.stackMappings.get(valuedStack).getEnergyValue() * (1 - (wrappedItemStack.getItemDamage() * 1.0F / wrappedItemStack.getMaxDamage())));
if (stackValue.compareTo(lowestValue) < 0)
{

View file

@ -1,58 +1,18 @@
package com.pahimar.ee3.util;
import com.pahimar.ee3.exchange.EnergyType;
import com.pahimar.ee3.exchange.EnergyValue;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.WrappedStack;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
import java.util.ArrayList;
import java.util.List;
public class EnergyValueHelper
{
@SuppressWarnings("unused")
public static List<WrappedStack> filterStacksByEnergyValue(float start, float end, EnergyValue filterValue)
{
return filterStacksByEnergyValue(EnergyValueRegistry.getInstance().getStacksInRange(start, end), filterValue);
}
public static List<WrappedStack> filterStacksByEnergyValue(List<WrappedStack> unfilteredStacks, EnergyValue filterValue)
{
List<WrappedStack> filteredStacks = new ArrayList<WrappedStack>();
for (WrappedStack stack : unfilteredStacks)
{
if (EnergyValueRegistry.getInstance().hasEnergyValue(stack))
{
EnergyValue value = EnergyValueRegistry.getInstance().getEnergyValue(stack);
boolean satisfiesFilter = true;
float[] valueSubValues = value.components;
float[] filterValueSubValues = filterValue.components;
for (int i = 0; i < valueSubValues.length; i++)
{
if (Float.compare(valueSubValues[i], filterValueSubValues[i]) < 0)
{
satisfiesFilter = false;
}
}
if (satisfiesFilter)
{
filteredStacks.add(stack);
}
}
}
return filteredStacks;
}
public static EnergyValue computeEnergyValueFromList(List<WrappedStack> wrappedStacks)
{
float[] computedSubValues = new float[EnergyType.TYPES.length];
float computedValue = 0f;
for (WrappedStack wrappedStack : wrappedStacks)
{
@ -97,10 +57,7 @@ public class EnergyValueHelper
stackSize = wrappedStack.getStackSize();
}
for (EnergyType energyType : EnergyType.TYPES)
{
computedSubValues[energyType.ordinal()] += wrappedStackValue.components[energyType.ordinal()] * stackSize;
}
computedValue += wrappedStackValue.getEnergyValue() * stackSize;
}
else
{
@ -108,31 +65,23 @@ public class EnergyValueHelper
}
}
return new EnergyValue(computedSubValues);
return new EnergyValue(computedValue);
}
public static EnergyValue factorEnergyValue(EnergyValue ExchangeEnergyValue, int factor)
public static EnergyValue factorEnergyValue(EnergyValue energyValue, int factor)
{
return factorEnergyValue(ExchangeEnergyValue, (float) factor);
return factorEnergyValue(energyValue, (float) factor);
}
public static EnergyValue factorEnergyValue(EnergyValue ExchangeEnergyValue, float factor)
public static EnergyValue factorEnergyValue(EnergyValue energyValue, float factor)
{
if ((Float.compare(factor, 0f) != 0) && (ExchangeEnergyValue != null))
if ((Float.compare(factor, 0f) != 0) && (energyValue != null))
{
float[] factorSubValues = ExchangeEnergyValue.components;
for (int i = 0; i < factorSubValues.length; i++)
{
factorSubValues[i] = factorSubValues[i] * 1f / factor;
}
return new EnergyValue(factorSubValues);
return new EnergyValue(energyValue.getEnergyValue() * 1f / factor, energyValue.getEnergyType());
}
else
{
return ExchangeEnergyValue;
return energyValue;
}
}
}