/** * MrCrayfish's Furniture Mod * Copyright (C) 2016 MrCrayfish (http://www.mrcrayfish.com/) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.mrcrayfish.furniture.blocks; import java.util.List; import java.util.Random; import com.mrcrayfish.furniture.DamageSourceFence; import com.mrcrayfish.furniture.MrCrayfishFurnitureMod; import com.mrcrayfish.furniture.init.FurnitureAchievements; import com.mrcrayfish.furniture.init.FurnitureBlocks; import com.mrcrayfish.furniture.init.FurnitureSounds; import com.mrcrayfish.furniture.util.CollisionHelper; import net.minecraft.block.Block; import net.minecraft.block.BlockFenceGate; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.effect.EntityLightningBolt; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.monster.EntityCreeper; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.SoundEvents; import net.minecraft.item.ItemStack; import net.minecraft.util.DamageSource; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class BlockElectricFence extends Block { public static final PropertyBool NORTH = PropertyBool.create("north"); public static final PropertyBool EAST = PropertyBool.create("east"); public static final PropertyBool SOUTH = PropertyBool.create("south"); public static final PropertyBool WEST = PropertyBool.create("west"); protected static final AxisAlignedBB[] BOUNDING_BOX = new AxisAlignedBB[] { new AxisAlignedBB(0.4375, 0.0, 0.4375, 0.5625, 1.0, 0.5625), new AxisAlignedBB(0.4375, 0.0, 0.4375, 0.5625, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.4375, 0.5625, 1.0, 0.5625), new AxisAlignedBB(0.0, 0.0, 0.4375, 0.5625, 1.0, 1.0), new AxisAlignedBB(0.4375, 0.0, 0.0, 0.5625, 1.0, 0.5625), new AxisAlignedBB(0.4375, 0.0, 0.0, 0.5625, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.0, 0.5625, 1.0, 0.5625), new AxisAlignedBB(0.0, 0.0, 0.0, 0.5625, 1.0, 1.0), new AxisAlignedBB(0.4375, 0.0, 0.4375, 1.0, 1.0, 0.5625), new AxisAlignedBB(0.4375, 0.0, 0.4375, 1.0, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.4375, 1.0, 1.0, 0.5625), new AxisAlignedBB(0.0, 0.0, 0.4375, 1.0, 1.0, 1.0), new AxisAlignedBB(0.4375, 0.0, 0.0, 1.0, 1.0, 0.5625), new AxisAlignedBB(0.4375, 0.0, 0.0, 1.0, 1.0, 1.0), new AxisAlignedBB(0.0, 0.0, 0.0, 1.0, 1.0, 0.5625), new AxisAlignedBB(0.0, 0.0, 0.0, 1.0, 1.0, 1.0) }; private static final AxisAlignedBB COLLISION_BOX_CENTER = new AxisAlignedBB(0.4375, 0.0, 0.4375, 0.5625, 1.0, 0.5625); private static final AxisAlignedBB COLLISION_BOX_NORTH = CollisionHelper.getBlockBounds(EnumFacing.NORTH, 0.5625, 0.0, 0.4375, 1.0, 1.0, 0.5625); private static final AxisAlignedBB COLLISION_BOX_EAST = CollisionHelper.getBlockBounds(EnumFacing.EAST, 0.5625, 0.0, 0.4375, 1.0, 1.0, 0.5625); private static final AxisAlignedBB COLLISION_BOX_SOUTH = CollisionHelper.getBlockBounds(EnumFacing.SOUTH, 0.5625, 0.0, 0.4375, 1.0, 1.0, 0.5625); private static final AxisAlignedBB COLLISION_BOX_WEST = CollisionHelper.getBlockBounds(EnumFacing.WEST, 0.5625, 0.0, 0.4375, 1.0, 1.0, 0.5625); public DamageSource electricFence = new DamageSourceFence("electricFence"); public BlockElectricFence(Material material) { super(material); this.setHardness(1.0F); this.setSoundType(SoundType.ANVIL); this.setLightLevel(0.2F); this.setDefaultState(this.blockState.getBaseState().withProperty(NORTH, Boolean.valueOf(false)).withProperty(EAST, Boolean.valueOf(false)).withProperty(SOUTH, Boolean.valueOf(false)).withProperty(WEST, Boolean.valueOf(false))); this.setCreativeTab(MrCrayfishFurnitureMod.tabFurniture); } @Override public boolean isOpaqueCube(IBlockState state) { return false; } @Override public boolean isFullCube(IBlockState state) { return false; } @Override public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { ((EntityPlayer) placer).addStat(FurnitureAchievements.modernTechnology); } @Override public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) { state = state.getActualState(source, pos); return BOUNDING_BOX[getBoundingBoxId(state)]; } private static int getBoundingBoxId(IBlockState state) { int i = 0; if (((Boolean)state.getValue(NORTH)).booleanValue()) { i |= 1 << EnumFacing.NORTH.getHorizontalIndex(); } if (((Boolean)state.getValue(EAST)).booleanValue()) { i |= 1 << EnumFacing.EAST.getHorizontalIndex(); } if (((Boolean)state.getValue(SOUTH)).booleanValue()) { i |= 1 << EnumFacing.SOUTH.getHorizontalIndex(); } if (((Boolean)state.getValue(WEST)).booleanValue()) { i |= 1 << EnumFacing.WEST.getHorizontalIndex(); } return i; } @Override public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB axisAligned, List<AxisAlignedBB> axisAlignedList, Entity collidingEntity) { state = state.getActualState(worldIn, pos); if (state.getValue(NORTH)) { super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_NORTH); } if (state.getValue(EAST)) { super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_EAST); } if (state.getValue(SOUTH)) { super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_SOUTH); } if (state.getValue(WEST)) { super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_WEST); } super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, COLLISION_BOX_CENTER); } @Override public void onEntityCollidedWithBlock(World world, BlockPos pos, IBlockState state, Entity entity) { if (!(entity instanceof EntityItem) && !entity.getName().equals("unknown")) { if (entity instanceof EntityCreeper) { EntityCreeper creeper = (EntityCreeper) entity; EntityLightningBolt lightning = new EntityLightningBolt(world, pos.getX(), pos.getY(), pos.getZ(), false); if(!creeper.getPowered()) { creeper.setFire(1); creeper.onStruckByLightning(lightning); } } else if (entity instanceof EntityPlayer) { if (!((EntityPlayer) entity).capabilities.isCreativeMode) { entity.attackEntityFrom(this.electricFence, (int) 2.0F); world.playSound(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D, FurnitureSounds.zap, SoundCategory.BLOCKS, 0.2F, 1.0F, false); this.sparkle(world, pos); ((EntityPlayer) entity).addStat(FurnitureAchievements.careful); } } else { entity.attackEntityFrom(this.electricFence, (int) 2.0F); world.playSound(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D, FurnitureSounds.zap, SoundCategory.BLOCKS, 0.2F, 1.0F, false); this.sparkle(world, pos); } } } private void sparkle(World worldIn, BlockPos pos) { IBlockState state = worldIn.getBlockState(pos); double d0 = 0.0625D; for (int l = 0; l < 6; ++l) { double d1 = (pos.getX() + RANDOM.nextFloat()); double d2 = (pos.getY() + RANDOM.nextFloat()); double d3 = (pos.getZ() + RANDOM.nextFloat()); if (l == 0 && !worldIn.getBlockState(pos.up()).getBlock().isOpaqueCube(state)) { d2 = (pos.getY() + 1) + d0; } if (l == 1 && !worldIn.getBlockState(pos.down()).getBlock().isOpaqueCube(state)) { d2 = (pos.getY() + 0) - d0; } if (l == 2 && !worldIn.getBlockState(pos.south()).getBlock().isOpaqueCube(state)) { d3 = (pos.getZ() + 1) + d0; } if (l == 3 && !worldIn.getBlockState(pos.north()).getBlock().isOpaqueCube(state)) { d3 = (pos.getZ() + 0) - d0; } if (l == 4 && !worldIn.getBlockState(pos.east()).getBlock().isOpaqueCube(state)) { d1 = (pos.getX() + 1) + d0; } if (l == 5 && !worldIn.getBlockState(pos.west()).getBlock().isOpaqueCube(state)) { d1 = (pos.getX() + 0) - d0; } if (d1 < pos.getX() || d1 > (pos.getX() + 1) || d2 < 0.0D || d2 > (pos.getY() + 1) || d3 < pos.getZ() || d3 > (pos.getZ() + 1)) { worldIn.spawnParticle(EnumParticleTypes.REDSTONE, d1, d2, d3, 0.0D, 0.0D, 0.0D); } } } @Override public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) { return state.withProperty(NORTH, Boolean.valueOf(isFence(world, pos.north()))).withProperty(EAST, Boolean.valueOf(isFence(world, pos.east()))).withProperty(SOUTH, Boolean.valueOf(isFence(world, pos.south()))).withProperty(WEST, Boolean.valueOf(isFence(world, pos.west()))); } @Override public int getMetaFromState(IBlockState state) { return 0; } @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] { NORTH, EAST, SOUTH, WEST }); } public boolean isFence(IBlockAccess world, BlockPos pos) { return world.getBlockState(pos).getBlock() == this; } }