mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
Some performance-oriented refactoring of TileComponentConfig
Add new type "SideConfig" which is observable, this could come in handy later. Add hasSideForData method in componentConfig, which is much more efficient than calling getSidesForData().contains. Refactor duplicate calls to getConfig into single instances. getConfig is a nontrivial method.
This commit is contained in:
1 parent
dc6312e1d3
commit
bc3274825a
9 files changed
+163
-70
No files matched your search
@@ -60,7 +60,7 @@ public class PacketConfigurationUpdate implements IMessageHandler<ConfigurationU
|
||||
}
|
||||
else if(message.clickType == 2)
|
||||
{
|
||||
((ISideConfiguration)tile).getConfig().getConfig(message.transmission)[message.configIndex.ordinal()] = 0;
|
||||
((ISideConfiguration)tile).getConfig().getConfig(message.transmission).set(message.configIndex, (byte) 0);
|
||||
}
|
||||
|
||||
tile.markDirty();
|
||||
|
||||
@@ -244,7 +244,7 @@ public class TileEntityGasTank extends TileEntityContainerBlock implements IGasH
|
||||
@Override
|
||||
public boolean canReceiveGas(EnumFacing side, Gas type)
|
||||
{
|
||||
if(configComponent.getSidesForData(TransmissionType.GAS, facing, 1).contains(side))
|
||||
if(configComponent.hasSideForData(TransmissionType.GAS, facing, 1, side))
|
||||
{
|
||||
return gasTank.canReceive(type);
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
|
||||
|
||||
for(TransmissionType transmission : configComponent.transmissions)
|
||||
{
|
||||
factory.configComponent.setConfig(transmission, configComponent.getConfig(transmission));
|
||||
factory.configComponent.setConfig(transmission, configComponent.getConfig(transmission).asByteArray());
|
||||
factory.configComponent.setEjecting(transmission, configComponent.isEjecting(transmission));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package mekanism.common.tile.component;
|
||||
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
public class SideConfig {
|
||||
private byte[] directions = new byte[EnumFacing.VALUES.length];
|
||||
private Observable observable = new Observable();
|
||||
|
||||
public SideConfig()
|
||||
{
|
||||
}
|
||||
|
||||
public SideConfig(byte[] b) {
|
||||
this(b[0], b[1], b[2], b[3], b[4], b[5]);
|
||||
assert b.length == EnumFacing.VALUES.length;
|
||||
}
|
||||
|
||||
public SideConfig(byte d, byte u, byte n, byte s, byte w, byte e)
|
||||
{
|
||||
setDirections(d, u, n, s, w, e);
|
||||
}
|
||||
|
||||
public void setDirections(byte d, byte u, byte n, byte s, byte w, byte e) {
|
||||
directions[0] = d;
|
||||
directions[1] = u;
|
||||
directions[2] = n;
|
||||
directions[3] = s;
|
||||
directions[4] = w;
|
||||
directions[5] = e;
|
||||
observable.notifyObservers(this);
|
||||
}
|
||||
|
||||
public byte get(EnumFacing f) {
|
||||
return directions[f.ordinal()];
|
||||
}
|
||||
|
||||
public byte[] asByteArray() {
|
||||
return directions;
|
||||
}
|
||||
|
||||
public void set(EnumFacing f, byte value) {
|
||||
directions[f.ordinal()] = value;
|
||||
observable.notifyObservers(this);
|
||||
}
|
||||
|
||||
public void addObserver(Observer o) {
|
||||
observable.addObserver(o);
|
||||
}
|
||||
|
||||
public void deleteObserver(Observer o) {
|
||||
observable.deleteObserver(o);
|
||||
}
|
||||
}
|
||||
@@ -25,12 +25,12 @@ public class TileComponentConfig implements ITileComponent
|
||||
|
||||
public TileEntityContainerBlock tileEntity;
|
||||
|
||||
public Map<Integer, byte[]> sideConfigs = new HashMap<Integer, byte[]>();
|
||||
public Map<Integer, ArrayList<SideData>> sideOutputs = new HashMap<Integer, ArrayList<SideData>>();
|
||||
public Map<Integer, Boolean> ejecting = new HashMap<Integer, Boolean>();
|
||||
public Map<Integer, Boolean> canEject = new HashMap<Integer, Boolean>();
|
||||
private Map<TransmissionType, SideConfig> sideConfigs = new HashMap<>();
|
||||
private Map<TransmissionType, ArrayList<SideData>> sideOutputs = new HashMap<>();
|
||||
private Map<TransmissionType, Boolean> ejecting = new HashMap<>();
|
||||
private Map<TransmissionType, Boolean> canEject = new HashMap<>();
|
||||
|
||||
public List<TransmissionType> transmissions = new ArrayList<TransmissionType>();
|
||||
public List<TransmissionType> transmissions = new ArrayList<>();
|
||||
|
||||
public TileComponentConfig(TileEntityContainerBlock tile, TransmissionType... types)
|
||||
{
|
||||
@@ -59,43 +59,48 @@ public class TileComponentConfig implements ITileComponent
|
||||
transmissions.add(type);
|
||||
}
|
||||
|
||||
sideOutputs.put(type.ordinal(), new ArrayList<SideData>());
|
||||
ejecting.put(type.ordinal(), false);
|
||||
canEject.put(type.ordinal(), true);
|
||||
sideOutputs.put(type, new ArrayList<>());
|
||||
ejecting.put(type, false);
|
||||
canEject.put(type, true);
|
||||
}
|
||||
|
||||
|
||||
public EnumSet<EnumFacing> getSidesForData(TransmissionType type, EnumFacing facing, int dataIndex)
|
||||
{
|
||||
EnumSet<EnumFacing> ret = EnumSet.noneOf(EnumFacing.class);
|
||||
SideConfig config = getConfig(type);
|
||||
EnumFacing[] translatedFacings = MekanismUtils.getBaseOrientations(facing);
|
||||
|
||||
for(EnumFacing f : EnumFacing.VALUES)
|
||||
for(int i = 0; i < EnumFacing.VALUES.length; i++)
|
||||
{
|
||||
EnumFacing side = MekanismUtils.getBaseOrientation(f, facing);
|
||||
|
||||
if(getConfig(type)[side.ordinal()] == dataIndex)
|
||||
if(config.get(translatedFacings[i]) == dataIndex)
|
||||
{
|
||||
ret.add(f);
|
||||
ret.add(translatedFacings[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public boolean hasSideForData(TransmissionType type, EnumFacing facing, int dataIndex, EnumFacing sideToTest)
|
||||
{
|
||||
return getConfig(type).get(sideToTest) == dataIndex;
|
||||
}
|
||||
|
||||
public void setCanEject(TransmissionType type, boolean eject)
|
||||
{
|
||||
canEject.put(type.ordinal(), eject);
|
||||
canEject.put(type, eject);
|
||||
}
|
||||
|
||||
public boolean canEject(TransmissionType type)
|
||||
{
|
||||
return canEject.get(type.ordinal());
|
||||
return canEject.get(type);
|
||||
}
|
||||
|
||||
public void fillConfig(TransmissionType type, int data)
|
||||
{
|
||||
byte sideData = (byte)data;
|
||||
|
||||
setConfig(type, new byte[] {sideData, sideData, sideData, sideData, sideData, sideData});
|
||||
setConfig(type, sideData, sideData, sideData, sideData, sideData, sideData);
|
||||
}
|
||||
|
||||
public void setIOConfig(TransmissionType type)
|
||||
@@ -115,25 +120,31 @@ public class TileComponentConfig implements ITileComponent
|
||||
fillConfig(type, 1);
|
||||
setCanEject(type, false);
|
||||
}
|
||||
|
||||
|
||||
public void setConfig(TransmissionType type, byte[] config)
|
||||
{
|
||||
sideConfigs.put(type.ordinal(), config);
|
||||
assert config.length == EnumFacing.VALUES.length;
|
||||
setConfig(type, config[0], config[1], config[2], config[3], config[4], config[5]);
|
||||
}
|
||||
|
||||
public void setConfig(TransmissionType type, byte d, byte u, byte n, byte s, byte w, byte e)
|
||||
{
|
||||
sideConfigs.put(type, new SideConfig(d, u, n, s, w, e));
|
||||
}
|
||||
|
||||
public void addOutput(TransmissionType type, SideData data)
|
||||
{
|
||||
sideOutputs.get(type.ordinal()).add(data);
|
||||
sideOutputs.get(type).add(data);
|
||||
}
|
||||
|
||||
public ArrayList<SideData> getOutputs(TransmissionType type)
|
||||
{
|
||||
return sideOutputs.get(type.ordinal());
|
||||
return sideOutputs.get(type);
|
||||
}
|
||||
|
||||
public byte[] getConfig(TransmissionType type)
|
||||
public SideConfig getConfig(TransmissionType type)
|
||||
{
|
||||
return sideConfigs.get(type.ordinal());
|
||||
return sideConfigs.get(type);
|
||||
}
|
||||
|
||||
public SideData getOutput(TransmissionType type, EnumFacing side, EnumFacing facing)
|
||||
@@ -152,8 +163,9 @@ public class TileComponentConfig implements ITileComponent
|
||||
{
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
int index = getConfig(type)[side.ordinal()];
|
||||
|
||||
SideConfig sideConfig = getConfig(type);
|
||||
int index = sideConfig.get(side);
|
||||
|
||||
if(index == -1)
|
||||
{
|
||||
@@ -161,7 +173,8 @@ public class TileComponentConfig implements ITileComponent
|
||||
}
|
||||
else if(index > getOutputs(type).size()-1)
|
||||
{
|
||||
index = getConfig(type)[side.ordinal()] = 0;
|
||||
sideConfig.set(side, (byte) 0);
|
||||
index = 0;
|
||||
}
|
||||
|
||||
return getOutputs(type).get(index);
|
||||
@@ -184,8 +197,8 @@ public class TileComponentConfig implements ITileComponent
|
||||
{
|
||||
if(nbtTags.getByteArray("config" + type.ordinal()).length > 0)
|
||||
{
|
||||
sideConfigs.put(type.ordinal(), nbtTags.getByteArray("config" + type.ordinal()));
|
||||
ejecting.put(type.ordinal(), nbtTags.getBoolean("ejecting" + type.ordinal()));
|
||||
sideConfigs.put(type, new SideConfig(nbtTags.getByteArray("config" + type.ordinal())));
|
||||
ejecting.put(type, nbtTags.getBoolean("ejecting" + type.ordinal()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,9 +220,9 @@ public class TileComponentConfig implements ITileComponent
|
||||
{
|
||||
byte[] array = new byte[6];
|
||||
dataStream.readBytes(array);
|
||||
|
||||
sideConfigs.put(type.ordinal(), array);
|
||||
ejecting.put(type.ordinal(), dataStream.readBoolean());
|
||||
|
||||
sideConfigs.put(type, new SideConfig(array));
|
||||
ejecting.put(type, dataStream.readBoolean());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,8 +231,8 @@ public class TileComponentConfig implements ITileComponent
|
||||
{
|
||||
for(TransmissionType type : transmissions)
|
||||
{
|
||||
nbtTags.setByteArray("config" + type.ordinal(), sideConfigs.get(type.ordinal()));
|
||||
nbtTags.setBoolean("ejecting" + type.ordinal(), ejecting.get(type.ordinal()));
|
||||
nbtTags.setByteArray("config" + type.ordinal(), sideConfigs.get(type).asByteArray());
|
||||
nbtTags.setBoolean("ejecting" + type.ordinal(), ejecting.get(type));
|
||||
}
|
||||
|
||||
nbtTags.setBoolean("sideDataStored", true);
|
||||
@@ -237,8 +250,8 @@ public class TileComponentConfig implements ITileComponent
|
||||
|
||||
for(TransmissionType type : transmissions)
|
||||
{
|
||||
data.add(sideConfigs.get(type.ordinal()));
|
||||
data.add(ejecting.get(type.ordinal()));
|
||||
data.add(sideConfigs.get(type).asByteArray());
|
||||
data.add(ejecting.get(type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,12 +260,12 @@ public class TileComponentConfig implements ITileComponent
|
||||
|
||||
public boolean isEjecting(TransmissionType type)
|
||||
{
|
||||
return ejecting.get(type.ordinal());
|
||||
return ejecting.get(type);
|
||||
}
|
||||
|
||||
public void setEjecting(TransmissionType type, boolean eject)
|
||||
{
|
||||
ejecting.put(type.ordinal(), eject);
|
||||
ejecting.put(type, eject);
|
||||
MekanismUtils.saveChunk(tileEntity);
|
||||
}
|
||||
}
|
||||
@@ -153,13 +153,18 @@ public class TileComponentEjector implements ITileComponent
|
||||
List<EnumFacing> outputSides = new ArrayList<EnumFacing>();
|
||||
ISideConfiguration configurable = (ISideConfiguration)tileEntity;
|
||||
|
||||
for(int i = 0; i < configurable.getConfig().getConfig(type).length; i++)
|
||||
SideConfig sideConfig = configurable.getConfig().getConfig(type);
|
||||
ArrayList<SideData> outputs = configurable.getConfig().getOutputs(type);
|
||||
|
||||
EnumFacing[] facings = MekanismUtils.getBaseOrientations(tileEntity.facing);
|
||||
|
||||
for(int i = 0; i < EnumFacing.VALUES.length; i++)
|
||||
{
|
||||
EnumFacing side = MekanismUtils.getBaseOrientation(EnumFacing.getFront(i), tileEntity.facing);
|
||||
|
||||
if(configurable.getConfig().getConfig(type)[side.ordinal()] == configurable.getConfig().getOutputs(type).indexOf(data))
|
||||
EnumFacing side = facings[i];
|
||||
|
||||
if(sideConfig.get(side) == outputs.indexOf(data))
|
||||
{
|
||||
outputSides.add(EnumFacing.getFront(i));
|
||||
outputSides.add(EnumFacing.VALUES[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,10 +60,10 @@ public abstract class TileEntityAdvancedElectricMachine<RECIPE extends AdvancedM
|
||||
*
|
||||
* @param soundPath - location of the sound effect
|
||||
* @param name - full name of this machine
|
||||
* @param perTick - how much energy this machine uses per tick.
|
||||
* @param secondaryPerTick - how much secondary energy (fuel) this machine uses per tick.
|
||||
* @param ticksRequired - how many ticks it takes to smelt an item.
|
||||
* @param maxEnergy - maximum amount of energy this machine can hold.
|
||||
* @param baseEnergyUsage - how much energy this machine uses per tick.
|
||||
* @param ticksRequired - how many ticks it takes to smelt an item.
|
||||
* @param secondaryPerTick - how much secondary energy (fuel) this machine uses per tick.
|
||||
*/
|
||||
public TileEntityAdvancedElectricMachine(String soundPath, String name, double maxEnergy, double baseEnergyUsage, int ticksRequired, int secondaryPerTick)
|
||||
{
|
||||
@@ -141,7 +141,7 @@ public abstract class TileEntityAdvancedElectricMachine<RECIPE extends AdvancedM
|
||||
|
||||
for(TransmissionType transmission : configComponent.transmissions)
|
||||
{
|
||||
factory.configComponent.setConfig(transmission, configComponent.getConfig(transmission));
|
||||
factory.configComponent.setConfig(transmission, configComponent.getConfig(transmission).asByteArray());
|
||||
factory.configComponent.setEjecting(transmission, configComponent.isEjecting(transmission));
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ public abstract class TileEntityElectricMachine<RECIPE extends BasicMachineRecip
|
||||
|
||||
for(TransmissionType transmission : configComponent.transmissions)
|
||||
{
|
||||
factory.configComponent.setConfig(transmission, configComponent.getConfig(transmission));
|
||||
factory.configComponent.setConfig(transmission, configComponent.getConfig(transmission).asByteArray());
|
||||
factory.configComponent.setEjecting(transmission, configComponent.isEjecting(transmission));
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,7 @@ import mekanism.api.IMekWrench;
|
||||
import mekanism.api.gas.Gas;
|
||||
import mekanism.api.gas.GasStack;
|
||||
import mekanism.api.transmitters.TransmissionType;
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.MekanismBlocks;
|
||||
import mekanism.common.MekanismFluids;
|
||||
import mekanism.common.MekanismItems;
|
||||
import mekanism.common.OreDictCache;
|
||||
import mekanism.common.*;
|
||||
import mekanism.common.Tier.BaseTier;
|
||||
import mekanism.common.Tier.BinTier;
|
||||
import mekanism.common.Tier.EnergyCubeTier;
|
||||
@@ -35,8 +31,6 @@ import mekanism.common.Tier.FluidTankTier;
|
||||
import mekanism.common.Tier.GasTankTier;
|
||||
import mekanism.common.Tier.InductionCellTier;
|
||||
import mekanism.common.Tier.InductionProviderTier;
|
||||
import mekanism.common.Upgrade;
|
||||
import mekanism.common.Version;
|
||||
import mekanism.common.base.IActiveState;
|
||||
import mekanism.common.base.IFactory;
|
||||
import mekanism.common.base.IFactory.RecipeType;
|
||||
@@ -60,6 +54,7 @@ import mekanism.common.network.PacketPersonalChest.PersonalChestPacketType;
|
||||
import mekanism.common.tile.TileEntityAdvancedBoundingBlock;
|
||||
import mekanism.common.tile.TileEntityBoundingBlock;
|
||||
import mekanism.common.tile.TileEntityPersonalChest;
|
||||
import mekanism.common.tile.component.SideConfig;
|
||||
import mekanism.common.util.UnitDisplayUtils.ElectricUnit;
|
||||
import mekanism.common.util.UnitDisplayUtils.TemperatureUnit;
|
||||
import net.minecraft.block.Block;
|
||||
@@ -465,7 +460,28 @@ public final class MekanismUtils
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an integer facing that converts a world-based orientation to a machine-based oriention.
|
||||
* Pre-calculated cache of translated block orientations
|
||||
*/
|
||||
private static final EnumFacing[][] baseOrientations = new EnumFacing[EnumFacing.VALUES.length][EnumFacing.VALUES.length];
|
||||
static {
|
||||
for (int blockFacing = 0; blockFacing < EnumFacing.VALUES.length; blockFacing++) {
|
||||
for (int side = 0; side < EnumFacing.VALUES.length; side++) {
|
||||
baseOrientations[blockFacing][side] = getBaseOrientation(EnumFacing.VALUES[side], EnumFacing.VALUES[blockFacing]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sides in the modified order relative to the machine-based orientation.
|
||||
* @param blockFacing - what orientation the block is facing
|
||||
* @return EnumFacing.VALUES, translated to machine orientation
|
||||
*/
|
||||
public static EnumFacing[] getBaseOrientations(EnumFacing blockFacing) {
|
||||
return baseOrientations[blockFacing.ordinal()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an integer facing that converts a world-based orientation to a machine-based orientation.
|
||||
* @param side - world based
|
||||
* @param blockFacing - what orientation the block is facing
|
||||
* @return machine orientation
|
||||
@@ -552,21 +568,22 @@ public final class MekanismUtils
|
||||
*/
|
||||
public static void incrementOutput(ISideConfiguration config, TransmissionType type, EnumFacing direction)
|
||||
{
|
||||
int side = direction.ordinal();
|
||||
int max = config.getConfig().getOutputs(type).size()-1;
|
||||
int current = config.getConfig().getOutputs(type).indexOf(config.getConfig().getOutputs(type).get(config.getConfig().getConfig(type)[side]));
|
||||
ArrayList<SideData> outputs = config.getConfig().getOutputs(type);
|
||||
SideConfig sideConfig = config.getConfig().getConfig(type);
|
||||
int max = outputs.size() - 1;
|
||||
int current = outputs.indexOf(outputs.get(sideConfig.get(direction)));
|
||||
|
||||
if(current < max)
|
||||
{
|
||||
config.getConfig().getConfig(type)[side] = (byte)(current+1);
|
||||
sideConfig.set(direction, (byte) (current+1));
|
||||
}
|
||||
else if(current == max)
|
||||
{
|
||||
config.getConfig().getConfig(type)[side] = 0;
|
||||
sideConfig.set(direction, (byte) 0);
|
||||
}
|
||||
|
||||
TileEntity tile = (TileEntity)config;
|
||||
|
||||
assert config instanceof TileEntity;
|
||||
TileEntity tile = (TileEntity) config;
|
||||
tile.markDirty();
|
||||
}
|
||||
|
||||
@@ -578,19 +595,21 @@ public final class MekanismUtils
|
||||
*/
|
||||
public static void decrementOutput(ISideConfiguration config, TransmissionType type, EnumFacing direction)
|
||||
{
|
||||
int side = direction.ordinal();
|
||||
int max = config.getConfig().getOutputs(type).size()-1;
|
||||
int current = config.getConfig().getOutputs(type).indexOf(config.getConfig().getOutputs(type).get(config.getConfig().getConfig(type)[side]));
|
||||
ArrayList<SideData> outputs = config.getConfig().getOutputs(type);
|
||||
SideConfig sideConfig = config.getConfig().getConfig(type);
|
||||
int max = outputs.size()-1;
|
||||
int current = outputs.indexOf(outputs.get(sideConfig.get(direction)));
|
||||
|
||||
if(current > 0)
|
||||
{
|
||||
config.getConfig().getConfig(type)[side] = (byte)(current-1);
|
||||
sideConfig.set(direction, (byte)(current - 1));
|
||||
}
|
||||
else if(current == 0)
|
||||
{
|
||||
config.getConfig().getConfig(type)[side] = (byte)max;
|
||||
sideConfig.set(direction, (byte)max);
|
||||
}
|
||||
|
||||
assert config instanceof TileEntity;
|
||||
TileEntity tile = (TileEntity)config;
|
||||
tile.markDirty();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user