Java Code Examples for net.minecraft.block.BlockState#getBlock()

The following examples show how to use net.minecraft.block.BlockState#getBlock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HallowCharmItem.java    From the-hallow with MIT License 7 votes vote down vote up
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
	PlayerEntity player = context.getPlayer();
	if (context.getWorld().isClient) return ActionResult.PASS;
	BlockState state = context.getWorld().getBlockState(context.getBlockPos());
	if(state.getBlock() == HallowedBlocks.HALLOWED_GATE) {
		if (context.getWorld().getDimension().getType() == DimensionType.OVERWORLD) {
			if (HallowedGateBlock.isValid(context.getWorld(), context.getBlockPos(), state)) {
				BlockPos pos = player.getBlockPos();
				CompoundTag tag = new CompoundTag();
				tag.putInt("x", pos.getX());
				tag.putInt("y", pos.getY());
				tag.putInt("z", pos.getZ());
				context.getStack().putSubTag("PortalLoc", tag);
				FabricDimensions.teleport(player, HallowedDimensions.THE_HALLOW);
				return ActionResult.SUCCESS;
			} else {
				player.addChatMessage(new TranslatableText("text.thehallow.gate_incomplete"), true);
			}
		} else {
			player.addChatMessage(new TranslatableText("text.thehallow.gate_in_wrong_dimension"), true);
		}
	}
	return ActionResult.PASS;
}
 
Example 2
Source File: ClientPlayNetworkHandler_smoothClientAnimationsMixin.java    From fabric-carpet with MIT License 7 votes vote down vote up
@Inject( method = "onChunkData", locals = LocalCapture.CAPTURE_FAILHARD, require = 0, at = @At(
        value = "INVOKE",
        target = "Lnet/minecraft/client/world/ClientWorld;getBlockEntity(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/entity/BlockEntity;",
        shift = At.Shift.AFTER
))
private void recreateMovingPistons(ChunkDataS2CPacket packet, CallbackInfo ci,
                                   Iterator var5, CompoundTag tag, BlockPos blockPos)
{
    if (CarpetSettings.smoothClientAnimations)
    {
        BlockEntity blockEntity = world.getBlockEntity(blockPos);
        if (blockEntity == null && "minecraft:piston".equals(tag.getString("id")))
        {
            BlockState blockState = world.getBlockState(blockPos);
            if (blockState.getBlock() == Blocks.MOVING_PISTON) {
                tag.putFloat("progress", Math.min(tag.getFloat("progress") + 0.5F, 1.0F));
                blockEntity = new PistonBlockEntity();
                blockEntity.fromTag(tag);
                world.setBlockEntity(blockPos, blockEntity);
                blockEntity.resetBlock();
            }
        }
    }
}
 
Example 3
Source File: DispenserBehaviorFireChargeMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 7 votes vote down vote up
@SuppressWarnings("UnresolvedMixinReference")
@Inject(method = "dispenseSilently(Lnet/minecraft/util/math/BlockPointer;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;", at = @At("HEAD"), cancellable = true)
private void convertNetherrack(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir)
{
    if (!CarpetExtraSettings.fireChargeConvertsToNetherrack)
        return;
    World world = pointer.getWorld();
    Direction direction = pointer.getBlockState().get(DispenserBlock.FACING);
    BlockPos front = pointer.getBlockPos().offset(direction);
    BlockState state = world.getBlockState(front);
    if (state.getBlock() == Blocks.COBBLESTONE)
    {
        world.setBlockState(front, Blocks.NETHERRACK.getDefaultState());
        stack.decrement(1);
        cir.setReturnValue(stack);
        cir.cancel();
    }
}
 
Example 4
Source File: MixinBlockItem.java    From multiconnect with MIT License 6 votes vote down vote up
@Inject(method = "canPlace", at = @At("HEAD"), cancellable = true)
private void onCanPlace(ItemPlacementContext context, BlockState state, CallbackInfoReturnable<Boolean> ci) {
    if (ConnectionInfo.protocolVersion <= Protocols.V1_12_2) {
        Block block = state.getBlock();
        if (block == Blocks.CHEST || block == Blocks.TRAPPED_CHEST) {
            World world = context.getWorld();
            BlockPos pos = context.getBlockPos();
            boolean foundAdjChest = false;
            for (Direction dir : Direction.Type.HORIZONTAL) {
                BlockState otherState = world.getBlockState(pos.offset(dir));
                if (otherState.getBlock() == block) {
                    if (foundAdjChest) {
                        ci.setReturnValue(false);
                        return;
                    }
                    foundAdjChest = true;
                    if (otherState.get(ChestBlock.CHEST_TYPE) != ChestType.SINGLE) {
                        ci.setReturnValue(false);
                        return;
                    }
                }
            }
        }
    }
}
 
Example 5
Source File: StandardWrenchItem.java    From Galacticraft-Rewoven with MIT License 6 votes vote down vote up
private void use(PlayerEntity player, BlockState state, WorldAccess iWorld, BlockPos pos, ItemStack stack) {
    Block block = state.getBlock();
    if (block instanceof Rotatable) {
        StateManager<Block, BlockState> manager = block.getStateManager();
        Collection<Property<?>> collection = manager.getProperties();
        String string_1 = Registry.BLOCK.getId(block).toString();
        if (!collection.isEmpty()) {
            CompoundTag compoundTag_1 = stack.getOrCreateSubTag("wrenchProp");
            String string_2 = compoundTag_1.getString(string_1);
            Property<?> property = manager.getProperty(string_2);
            if (property == null) {
                property = collection.iterator().next();
            }
            if (property.getName().equals("facing")) {
                BlockState blockState_2 = cycle(state, property, player.isSneaking());
                iWorld.setBlockState(pos, blockState_2, 18);
                stack.damage(2, player, (playerEntity) -> playerEntity.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
            }
        }
    }
}
 
Example 6
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack)
{
    if (!CarpetExtraSettings.dispensersPlayRecords)
        return super.dispenseSilently(source, stack);
    
    Direction direction = source.getBlockState().get(DispenserBlock.FACING);
    BlockPos pos = source.getBlockPos().offset(direction);
    World world = source.getWorld();
    BlockState state = world.getBlockState(pos);
    
    if (state.getBlock() == Blocks.JUKEBOX)
    {
        JukeboxBlockEntity jukebox = (JukeboxBlockEntity) world.getBlockEntity(pos);
        if (jukebox != null)
        {
            ItemStack itemStack = jukebox.getRecord();
            ((JukeboxBlock) state.getBlock()).setRecord(world, pos, state, stack);
            world.playLevelEvent(null, 1010, pos, Item.getRawId(stack.getItem()));
            
            return itemStack;
        }
    }
    
    return super.dispenseSilently(source, stack);
}
 
Example 7
Source File: SawmillBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving)
{
    if (state.getBlock() != newState.getBlock())
    {
        TileEntity te = worldIn.getTileEntity(pos);

        if (te instanceof SawmillTileEntity)
        {
            dropInventoryItems(worldIn, pos, ((SawmillTileEntity) te).getInventory());
            worldIn.updateComparatorOutputLevel(pos, this);
        }

        super.onReplaced(state, worldIn, pos, newState, isMoving);
    }
}
 
Example 8
Source File: LivingEntityMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Inject(
        method = "onDeath",
        at = @At(value = "INVOKE",
                target = "Lnet/minecraft/entity/damage/DamageSource;getAttacker()Lnet/minecraft/entity/Entity;",
                shift = At.Shift.BEFORE)
)
private void convertSandToSoulsand(DamageSource damageSource_1, CallbackInfo ci)
{
    if (!CarpetExtraSettings.mobInFireConvertsSandToSoulsand)
        return;
    
    BlockPos pos = new BlockPos(this.getX(), this.getY(), this.getZ());
    BlockState statePos = this.world.getBlockState(pos);
    
    BlockPos below = pos.down(1);
    BlockState stateBelow = this.world.getBlockState(below);
    
    if (statePos.getBlock() == Blocks.FIRE && stateBelow.getBlock().matches(BlockTags.SAND))
    {
        this.world.setBlockState(below, Blocks.SOUL_SAND.getDefaultState());
    }
}
 
Example 9
Source File: WitchWaterBubbleColumnBlock.java    From the-hallow with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction dir, BlockState state2, IWorld world, BlockPos pos, BlockPos pos2) {
	if (!state.canPlaceAt(world, pos)) {
		return HallowedBlocks.WITCH_WATER_BLOCK.getDefaultState();
	} else {
		if (dir == Direction.DOWN) {
			world.setBlockState(pos, HallowedBlocks.WITCH_WATER_BUBBLE_COLUMN.getDefaultState().with(DRAG, calculateDrag(world, pos2)), 2);
		} else if (dir == Direction.UP && state2.getBlock() != HallowedBlocks.WITCH_WATER_BUBBLE_COLUMN && isStillWater(world, pos2)) {
			world.getBlockTickScheduler().schedule(pos, this, this.getTickRate(world));
		}
		world.getFluidTickScheduler().schedule(pos, HallowedFluids.WITCH_WATER, HallowedFluids.WITCH_WATER.getTickRate(world));
		return super.getStateForNeighborUpdate(state, dir, state2, world, pos, pos2);
	}
}
 
Example 10
Source File: CarpetExpression.java    From fabric-carpet with MIT License 5 votes vote down vote up
private boolean tryBreakBlock_copy_from_ServerPlayerInteractionManager(ServerPlayerEntity player, BlockPos blockPos_1)
{
    //this could be done little better, by hooking up event handling not in try_break_block but wherever its called
    // so we can safely call it here
    // but that woudl do for now.
    BlockState blockState_1 = player.world.getBlockState(blockPos_1);
    if (!player.getMainHandStack().getItem().canMine(blockState_1, player.world, blockPos_1, player)) {
        return false;
    } else {
        BlockEntity blockEntity_1 = player.world.getBlockEntity(blockPos_1);
        Block block_1 = blockState_1.getBlock();
        if ((block_1 instanceof CommandBlock || block_1 instanceof StructureBlock || block_1 instanceof JigsawBlock) && !player.isCreativeLevelTwoOp()) {
            player.world.updateListeners(blockPos_1, blockState_1, blockState_1, 3);
            return false;
        } else if (player.canMine(player.world, blockPos_1, player.interactionManager.getGameMode())) {
            return false;
        } else {
            block_1.onBreak(player.world, blockPos_1, blockState_1, player);
            boolean boolean_1 = player.world.removeBlock(blockPos_1, false);
            if (boolean_1) {
                block_1.onBroken(player.world, blockPos_1, blockState_1);
            }

            if (player.isCreative()) {
                return true;
            } else {
                ItemStack itemStack_1 = player.getMainHandStack();
                boolean boolean_2 = player.isUsingEffectiveTool(blockState_1);
                itemStack_1.postMine(player.world, blockState_1, blockPos_1, player);
                if (boolean_1 && boolean_2) {
                    ItemStack itemStack_2 = itemStack_1.isEmpty() ? ItemStack.EMPTY : itemStack_1.copy();
                    block_1.afterBreak(player.world, player, blockPos_1, blockState_1, blockEntity_1, itemStack_2);
                }

                return true;
            }
        }
    }
}
 
Example 11
Source File: MixinFarmlandBlock.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Inject(method = "hasCrop", cancellable = true, at = @At("HEAD"))
private static void onHasCrop(BlockView world, BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
	final BlockState ourState = world.getBlockState(pos);
	final BlockState cropState = world.getBlockState(pos.up());

	if (cropState.getBlock() instanceof IPlantable && ((IForgeBlockState) ourState).canSustainPlant(world, pos, Direction.UP, (IPlantable) cropState.getBlock())) {
		cir.setReturnValue(true);
		cir.cancel();
	}
}
 
Example 12
Source File: ChoppingBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving)
{
    if (newState.getBlock() != state.getBlock())
    {
        TileEntity tileentity = worldIn.getTileEntity(pos);

        if (tileentity instanceof ChoppingBlockTileEntity)
        {
            dropInventoryItems(worldIn, pos, ((ChoppingBlockTileEntity) tileentity).getSlotInventory());
            worldIn.updateComparatorOutputLevel(pos, this);
        }
        super.onReplaced(state, worldIn, pos, newState, isMoving);
    }
}
 
Example 13
Source File: MixinPlantBlock.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public BlockState getPlant(BlockView world, BlockPos pos) {
	final BlockState blockState = world.getBlockState(pos);

	if (blockState.getBlock() != (Object) this) {
		return ((Block) (Object) this).getDefaultState();
	}

	return blockState;
}
 
Example 14
Source File: HallowedMushroomPlantBlock.java    From the-hallow with MIT License 5 votes vote down vote up
@Override
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
	if (super.canPlaceAt(state, world, pos)) return true;
	BlockPos downPos = pos.down();
	BlockState downState = world.getBlockState(downPos);
	Block downBlock = downState.getBlock();
	return (downBlock == HallowedBlocks.DECEASED_GRASS_BLOCK || downBlock == HallowedBlocks.DECEASED_DIRT || downBlock == HallowedBlocks.DECEASED_MOSS);
}
 
Example 15
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected ItemStack dispenseSilently(BlockPointer blockPointer_1, ItemStack itemStack_1)
{
    if (!CarpetExtraSettings.dispensersTillSoil)
        return super.dispenseSilently(blockPointer_1, itemStack_1);
    
    World world = blockPointer_1.getWorld();
    Direction direction = blockPointer_1.getBlockState().get(DispenserBlock.FACING);
    BlockPos front = blockPointer_1.getBlockPos().offset(direction);
    BlockPos down = blockPointer_1.getBlockPos().down().offset(direction);
    BlockState frontState = world.getBlockState(front);
    BlockState downState = world.getBlockState(down);
    
    if (isFarmland(frontState) || isFarmland(downState))
        return itemStack_1;
    
    if (canDirectlyTurnToFarmland(frontState))
        world.setBlockState(front, Blocks.FARMLAND.getDefaultState());
    else if (canDirectlyTurnToFarmland(downState))
        world.setBlockState(down, Blocks.FARMLAND.getDefaultState());
    else if (frontState.getBlock() == Blocks.COARSE_DIRT)
        world.setBlockState(front, Blocks.DIRT.getDefaultState());
    else if (downState.getBlock() == Blocks.COARSE_DIRT)
        world.setBlockState(down, Blocks.DIRT.getDefaultState());
    
    if (itemStack_1.damage(1, world.random, null))
        itemStack_1.setCount(0);
    
    return itemStack_1;
}
 
Example 16
Source File: MixinCactusBlock.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Redirect(method = "canPlaceAt", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;getBlock()Lnet/minecraft/block/Block;"))
private Block redirectSoilCheck(BlockState blockState) {
	if (((IForgeBlockState) blockState).canSustainPlant(currentWorld.get(), currentBlockPos.get().down(), Direction.UP, this)) {
		return Blocks.CACTUS; // Forces condition to be true.
	}

	return blockState.getBlock(); // Pass this to let injects from fabric mods maybe work
}
 
Example 17
Source File: ModificationTable.java    From MiningGadgets with MIT License 5 votes vote down vote up
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
    if (newState.getBlock() != this) {
        TileEntity tileEntity = worldIn.getTileEntity(pos);
        if (tileEntity != null) {
            LazyOptional<IItemHandler> cap = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
            cap.ifPresent(handler -> {
                for(int i = 0; i < handler.getSlots(); ++i) {
                    InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), handler.getStackInSlot(i));
                }
            });
        }
        super.onReplaced(state, worldIn, pos, newState, isMoving);
    }
}
 
Example 18
Source File: DryingRackBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving)
{
    if (newState.getBlock() != state.getBlock())
    {
        TileEntity tileentity = worldIn.getTileEntity(pos);

        if (tileentity instanceof DryingRackTileEntity)
        {
            dropInventoryItems(worldIn, pos, ((DryingRackTileEntity) tileentity).inventory());
            worldIn.updateComparatorOutputLevel(pos, this);
        }
        super.onReplaced(state, worldIn, pos, newState, isMoving);
    }
}
 
Example 19
Source File: DeaderBushBlock.java    From the-hallow with MIT License 4 votes vote down vote up
@Override
protected boolean canPlantOnTop(BlockState state, BlockView view, BlockPos pos) {
	Block block = state.getBlock();
	return block == HallowedBlocks.TAINTED_SAND || block == Blocks.SAND || block == Blocks.RED_SAND;
}
 
Example 20
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean canDirectlyTurnToFarmland(BlockState state)
{
    return state.getBlock() == Blocks.DIRT || state.getBlock() == Blocks.GRASS_BLOCK || state.getBlock() == Blocks.GRASS_PATH;
}