mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
242 lines
7.5 KiB
Java
242 lines
7.5 KiB
Java
package mekanism.common.block;
|
|
|
|
import java.util.Random;
|
|
|
|
import mekanism.api.Coord4D;
|
|
import mekanism.common.Mekanism;
|
|
import mekanism.common.MekanismBlocks;
|
|
import mekanism.common.block.property.PropertyColor;
|
|
import mekanism.common.block.states.BlockStateFacing;
|
|
import mekanism.common.block.states.BlockStateGlowPanel;
|
|
import mekanism.common.tile.TileEntityGlowPanel;
|
|
import mekanism.common.util.MekanismUtils;
|
|
import mekanism.common.util.MultipartUtils;
|
|
import net.minecraft.block.Block;
|
|
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.entity.item.EntityItem;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.util.BlockRenderLayer;
|
|
import net.minecraft.util.EnumBlockRenderType;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.RayTraceResult;
|
|
import net.minecraft.util.math.Vec3d;
|
|
import net.minecraft.world.IBlockAccess;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.common.property.IExtendedBlockState;
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
|
|
public class BlockGlowPanel extends Block implements ITileEntityProvider
|
|
{
|
|
private static Random rand = new Random();
|
|
public static AxisAlignedBB[] bounds = new AxisAlignedBB[6];
|
|
|
|
static
|
|
{
|
|
AxisAlignedBB cuboid = new AxisAlignedBB(0.25, 0, 0.25, 0.75, 0.125, 0.75);
|
|
Vec3d fromOrigin = new Vec3d(-0.5, -0.5, -0.5);
|
|
|
|
for(EnumFacing side : EnumFacing.VALUES)
|
|
{
|
|
bounds[side.ordinal()] = MultipartUtils.rotate(cuboid.offset(fromOrigin.xCoord, fromOrigin.yCoord, fromOrigin.zCoord), side).offset(-fromOrigin.xCoord, -fromOrigin.zCoord, -fromOrigin.zCoord);
|
|
}
|
|
}
|
|
|
|
public BlockGlowPanel()
|
|
{
|
|
super(Material.PISTON);
|
|
setCreativeTab(Mekanism.tabMekanism);
|
|
setHardness(1F);
|
|
setResistance(10F);
|
|
}
|
|
|
|
@Override
|
|
public int getMetaFromState(IBlockState state)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public BlockStateContainer createBlockState()
|
|
{
|
|
return new BlockStateGlowPanel(this);
|
|
}
|
|
|
|
@Override
|
|
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
|
|
{
|
|
TileEntityGlowPanel tileEntity = (TileEntityGlowPanel) MekanismUtils.getTileEntitySafe(world, pos);
|
|
return tileEntity != null ? state.withProperty(BlockStateFacing.facingProperty, tileEntity.side) : state;
|
|
}
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
@Override
|
|
public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
|
|
{
|
|
TileEntityGlowPanel tileEntity = (TileEntityGlowPanel) MekanismUtils.getTileEntitySafe(world, pos);
|
|
|
|
if(tileEntity != null)
|
|
{
|
|
state = state.withProperty(BlockStateFacing.facingProperty, tileEntity.side);
|
|
|
|
if(state instanceof IExtendedBlockState)
|
|
{
|
|
return ((IExtendedBlockState)state).withProperty(PropertyColor.INSTANCE, new PropertyColor(tileEntity.colour));
|
|
}
|
|
}
|
|
|
|
return state;
|
|
}
|
|
|
|
@Override
|
|
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block, BlockPos neighbor)
|
|
{
|
|
TileEntityGlowPanel tileEntity = (TileEntityGlowPanel)world.getTileEntity(pos);
|
|
|
|
if(!world.isRemote && !canStay(tileEntity))
|
|
{
|
|
float motion = 0.7F;
|
|
double motionX = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
double motionY = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
double motionZ = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
ItemStack stack = new ItemStack(MekanismBlocks.GlowPanel, 1, tileEntity.colour.getMetaValue());
|
|
EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, pos.getZ() + motionZ, stack);
|
|
|
|
world.spawnEntity(entityItem);
|
|
world.setBlockToAir(pos);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor)
|
|
{
|
|
TileEntityGlowPanel tileEntity = (TileEntityGlowPanel) MekanismUtils.getTileEntitySafe(world, pos);
|
|
|
|
if(!tileEntity.getWorld().isRemote && !canStay(tileEntity))
|
|
{
|
|
float motion = 0.7F;
|
|
double motionX = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
double motionY = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
double motionZ = (rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
ItemStack stack = new ItemStack(MekanismBlocks.GlowPanel, 1, tileEntity.colour.getMetaValue());
|
|
EntityItem entityItem = new EntityItem(tileEntity.getWorld(), pos.getX() + motionX, pos.getY() + motionY, pos.getZ() + motionZ, stack);
|
|
|
|
tileEntity.getWorld().spawnEntity(entityItem);
|
|
tileEntity.getWorld().setBlockToAir(pos);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess world, BlockPos pos)
|
|
{
|
|
TileEntity tile = MekanismUtils.getTileEntitySafe(world, pos);
|
|
|
|
if(tile != null && tile instanceof TileEntityGlowPanel)
|
|
{
|
|
return bounds[((TileEntityGlowPanel)tile).side.ordinal()];
|
|
}
|
|
|
|
return super.getBoundingBox(state, world, pos);
|
|
}
|
|
|
|
@Override
|
|
public boolean canPlaceBlockOnSide(World world, BlockPos pos, EnumFacing side)
|
|
{
|
|
return world.isSideSolid(pos.offset(side.getOpposite()), side);
|
|
}
|
|
|
|
public static boolean canStay(TileEntityGlowPanel tileEntity)
|
|
{
|
|
Coord4D adj = new Coord4D(tileEntity.getPos().offset(tileEntity.side), tileEntity.getWorld());
|
|
return tileEntity.getWorld().isSideSolid(adj.getPos(), tileEntity.side.getOpposite());
|
|
}
|
|
|
|
@Override
|
|
public int getLightValue(IBlockState state, IBlockAccess world, BlockPos pos)
|
|
{
|
|
return 15;
|
|
}
|
|
|
|
@Override
|
|
public int quantityDropped(Random random)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player)
|
|
{
|
|
TileEntityGlowPanel tileEntity = (TileEntityGlowPanel)world.getTileEntity(pos);
|
|
return new ItemStack(MekanismBlocks.GlowPanel, 1, tileEntity.colour.getMetaValue());
|
|
}
|
|
|
|
@Override
|
|
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
|
|
{
|
|
if(!player.capabilities.isCreativeMode && !world.isRemote && willHarvest)
|
|
{
|
|
float motion = 0.7F;
|
|
double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
EntityItem entityItem = new EntityItem(world, pos.getX() + motionX, pos.getY() + motionY, pos.getZ() + motionZ, getPickBlock(state, null, world, pos, player));
|
|
|
|
world.spawnEntity(entityItem);
|
|
}
|
|
|
|
return super.removedByPlayer(state, world, pos, player, willHarvest);
|
|
}
|
|
|
|
@Override
|
|
public TileEntity createNewTileEntity(World worldIn, int meta)
|
|
{
|
|
return new TileEntityGlowPanel();
|
|
}
|
|
|
|
@Override
|
|
public boolean canRenderInLayer(IBlockState state, BlockRenderLayer layer)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public EnumBlockRenderType getRenderType(IBlockState state)
|
|
{
|
|
return EnumBlockRenderType.MODEL;
|
|
}
|
|
|
|
@Override
|
|
public boolean isBlockNormalCube(IBlockState state)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean isOpaqueCube(IBlockState state)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean isFullCube(IBlockState state)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean isFullBlock(IBlockState state)
|
|
{
|
|
return false;
|
|
}
|
|
}
|