mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
Fixed Infusing Factory losing infuse storage, implement CTM model caching
This commit is contained in:
1 parent
19453b56f3
commit
f00b35a017
8 files changed
+311
-39
No files matched your search
@@ -12,6 +12,7 @@ import gnu.trove.map.TIntObjectMap;
|
||||
import gnu.trove.map.hash.TIntObjectHashMap;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
@@ -157,6 +158,19 @@ public class CTM {
|
||||
return submapCache;
|
||||
}
|
||||
|
||||
public int[] createSubmapIndices(long data, EnumFacing side){
|
||||
submapCache = new int[] { 18, 19, 17, 16 };
|
||||
|
||||
buildConnectionMap(data, side);
|
||||
|
||||
// Map connections to submap indeces
|
||||
for (int i = 0; i < 4; i++) {
|
||||
fillSubmaps(i);
|
||||
}
|
||||
|
||||
return submapCache;
|
||||
}
|
||||
|
||||
public int[] getSubmapIndices() {
|
||||
return submapCache;
|
||||
}
|
||||
@@ -174,6 +188,18 @@ public class CTM {
|
||||
connectionMap.put(dir, dir.isConnected(this, world, pos, side, state));
|
||||
}
|
||||
}
|
||||
|
||||
public void buildConnectionMap(long data, EnumFacing side){
|
||||
for (Dir dir : Dir.VALUES){
|
||||
connectionMap.put(dir, false);
|
||||
}
|
||||
List<CTMConnections> connections = CTMConnections.decode(data);
|
||||
for (CTMConnections loc : connections){
|
||||
if (loc.getDirForSide(side) != null){
|
||||
connectionMap.put(loc.getDirForSide(side), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fillSubmaps(int idx) {
|
||||
Dir[] dirs = submapMap.get(idx);
|
||||
@@ -298,7 +324,7 @@ public class CTM {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public IBlockState getBlockOrFacade(IBlockAccess world, BlockPos pos, EnumFacing side) {
|
||||
public static IBlockState getBlockOrFacade(IBlockAccess world, BlockPos pos, EnumFacing side) {
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -6,9 +6,17 @@ import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import static mekanism.client.render.ctm.CTMConnections.*;
|
||||
|
||||
public class CTMBlockRenderContext
|
||||
{
|
||||
private static final CTMConnections[] CACHED_LOCATIONS = ArrayUtils.removeElements(CTMConnections.VALUES, UP_UP, DOWN_DOWN, EAST_EAST, WEST_WEST, NORTH_NORTH, SOUTH_SOUTH);
|
||||
|
||||
private EnumMap<EnumFacing, CTM> ctmData = new EnumMap<>(EnumFacing.class);
|
||||
|
||||
private long data;
|
||||
|
||||
public CTMBlockRenderContext(IBlockAccess world, BlockPos pos)
|
||||
{
|
||||
@@ -18,6 +26,20 @@ public class CTMBlockRenderContext
|
||||
ctm.createSubmapIndices(world, pos, face);
|
||||
ctmData.put(face, ctm);
|
||||
}
|
||||
|
||||
data = CTMConnections.getData(world, pos, CACHED_LOCATIONS);
|
||||
}
|
||||
|
||||
public CTMBlockRenderContext(long d)
|
||||
{
|
||||
data = d;
|
||||
|
||||
for(EnumFacing face : EnumFacing.VALUES)
|
||||
{
|
||||
CTM ctm = createCTM();
|
||||
ctm.createSubmapIndices(data, face);
|
||||
ctmData.put(face, ctm);
|
||||
}
|
||||
}
|
||||
|
||||
protected CTM createCTM()
|
||||
@@ -29,4 +51,9 @@ public class CTMBlockRenderContext
|
||||
{
|
||||
return ctmData.get(face);
|
||||
}
|
||||
|
||||
public long serialize()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package mekanism.client.render.ctm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import mekanism.api.MekanismAPI;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
/**
|
||||
* Represents all the different spot for connection locations for a ctm block
|
||||
*/
|
||||
public enum CTMConnections
|
||||
{
|
||||
UP(Dir.TOP),
|
||||
DOWN(Dir.BOTTOM),
|
||||
NORTH(EnumFacing.EAST, Dir.RIGHT),
|
||||
SOUTH(EnumFacing.EAST, Dir.LEFT),
|
||||
EAST(Dir.RIGHT),
|
||||
WEST(Dir.LEFT),
|
||||
|
||||
NORTH_EAST(EnumFacing.UP, Dir.TOP_RIGHT),
|
||||
NORTH_WEST(EnumFacing.UP, Dir.TOP_LEFT),
|
||||
SOUTH_EAST(EnumFacing.UP, Dir.BOTTOM_RIGHT),
|
||||
SOUTH_WEST(EnumFacing.UP, Dir.BOTTOM_LEFT),
|
||||
|
||||
NORTH_UP(EnumFacing.EAST, Dir.TOP_RIGHT),
|
||||
NORTH_DOWN(EnumFacing.EAST, Dir.BOTTOM_RIGHT),
|
||||
SOUTH_UP(EnumFacing.EAST, Dir.TOP_LEFT),
|
||||
SOUTH_DOWN(EnumFacing.EAST, Dir.BOTTOM_LEFT),
|
||||
|
||||
EAST_UP(Dir.TOP_RIGHT),
|
||||
EAST_DOWN(Dir.BOTTOM_RIGHT),
|
||||
WEST_UP(Dir.TOP_LEFT),
|
||||
WEST_DOWN(Dir.BOTTOM_LEFT),
|
||||
|
||||
NORTH_EAST_UP(EnumFacing.EAST, Dir.TOP_RIGHT, true),
|
||||
NORTH_EAST_DOWN(EnumFacing.EAST, Dir.BOTTOM_RIGHT, true),
|
||||
|
||||
SOUTH_EAST_UP(EnumFacing.EAST, Dir.TOP_LEFT, true),
|
||||
SOUTH_EAST_DOWN(EnumFacing.EAST, Dir.BOTTOM_LEFT, true),
|
||||
|
||||
SOUTH_WEST_UP(EnumFacing.WEST, Dir.TOP_LEFT, true),
|
||||
SOUTH_WEST_DOWN(EnumFacing.WEST, Dir.BOTTOM_LEFT, true),
|
||||
|
||||
NORTH_WEST_UP(EnumFacing.WEST, Dir.TOP_RIGHT, true),
|
||||
NORTH_WEST_DOWN(EnumFacing.WEST, Dir.BOTTOM_RIGHT, true),
|
||||
|
||||
UP_UP(EnumFacing.UP, null, true),
|
||||
DOWN_DOWN(EnumFacing.DOWN, null, true),
|
||||
NORTH_NORTH(EnumFacing.NORTH, null, true),
|
||||
SOUTH_SOUTH(EnumFacing.SOUTH, null, true),
|
||||
EAST_EAST(EnumFacing.EAST, null, true),
|
||||
WEST_WEST(EnumFacing.WEST, null, true),
|
||||
|
||||
;
|
||||
|
||||
public static final CTMConnections[] VALUES = values();
|
||||
|
||||
/**
|
||||
* The enum facing directions needed to get to this connection location
|
||||
*/
|
||||
private EnumFacing normal;
|
||||
private Dir dir;
|
||||
private boolean offset;
|
||||
|
||||
private CTMConnections(Dir dir) {
|
||||
this(EnumFacing.SOUTH, dir);
|
||||
}
|
||||
|
||||
private CTMConnections(Dir dir, boolean offset) {
|
||||
this(EnumFacing.SOUTH, dir, offset);
|
||||
}
|
||||
|
||||
private CTMConnections(EnumFacing normal, Dir dir){
|
||||
this(normal, dir, false);
|
||||
}
|
||||
|
||||
private CTMConnections(EnumFacing normal, Dir dir, boolean offset) {
|
||||
this.normal = normal;
|
||||
this.dir = dir;
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
public Dir getDirForSide(EnumFacing facing){
|
||||
return dir == null ? null : dir;//TODO .relativize()
|
||||
}
|
||||
|
||||
public EnumFacing clipOrDestroy(EnumFacing direction) {
|
||||
EnumFacing[] dirs = dir == null ? new EnumFacing[] {normal, normal} : dir.getNormalizedDirs(direction);
|
||||
if (dirs[0] == direction) {
|
||||
return dirs.length > 1 ? dirs[1] : null;
|
||||
} else if (dirs.length > 1 && dirs[1] == direction) {
|
||||
return dirs[0];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public BlockPos transform(BlockPos pos) {
|
||||
if (dir != null) {
|
||||
for (EnumFacing facing : dir.getNormalizedDirs(normal)) {
|
||||
pos = pos.offset(facing);
|
||||
}
|
||||
} else {
|
||||
pos = pos.offset(normal);
|
||||
}
|
||||
|
||||
if (offset) {
|
||||
pos = pos.offset(normal);
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
public static CTMConnections fromFacing(EnumFacing facing){
|
||||
switch (facing){
|
||||
case NORTH: return NORTH;
|
||||
case SOUTH: return SOUTH;
|
||||
case EAST: return EAST;
|
||||
case WEST: return WEST;
|
||||
case UP: return UP;
|
||||
case DOWN: return DOWN;
|
||||
default: return NORTH;
|
||||
}
|
||||
}
|
||||
|
||||
public static EnumFacing toFacing(CTMConnections loc){
|
||||
switch (loc){
|
||||
case NORTH: return EnumFacing.NORTH;
|
||||
case SOUTH: return EnumFacing.SOUTH;
|
||||
case EAST: return EnumFacing.EAST;
|
||||
case WEST: return EnumFacing.WEST;
|
||||
case UP: return EnumFacing.UP;
|
||||
case DOWN: return EnumFacing.DOWN;
|
||||
default: return EnumFacing.NORTH;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<CTMConnections> decode(long data) {
|
||||
List<CTMConnections> list = new ArrayList<>();
|
||||
for (CTMConnections loc : values()) {
|
||||
if ((1 & (data >> loc.ordinal())) != 0) {
|
||||
list.add(loc);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public long getMask(){
|
||||
return 1 << ordinal();
|
||||
}
|
||||
|
||||
public static List<CTMConnections> getConnections(IBlockAccess world, BlockPos pos, CTMConnections[] values){
|
||||
List<CTMConnections> locs = new ArrayList<>();
|
||||
IBlockState state = world.getBlockState(pos);
|
||||
for (CTMConnections loc : values) {
|
||||
BlockPos second = loc.transform(pos);
|
||||
if (state.equals(CTM.getBlockOrFacade(world, second, null))){
|
||||
locs.add(loc);
|
||||
}
|
||||
}
|
||||
return locs;
|
||||
}
|
||||
|
||||
public static long getData(IBlockAccess world, BlockPos pos, CTMConnections[] values){
|
||||
List<CTMConnections> locs = getConnections(world, pos, values);
|
||||
long data = 0;
|
||||
for (CTMConnections loc : locs){
|
||||
data = data | loc.getMask();
|
||||
}
|
||||
if (MekanismAPI.debug) {
|
||||
String s = Long.toBinaryString(data);
|
||||
while (s.length() < 32) {
|
||||
s = "0" + s;
|
||||
}
|
||||
System.out.println(pos + ": " + s);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package mekanism.client.render.ctm;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.vecmath.Matrix4f;
|
||||
@@ -20,43 +22,48 @@ import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.BlockRenderLayer;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.client.MinecraftForgeClient;
|
||||
import net.minecraftforge.client.model.IPerspectiveAwareModel;
|
||||
import net.minecraftforge.common.model.TRSRTransformation;
|
||||
import net.minecraftforge.common.property.IExtendedBlockState;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.MultimapBuilder;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.collect.Tables;
|
||||
|
||||
/**
|
||||
* Model for all chisel blocks
|
||||
* Model for all CTM blocks, credit to Chisel
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class CTMModelFactory implements IPerspectiveAwareModel
|
||||
{
|
||||
private List<BakedQuad> face;
|
||||
private List<BakedQuad> general;
|
||||
private ListMultimap<BlockRenderLayer, BakedQuad> genQuads = MultimapBuilder.enumKeys(BlockRenderLayer.class).arrayListValues().build();
|
||||
private Table<BlockRenderLayer, EnumFacing, List<BakedQuad>> faceQuads = Tables.newCustomTable(Maps.newEnumMap(BlockRenderLayer.class), () -> Maps.newEnumMap(EnumFacing.class));
|
||||
|
||||
private ModelCTM model;
|
||||
|
||||
private CTMOverride override = new CTMOverride();
|
||||
|
||||
public CTMModelFactory(List<BakedQuad> f, List<BakedQuad> g, ModelCTM m)
|
||||
public CTMModelFactory(ModelCTM m)
|
||||
{
|
||||
face = f;
|
||||
general = g;
|
||||
model = m;
|
||||
}
|
||||
|
||||
public CTMModelFactory(ModelCTM model)
|
||||
{
|
||||
this(Collections.emptyList(), Collections.emptyList(), model);
|
||||
}
|
||||
|
||||
|
||||
private static Cache<Integer, CTMModelFactory> ctmCache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).maximumSize(500).<Integer, CTMModelFactory>build();
|
||||
|
||||
@Override
|
||||
public List<BakedQuad> getQuads(IBlockState stateIn, EnumFacing side, long rand)
|
||||
{
|
||||
@@ -65,14 +72,29 @@ public class CTMModelFactory implements IPerspectiveAwareModel
|
||||
if(stateIn != null && stateIn.getBlock() instanceof ICTMBlock && stateIn instanceof IExtendedBlockState)
|
||||
{
|
||||
IExtendedBlockState state = (IExtendedBlockState)stateIn;
|
||||
IBlockState clean = state.getClean();
|
||||
CTMBlockRenderContext ctxList = state.getValue(BlockStateBasic.ctmProperty);
|
||||
baked = createModel(stateIn, model, ctxList);
|
||||
|
||||
try {
|
||||
if(ctxList == null)
|
||||
{
|
||||
baked = ctmCache.get(Objects.hash(clean, -1), () -> createModel(state, model, null));
|
||||
}
|
||||
else {
|
||||
long serialized = ctxList.serialize();
|
||||
baked = ctmCache.get(Objects.hash(clean, serialized), () -> createModel(state, model, ctxList));
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return Lists.newArrayList();
|
||||
}
|
||||
}
|
||||
else {
|
||||
baked = this;
|
||||
}
|
||||
|
||||
return side == null ? baked.general : FluentIterable.from(baked.face).filter(quad -> quad.getFace() == side).toList();
|
||||
BlockRenderLayer layer = MinecraftForgeClient.getRenderLayer();
|
||||
return side == null ? baked.genQuads.get(layer) : layer == null ? baked.faceQuads.column(side).values().stream().flatMap(List::stream).collect(Collectors.toList()) : baked.faceQuads.get(layer, side);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,33 +129,48 @@ public class CTMModelFactory implements IPerspectiveAwareModel
|
||||
|
||||
private CTMModelFactory createModel(IBlockState state, ModelCTM model, CTMBlockRenderContext ctx)
|
||||
{
|
||||
List<BakedQuad> faceQuads = Lists.newArrayList();
|
||||
List<BakedQuad> generalQuads = Lists.newArrayList();
|
||||
CTMModelFactory ret = new CTMModelFactory(model);
|
||||
IBakedModel baked = model.getModel(state);
|
||||
int quadGoal = ctx == null ? 1 : CTM.QUADS_PER_SIDE;
|
||||
List<BakedQuad> quads = Lists.newArrayList();
|
||||
|
||||
for(EnumFacing facing : EnumFacing.VALUES)
|
||||
for(BlockRenderLayer layer : BlockRenderLayer.values())
|
||||
{
|
||||
TextureCTM face = model.getFace(facing);
|
||||
ICTMBlock block = (ICTMBlock)state.getBlock();
|
||||
CTMData data = block.getCTMData(state);
|
||||
|
||||
if(block.getOverrideTexture(state, facing) != null)
|
||||
{
|
||||
face = CTMRegistry.textureCache.get(block.getOverrideTexture(state, facing));
|
||||
}
|
||||
for(EnumFacing facing : EnumFacing.VALUES)
|
||||
{
|
||||
TextureCTM face = model.getFace(facing);
|
||||
|
||||
if(ctx == null || layer == face.layer)
|
||||
{
|
||||
ICTMBlock block = (ICTMBlock)state.getBlock();
|
||||
CTMData data = block.getCTMData(state);
|
||||
|
||||
if(block.getOverrideTexture(state, facing) != null)
|
||||
{
|
||||
face = CTMRegistry.textureCache.get(block.getOverrideTexture(state, facing));
|
||||
}
|
||||
|
||||
int quadGoal = ctx == null ? 1 : CTM.QUADS_PER_SIDE;
|
||||
IBakedModel baked = model.getModel(state);
|
||||
List<BakedQuad> origFaceQuads = baked.getQuads(state, facing, 0);
|
||||
List<BakedQuad> origGeneralQuads = FluentIterable.from(baked.getQuads(state, null, 0)).filter(q -> q.getFace() == facing).toList();
|
||||
addAllQuads(origFaceQuads, face, ctx, state, quadGoal, faceQuads);
|
||||
addAllQuads(origGeneralQuads, face, ctx, state, quadGoal, generalQuads);
|
||||
List<BakedQuad> temp = baked.getQuads(state, facing, 0);
|
||||
addAllQuads(temp, face, ctx, state, quadGoal, quads);
|
||||
ret.faceQuads.put(layer, facing, ImmutableList.copyOf(quads));
|
||||
|
||||
temp = FluentIterable.from(baked.getQuads(state, null, 0)).filter(q -> q.getFace() == facing).toList();
|
||||
addAllQuads(temp, face, ctx, state, quadGoal, quads);
|
||||
ret.genQuads.putAll(layer, temp);
|
||||
}
|
||||
else {
|
||||
ret.faceQuads.put(layer, facing, Lists.newArrayList());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new CTMModelFactory(faceQuads, generalQuads, model);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private void addAllQuads(List<BakedQuad> from, TextureCTM tex, @Nullable CTMBlockRenderContext ctx, IBlockState state, int quadGoal, List<BakedQuad> to)
|
||||
{
|
||||
to.clear();
|
||||
|
||||
for(BakedQuad q : from)
|
||||
{
|
||||
to.addAll(tex.transformQuad(q, ctx == null ? null : ctx, quadGoal));
|
||||
|
||||
@@ -75,10 +75,10 @@ public class CTMRegistry
|
||||
|
||||
callbacks[0] = new TextureSpriteCallback(new ResourceLocation(domain + ":blocks/ctm/" + name));
|
||||
callbacks[1] = new TextureSpriteCallback(new ResourceLocation(domain + ":blocks/ctm/" + name + "-ctm"));
|
||||
|
||||
|
||||
register(callbacks[0]);
|
||||
register(callbacks[1]);
|
||||
|
||||
return new TextureCTM(BlockRenderLayer.SOLID, callbacks);
|
||||
return new TextureCTM(BlockRenderLayer.CUTOUT, callbacks);
|
||||
}
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public enum Dir {
|
||||
return connection;
|
||||
}
|
||||
|
||||
private EnumFacing[] getNormalizedDirs(EnumFacing normal) {
|
||||
public EnumFacing[] getNormalizedDirs(EnumFacing normal) {
|
||||
if (normal == NORMAL) {
|
||||
return dirs;
|
||||
} else if (normal == NORMAL.getOpposite()) {
|
||||
|
||||
@@ -232,6 +232,7 @@ public class TileEntityFactory extends TileEntityNoisyElectricBlock implements I
|
||||
factory.recipeType = recipeType;
|
||||
factory.upgradeComponent.setSupported(Upgrade.GAS, recipeType.fuelEnergyUpgrades());
|
||||
factory.securityComponent.readFrom(securityComponent);
|
||||
factory.infuseStored = infuseStored;
|
||||
|
||||
for(int i = 0; i < tier.processes+5; i++)
|
||||
{
|
||||
|
||||
@@ -255,8 +255,7 @@ public class TileEntityMetallurgicInfuser extends TileEntityNoisyElectricBlock i
|
||||
}
|
||||
|
||||
//Infuser
|
||||
factory.infuseStored.amount = infuseStored.amount;
|
||||
factory.infuseStored.type = infuseStored.type;
|
||||
factory.infuseStored = infuseStored;
|
||||
|
||||
factory.inventory[5] = inventory[2];
|
||||
factory.inventory[1] = inventory[4];
|
||||
|
||||
Reference in new issue
Block a user