Jump to content

[1.12] Half slabs display issue when smooth lighting is off?


Recommended Posts

Posted

Hi all,

 

I have custom slabs that should normally behave exactly like normal ones as far as the display is concerned. When smooth lightning is on (minimum or maximum), it works fine. When smooth lightning is off, the top part of the slab (or the bottom if it's a top slab) is shown as a dark area.

 

Here is my class:

package org.millenaire.common.block;

import java.util.Random;

import org.millenaire.common.forge.Mill;
import org.millenaire.common.item.IMetaBlockName;

import net.minecraft.block.Block;
import net.minecraft.block.BlockSlab;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public abstract class BlockPath extends BlockSlab implements IMetaBlockName {

	public static class BlockPathDouble extends BlockPath {
		public BlockPathDouble(final String blockName, MapColor color) {
			super(blockName, color);
		}

		@Override
		public boolean isDouble() {
			return true;
		}
	}

	public static class BlockPathSlab extends BlockPath {
		public BlockPathSlab(final String blockName, MapColor color) {
			super(blockName, color);
		}

		@Override
		public boolean isDouble() {
			return false;
		}
	}
	
	public static enum Variant implements IStringSerializable {
		DEFAULT;

		@Override
		public String getName() {
			return "default";
		}
	}

	//Variant with only default as slab-related blocks need them
	public static final PropertyEnum<Variant> VARIANT = PropertyEnum.<Variant>create("variant", Variant.class);
	//whether the path is "stable", i.e. villagers won't clear it. All player-laid pathes are stable.
	public static final PropertyBool STABLE = PropertyBool.create("stable");

	
	private final String singleSlabBlockName, doubleSlabName;

	/**
	 * singleSlabBlockName to have a reference to it for item drops
	 */
	public BlockPath(final String blockName, MapColor color) {
		super(Material.GROUND, color);
		
		singleSlabBlockName = blockName + "_slab";
		doubleSlabName = blockName;
		
		//same for single and double slabs
		setUnlocalizedName(Mill.MODID + "." + doubleSlabName);
		
		if (this.isDouble()) {			
			setRegistryName(doubleSlabName);
		} else {
			setRegistryName(singleSlabBlockName);
		}

		IBlockState iblockstate = this.blockState.getBaseState();

		iblockstate = iblockstate.withProperty(STABLE, false);

		if (!this.isDouble()) {
			iblockstate = iblockstate.withProperty(HALF, BlockSlab.EnumBlockHalf.BOTTOM);
		}

		this.setDefaultState(iblockstate);

		setHarvestLevel("shovel", 0);
		setHardness(0.8F);
		setCreativeTab(MillBlocks.tabMillenaire);
		setSoundType(SoundType.GROUND);
	}

	@Override
	protected BlockStateContainer createBlockState() {
		return this.isDouble() ? new BlockStateContainer(this, new IProperty[] { VARIANT, STABLE })
				: new BlockStateContainer(this, new IProperty[] { VARIANT, HALF, STABLE });
	}

	/**
	 * Gets the metadata of the item this Block can drop. This method is called when
	 * the block gets destroyed. It returns the metadata of the dropped item based
	 * on the old metadata of the block.
	 */
	@Override
	public int damageDropped(final IBlockState state) {
		return 0;
	}

	@Override
	public ItemStack getItem(final World worldIn, final BlockPos pos, final IBlockState state) {
		return new ItemStack(Block.getBlockFromName(Mill.MODID+":"+singleSlabBlockName), 1, 0);
	}

	/**
	 * Unlike regular slabs we drop the version corresponding to the actual block
	 */
	@Override
	public Item getItemDropped(final IBlockState state, final Random rand, final int fortune) {
		if (isDouble())
			return Item.getItemFromBlock(Block.getBlockFromName(Mill.MODID+":"+doubleSlabName));
		else
			return Item.getItemFromBlock(Block.getBlockFromName(Mill.MODID+":"+singleSlabBlockName));
	}

	/**
	 * Convert the BlockState into the correct metadata value
	 */
	@Override
	public int getMetaFromState(final IBlockState state) {
		int i = 0;

		if (state.getValue(STABLE)) {
			i |= 1;
		}

		if (!this.isDouble() && (state.getValue(HALF) == BlockSlab.EnumBlockHalf.TOP)) {
			i |= 8;
		}

		return i;
	}

	/**
	 * Convert the given metadata into a BlockState for this Block
	 */
	@Override
	public IBlockState getStateFromMeta(final int meta) {
		IBlockState iblockstate = this.getDefaultState().withProperty(VARIANT, Variant.DEFAULT);
		
		if ((meta & 1) == 1) {
			iblockstate = iblockstate.withProperty(STABLE, true);
		}

		if (!this.isDouble()) {
			iblockstate = iblockstate.withProperty(HALF,
					(meta & 8) == 0 ? BlockSlab.EnumBlockHalf.BOTTOM : BlockSlab.EnumBlockHalf.TOP);
		}

		return iblockstate;
	}

	@Override
	public String getUnlocalizedName(int meta) {
		//we don't actually have variants for this block
		return getUnlocalizedName();
	}
	
	@SideOnly(Side.CLIENT)
	public void initModel() {
		if (isDouble()) {
			ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0,
					new ModelResourceLocation(getRegistryName(), "stable=false,variant=default"));
		} else {
			ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0,
					new ModelResourceLocation(getRegistryName(), "half=bottom,stable=false,variant=default"));
		}
			
	}
	
	@Override
	public Comparable<?> getTypeForItem(final ItemStack stack) {
		return Variant.DEFAULT;
	}

	@Override
	public IProperty<?> getVariantProperty() {
		return VARIANT;
	}

	@Override
	public String getSpecialName(final ItemStack stack) {
		//we don't actually have variants for this block
		return getUnlocalizedName();
	}
	
	/*
	 * Gets the single slab of this block, whether or not it is the single block itself
	 */
	public BlockPath getSingleSlab() {
		return (BlockPath)Block.getBlockFromName(Mill.MODID+":"+singleSlabBlockName);
	}
	
	/*
	 * Gets the double slab of this block, whether or not it is the double block itself
	 */
	public BlockPath getDoubleSlab() {
		return (BlockPath)Block.getBlockFromName(Mill.MODID+":"+doubleSlabName);
	}
	
	/*
	 * Unlike normal slabs we always drop 1 since double blocks drop a full block
	 */
	public int quantityDropped(Random random)
    {
        return 1;
    }
	
	/*
	 * Overriding the one from BlockSlab that can't deal without double slabs being set directly
	 * 	 */
	@Override
	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
    {
        
        if (this.isDouble())
        {
            return getStateFromMeta(meta).withProperty(STABLE, true);
        }
        else
        {
        	IBlockState iblockstate = super.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer).withProperty(HALF, BlockSlab.EnumBlockHalf.BOTTOM).withProperty(STABLE, true);

            return facing != EnumFacing.DOWN && (facing == EnumFacing.UP || (double)hitY <= 0.5D) ? iblockstate : iblockstate.withProperty(HALF, BlockSlab.EnumBlockHalf.TOP);
        }
    }
}

 

One of my blockstate:

{
	"forge_marker": 1,
	"variants": {
		"half=bottom,stable=false,variant=default": {
			"model": "millenaire:pathsandstone_bottom"
		},
		"half=top,stable=false,variant=default": {
			"model": "millenaire:pathsandstone_top"
		},
		"half=bottom,stable=true,variant=default": {
			"model": "millenaire:pathsandstone_bottom"
		},
		"half=top,stable=true,variant=default": {
			"model": "millenaire:pathsandstone_top"
		}
	}
}

 

And one of my block model:

 

{
    "parent": "block/half_slab",
    "textures": {
        "bottom": "millenaire:blocks/pathbottom",
        "top": "millenaire:blocks/pathsandstone",
        "side": "millenaire:blocks/pathsandstone_halfside",
        "particle" : "millenaire:blocks/pathsandstone"
    }
}

 

Those two JSONs are pretty much identical to those used by Minecraft's slabs, with just particles added to the model.

 

Must be something stupid, but what? Any ideas?

 

Thanks!

 

2018-02-24_18.20.29.png

Posted

As I discovered in this topic, you need to set Block#useNeighborBrightness to true. For some reason, Vanilla does this in Block.registerBlocks instead of in the constructors of the appropriate Block classes.

  • Like 2

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.