Sorry if i'm resurrecting this topic, but i'm looking exactly for a solution very similar to that.
I tried to adapt to my logic, but i'm doing something wrong.
Basically, i need to verify if there's at least one of an array of blocks around my Custom Block.
package com.ouroboros.plantae.objects.blocks;
import java.util.Random;
import javax.annotation.Nullable;
import com.ouroboros.plantae.init.BlockInit;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.IFluidState;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.tags.FluidTags;
import net.minecraft.tags.Tag;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.server.ServerWorld;
public class VibraniumBlock extends Block {
public VibraniumBlock(Block.Properties properties) {
super(properties);
}
/**
* Change the block
*/
@Override
public void func_225534_a_(BlockState blockState, ServerWorld serverWorld, BlockPos blockPos, Random random) {
boolean absorbingHeat =
this.isNearBy(serverWorld, blockPos, FluidTags.LAVA) ||
this.isNearBy(serverWorld, serverWorld, blockPos, Blocks.MAGMA_BLOCK);
boolean losingHeat =
this.isNearBy(serverWorld, blockPos, FluidTags.WATER) ||
this.isNearBy(serverWorld, serverWorld, blockPos, Blocks.ICE, Blocks.BLUE_ICE, Blocks.FROSTED_ICE ,Blocks.PACKED_ICE, Blocks.SNOW);
if(absorbingHeat && losingHeat) {
serverWorld.setBlockState(blockPos, BlockInit.vibranium_block.getDefaultState(), 2);
}
else if(absorbingHeat) {
serverWorld.setBlockState(blockPos, BlockInit.vibranium_heated_block.getDefaultState(), 2);
}
else if(losingHeat) {
//serverWorld.setBlockState(blockPos, BlockInit.vibranium_frozen_block.getDefaultState(), 2);
}
}
@Override @Nullable
public BlockState getStateForPlacement(BlockItemUseContext context) {
scheduleTick(context.getWorld(), context.getPos());
return this.getDefaultState();
}
/**
* Update the provided state given the provided neighbor facing and neighbor state, returning a new state.
* For example, fences make their connections to the passed in state if possible, and wet concrete powder immediately
* returns its solidified counterpart.
* Note that this method should ideally consider only the specific face passed in.
*/
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
scheduleTick(worldIn, currentPos);
return stateIn;
}
private void scheduleTick(IWorld worldIn, BlockPos currentPos) {
if(this.isNearBy(worldIn, currentPos, FluidTags.LAVA))
worldIn.getPendingBlockTicks().scheduleTick(currentPos, this, 60 + worldIn.getRandom().nextInt(40));
}
/**
* Verify if the current block is near by facing some fluid
*/
protected boolean isNearBy(IBlockReader blockReader, BlockPos blockPos, Tag<Fluid> fluid) {
IFluidState iFluidState;
for(Direction direction : Direction.values()) {
iFluidState = blockReader.getFluidState(blockPos.offset(direction));
if(iFluidState.isTagged(fluid))
return true;
}
return false;
}
/**
* Verify if the current block is near by facing some block
*/
protected boolean isNearBy(ServerWorld serverWorld, IBlockReader blockReader, BlockPos blockPos, Block ...arBlocks) {
BlockState blockState;
for(Direction direction : Direction.values()) {
blockState = blockReader.getBlockState(blockPos.offset(direction));
for(Block block: arBlocks) {
//TODO: Verify if, at least, one of the blocks are placed around this block
}
}
return false;
}
}