Skip to content

Commit

Permalink
Add a basic cache for cable models to avoid constant regeneration. (#…
Browse files Browse the repository at this point in the history
…3185)

* Add a basic cache for cable models to avoid constant regeneration.
* Improved equals/hashCode to ignore cable unrelated parts or facades.
  • Loading branch information
yueh committed Oct 31, 2017
1 parent c1fa77d commit cc4599b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,6 +53,8 @@
public class CableBusBakedModel implements IBakedModel
{

private static final Map<CableBusRenderState, List<BakedQuad>> CABLE_MODEL_CACHE = new HashMap<>();

private final CableBuilder cableBuilder;

private final FacadeBuilder facadeBuilder;
Expand Down Expand Up @@ -88,8 +91,15 @@ public List<BakedQuad> getQuads( @Nullable IBlockState state, @Nullable EnumFaci
// translucent facades further down below.
if( layer == BlockRenderLayer.CUTOUT )
{

// First, handle the cable at the center of the cable bus
this.addCableQuads( renderState, quads );
final List<BakedQuad> cableModel = CABLE_MODEL_CACHE.computeIfAbsent( renderState, k ->
{
final List<BakedQuad> model = new ArrayList<>();
this.addCableQuads( renderState, model );
return model;
} );
quads.addAll( cableModel );

// Then handle attachments
for( EnumFacing facing : EnumFacing.values() )
Expand Down Expand Up @@ -308,10 +318,8 @@ public List<TextureAtlasSprite> getParticleTextures( CableBusRenderState renderS

TextureAtlasSprite particleTexture = bakedModel.getParticleTexture();

// If a part sub-model has no particle texture (indicated by it being the missing texture), don't
// add
// it,
// so we don't get ugly missing texture break particles.
// If a part sub-model has no particle texture (indicated by it being the missing texture),
// don't add it, so we don't get ugly missing texture break particles.
if( this.textureMap.getMissingSprite() != particleTexture )
{
result.add( particleTexture );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;

import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
Expand Down Expand Up @@ -153,4 +154,43 @@ public List<AxisAlignedBB> getBoundingBoxes()
return this.boundingBoxes;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ( ( this.attachmentConnections == null ) ? 0 : this.attachmentConnections.hashCode() );
result = prime * result + ( ( this.cableBusAdjacent == null ) ? 0 : this.cableBusAdjacent.hashCode() );
result = prime * result + ( ( this.cableColor == null ) ? 0 : this.cableColor.hashCode() );
result = prime * result + ( ( this.cableType == null ) ? 0 : this.cableType.hashCode() );
result = prime * result + ( ( this.channelsOnSide == null ) ? 0 : this.channelsOnSide.hashCode() );
result = prime * result + ( ( this.connectionTypes == null ) ? 0 : this.connectionTypes.hashCode() );
result = prime * result + ( ( this.coreType == null ) ? 0 : this.coreType.hashCode() );
return result;
}

@Override
public boolean equals( Object obj )
{
if( this == obj )
{
return true;
}
if( obj == null )
{
return false;
}
if( getClass() != obj.getClass() )
{
return false;
}

final CableBusRenderState other = (CableBusRenderState) obj;

return this.cableColor == other.cableColor && this.cableType == other.cableType && this.coreType == other.coreType && Objects
.equals( this.attachmentConnections, other.attachmentConnections ) && Objects.equals( this.cableBusAdjacent,
other.cableBusAdjacent ) && Objects.equals( this.channelsOnSide, other.channelsOnSide ) && Objects.equals( this.connectionTypes,
other.connectionTypes );
}

}

0 comments on commit cc4599b

Please sign in to comment.