mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-27 23:33:08 +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
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user