Search in sources :

Example 6 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Tropicraft by Tropicraft.

the class TropicraftSpawnEgg method use.

public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
    ItemStack heldItem = player.getItemInHand(hand);
    if (world.isClientSide) {
        return InteractionResultHolder.pass(heldItem);
    } else {
        HitResult rayTraceResult = getPlayerPOVHitResult(world, player, ClipContext.Fluid.SOURCE_ONLY);
        if (rayTraceResult.getType() != HitResult.Type.BLOCK) {
            return InteractionResultHolder.pass(heldItem);
        } else {
            BlockHitResult traceResult = (BlockHitResult) rayTraceResult;
            BlockPos tracePos = traceResult.getBlockPos();
            if (!(world.getBlockState(tracePos).getBlock() instanceof LiquidBlock)) {
                return InteractionResultHolder.pass(heldItem);
            } else if (world.mayInteract(player, tracePos) && player.mayUseItemAt(tracePos, traceResult.getDirection(), heldItem)) {
                EntityType<?> type = typeIn.get();
                if (type.spawn((ServerLevel) world, heldItem, player, tracePos, MobSpawnType.SPAWN_EGG, false, false) == null) {
                    return InteractionResultHolder.pass(heldItem);
                } else {
                    if (!player.getAbilities().instabuild) {
                        heldItem.shrink(1);
                    }
                    player.awardStat(Stats.ITEM_USED.get(this));
                    return InteractionResultHolder.success(heldItem);
                }
            } else {
                return InteractionResultHolder.fail(heldItem);
            }
        }
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) EntityType(net.minecraft.world.entity.EntityType) BlockPos(net.minecraft.core.BlockPos) LiquidBlock(net.minecraft.world.level.block.LiquidBlock) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 7 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MagicPlugin by elBukkit.

the class CompatibilityUtils method setAutoBlockState.

@Override
public boolean setAutoBlockState(Block block, Location target, BlockFace facing, boolean physics, Player originator) {
    if (block == null || facing == null || target == null)
        return false;
    net.minecraft.world.level.block.state.BlockState blockState = ((CraftBlock) block).getNMS();
    if (blockState == null)
        return false;
    net.minecraft.world.level.block.Block nmsBlock = blockState.getBlock();
    ItemStack blockItem = new ItemStack(block.getType());
    ServerPlayer originatorHandle = ((CraftPlayer) originator).getHandle();
    ServerLevel world = ((CraftWorld) block.getWorld()).getHandle();
    Object item = platform.getItemUtils().getHandle(platform.getItemUtils().makeReal(blockItem));
    if (originatorHandle == null || world == null || item == null) {
        return false;
    }
    BlockPos blockPosition = new BlockPos(block.getX(), block.getY(), block.getZ());
    Vec3 vec3D = new Vec3(target.getX(), target.getY(), target.getZ());
    Direction direction;
    try {
        direction = Direction.valueOf(facing.name());
    } catch (Exception ex) {
        platform.getLogger().log(Level.SEVERE, "Could not translate to NMS direction: " + facing);
        return false;
    }
    BlockHitResult hitResult = new BlockHitResult(vec3D, direction, blockPosition, false);
    BlockPlaceContext actionContext = new BlockPlaceContext(originatorHandle, InteractionHand.MAIN_HAND, (net.minecraft.world.item.ItemStack) item, hitResult);
    net.minecraft.world.level.block.state.BlockState state = nmsBlock.getStateForPlacement(actionContext);
    if (state == null)
        return false;
    ((CraftBlock) block).setTypeAndData(state, physics);
    return true;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) CraftPlayer(org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer) CraftBlock(org.bukkit.craftbukkit.v1_17_R1.block.CraftBlock) Direction(net.minecraft.core.Direction) ServerPlayer(net.minecraft.server.level.ServerPlayer) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) ItemStack(org.bukkit.inventory.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CraftWorld(org.bukkit.craftbukkit.v1_17_R1.CraftWorld)

Example 8 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MagicPlugin by elBukkit.

the class CompatibilityUtils method setAutoBlockState.

@Override
public boolean setAutoBlockState(Block block, Location target, BlockFace facing, boolean physics, Player originator) {
    if (block == null || facing == null || target == null)
        return false;
    net.minecraft.world.level.block.state.BlockState blockState = ((CraftBlock) block).getNMS();
    if (blockState == null)
        return false;
    net.minecraft.world.level.block.Block nmsBlock = blockState.getBlock();
    ItemStack blockItem = new ItemStack(block.getType());
    ServerPlayer originatorHandle = ((CraftPlayer) originator).getHandle();
    ServerLevel world = ((CraftWorld) block.getWorld()).getHandle();
    Object item = platform.getItemUtils().getHandle(platform.getItemUtils().makeReal(blockItem));
    if (originatorHandle == null || world == null || item == null) {
        return false;
    }
    BlockPos blockPosition = new BlockPos(block.getX(), block.getY(), block.getZ());
    Vec3 vec3D = new Vec3(target.getX(), target.getY(), target.getZ());
    Direction direction;
    try {
        direction = Direction.valueOf(facing.name());
    } catch (Exception ex) {
        platform.getLogger().log(Level.SEVERE, "Could not translate to NMS direction: " + facing);
        return false;
    }
    BlockHitResult hitResult = new BlockHitResult(vec3D, direction, blockPosition, false);
    BlockPlaceContext actionContext = new BlockPlaceContext(originatorHandle, InteractionHand.MAIN_HAND, (net.minecraft.world.item.ItemStack) item, hitResult);
    net.minecraft.world.level.block.state.BlockState state = nmsBlock.getStateForPlacement(actionContext);
    if (state == null)
        return false;
    CraftBlock cBlock = (CraftBlock) block;
    CraftBlock.setTypeAndData(cBlock.getHandle(), cBlock.getPosition(), cBlock.getNMS(), state, physics);
    return true;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) CraftPlayer(org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer) CraftBlock(org.bukkit.craftbukkit.v1_18_R2.block.CraftBlock) Direction(net.minecraft.core.Direction) ServerPlayer(net.minecraft.server.level.ServerPlayer) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) ItemStack(org.bukkit.inventory.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld)

Example 9 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MagicPlugin by elBukkit.

the class CompatibilityUtils method setAutoBlockState.

@Override
public boolean setAutoBlockState(Block block, Location target, BlockFace facing, boolean physics, Player originator) {
    if (block == null || facing == null || target == null)
        return false;
    net.minecraft.world.level.block.state.BlockState blockState = ((CraftBlock) block).getNMS();
    if (blockState == null)
        return false;
    net.minecraft.world.level.block.Block nmsBlock = blockState.getBlock();
    ItemStack blockItem = new ItemStack(block.getType());
    ServerPlayer originatorHandle = ((CraftPlayer) originator).getHandle();
    ServerLevel world = ((CraftWorld) block.getWorld()).getHandle();
    Object item = platform.getItemUtils().getHandle(platform.getItemUtils().makeReal(blockItem));
    if (originatorHandle == null || world == null || item == null) {
        return false;
    }
    BlockPos blockPosition = new BlockPos(block.getX(), block.getY(), block.getZ());
    Vec3 vec3D = new Vec3(target.getX(), target.getY(), target.getZ());
    Direction direction;
    try {
        direction = Direction.valueOf(facing.name());
    } catch (Exception ex) {
        platform.getLogger().log(Level.SEVERE, "Could not translate to NMS direction: " + facing);
        return false;
    }
    BlockHitResult hitResult = new BlockHitResult(vec3D, direction, blockPosition, false);
    BlockPlaceContext actionContext = new BlockPlaceContext(originatorHandle, InteractionHand.MAIN_HAND, (net.minecraft.world.item.ItemStack) item, hitResult);
    net.minecraft.world.level.block.state.BlockState state = nmsBlock.getStateForPlacement(actionContext);
    if (state == null)
        return false;
    CraftBlock cBlock = (CraftBlock) block;
    CraftBlock.setTypeAndData(cBlock.getHandle(), cBlock.getPosition(), cBlock.getNMS(), state, physics);
    return true;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) CraftPlayer(org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer) CraftBlock(org.bukkit.craftbukkit.v1_18_R1.block.CraftBlock) Direction(net.minecraft.core.Direction) ServerPlayer(net.minecraft.server.level.ServerPlayer) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) ItemStack(org.bukkit.inventory.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 10 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project SolarCraftRepository by FINDERFEED.

the class SolarAbilities method struckLightning.

// 1
// public static void SummonFireball(ServerLevel world, Player enti) {
// if (enti.getPersistentData().getBoolean("solar_forge_can_player_use_fireball")) {
// if (!enti.isCreative()) {
// double mana = CapabilitySolarMana.getSolarMana(enti).orElseThrow(RuntimeException::new).getMana();
// CapabilitySolarMana.getSolarMana(enti).orElseThrow(RuntimeException::new).setMana(mana - 50);
// }
// LargeFireball fireball = new LargeFireball(world, enti, enti.getLookAngle().x, enti.getLookAngle().y, enti.getLookAngle().z);
// fireball.setPos(enti.position().x + enti.getLookAngle().x * 1.5, enti.position().y + enti.getLookAngle().y * 1.5, enti.position().z + enti.getLookAngle().z * 1.5);
// fireball.explosionPower = 6;
// world.addFreshEntity(fireball);
// }
// }
// 2
public static void struckLightning(ServerLevel world, Player entity) {
    if (entity.getPersistentData().getBoolean("solar_forge_can_player_use_lightning")) {
        if (!entity.isCreative()) {
            double mana = CapabilitySolarMana.getSolarMana(entity).orElseThrow(RuntimeException::new).getMana();
            CapabilitySolarMana.getSolarMana(entity).orElseThrow(RuntimeException::new).setMana(mana - 50);
        }
        Vec3 vec = entity.getLookAngle().multiply(200, 200, 200);
        ClipContext ctx = new ClipContext(entity.position().add(0, 1.5, 0), entity.position().add(0, 1.5, 0).add(vec), ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity);
        BlockHitResult result = world.clip(ctx);
        if (result.getType() == HitResult.Type.BLOCK) {
            BlockPos pos = result.getBlockPos();
            if (world.canSeeSky(pos.above())) {
                LightningBolt entityBolt = new LightningBolt(EntityType.LIGHTNING_BOLT, world);
                entityBolt.setPos(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
                world.addFreshEntity(entityBolt);
                world.explode(entity, pos.getX(), pos.getY(), pos.getZ(), 6, true, Explosion.BlockInteraction.BREAK);
                System.out.println(entityBolt.position());
            }
        }
    }
}
Also used : LightningBolt(net.minecraft.world.entity.LightningBolt) ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)178 BlockPos (net.minecraft.core.BlockPos)115 Vec3 (net.minecraft.world.phys.Vec3)78 BlockState (net.minecraft.world.level.block.state.BlockState)65 ItemStack (net.minecraft.world.item.ItemStack)61 HitResult (net.minecraft.world.phys.HitResult)51 Direction (net.minecraft.core.Direction)46 Level (net.minecraft.world.level.Level)40 ClipContext (net.minecraft.world.level.ClipContext)32 Player (net.minecraft.world.entity.player.Player)31 ServerLevel (net.minecraft.server.level.ServerLevel)23 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)20 Entity (net.minecraft.world.entity.Entity)19 InteractionHand (net.minecraft.world.InteractionHand)18 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)18 ServerPlayer (net.minecraft.server.level.ServerPlayer)17 LivingEntity (net.minecraft.world.entity.LivingEntity)17 AABB (net.minecraft.world.phys.AABB)17 EntityHitResult (net.minecraft.world.phys.EntityHitResult)17 Minecraft (net.minecraft.client.Minecraft)16