Skip to content

Instantly share code, notes, and snippets.

@artillect
Created November 27, 2016 03:10
Show Gist options
  • Save artillect/2d0314c06fa1a43428ea1299b03c3759 to your computer and use it in GitHub Desktop.
Save artillect/2d0314c06fa1a43428ea1299b03c3759 to your computer and use it in GitHub Desktop.
package com.artillect.voltaics.block;
import com.artillect.voltaics.tileentity.TileEntityVoltaicPile;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
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.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
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 static final PropertyDirection = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
public class BlockVoltaicPile extends BlockTEBase {
public BlockVoltaicPile(Material material, String name, boolean addToTab) {
super(material, name, addToTab);
setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
@SideOnly(Side.CLIENT)
public void initModel() {
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
world.setBlockState(pos, state.withProperty(FACING, getFacingFromEntity(pos, placer)), 2);
}
public static EnumFacing getFacingFromEntity(BlockPos clickedBlock, EntityLivingBase entity) {
return EnumFacing.getFacingFromVector(
(float) (entity.posX - clickedBlock.getX()),
(float) (entity.posY - clickedBlock.getY()),
(float) (entity.posZ - clickedBlock.getZ()));
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7));
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(FACING).getIndex();
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, FACING);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment