Skip to content

Commit

Permalink
Fixed disguisable blocks rendering on all layers when not disguised
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Feb 7, 2020
1 parent 6c5cc4d commit 5f90e9d
Showing 1 changed file with 24 additions and 6 deletions.
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.DirectionalBlock;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.client.renderer.model.BakedQuad;
import net.minecraft.client.renderer.model.IBakedModel;
Expand All @@ -21,31 +22,44 @@
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.function.Predicate;

public class DisguiseBakedModel extends BakedModelWrapper<IBakedModel> {
public static final ModelProperty<ItemStack> DISGUISE = new ModelProperty<>();

private final Predicate<RenderType> renderTypeLookup;

public DisguiseBakedModel(IBakedModel originalModel) {
this(originalModel, RenderType.solid());
}

public DisguiseBakedModel(IBakedModel originalModel, RenderType defaultRenderType) {
this(originalModel, rt -> rt == defaultRenderType);
}

public DisguiseBakedModel(IBakedModel originalModel, Predicate<RenderType> renderTypeLookup) {
super(originalModel);

this.renderTypeLookup = renderTypeLookup;
}

@Nonnull
@Override
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull Random rand, @Nonnull IModelData extraData) {
if(extraData.hasProperty(DISGUISE)) {
if (extraData.hasProperty(DISGUISE)) {
ItemStack disguise = extraData.getData(DISGUISE);

if(disguise != null && disguise.getItem() instanceof BlockItem) {
if (disguise != null && disguise.getItem() instanceof BlockItem) {
BlockItem disguiseItem = (BlockItem) disguise.getItem();
BlockState disguiseState = disguiseItem.getBlock().getDefaultState();
if(disguiseState.has(DirectionalBlock.FACING))
if (disguiseState.has(DirectionalBlock.FACING))
disguiseState = disguiseState.with(DirectionalBlock.FACING, state.get(DirectionalBlock.FACING));

if(RenderTypeLookup.canRenderInLayer(disguiseState, MinecraftForgeClient.getRenderLayer())) {
if (RenderTypeLookup.canRenderInLayer(disguiseState, MinecraftForgeClient.getRenderLayer())) {
IBakedModel model = Minecraft.getInstance().getBlockRendererDispatcher().getBlockModelShapes().getModel(disguiseState);

// Avoid infinite recursion when setting the disguise to another disguisable block
if(model instanceof DisguiseBakedModel) {
if (model instanceof DisguiseBakedModel) {
return ((DisguiseBakedModel) model).getSuperQuads(state, side, rand, extraData);
}

Expand All @@ -60,7 +74,11 @@ public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction
}

private List<BakedQuad> getSuperQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull Random rand, @Nonnull IModelData extraData) {
return super.getQuads(state, side, rand, extraData);
if (renderTypeLookup.test(MinecraftForgeClient.getRenderLayer())) {
return super.getQuads(state, side, rand, extraData);
} else {
return Collections.emptyList();
}
}

@Nonnull
Expand Down

0 comments on commit 5f90e9d

Please sign in to comment.