public class ElectricFurnaceAdv extends BlockContainerTomsMod { public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public static final PropertyBool ACTIVE = PropertyBool.create("active"); public ElectricFurnaceAdv() { super(Material.IRON); } @Override public TileEntity createNewTileEntity(World worldIn, int meta) { return new TileEntityElectricFurnaceAdv(); } @Override public IBlockState onBlockPlaced( World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { EnumFacing f = TomsModUtils.getDirectionFacing(placer, false); return this.getDefaultState().withProperty(FACING, f.getOpposite()).withProperty(ACTIVE, false); } @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {FACING, ACTIVE}); } /** Convert the given metadata into a BlockState for this Block */ @Override public IBlockState getStateFromMeta(int meta) { EnumFacing enumfacing = EnumFacing.getFront(meta % 6); if (enumfacing.getAxis() == EnumFacing.Axis.Y) { enumfacing = EnumFacing.NORTH; } // System.out.println("getState"); return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(ACTIVE, meta > 5); } /** Convert the BlockState into the correct metadata value */ @Override public int getMetaFromState(IBlockState state) { // System.out.println("getMeta"); return state.getValue(FACING).getIndex() + (state.getValue(ACTIVE) ? 6 : 0); } @Override public boolean onBlockActivated( World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { if (!worldIn.isRemote) playerIn.openGui( CoreInit.modInstance, GuiIDs.electricFurnaceAdv.ordinal(), worldIn, pos.getX(), pos.getY(), pos.getZ()); return true; } }
public class BlockCircuitDesignTable extends Block implements ITileEntityProvider { public static Block instance; public BlockCircuitDesignTable(Material par2Material) { super(par2Material); } @Override public int getMetaFromState(IBlockState state) { return state.getValue(FACING).getIndex(); } @Override public IBlockState getStateFromMeta(int meta) { return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta)); } // It's not an opaque cube, so you need this. @Override public boolean isOpaqueCube(IBlockState state) { return false; } @Override public boolean shouldSideBeRendered( IBlockState state, IBlockAccess access, BlockPos pos, EnumFacing side) { return false; } public static final PropertyDirection FACING = PropertyDirection.create("facing"); @Override public BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {FACING}); } @Override public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack stack) { int l = MathHelper.floor_double((double) (player.rotationYaw * 4.0F / 360.0F) + 2.5D) & 3; world.setBlockState(pos, getDefaultState().withProperty(FACING, EnumFacing.fromAngle(90 * l))); } @Override public boolean onBlockActivated( World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack held, EnumFacing facing, float fx, float par8, float par9) { System.out.println("Im awake!"); if (world.isRemote == false) { player.openGui(LabStuffMain.instance, 0, world, pos.getX(), pos.getY(), pos.getZ()); return true; } return false; } @Override public TileEntity createNewTileEntity(World var1, int var2) { // TODO Auto-generated method stub return new TileEntityCircuitDesignTable(); } }
public class GenericBlock extends Block implements WailaProvider, IOrientedBlock { public static final PropertyDirection FACING_HORIZ = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public static final PropertyDirection FACING = PropertyDirection.create("facing"); public enum MetaUsage { HORIZROTATION, ROTATION, NONE } public MetaUsage getMetaUsage() { return MetaUsage.HORIZROTATION; } public GenericBlock(Material material, String name) { this(material, name, null, null); } public GenericBlock(Material material, String name, Class<? extends GenericTE> clazz) { this(material, name, clazz, null); } public GenericBlock( Material material, String name, Class<? extends GenericTE> clazz, Class<? extends ItemBlock> itemBlockClass) { super(material); // setDefaultState(this.blockState.getBaseState().withProperty(FACING_HORIZ, // EnumFacing.NORTH)); this.setCreativeTab(AquaMunda.creativeTab); register(name, clazz, itemBlockClass); } protected void register( String name, Class<? extends GenericTE> clazz, Class<? extends ItemBlock> itemBlockClass) { setRegistryName(name); setUnlocalizedName(name); if (itemBlockClass == null) { GameRegistry.registerBlock(this); } else { GameRegistry.registerBlock(this, itemBlockClass); } } @SideOnly(Side.CLIENT) public void initModel() { ModelLoader.setCustomModelResourceLocation( Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory")); } @Override public List<String> getWailaBody( ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) { return currenttip; } @Override public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack itemStack) { switch (getMetaUsage()) { case HORIZROTATION: world.setBlockState( pos, state.withProperty(FACING_HORIZ, placer.getHorizontalFacing().getOpposite()), 2); break; case ROTATION: world.setBlockState(pos, state.withProperty(FACING, getFacingFromEntity(pos, placer)), 2); break; case NONE: break; } } public static EnumFacing getFacingFromEntity(BlockPos clickedBlock, EntityLivingBase entityIn) { if (MathHelper.abs((float) entityIn.posX - clickedBlock.getX()) < 2.0F && MathHelper.abs((float) entityIn.posZ - clickedBlock.getZ()) < 2.0F) { double d0 = entityIn.posY + entityIn.getEyeHeight(); if (d0 - clickedBlock.getY() > 2.0D) { return EnumFacing.UP; } if (clickedBlock.getY() - d0 > 0.0D) { return EnumFacing.DOWN; } } return entityIn.getHorizontalFacing().getOpposite(); } @Override public EnumFacing getFrontDirection(IBlockState state) { switch (getMetaUsage()) { case HORIZROTATION: return state.getValue(FACING_HORIZ); case ROTATION: return state.getValue(FACING); case NONE: return EnumFacing.NORTH; } return EnumFacing.NORTH; } public EnumFacing getRightDirection(IBlockState state) { return getFrontDirection(state).rotateYCCW(); } public EnumFacing getLeftDirection(IBlockState state) { return getFrontDirection(state).rotateY(); } public static EnumFacing getFrontDirection(MetaUsage metaUsage, IBlockState state) { EnumFacing orientation; switch (metaUsage) { case HORIZROTATION: orientation = BlockTools.getOrientationHoriz(state); break; case ROTATION: orientation = BlockTools.getOrientation(state); break; case NONE: default: orientation = EnumFacing.SOUTH; break; } return orientation; } protected EnumFacing getOrientation(int x, int y, int z, EntityLivingBase entityLivingBase) { switch (getMetaUsage()) { case HORIZROTATION: return BlockTools.determineOrientationHoriz(entityLivingBase); case ROTATION: return BlockTools.determineOrientation(x, y, z, entityLivingBase); } return null; } @Override public EnumFacing worldToBlockSpace(World world, BlockPos pos, EnumFacing side) { switch (getMetaUsage()) { case HORIZROTATION: return BlockTools.worldToBlockSpaceHoriz(side, world.getBlockState(pos)); case ROTATION: return BlockTools.worldToBlockSpace(side, world.getBlockState(pos)); case NONE: default: return side; } } public Vector blockToWorldSpace(World world, BlockPos pos, Vector v) { switch (getMetaUsage()) { case HORIZROTATION: return BlockTools.blockToWorldSpaceHoriz(v, world.getBlockState(pos)); case ROTATION: return BlockTools.blockToWorldSpace(v, world.getBlockState(pos)); case NONE: default: return v; } } @Override public IBlockState getStateFromMeta(int meta) { switch (getMetaUsage()) { case HORIZROTATION: return getDefaultState().withProperty(FACING_HORIZ, getFacingHoriz(meta)); case ROTATION: return getDefaultState().withProperty(FACING, getFacing(meta)); case NONE: return getDefaultState(); } return getDefaultState(); } public static EnumFacing getFacingHoriz(int meta) { return EnumFacing.values()[meta + 2]; } public static EnumFacing getFacing(int meta) { return EnumFacing.values()[meta & 7]; } @Override public int getMetaFromState(IBlockState state) { switch (getMetaUsage()) { case HORIZROTATION: return state.getValue(FACING_HORIZ).getIndex() - 2; case ROTATION: return state.getValue(FACING).getIndex(); case NONE: return 0; } return 0; } @Override protected BlockState createBlockState() { switch (getMetaUsage()) { case HORIZROTATION: return new BlockState(this, FACING_HORIZ); case ROTATION: return new BlockState(this, FACING); case NONE: return super.createBlockState(); } return super.createBlockState(); } }
public class BlockCargoLoader extends BlockAdvancedTile implements IShiftDescription, ISortableBlock { /*private IIcon iconMachineSide; private IIcon iconInput; private IIcon iconFrontLoader; private IIcon iconFrontUnloader; private IIcon iconItemInput; private IIcon iconItemOutput;*/ private enum EnumLoaderType implements IStringSerializable { CARGO_LOADER(0, "cargo_loader"), CARGO_UNLOADER(1, "cargo_unloader"); private final int meta; private final String name; private EnumLoaderType(int meta, String name) { this.meta = meta; this.name = name; } public int getMeta() { return this.meta; } public static EnumLoaderType byMetadata(int meta) { return values()[meta]; } public static EnumLoaderType byIndex(int index) { return values()[index]; } @Override public String getName() { return this.name; } } public static final PropertyEnum TYPE = PropertyEnum.create("type", EnumLoaderType.class); public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public static final int METADATA_CARGO_LOADER = 0; public static final int METADATA_CARGO_UNLOADER = 4; public BlockCargoLoader(String assetName) { super(Material.rock); this.setHardness(1.0F); this.setStepSound(Block.soundTypeMetal); // this.setBlockTextureName(GalacticraftCore.TEXTURE_PREFIX + assetName); this.setUnlocalizedName(assetName); } @Override public int getRenderType() { return GalacticraftCore.proxy.getBlockRender(this); } @SideOnly(Side.CLIENT) @Override public void getSubBlocks(Item par1, CreativeTabs par2CreativeTabs, List<ItemStack> par3List) { par3List.add(new ItemStack(par1, 1, BlockCargoLoader.METADATA_CARGO_LOADER)); par3List.add(new ItemStack(par1, 1, BlockCargoLoader.METADATA_CARGO_UNLOADER)); } @Override public CreativeTabs getCreativeTabToDisplayOn() { return GalacticraftCore.galacticraftBlocksTab; } @Override public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) { super.onBlockAdded(worldIn, pos, state); TileEntity tileEntity = worldIn.getTileEntity(pos); if (tileEntity != null) { if (tileEntity instanceof TileEntityCargoLoader) { ((TileEntityCargoLoader) tileEntity).checkForCargoEntity(); } else if (tileEntity instanceof TileEntityCargoUnloader) { ((TileEntityCargoUnloader) tileEntity).checkForCargoEntity(); } } } /*@Override @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister par1IconRegister) { this.iconInput = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "machine_input"); this.iconMachineSide = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "machine_blank"); this.iconFrontLoader = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "machine_cargoloader"); this.iconFrontUnloader = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "machine_cargounloader"); this.iconItemInput = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "machine_item_input"); this.iconItemOutput = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "machine_item_output"); }*/ @Override public boolean onMachineActivated( World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { playerIn.openGui(GalacticraftCore.instance, -1, worldIn, pos.getX(), pos.getY(), pos.getZ()); return true; } /*@Override public IIcon getIcon(int side, int metadata) { int shiftedMeta = metadata; if (side == 0 || side == 1) { return this.iconMachineSide; } if (metadata >= BlockCargoLoader.METADATA_CARGO_UNLOADER) { shiftedMeta -= BlockCargoLoader.METADATA_CARGO_UNLOADER; if (side == shiftedMeta + 2) { return this.iconInput; } else if (side == ForgeDirection.getOrientation(shiftedMeta + 2).getOpposite().ordinal()) { return metadata < 4 ? this.iconItemInput : this.iconItemOutput; } else { return metadata < 4 ? this.iconFrontLoader : this.iconFrontUnloader; } } else if (metadata >= BlockCargoLoader.METADATA_CARGO_LOADER) { shiftedMeta -= BlockCargoLoader.METADATA_CARGO_LOADER; if (side == shiftedMeta + 2) { return this.iconInput; } else if (side == ForgeDirection.getOrientation(shiftedMeta + 2).getOpposite().ordinal()) { return metadata < 4 ? this.iconItemInput : this.iconItemOutput; } else { return metadata < 4 ? this.iconFrontLoader : this.iconFrontUnloader; } } return this.iconMachineSide; }*/ @Override public TileEntity createTileEntity(World world, IBlockState state) { if (getMetaFromState(state) < BlockCargoLoader.METADATA_CARGO_UNLOADER) { return new TileEntityCargoLoader(); } else { return new TileEntityCargoUnloader(); } } @Override public boolean onUseWrench( World world, BlockPos pos, EntityPlayer entityPlayer, EnumFacing side, float hitX, float hitY, float hitZ) { int metadata = getMetaFromState(world.getBlockState(pos)); int change = world.getBlockState(pos).getValue(FACING).rotateY().getHorizontalIndex(); world.setBlockState(pos, this.getStateFromMeta(metadata - (metadata % 4) + change), 3); TileEntity te = world.getTileEntity(pos); if (te instanceof TileBaseUniversalElectrical) { ((TileBaseUniversalElectrical) te).updateFacing(); } return true; } @Override public void onBlockPlacedBy( World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { final int angle = MathHelper.floor_double(placer.rotationYaw * 4.0F / 360.0F + 0.5D) & 3; int change = EnumFacing.getHorizontal(angle).getOpposite().getHorizontalIndex(); if (stack.getItemDamage() >= METADATA_CARGO_UNLOADER) { change += METADATA_CARGO_UNLOADER; } else if (stack.getItemDamage() >= METADATA_CARGO_LOADER) { change += METADATA_CARGO_LOADER; } worldIn.setBlockState(pos, getStateFromMeta(change), 3); for (int dX = -2; dX < 3; dX++) { for (int dZ = -2; dZ < 3; dZ++) { final Block block = worldIn.getBlockState(pos.add(dX, 0, dZ)).getBlock(); if (block == GCBlocks.landingPadFull) { worldIn.markBlockForUpdate(pos.add(dX, 0, dZ)); } } } } @Override public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state) { super.onBlockDestroyedByPlayer(worldIn, pos, state); for (int dX = -2; dX < 3; dX++) { for (int dZ = -2; dZ < 3; dZ++) { BlockPos newPos = new BlockPos(pos.getX() + dX, pos.getY(), pos.getZ() + dZ); final Block block = worldIn.getBlockState(newPos).getBlock(); if (block == GCBlocks.landingPadFull) { worldIn.markBlockForUpdate(newPos); } } } } @Override public int damageDropped(IBlockState state) { return getMetaFromState(state); } @Override public String getShiftDescription(int meta) { switch (meta) { case METADATA_CARGO_LOADER: return GCCoreUtil.translate("tile.cargo_loader.description"); case METADATA_CARGO_UNLOADER: return GCCoreUtil.translate("tile.cargo_unloader.description"); } return ""; } @Override public boolean showDescription(int meta) { return true; } public IBlockState getStateFromMeta(int meta) { EnumFacing enumfacing = EnumFacing.getHorizontal(meta % 4); EnumLoaderType type = EnumLoaderType.byMetadata((int) Math.floor(meta / 4.0)); return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(TYPE, type); } public int getMetaFromState(IBlockState state) { return ((EnumFacing) state.getValue(FACING)).getHorizontalIndex() + ((EnumLoaderType) state.getValue(TYPE)).getMeta() * 4; } protected BlockState createBlockState() { return new BlockState(this, FACING, TYPE); } @Override public EnumSortCategoryBlock getCategory(int meta) { return EnumSortCategoryBlock.MACHINE; } }
public class BlockPistonBase extends Block { public static final PropertyDirection FACING = PropertyDirection.create("facing"); public static final PropertyBool EXTENDED = PropertyBool.create("extended"); /** This piston is the sticky one? */ private final boolean isSticky; public BlockPistonBase(boolean isSticky) { super(Material.piston); this.setDefaultState( this.blockState .getBaseState() .withProperty(FACING, EnumFacing.NORTH) .withProperty(EXTENDED, Boolean.valueOf(false))); this.isSticky = isSticky; this.setStepSound(soundTypePiston); this.setHardness(0.5F); this.setCreativeTab(CreativeTabs.tabRedstone); } /** Used to determine ambient occlusion and culling when rebuilding chunks for render */ public boolean isOpaqueCube() { return false; } /** Called by ItemBlocks after a block is set in the world, to allow post-place logic */ public void onBlockPlacedBy( World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { worldIn.setBlockState( pos, state.withProperty(FACING, getFacingFromEntity(worldIn, pos, placer)), 2); if (!worldIn.isRemote) { this.checkForMove(worldIn, pos, state); } } /** Called when a neighboring block changes. */ public void onNeighborBlockChange( World worldIn, BlockPos pos, IBlockState state, Block neighborBlock) { if (!worldIn.isRemote) { this.checkForMove(worldIn, pos, state); } } public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) { if (!worldIn.isRemote && worldIn.getTileEntity(pos) == null) { this.checkForMove(worldIn, pos, state); } } /** * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments * to the IBlockstate */ public IBlockState onBlockPlaced( World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return this.getDefaultState() .withProperty(FACING, getFacingFromEntity(worldIn, pos, placer)) .withProperty(EXTENDED, Boolean.valueOf(false)); } private void checkForMove(World worldIn, BlockPos pos, IBlockState state) { EnumFacing enumfacing = (EnumFacing) state.getValue(FACING); boolean flag = this.shouldBeExtended(worldIn, pos, enumfacing); if (flag && !((Boolean) state.getValue(EXTENDED)).booleanValue()) { if ((new BlockPistonStructureHelper(worldIn, pos, enumfacing, true)).canMove()) { worldIn.addBlockEvent(pos, this, 0, enumfacing.getIndex()); } } else if (!flag && ((Boolean) state.getValue(EXTENDED)).booleanValue()) { worldIn.setBlockState(pos, state.withProperty(EXTENDED, Boolean.valueOf(false)), 2); worldIn.addBlockEvent(pos, this, 1, enumfacing.getIndex()); } } private boolean shouldBeExtended(World worldIn, BlockPos pos, EnumFacing facing) { for (EnumFacing enumfacing : EnumFacing.values()) { if (enumfacing != facing && worldIn.isSidePowered(pos.offset(enumfacing), enumfacing)) { return true; } } if (worldIn.isSidePowered(pos, EnumFacing.DOWN)) { return true; } else { BlockPos blockpos = pos.up(); for (EnumFacing enumfacing1 : EnumFacing.values()) { if (enumfacing1 != EnumFacing.DOWN && worldIn.isSidePowered(blockpos.offset(enumfacing1), enumfacing1)) { return true; } } return false; } } /** Called on both Client and Server when World#addBlockEvent is called */ public boolean onBlockEventReceived( World worldIn, BlockPos pos, IBlockState state, int eventID, int eventParam) { EnumFacing enumfacing = (EnumFacing) state.getValue(FACING); if (!worldIn.isRemote) { boolean flag = this.shouldBeExtended(worldIn, pos, enumfacing); if (flag && eventID == 1) { worldIn.setBlockState(pos, state.withProperty(EXTENDED, Boolean.valueOf(true)), 2); return false; } if (!flag && eventID == 0) { return false; } } if (eventID == 0) { if (!this.doMove(worldIn, pos, enumfacing, true)) { return false; } worldIn.setBlockState(pos, state.withProperty(EXTENDED, Boolean.valueOf(true)), 2); worldIn.playSoundEffect( (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, "tile.piston.out", 0.5F, worldIn.rand.nextFloat() * 0.25F + 0.6F); } else if (eventID == 1) { TileEntity tileentity1 = worldIn.getTileEntity(pos.offset(enumfacing)); if (tileentity1 instanceof TileEntityPiston) { ((TileEntityPiston) tileentity1).clearPistonTileEntity(); } worldIn.setBlockState( pos, Blocks.piston_extension .getDefaultState() .withProperty(BlockPistonMoving.FACING, enumfacing) .withProperty( BlockPistonMoving.TYPE, this.isSticky ? BlockPistonExtension.EnumPistonType.STICKY : BlockPistonExtension.EnumPistonType.DEFAULT), 3); worldIn.setTileEntity( pos, BlockPistonMoving.newTileEntity( this.getStateFromMeta(eventParam), enumfacing, false, true)); if (this.isSticky) { BlockPos blockpos = pos.add( enumfacing.getFrontOffsetX() * 2, enumfacing.getFrontOffsetY() * 2, enumfacing.getFrontOffsetZ() * 2); Block block = worldIn.getBlockState(blockpos).getBlock(); boolean flag1 = false; if (block == Blocks.piston_extension) { TileEntity tileentity = worldIn.getTileEntity(blockpos); if (tileentity instanceof TileEntityPiston) { TileEntityPiston tileentitypiston = (TileEntityPiston) tileentity; if (tileentitypiston.getFacing() == enumfacing && tileentitypiston.isExtending()) { tileentitypiston.clearPistonTileEntity(); flag1 = true; } } } if (!flag1 && block.getMaterial() != Material.air && canPush(block, worldIn, blockpos, enumfacing.getOpposite(), false) && (block.getMobilityFlag() == 0 || block == Blocks.piston || block == Blocks.sticky_piston)) { this.doMove(worldIn, pos, enumfacing, false); } } else { worldIn.setBlockToAir(pos.offset(enumfacing)); } worldIn.playSoundEffect( (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, "tile.piston.in", 0.5F, worldIn.rand.nextFloat() * 0.15F + 0.6F); } return true; } public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos) { IBlockState iblockstate = worldIn.getBlockState(pos); if (iblockstate.getBlock() == this && ((Boolean) iblockstate.getValue(EXTENDED)).booleanValue()) { float f = 0.25F; EnumFacing enumfacing = (EnumFacing) iblockstate.getValue(FACING); if (enumfacing != null) { switch (enumfacing) { case DOWN: this.setBlockBounds(0.0F, 0.25F, 0.0F, 1.0F, 1.0F, 1.0F); break; case UP: this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.75F, 1.0F); break; case NORTH: this.setBlockBounds(0.0F, 0.0F, 0.25F, 1.0F, 1.0F, 1.0F); break; case SOUTH: this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.75F); break; case WEST: this.setBlockBounds(0.25F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F); break; case EAST: this.setBlockBounds(0.0F, 0.0F, 0.0F, 0.75F, 1.0F, 1.0F); } } } else { this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F); } } /** Sets the block's bounds for rendering it as an item */ public void setBlockBoundsForItemRender() { this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F); } /** Add all collision boxes of this Block to the list that intersect with the given mask. */ public void addCollisionBoxesToList( World worldIn, BlockPos pos, IBlockState state, AxisAlignedBB mask, List<AxisAlignedBB> list, Entity collidingEntity) { this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F); super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity); } public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state) { this.setBlockBoundsBasedOnState(worldIn, pos); return super.getCollisionBoundingBox(worldIn, pos, state); } public boolean isFullCube() { return false; } public static EnumFacing getFacing(int meta) { int i = meta & 7; return i > 5 ? null : EnumFacing.getFront(i); } public static EnumFacing getFacingFromEntity( World worldIn, BlockPos clickedBlock, EntityLivingBase entityIn) { if (MathHelper.abs((float) entityIn.posX - (float) clickedBlock.getX()) < 2.0F && MathHelper.abs((float) entityIn.posZ - (float) clickedBlock.getZ()) < 2.0F) { double d0 = entityIn.posY + (double) entityIn.getEyeHeight(); if (d0 - (double) clickedBlock.getY() > 2.0D) { return EnumFacing.UP; } if ((double) clickedBlock.getY() - d0 > 0.0D) { return EnumFacing.DOWN; } } return entityIn.getHorizontalFacing().getOpposite(); } public static boolean canPush( Block blockIn, World worldIn, BlockPos pos, EnumFacing direction, boolean allowDestroy) { if (blockIn == Blocks.obsidian) { return false; } else if (!worldIn.getWorldBorder().contains(pos)) { return false; } else if (pos.getY() >= 0 && (direction != EnumFacing.DOWN || pos.getY() != 0)) { if (pos.getY() <= worldIn.getHeight() - 1 && (direction != EnumFacing.UP || pos.getY() != worldIn.getHeight() - 1)) { if (blockIn != Blocks.piston && blockIn != Blocks.sticky_piston) { if (blockIn.getBlockHardness(worldIn, pos) == -1.0F) { return false; } if (blockIn.getMobilityFlag() == 2) { return false; } if (blockIn.getMobilityFlag() == 1) { if (!allowDestroy) { return false; } return true; } } else if (((Boolean) worldIn.getBlockState(pos).getValue(EXTENDED)).booleanValue()) { return false; } return !(blockIn instanceof ITileEntityProvider); } else { return false; } } else { return false; } } private boolean doMove(World worldIn, BlockPos pos, EnumFacing direction, boolean extending) { if (!extending) { worldIn.setBlockToAir(pos.offset(direction)); } BlockPistonStructureHelper blockpistonstructurehelper = new BlockPistonStructureHelper(worldIn, pos, direction, extending); List<BlockPos> list = blockpistonstructurehelper.getBlocksToMove(); List<BlockPos> list1 = blockpistonstructurehelper.getBlocksToDestroy(); if (!blockpistonstructurehelper.canMove()) { return false; } else { int i = list.size() + list1.size(); Block[] ablock = new Block[i]; EnumFacing enumfacing = extending ? direction : direction.getOpposite(); for (int j = list1.size() - 1; j >= 0; --j) { BlockPos blockpos = (BlockPos) list1.get(j); Block block = worldIn.getBlockState(blockpos).getBlock(); block.dropBlockAsItem(worldIn, blockpos, worldIn.getBlockState(blockpos), 0); worldIn.setBlockToAir(blockpos); --i; ablock[i] = block; } for (int k = list.size() - 1; k >= 0; --k) { BlockPos blockpos2 = (BlockPos) list.get(k); IBlockState iblockstate = worldIn.getBlockState(blockpos2); Block block1 = iblockstate.getBlock(); block1.getMetaFromState(iblockstate); worldIn.setBlockToAir(blockpos2); blockpos2 = blockpos2.offset(enumfacing); worldIn.setBlockState( blockpos2, Blocks.piston_extension.getDefaultState().withProperty(FACING, direction), 4); worldIn.setTileEntity( blockpos2, BlockPistonMoving.newTileEntity(iblockstate, direction, extending, false)); --i; ablock[i] = block1; } BlockPos blockpos1 = pos.offset(direction); if (extending) { BlockPistonExtension.EnumPistonType blockpistonextension$enumpistontype = this.isSticky ? BlockPistonExtension.EnumPistonType.STICKY : BlockPistonExtension.EnumPistonType.DEFAULT; IBlockState iblockstate1 = Blocks.piston_head .getDefaultState() .withProperty(BlockPistonExtension.FACING, direction) .withProperty(BlockPistonExtension.TYPE, blockpistonextension$enumpistontype); IBlockState iblockstate2 = Blocks.piston_extension .getDefaultState() .withProperty(BlockPistonMoving.FACING, direction) .withProperty( BlockPistonMoving.TYPE, this.isSticky ? BlockPistonExtension.EnumPistonType.STICKY : BlockPistonExtension.EnumPistonType.DEFAULT); worldIn.setBlockState(blockpos1, iblockstate2, 4); worldIn.setTileEntity( blockpos1, BlockPistonMoving.newTileEntity(iblockstate1, direction, true, false)); } for (int l = list1.size() - 1; l >= 0; --l) { worldIn.notifyNeighborsOfStateChange((BlockPos) list1.get(l), ablock[i++]); } for (int i1 = list.size() - 1; i1 >= 0; --i1) { worldIn.notifyNeighborsOfStateChange((BlockPos) list.get(i1), ablock[i++]); } if (extending) { worldIn.notifyNeighborsOfStateChange(blockpos1, Blocks.piston_head); worldIn.notifyNeighborsOfStateChange(pos, this); } return true; } } /** * Possibly modify the given BlockState before rendering it on an Entity (Minecarts, Endermen, * ...) */ public IBlockState getStateForEntityRender(IBlockState state) { return this.getDefaultState().withProperty(FACING, EnumFacing.UP); } /** Convert the given metadata into a BlockState for this Block */ public IBlockState getStateFromMeta(int meta) { return this.getDefaultState() .withProperty(FACING, getFacing(meta)) .withProperty(EXTENDED, Boolean.valueOf((meta & 8) > 0)); } /** Convert the BlockState into the correct metadata value */ public int getMetaFromState(IBlockState state) { int i = 0; i = i | ((EnumFacing) state.getValue(FACING)).getIndex(); if (((Boolean) state.getValue(EXTENDED)).booleanValue()) { i |= 8; } return i; } protected BlockState createBlockState() { return new BlockState(this, new IProperty[] {FACING, EXTENDED}); } }
public class BlockSmelteryIO extends BlockEnumSmeltery<BlockSmelteryIO.IOType> { public static final PropertyEnum<IOType> TYPE = PropertyEnum.create("type", IOType.class); public static PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public BlockSmelteryIO() { super(TYPE, IOType.class); } @Override protected BlockState createBlockState() { return new BlockState(this, TYPE, FACING); } @Override public IBlockState getStateFromMeta(int meta) { int horIndex = (meta >> 2) & 0xF; return this.getDefaultState() .withProperty(prop, fromMeta(meta)) .withProperty(FACING, EnumFacing.HORIZONTALS[horIndex]); } @Override public int getMetaFromState(IBlockState state) { // 4 direction states -> upper 2 bit for rotation return state.getValue(prop).getMeta() | (state.getValue(FACING).getHorizontalIndex() << 2); } @Override public TileEntity createNewTileEntity(World worldIn, int meta) { return new TileDrain(); } @Override public IBlockState onBlockPlaced( World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { EnumFacing side = facing; if (side == EnumFacing.UP || side == EnumFacing.DOWN) { side = placer.getHorizontalFacing().getOpposite(); } // set rotation return this.getDefaultState().withProperty(FACING, side); } @Override public boolean onBlockActivated( World worldIn, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) { // we allow to insert buckets into the smeltery TileEntity te = worldIn.getTileEntity(pos); if (!(te instanceof IFluidHandler)) { return false; } IFluidHandler tank = (IFluidHandler) te; side = side.getOpposite(); ItemStack stack = player.getHeldItem(); if (stack == null) { return false; } // regular bucket ItemStack result = FluidUtil.tryEmptyBucket(stack, tank, side); if (result != null) { if (!player.capabilities.isCreativeMode) { player.inventory.decrStackSize(player.inventory.currentItem, 1); PlayerHelper.spawnItemAtPlayer(player, result); } return true; } // universal bucket ItemStack copy = stack.copy(); if (FluidUtil.tryEmptyFluidContainerItem(stack, tank, side, player)) { if (player.capabilities.isCreativeMode) { // reset the stack that got modified player.inventory.setInventorySlotContents(player.inventory.currentItem, copy); } return true; } // prevent interaction of the item if it's a fluidcontainer. Prevents placing liquids when // interacting with the tank return FluidContainerRegistry.isFilledContainer(stack) || stack.getItem() instanceof IFluidContainerItem; } // at most 4 public enum IOType implements IStringSerializable, EnumBlock.IEnumMeta { DRAIN; public final int meta; IOType() { meta = ordinal(); } @Override public String getName() { return this.toString(); } @Override public int getMeta() { return meta; } } }
public class BlockConveyer extends BlockContainer { public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public TileEntityConveyer conveyerEntity; public BlockConveyer(String unlocalizedName) { super(Material.wood); this.setUnlocalizedName(unlocalizedName); this.setHardness(1.0F); this.setStepSound(Block.soundTypeWood); this.setCreativeTab(MTTCMod.mttcTab); } @Override public boolean onBlockActivated( World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { return true; } @Override public void onBlockPlacedBy( World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { worldIn.setBlockState( pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2); } @Override public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) { this.setDefaultFacing(worldIn, pos, state); } private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) { if (!worldIn.isRemote) { Block block = worldIn.getBlockState(pos.north()).getBlock(); Block block1 = worldIn.getBlockState(pos.south()).getBlock(); Block block2 = worldIn.getBlockState(pos.west()).getBlock(); Block block3 = worldIn.getBlockState(pos.east()).getBlock(); EnumFacing enumfacing = (EnumFacing) state.getValue(FACING); if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock()) { enumfacing = EnumFacing.SOUTH; } else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock()) { enumfacing = EnumFacing.NORTH; } else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock()) { enumfacing = EnumFacing.EAST; } else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock()) { enumfacing = EnumFacing.WEST; } worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2); } } @Override public void breakBlock(World worldIn, BlockPos pos, IBlockState state) { dropItems(worldIn, pos); super.breakBlock(worldIn, pos, state); } @Override public Item getItemDropped(IBlockState state, Random rand, int fortune) { return null; } private void dropItems(World worldIn, BlockPos pos) { Random rand = new Random(); TileEntity tileEntity = worldIn.getTileEntity(pos); if (!(tileEntity instanceof IInventory)) { return; } IInventory inventory = (IInventory) tileEntity; for (int i = 0; i < inventory.getSizeInventory(); i++) { ItemStack item = inventory.getStackInSlot(i); if (item != null && item.stackSize > 0) { float rx = rand.nextFloat() * 0.8F + 0.1F; float ry = rand.nextFloat() * 0.8F + 0.1F; float rz = rand.nextFloat() * 0.8F + 0.1F; EntityItem entityItem = new EntityItem( worldIn, pos.getX() + rx, pos.getY() + ry, pos.getZ() + rz, new ItemStack(item.getItem(), item.stackSize, item.getItemDamage())); if (item.hasTagCompound()) { entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy()); } float factor = 0.05F; entityItem.motionX = rand.nextGaussian() * factor; entityItem.motionY = rand.nextGaussian() * factor + 0.2F; entityItem.motionZ = rand.nextGaussian() * factor; worldIn.spawnEntityInWorld(entityItem); item.stackSize = 0; } } } @Override public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) { // ItemStack stack = new ItemStack(this); // OreDictionary.registerOre("barrelBlock", stack); // ThaumcraftApi.objectTags.put(Arrays.asList(stack.getItem(),0), new // AspectList().add(Aspect.FIRE, 6).add(Aspect.CRYSTAL, 2)); conveyerEntity = new TileEntityConveyer(); return conveyerEntity; } }
public abstract class BlockButton extends Block { public static final PropertyDirection FACING = PropertyDirection.create("facing"); public static final PropertyBool POWERED = PropertyBool.create("powered"); private final boolean wooden; protected BlockButton(boolean wooden) { super(Material.circuits); this.setDefaultState( this.blockState .getBaseState() .withProperty(FACING, EnumFacing.NORTH) .withProperty(POWERED, Boolean.valueOf(false))); this.setTickRandomly(true); this.setCreativeTab(CreativeTabs.tabRedstone); this.wooden = wooden; } public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state) { return null; } /** How many world ticks before ticking */ public int tickRate(World worldIn) { return this.wooden ? 30 : 20; } /** Used to determine ambient occlusion and culling when rebuilding chunks for render */ public boolean isOpaqueCube() { return false; } public boolean isFullCube() { return false; } /** Check whether this Block can be placed on the given side */ public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side) { return func_181088_a(worldIn, pos, side.getOpposite()); } public boolean canPlaceBlockAt(World worldIn, BlockPos pos) { for (EnumFacing enumfacing : EnumFacing.values()) { if (func_181088_a(worldIn, pos, enumfacing)) { return true; } } return false; } protected static boolean func_181088_a( World p_181088_0_, BlockPos p_181088_1_, EnumFacing p_181088_2_) { return p_181088_2_ == EnumFacing.DOWN && World.doesBlockHaveSolidTopSurface(p_181088_0_, p_181088_1_.down()) ? true : p_181088_0_.isSideSolid(p_181088_1_.offset(p_181088_2_), p_181088_2_.getOpposite()); } /** * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments * to the IBlockstate */ public IBlockState onBlockPlaced( World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return func_181088_a(worldIn, pos, facing.getOpposite()) ? this.getDefaultState() .withProperty(FACING, facing) .withProperty(POWERED, Boolean.valueOf(false)) : this.getDefaultState() .withProperty(FACING, EnumFacing.DOWN) .withProperty(POWERED, Boolean.valueOf(false)); } /** Called when a neighboring block changes. */ public void onNeighborBlockChange( World worldIn, BlockPos pos, IBlockState state, Block neighborBlock) { if (this.checkForDrop(worldIn, pos, state) && !func_181088_a(worldIn, pos, ((EnumFacing) state.getValue(FACING)).getOpposite())) { this.dropBlockAsItem(worldIn, pos, state, 0); worldIn.setBlockToAir(pos); } } private boolean checkForDrop(World worldIn, BlockPos pos, IBlockState state) { if (this.canPlaceBlockAt(worldIn, pos)) { return true; } else { this.dropBlockAsItem(worldIn, pos, state, 0); worldIn.setBlockToAir(pos); return false; } } public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos) { this.updateBlockBounds(worldIn.getBlockState(pos)); } private void updateBlockBounds(IBlockState state) { EnumFacing enumfacing = (EnumFacing) state.getValue(FACING); boolean flag = ((Boolean) state.getValue(POWERED)).booleanValue(); float f = 0.25F; float f1 = 0.375F; float f2 = (float) (flag ? 1 : 2) / 16.0F; float f3 = 0.125F; float f4 = 0.1875F; switch (enumfacing) { case EAST: this.setBlockBounds(0.0F, 0.375F, 0.3125F, f2, 0.625F, 0.6875F); break; case WEST: this.setBlockBounds(1.0F - f2, 0.375F, 0.3125F, 1.0F, 0.625F, 0.6875F); break; case SOUTH: this.setBlockBounds(0.3125F, 0.375F, 0.0F, 0.6875F, 0.625F, f2); break; case NORTH: this.setBlockBounds(0.3125F, 0.375F, 1.0F - f2, 0.6875F, 0.625F, 1.0F); break; case UP: this.setBlockBounds(0.3125F, 0.0F, 0.375F, 0.6875F, 0.0F + f2, 0.625F); break; case DOWN: this.setBlockBounds(0.3125F, 1.0F - f2, 0.375F, 0.6875F, 1.0F, 0.625F); } } public boolean onBlockActivated( World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { if (((Boolean) state.getValue(POWERED)).booleanValue()) { return true; } else { worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(true)), 3); worldIn.markBlockRangeForRenderUpdate(pos, pos); worldIn.playSoundEffect( (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, "random.click", 0.3F, 0.6F); this.notifyNeighbors(worldIn, pos, (EnumFacing) state.getValue(FACING)); worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn)); return true; } } public void breakBlock(World worldIn, BlockPos pos, IBlockState state) { if (((Boolean) state.getValue(POWERED)).booleanValue()) { this.notifyNeighbors(worldIn, pos, (EnumFacing) state.getValue(FACING)); } super.breakBlock(worldIn, pos, state); } public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side) { return ((Boolean) state.getValue(POWERED)).booleanValue() ? 15 : 0; } public int getStrongPower( IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side) { return !((Boolean) state.getValue(POWERED)).booleanValue() ? 0 : (state.getValue(FACING) == side ? 15 : 0); } /** * Can this block provide power. Only wire currently seems to have this change based on its state. */ public boolean canProvidePower() { return true; } /** Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.) */ public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random) {} public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) { if (!worldIn.isRemote) { if (((Boolean) state.getValue(POWERED)).booleanValue()) { if (this.wooden) { this.checkForArrows(worldIn, pos, state); } else { worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false))); this.notifyNeighbors(worldIn, pos, (EnumFacing) state.getValue(FACING)); worldIn.playSoundEffect( (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, "random.click", 0.3F, 0.5F); worldIn.markBlockRangeForRenderUpdate(pos, pos); } } } } /** Sets the block's bounds for rendering it as an item */ public void setBlockBoundsForItemRender() { float f = 0.1875F; float f1 = 0.125F; float f2 = 0.125F; this.setBlockBounds(0.5F - f, 0.5F - f1, 0.5F - f2, 0.5F + f, 0.5F + f1, 0.5F + f2); } /** Called When an Entity Collided with the Block */ public void onEntityCollidedWithBlock( World worldIn, BlockPos pos, IBlockState state, Entity entityIn) { if (!worldIn.isRemote) { if (this.wooden) { if (!((Boolean) state.getValue(POWERED)).booleanValue()) { this.checkForArrows(worldIn, pos, state); } } } } private void checkForArrows(World worldIn, BlockPos pos, IBlockState state) { this.updateBlockBounds(state); List<? extends Entity> list = worldIn.<Entity>getEntitiesWithinAABB( EntityArrow.class, new AxisAlignedBB( (double) pos.getX() + this.minX, (double) pos.getY() + this.minY, (double) pos.getZ() + this.minZ, (double) pos.getX() + this.maxX, (double) pos.getY() + this.maxY, (double) pos.getZ() + this.maxZ)); boolean flag = !list.isEmpty(); boolean flag1 = ((Boolean) state.getValue(POWERED)).booleanValue(); if (flag && !flag1) { worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(true))); this.notifyNeighbors(worldIn, pos, (EnumFacing) state.getValue(FACING)); worldIn.markBlockRangeForRenderUpdate(pos, pos); worldIn.playSoundEffect( (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, "random.click", 0.3F, 0.6F); } if (!flag && flag1) { worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false))); this.notifyNeighbors(worldIn, pos, (EnumFacing) state.getValue(FACING)); worldIn.markBlockRangeForRenderUpdate(pos, pos); worldIn.playSoundEffect( (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, "random.click", 0.3F, 0.5F); } if (flag) { worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn)); } } private void notifyNeighbors(World worldIn, BlockPos pos, EnumFacing facing) { worldIn.notifyNeighborsOfStateChange(pos, this); worldIn.notifyNeighborsOfStateChange(pos.offset(facing.getOpposite()), this); } /** Convert the given metadata into a BlockState for this Block */ public IBlockState getStateFromMeta(int meta) { EnumFacing enumfacing; switch (meta & 7) { case 0: enumfacing = EnumFacing.DOWN; break; case 1: enumfacing = EnumFacing.EAST; break; case 2: enumfacing = EnumFacing.WEST; break; case 3: enumfacing = EnumFacing.SOUTH; break; case 4: enumfacing = EnumFacing.NORTH; break; case 5: default: enumfacing = EnumFacing.UP; } return this.getDefaultState() .withProperty(FACING, enumfacing) .withProperty(POWERED, Boolean.valueOf((meta & 8) > 0)); } /** Convert the BlockState into the correct metadata value */ public int getMetaFromState(IBlockState state) { int i; switch ((EnumFacing) state.getValue(FACING)) { case EAST: i = 1; break; case WEST: i = 2; break; case SOUTH: i = 3; break; case NORTH: i = 4; break; case UP: default: i = 5; break; case DOWN: i = 0; } if (((Boolean) state.getValue(POWERED)).booleanValue()) { i |= 8; } return i; } protected BlockState createBlockState() { return new BlockState(this, new IProperty[] {FACING, POWERED}); } }
public class BlockScreen extends BlockAdvanced implements IShiftDescription, IPartialSealableBlock, ITileEntityProvider, ISortableBlock { /*private IIcon iconFront; private IIcon iconSide;*/ public static final PropertyDirection FACING = PropertyDirection.create("facing"); public static final PropertyBool LEFT = PropertyBool.create("left"); public static final PropertyBool RIGHT = PropertyBool.create("right"); public static final PropertyBool UP = PropertyBool.create("up"); public static final PropertyBool DOWN = PropertyBool.create("down"); // Metadata: 0-5 = direction of screen back; bit 3 = reserved for future use protected BlockScreen(String assetName) { super(Material.circuits); this.setDefaultState( this.blockState .getBaseState() .withProperty(FACING, EnumFacing.NORTH) .withProperty(LEFT, false) .withProperty(RIGHT, false) .withProperty(UP, false) .withProperty(DOWN, false)); this.setHardness(0.1F); this.setStepSound(Block.soundTypeGlass); // this.setBlockTextureName("glass"); this.setUnlocalizedName(assetName); } @Override public boolean isSideSolid(IBlockAccess world, BlockPos pos, EnumFacing direction) { return direction.ordinal() != getMetaFromState(world.getBlockState(pos)); } @Override public void breakBlock(World worldIn, BlockPos pos, IBlockState state) { ((TileEntityScreen) worldIn.getTileEntity(pos)).breakScreen(state); super.breakBlock(worldIn, pos, state); } @Override public boolean isOpaqueCube() { return false; } @Override public boolean isFullCube() { return false; } @Override public int getRenderType() { return GalacticraftCore.proxy.getBlockRender(this); } /*@Override @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister par1IconRegister) { this.iconFront = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "screenFront"); this.iconSide = par1IconRegister.registerIcon(GalacticraftCore.TEXTURE_PREFIX + "screenSide"); } @Override public IIcon getIcon(int side, int metadata) { if (side == (metadata & 7)) { return this.iconSide; } return this.iconFront; }*/ @Override public void onBlockPlacedBy( World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { final int angle = MathHelper.floor_double(placer.rotationYaw * 4.0F / 360.0F + 0.5D) & 3; int change = EnumFacing.getHorizontal(angle).getOpposite().getIndex(); worldIn.setBlockState(pos, getStateFromMeta(change), 3); } @Override public boolean onUseWrench( World world, BlockPos pos, EntityPlayer entityPlayer, EnumFacing side, float hitX, float hitY, float hitZ) { int change = world.getBlockState(pos).getValue(FACING).rotateY().getIndex(); world.setBlockState(pos, this.getStateFromMeta(change), 3); return true; } @Override public TileEntity createNewTileEntity(World world, int meta) { return new TileEntityScreen(); } @Override public CreativeTabs getCreativeTabToDisplayOn() { return GalacticraftCore.galacticraftBlocksTab; } @Override public boolean onMachineActivated( World world, BlockPos pos, IBlockState state, EntityPlayer entityPlayer, EnumFacing side, float hitX, float hitY, float hitZ) { TileEntity tile = world.getTileEntity(pos); if (tile instanceof TileEntityScreen) { ((TileEntityScreen) tile).changeChannel(); return true; } return false; } @Override public void onNeighborBlockChange( World worldIn, BlockPos pos, IBlockState state, Block neighborBlock) { TileEntity tile = worldIn.getTileEntity(pos); if (tile instanceof TileEntityScreen) { ((TileEntityScreen) tile).refreshConnections(true); } } @Override public String getShiftDescription(int meta) { return GCCoreUtil.translate(this.getUnlocalizedName() + ".description"); } @Override public boolean showDescription(int meta) { return true; } @Override public boolean isSealed(World worldIn, BlockPos pos, EnumFacing direction) { return true; } @Override public MovingObjectPosition collisionRayTrace(World worldIn, BlockPos pos, Vec3 start, Vec3 end) { final int metadata = getMetaFromState(worldIn.getBlockState(pos)) & 7; float boundsFront = 0.094F; float boundsBack = 1.0F - boundsFront; switch (metadata) { case 0: this.setBlockBounds(0F, 0F, 0F, 1.0F, boundsBack, 1.0F); break; case 1: this.setBlockBounds(0F, boundsFront, 0F, 1.0F, 1.0F, 1.0F); break; case 2: this.setBlockBounds(0F, 0F, boundsFront, 1.0F, 1.0F, 1.0F); break; case 3: this.setBlockBounds(0F, 0F, 0F, 1.0F, 1.0F, boundsBack); break; case 4: this.setBlockBounds(boundsFront, 0F, 0F, 1.0F, 1.0F, 1.0F); break; case 5: this.setBlockBounds(0F, 0F, 0F, boundsBack, 1.0F, 1.0F); break; } return super.collisionRayTrace(worldIn, pos, start, end); } @Override public IBlockState getStateFromMeta(int meta) { EnumFacing enumfacing = EnumFacing.getFront(meta); return this.getDefaultState().withProperty(FACING, enumfacing); } @Override public int getMetaFromState(IBlockState state) { return (state.getValue(FACING)).getIndex(); } @Override protected BlockState createBlockState() { return new BlockState(this, FACING, LEFT, RIGHT, UP, DOWN); } @Override public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) { TileEntityScreen screen = (TileEntityScreen) worldIn.getTileEntity(pos); return state .withProperty(LEFT, screen.connectedLeft) .withProperty(RIGHT, screen.connectedRight) .withProperty(UP, screen.connectedUp) .withProperty(DOWN, screen.connectedDown); } @Override public EnumSortCategoryBlock getCategory(int meta) { return EnumSortCategoryBlock.MACHINE; } }
public class BlockDispenser extends Block implements BlockContainer { public static final PropertyDirection FACING = PropertyDirection.create("facing"); public static final PropertyBool TRIGGERED = PropertyBool.create("triggered"); /** Registry for all dispense behaviors. */ public static final RegistryDefaulted<Item, IBehaviorDispenseItem> dispenseBehaviorRegistry = new RegistryDefaulted<>(new BehaviorDefaultDispenseItem()); protected Random rand = new Random(); /* BlockState methods */ public BlockState createBlockState() { return new BlockState(this, FACING, TRIGGERED); } public IBlockState getDefaultState() { return this.getBaseState() .withProperty(FACING, EnumDirection.NORTH) .withProperty(TRIGGERED, false); } public int getMetaFromState(IBlockState state) { return state.getValue(FACING).getIndex() | (state.getValue(TRIGGERED) ? 8 : 0); } public IBlockState getStateFromMeta(int data) { return this.getDefaultState() .withProperty(FACING, getFacing(data)) .withProperty(TRIGGERED, (data & 8) > 0); } public IBlockState getStateForEntityRender(IBlockState state) { return this.getDefaultState().withProperty(FACING, EnumDirection.SOUTH); } /* Event hooks */ public void didPlaceBlock(World world, BlockPos pos, IBlockState state) { super.didPlaceBlock(world, pos, state); this.setDefaultDirection(world, pos, state); } private void setDefaultDirection(World world, BlockPos pos, IBlockState state) { if (!world.isRemote) { EnumDirection var4 = state.getValue(FACING); boolean var5 = world.getBlockState(pos.offsetNorth()).getBlock().isFullBlock(); boolean var6 = world.getBlockState(pos.offsetSouth()).getBlock().isFullBlock(); if (var4 == EnumDirection.NORTH && var5 && !var6) { var4 = EnumDirection.SOUTH; } else if (var4 == EnumDirection.SOUTH && var6 && !var5) { var4 = EnumDirection.NORTH; } else { boolean var7 = world.getBlockState(pos.offsetWest()).getBlock().isFullBlock(); boolean var8 = world.getBlockState(pos.offsetEast()).getBlock().isFullBlock(); if (var4 == EnumDirection.WEST && var7 && !var8) { var4 = EnumDirection.EAST; } else if (var4 == EnumDirection.EAST && var8 && !var7) { var4 = EnumDirection.WEST; } } world.setBlockState(pos, state.withProperty(FACING, var4).withProperty(TRIGGERED, false), 2); } } public boolean didActivateBlock( World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumDirection side, float hitX, float hitY, float hitZ) { if (!world.isRemote) { world.withTileEntity(pos, TileEntityDispenser.class, player::displayGUIChest); } return true; } protected void dispenseItem(World world, BlockPos pos) { world.withTileEntity( pos, TileEntityDispenser.class, var4 -> { int var5 = var4.func_146017_i(); if (var5 < 0) { world.playSound(1001, pos, 0); } else { ItemStack var6 = var4.get(var5); IBehaviorDispenseItem var7 = this.getDispenserLogic(var6); if (var7 != IBehaviorDispenseItem.itemDispenseBehaviorProvider) { ItemStack var8 = var7.dispense(new BlockSource(world, pos), var6); var4.set(var5, var8.stackSize == 0 ? null : var8); } } }); } protected IBehaviorDispenseItem getDispenserLogic(ItemStack stack) { return dispenseBehaviorRegistry.get(stack == null ? null : stack.getItem()); } public void neighborDidChange( World world, BlockPos pos, IBlockState state, IBlock neighborBlock) { boolean var5 = world.isBlockBeingPowered(pos) || world.isBlockBeingPowered(pos.offsetUp()); boolean var6 = state.getValue(TRIGGERED); if (var5 && !var6) { world.scheduleUpdate(pos, this, this.tickRate(world)); world.setBlockState(pos, state.withProperty(TRIGGERED, true), 4); } else if (!var5 && var6) { world.setBlockState(pos, state.withProperty(TRIGGERED, false), 4); } } public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) { if (!world.isRemote) { this.dispenseItem(world, pos); } } /** Returns a new instance of a block's tile entity class. Called on placing the block. */ public TileEntity createTileEntity(World world, int meta) { return new TileEntityDispenser(); } public IBlockState didPlaceBlock( World world, BlockPos pos, EnumDirection facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase player) { return this.getDefaultState() .withProperty(FACING, BlockPistonBase.func_180695_a(world, pos, player)) .withProperty(TRIGGERED, false); } public void didPlaceBlock( World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack stack) { world.setBlockState( pos, state.withProperty(FACING, BlockPistonBase.func_180695_a(world, pos, player)), 2); world.withTileEntity( pos, TileEntityDispenser.class, t -> stack.withDisplayName(t::setCustomName)); } public void didBreakBlock(World world, BlockPos pos, IBlockState state) { world.withTileEntity( pos, TileEntityDispenser.class, t -> { InventoryHelper.dropInventoryItems(world, pos, t); world.updateComparatorOutputLevel(pos, this); }); super.didBreakBlock(world, pos, state); } /** Get the position where the dispenser at the given Coordinates should dispense to. */ public static IPosition getDispensePosition(IBlockSource coords) { EnumDirection var1 = getFacing(coords.getBlockMetadata()); double var2 = coords.getX() + 0.7D * var1.getOffsetX(); double var4 = coords.getY() + 0.7D * var1.getOffsetY(); double var6 = coords.getZ() + 0.7D * var1.getOffsetZ(); return new PositionImpl(var2, var4, var6); } /** Get the facing of a dispenser with the given metadata */ public static EnumDirection getFacing(int meta) { return EnumDirection.fromIndex(meta & 7); } public boolean hasComparatorInputOverride() { return true; } public int getComparatorInputOverride(World world, BlockPos pos) { return Container.getRedstonePower(world.getTileEntity(pos)); } /** The type of render function that is called for this block */ public int getRenderType() { return 3; } /* Ticking methods */ public int tickRate(World world) { return 4; } /* Property methods */ public String getBlockName() { return "dispenser"; } public CreativeTab getCreativeTab() { return CreativeTab.tabRedstone; } public SoundType getStepSound() { return soundTypePiston; } public float getHardness() { return 3.5F; } public Material getMaterial() { return Material.rock; } }
public class BlockKeypadFurnace extends BlockOwnable { public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public static final PropertyBool OPEN = PropertyBool.create("open"); public BlockKeypadFurnace(Material materialIn) { super(materialIn); } /** * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the * shared face of two adjacent blocks and also whether the player can attach torches, redstone * wire, etc to this block. */ public boolean isOpaqueCube() { return false; } /** * If this block doesn't render as an ordinary block it will return False (examples: signs, * buttons, stairs, etc) */ public boolean isNormalCube() { return false; } public boolean onBlockActivated( World par1World, BlockPos pos, IBlockState state, EntityPlayer par5EntityPlayer, EnumFacing side, float par7, float par8, float par9) { if (!par1World.isRemote) { if (par5EntityPlayer.getCurrentEquippedItem() == null || (par5EntityPlayer.getCurrentEquippedItem() != null && par5EntityPlayer.getCurrentEquippedItem().getItem() != mod_SecurityCraft.Codebreaker)) { TileEntityKeypadFurnace TE = (TileEntityKeypadFurnace) par1World.getTileEntity(pos); if (TE.getPassword() != null && !TE.getPassword().isEmpty()) { par5EntityPlayer.openGui( mod_SecurityCraft.instance, GuiHandler.INSERT_PASSWORD_ID, par1World, pos.getX(), pos.getY(), pos.getZ()); } else { par5EntityPlayer.openGui( mod_SecurityCraft.instance, GuiHandler.SETUP_PASSWORD_ID, par1World, pos.getX(), pos.getY(), pos.getZ()); } } else { if (mod_SecurityCraft.instance.configHandler.allowCodebreakerItem) { if (((IPasswordProtected) par1World.getTileEntity(pos)).getPassword() != null) { activate(par1World, pos, par5EntityPlayer); } } else { PlayerUtils.sendMessageToPlayer( par5EntityPlayer, StatCollector.translateToLocal("tile.keypadFurnace.name"), StatCollector.translateToLocal("messages.codebreakerDisabled"), EnumChatFormatting.RED); } } } return true; } public static void activate(World par1World, BlockPos pos, EntityPlayer player) { if (!BlockUtils.getBlockPropertyAsBoolean(par1World, pos, BlockKeypadFurnace.OPEN)) { BlockUtils.setBlockProperty(par1World, pos, BlockKeypadFurnace.OPEN, true, false); } par1World.playAuxSFXAtEntity((EntityPlayer) null, 1006, pos, 0); player.openGui(mod_SecurityCraft.instance, 16, par1World, pos.getX(), pos.getY(), pos.getZ()); } public IBlockState onBlockPlaced( World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return this.getDefaultState() .withProperty(FACING, placer.getHorizontalFacing().getOpposite()) .withProperty(OPEN, false); } @SideOnly(Side.CLIENT) public IBlockState getStateForEntityRender(IBlockState state) { return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH); } public IBlockState getStateFromMeta(int meta) { if (meta <= 5) { return this.getDefaultState() .withProperty( FACING, EnumFacing.values()[meta].getAxis() == EnumFacing.Axis.Y ? EnumFacing.NORTH : EnumFacing.values()[meta]) .withProperty(OPEN, false); } else { return this.getDefaultState() .withProperty(FACING, EnumFacing.values()[meta - 6]) .withProperty(OPEN, true); } } public int getMetaFromState(IBlockState state) { if (((Boolean) state.getValue(OPEN)).booleanValue()) { return (((EnumFacing) state.getValue(FACING)).getIndex() + 6); } else { return ((EnumFacing) state.getValue(FACING)).getIndex(); } } protected BlockState createBlockState() { return new BlockState(this, new IProperty[] {FACING, OPEN}); } public TileEntity createNewTileEntity(World var1, int var2) { return new TileEntityKeypadFurnace(); } }