Search in sources :

Example 6 with DoorBlock

use of net.minecraft.block.DoorBlock in project ChocolateQuestRepoured by TeamChocoQuest.

the class EntityAIOpenCloseDoor method canMoveThroughDoor.

public static boolean canMoveThroughDoor(IBlockReader world, BlockPos pos, Direction enterFacing, boolean canOpenCloseDoors) {
    BlockState state = world.getBlockState(pos);
    if (!(state.getBlock() instanceof DoorBlock)) {
        return false;
    }
    state = state.getActualState(world, pos);
    boolean doorOpen = state.getValue(DoorBlock.OPEN);
    Direction doorFacing = state.getValue(DoorBlock.FACING);
    Direction.Axis blockedAxis = doorOpen ? doorFacing.rotateY().getAxis() : doorFacing.getAxis();
    if (enterFacing.getAxis() != blockedAxis) {
        return true;
    }
    if (!canOpenCloseDoors) {
        return false;
    }
    if (state.getMaterial() == Material.WOOD) {
        return true;
    }
    if (doorOpen) {
        // TODO check for levers which can be deactivated? -> requires a check for every other possible redstone signal...
        return false;
    }
    BlockPos.Mutable mutablePos = new BlockPos.Mutable();
    if (state.getValue(DoorBlock.HALF) == EnumDoorHalf.UPPER) {
        pos = pos.down();
    }
    if (isPressurePlate(world, mutablePos.setPos(pos).move(enterFacing))) {
        return true;
    }
    if (isButtonOrLeverWithOrientation(world, mutablePos.setPos(pos).move(enterFacing).move(enterFacing.rotateY()).move(Direction.UP), enterFacing)) {
        return true;
    }
    if (isButtonOrLeverWithOrientation(world, mutablePos.setPos(pos).move(enterFacing).move(enterFacing.rotateYCCW()).move(Direction.UP), enterFacing)) {
        return true;
    }
    if (isButtonOrLeverWithOrientation(world, mutablePos.setPos(pos).move(enterFacing).move(enterFacing.rotateY()), enterFacing)) {
        return true;
    }
    if (isButtonOrLeverWithOrientation(world, mutablePos.setPos(pos).move(enterFacing).move(enterFacing.rotateYCCW()), enterFacing)) {
        return true;
    }
    return false;
}
Also used : BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction) DoorBlock(net.minecraft.block.DoorBlock)

Example 7 with DoorBlock

use of net.minecraft.block.DoorBlock in project ChocolateQuestRepoured by TeamChocoQuest.

the class EntityAIOpenCloseDoor method openDoor.

private boolean openDoor() {
    BlockState state = this.world.getBlockState(this.doorPos);
    if (!(state.getBlock() instanceof DoorBlock)) {
        return false;
    }
    state = state.getActualState(this.world, this.doorPos);
    boolean doorOpen = state.getValue(DoorBlock.OPEN);
    Direction doorFacing = state.getValue(DoorBlock.FACING);
    Direction.Axis blockedAxis = doorOpen ? doorFacing.rotateY().getAxis() : doorFacing.getAxis();
    if (this.doorEnterFacing.getAxis() != blockedAxis) {
        return false;
    }
    if (state.getMaterial() == Material.WOOD) {
        this.doorBlock.toggleDoor(this.world, this.doorPos, !doorOpen);
        return true;
    }
    if (!doorOpen) {
        BlockPos pos = state.getValue(DoorBlock.HALF) == EnumDoorHalf.UPPER ? this.doorPos.down() : this.doorPos;
        BlockPos.Mutable mutablePos = new BlockPos.Mutable();
        if (this.isPressurePlate(mutablePos.setPos(pos).move(this.doorEnterFacing))) {
            return true;
        }
        if (this.activateButtonOrLeverWithOrientation(mutablePos.setPos(pos).move(this.doorEnterFacing).move(this.doorEnterFacing.rotateY()).move(Direction.UP), this.doorEnterFacing)) {
            return true;
        }
        if (this.activateButtonOrLeverWithOrientation(mutablePos.setPos(pos).move(this.doorEnterFacing).move(this.doorEnterFacing.rotateYCCW()).move(Direction.UP), this.doorEnterFacing)) {
            return true;
        }
        if (this.activateButtonOrLeverWithOrientation(mutablePos.setPos(pos).move(this.doorEnterFacing).move(this.doorEnterFacing.rotateY()), this.doorEnterFacing)) {
            return true;
        }
        if (this.activateButtonOrLeverWithOrientation(mutablePos.setPos(pos).move(this.doorEnterFacing).move(this.doorEnterFacing.rotateYCCW()), this.doorEnterFacing)) {
            return true;
        }
    }
    return false;
}
Also used : BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction) DoorBlock(net.minecraft.block.DoorBlock)

Example 8 with DoorBlock

use of net.minecraft.block.DoorBlock in project ChocolateQuestRepoured by TeamChocoQuest.

the class EntityAIOpenCloseDoor method canUse.

@Override
public boolean canUse() {
    if (!this.entity.canOpenDoors()) {
        return false;
    }
    if (!this.entity.isPathFinding()) {
        return false;
    }
    this.doorPos.set(this.entity.getX(), this.entity.getY(), this.entity.getZ());
    BlockState state = this.world.getBlockState(this.doorPos);
    if (state.getBlock() instanceof DoorBlock) {
        this.doorBlock = (DoorBlock) state.getBlock();
        this.doorEnterFacing = this.entity.getHorizontalFacing().getOpposite();
        return canMoveThroughDoor(this.world, this.doorPos, this.doorEnterFacing, true);
    }
    Path path = this.entity.getNavigation().getPath();
    int end = Math.min(path.getCurrentPathIndex() + 2, path.getCurrentPathLength());
    for (int i = path.getCurrentPathIndex(); i < end; i++) {
        PathPoint pathPoint = path.getPathPointFromIndex(i);
        this.doorPos.set(pathPoint.x, pathPoint.y, pathPoint.z);
        if (this.entity.getDistanceSq(this.doorPos.getX() + 0.5D, this.doorPos.getY(), this.doorPos.getZ() + 0.5D) >= 1.5D * 1.5D) {
            continue;
        }
        state = this.world.getBlockState(this.doorPos);
        if (state.getBlock() instanceof DoorBlock) {
            this.doorBlock = (DoorBlock) state.getBlock();
            if (i > 0) {
                PathPoint pathPoint1 = path.getPathPointFromIndex(i - 1);
                this.doorEnterFacing = Direction.getFacingFromVector(pathPoint1.x - pathPoint.x, pathPoint1.y - pathPoint.y, pathPoint1.z - pathPoint.z);
            } else {
                this.doorEnterFacing = this.entity.getHorizontalFacing().getOpposite();
            }
            return canMoveThroughDoor(this.world, this.doorPos, this.doorEnterFacing, true);
        }
    }
    return false;
}
Also used : Path(net.minecraft.pathfinding.Path) PathPoint(net.minecraft.pathfinding.PathPoint) BlockState(net.minecraft.block.BlockState) PathPoint(net.minecraft.pathfinding.PathPoint) DoorBlock(net.minecraft.block.DoorBlock)

Example 9 with DoorBlock

use of net.minecraft.block.DoorBlock in project Arclight by IzzelAliz.

the class PlayerInteractionManagerMixin_1_14 method func_219441_a.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public ActionResultType func_219441_a(PlayerEntity playerIn, World worldIn, ItemStack stackIn, Hand handIn, BlockRayTraceResult blockRaytraceResultIn) {
    BlockPos blockpos = blockRaytraceResultIn.getPos();
    BlockState blockstate = worldIn.getBlockState(blockpos);
    ActionResultType resultType = ActionResultType.PASS;
    boolean cancelledBlock = false;
    if (this.gameType == GameType.SPECTATOR) {
        INamedContainerProvider provider = blockstate.getContainer(worldIn, blockpos);
        cancelledBlock = !(provider instanceof INamedContainerProvider);
    }
    if (playerIn.getCooldownTracker().hasCooldown(stackIn.getItem())) {
        cancelledBlock = true;
    }
    PlayerInteractEvent bukkitEvent = CraftEventFactory.callPlayerInteractEvent(playerIn, Action.RIGHT_CLICK_BLOCK, blockpos, blockRaytraceResultIn.getFace(), stackIn, cancelledBlock, handIn);
    bridge$setFiredInteract(true);
    bridge$setInteractResult(bukkitEvent.useItemInHand() == Event.Result.DENY);
    if (bukkitEvent.useInteractedBlock() == Event.Result.DENY) {
        if (blockstate.getBlock() instanceof DoorBlock) {
            boolean bottom = blockstate.get(DoorBlock.HALF) == DoubleBlockHalf.LOWER;
            ((ServerPlayerEntity) playerIn).connection.sendPacket(new SChangeBlockPacket(this.world, bottom ? blockpos.up() : blockpos.down()));
        } else if (blockstate.getBlock() instanceof CakeBlock) {
            ((ServerPlayerEntityBridge) playerIn).bridge$getBukkitEntity().sendHealthUpdate();
        }
        ((ServerPlayerEntityBridge) playerIn).bridge$getBukkitEntity().updateInventory();
        resultType = ((bukkitEvent.useItemInHand() != Event.Result.ALLOW) ? ActionResultType.SUCCESS : ActionResultType.PASS);
    } else if (this.gameType == GameType.SPECTATOR) {
        INamedContainerProvider inamedcontainerprovider = blockstate.getContainer(worldIn, blockpos);
        if (inamedcontainerprovider != null) {
            playerIn.openContainer(inamedcontainerprovider);
            return ActionResultType.SUCCESS;
        } else {
            return ActionResultType.PASS;
        }
    } else {
        net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock event = ForgeHooks.onRightClickBlock(playerIn, handIn, blockpos, blockRaytraceResultIn.getFace());
        if (event.isCanceled())
            return event.getCancellationResult();
        ItemUseContext itemusecontext = new ItemUseContext(playerIn, handIn, blockRaytraceResultIn);
        if (event.getUseItem() != net.minecraftforge.eventbus.api.Event.Result.DENY) {
            ActionResultType result = stackIn.onItemUseFirst(itemusecontext);
            if (result != ActionResultType.PASS)
                return result;
        }
        boolean flag = !playerIn.getHeldItemMainhand().isEmpty() || !playerIn.getHeldItemOffhand().isEmpty();
        boolean flag1 = !(playerIn.isSneaking() && flag) || (playerIn.getHeldItemMainhand().doesSneakBypassUse(worldIn, blockpos, playerIn) && playerIn.getHeldItemOffhand().doesSneakBypassUse(worldIn, blockpos, playerIn));
        boolean flag2 = blockstate.onBlockActivated(worldIn, playerIn, handIn, blockRaytraceResultIn);
        if (event.getUseBlock() != net.minecraftforge.eventbus.api.Event.Result.DENY && flag1 && flag2) {
            return ActionResultType.SUCCESS;
        } else {
            if (flag1) {
                resultType = flag2 ? ActionResultType.SUCCESS : ActionResultType.FAIL;
            }
            if (!stackIn.isEmpty() && resultType != ActionResultType.SUCCESS && !bridge$getInteractResult()) {
                if (event.getUseItem() == net.minecraftforge.eventbus.api.Event.Result.DENY) {
                    return ActionResultType.PASS;
                }
                if (this.isCreative()) {
                    int i = stackIn.getCount();
                    resultType = stackIn.onItemUse(itemusecontext);
                    stackIn.setCount(i);
                    return resultType;
                } else {
                    return stackIn.onItemUse(itemusecontext);
                }
            } else {
                return resultType;
            }
        }
    }
    return resultType;
}
Also used : ItemUseContext(net.minecraft.item.ItemUseContext) ActionResultType(net.minecraft.util.ActionResultType) SChangeBlockPacket(net.minecraft.network.play.server.SChangeBlockPacket) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) INamedContainerProvider(net.minecraft.inventory.container.INamedContainerProvider) ServerPlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge) CakeBlock(net.minecraft.block.CakeBlock) BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) DoorBlock(net.minecraft.block.DoorBlock) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 10 with DoorBlock

use of net.minecraft.block.DoorBlock in project Arclight by IzzelAliz.

the class PlayerInteractionManagerMixin method func_225416_a.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void func_225416_a(BlockPos blockPos, CPlayerDiggingPacket.Action action, Direction direction, int i) {
    double d0 = this.player.posX - (blockPos.getX() + 0.5);
    double d2 = this.player.posY - (blockPos.getY() + 0.5) + 1.5;
    double d3 = this.player.posZ - (blockPos.getZ() + 0.5);
    double d4 = d0 * d0 + d2 * d2 + d3 * d3;
    double dist = player.getAttribute(net.minecraft.entity.player.PlayerEntity.REACH_DISTANCE).getValue() + 1;
    dist *= dist;
    net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock forgeEvent = net.minecraftforge.common.ForgeHooks.onLeftClickBlock(player, blockPos, direction);
    if (forgeEvent.isCanceled() || (!this.isCreative() && forgeEvent.getUseItem() == net.minecraftforge.eventbus.api.Event.Result.DENY)) {
        // Restore block and te data
        player.connection.sendPacket(this.bridge$diggingPacket(blockPos, world.getBlockState(blockPos), action, false, "mod canceled"));
        world.notifyBlockUpdate(blockPos, world.getBlockState(blockPos), world.getBlockState(blockPos), 3);
        return;
    }
    if (d4 > dist) {
        this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, false, "too far"));
    } else if (blockPos.getY() >= i) {
        this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, false, "too high"));
    } else if (action == CPlayerDiggingPacket.Action.START_DESTROY_BLOCK) {
        if (!this.world.isBlockModifiable(this.player, blockPos)) {
            CraftEventFactory.callPlayerInteractEvent(this.player, Action.LEFT_CLICK_BLOCK, blockPos, direction, this.player.inventory.getCurrentItem(), Hand.MAIN_HAND);
            this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, false, "may not interact"));
            TileEntity tileentity = this.world.getTileEntity(blockPos);
            if (tileentity != null) {
                this.player.connection.sendPacket(tileentity.getUpdatePacket());
            }
            return;
        }
        PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(this.player, Action.LEFT_CLICK_BLOCK, blockPos, direction, this.player.inventory.getCurrentItem(), Hand.MAIN_HAND);
        if (event.isCancelled()) {
            this.player.connection.sendPacket(new SChangeBlockPacket(this.world, blockPos));
            TileEntity tileentity2 = this.world.getTileEntity(blockPos);
            if (tileentity2 != null) {
                this.player.connection.sendPacket(tileentity2.getUpdatePacket());
            }
            return;
        }
        if (this.isCreative()) {
            if (!this.world.extinguishFire(null, blockPos, direction)) {
                this.bridge$creativeHarvestBlock(blockPos, action, "creative destroy");
            } else {
                this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, true, "fire put out"));
            }
            return;
        }
        if (this.player.blockActionRestricted(this.world, blockPos, this.gameType)) {
            this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, false, "block action restricted"));
            return;
        }
        this.initialDamage = this.ticks;
        float f = 1.0f;
        BlockState iblockdata = this.world.getBlockState(blockPos);
        if (event.useInteractedBlock() == org.bukkit.event.Event.Result.DENY) {
            BlockState data = this.world.getBlockState(blockPos);
            if (data.getBlock() instanceof DoorBlock) {
                boolean bottom = data.get(DoorBlock.HALF) == DoubleBlockHalf.LOWER;
                this.player.connection.sendPacket(new SChangeBlockPacket(this.world, blockPos));
                this.player.connection.sendPacket(new SChangeBlockPacket(this.world, bottom ? blockPos.up() : blockPos.down()));
            } else if (data.getBlock() instanceof TrapDoorBlock) {
                this.player.connection.sendPacket(new SChangeBlockPacket(this.world, blockPos));
            }
        } else if (!iblockdata.isAir()) {
            if (forgeEvent.getUseBlock() != net.minecraftforge.eventbus.api.Event.Result.DENY) {
                iblockdata.onBlockClicked(this.world, blockPos, this.player);
            }
            f = iblockdata.getPlayerRelativeBlockHardness(this.player, this.player.world, blockPos);
            this.world.extinguishFire(null, blockPos, direction);
        }
        if (event.useItemInHand() == Event.Result.DENY) {
            if (f > 1.0f) {
                this.player.connection.sendPacket(new SChangeBlockPacket(this.world, blockPos));
            }
            return;
        }
        BlockDamageEvent blockEvent = CraftEventFactory.callBlockDamageEvent(this.player, blockPos.getX(), blockPos.getY(), blockPos.getZ(), this.player.inventory.getCurrentItem(), f >= 1.0f);
        if (blockEvent.isCancelled()) {
            this.player.connection.sendPacket(new SChangeBlockPacket(this.world, blockPos));
            return;
        }
        if (blockEvent.getInstaBreak()) {
            f = 2.0f;
        }
        if (!iblockdata.isAir() && f >= 1.0f) {
            this.bridge$creativeHarvestBlock(blockPos, action, "insta mine");
        } else {
            if (this.isDestroyingBlock) {
                this.player.connection.sendPacket(this.bridge$diggingPacket(this.destroyPos, this.world.getBlockState(this.destroyPos), CPlayerDiggingPacket.Action.START_DESTROY_BLOCK, false, "abort destroying since another started (client insta mine, server disagreed)"));
            }
            this.isDestroyingBlock = true;
            this.destroyPos = blockPos;
            int j = (int) (f * 10.0f);
            this.world.sendBlockBreakProgress(this.player.getEntityId(), blockPos, j);
            this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, true, "actual start of destroying"));
            this.durabilityRemainingOnBlock = j;
        }
    } else if (action == CPlayerDiggingPacket.Action.STOP_DESTROY_BLOCK) {
        if (blockPos.equals(this.destroyPos)) {
            int k = this.ticks - this.initialDamage;
            BlockState iblockdata = this.world.getBlockState(blockPos);
            if (!iblockdata.isAir()) {
                float f2 = iblockdata.getPlayerRelativeBlockHardness(this.player, this.player.world, blockPos) * (k + 1);
                if (f2 >= 0.7f) {
                    this.isDestroyingBlock = false;
                    this.world.sendBlockBreakProgress(this.player.getEntityId(), blockPos, -1);
                    this.bridge$creativeHarvestBlock(blockPos, action, "destroyed");
                    return;
                }
                if (!this.receivedFinishDiggingPacket) {
                    this.isDestroyingBlock = false;
                    this.receivedFinishDiggingPacket = true;
                    this.delayedDestroyPos = blockPos;
                    this.initialBlockDamage = this.initialDamage;
                }
            }
        }
        this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, true, "stopped destroying"));
    } else if (action == CPlayerDiggingPacket.Action.ABORT_DESTROY_BLOCK) {
        this.isDestroyingBlock = false;
        if (!Objects.equals(this.destroyPos, blockPos)) {
            ArclightMod.LOGGER.debug("Mismatch in destroy block pos: " + this.destroyPos + " " + blockPos);
            this.world.sendBlockBreakProgress(this.player.getEntityId(), this.destroyPos, -1);
            this.player.connection.sendPacket(this.bridge$diggingPacket(this.destroyPos, this.world.getBlockState(this.destroyPos), action, true, "aborted mismatched destroying"));
        }
        this.world.sendBlockBreakProgress(this.player.getEntityId(), blockPos, -1);
        this.player.connection.sendPacket(this.bridge$diggingPacket(blockPos, this.world.getBlockState(blockPos), action, true, "aborted destroying"));
    }
}
Also used : SChangeBlockPacket(net.minecraft.network.play.server.SChangeBlockPacket) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) TileEntity(net.minecraft.tileentity.TileEntity) TrapDoorBlock(net.minecraft.block.TrapDoorBlock) BlockState(net.minecraft.block.BlockState) BlockDamageEvent(org.bukkit.event.block.BlockDamageEvent) TrapDoorBlock(net.minecraft.block.TrapDoorBlock) DoorBlock(net.minecraft.block.DoorBlock) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

BlockState (net.minecraft.block.BlockState)13 DoorBlock (net.minecraft.block.DoorBlock)13 BlockPos (net.minecraft.util.math.BlockPos)9 Direction (net.minecraft.util.Direction)6 FenceBlock (net.minecraft.block.FenceBlock)4 TrapDoorBlock (net.minecraft.block.TrapDoorBlock)4 Overwrite (org.spongepowered.asm.mixin.Overwrite)4 SChangeBlockPacket (net.minecraft.network.play.server.SChangeBlockPacket)3 PathPoint (net.minecraft.pathfinding.PathPoint)3 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)3 ServerPlayerEntityBridge (io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge)2 CakeBlock (net.minecraft.block.CakeBlock)2 INamedContainerProvider (net.minecraft.inventory.container.INamedContainerProvider)2 ItemUseContext (net.minecraft.item.ItemUseContext)2 Path (net.minecraft.pathfinding.Path)2 ActionResultType (net.minecraft.util.ActionResultType)2 AbstractRailBlock (net.minecraft.block.AbstractRailBlock)1 Block (net.minecraft.block.Block)1 WallBlock (net.minecraft.block.WallBlock)1 Material (net.minecraft.block.material.Material)1