Search in sources :

Example 81 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project COMP5541TeamZombieCode by yum-feng.

the class ThrowablePokemonItem method onHit.

protected void onHit(HitResult r) {
    super.onHit(r);
    if (!this.level.isClientSide) {
        this.level.broadcastEntityEvent(this, (byte) 1);
        this.discard();
        if (r.getType() == HitResult.Type.BLOCK) {
            BlockHitResult bhr = (BlockHitResult) r;
            BlockPos bp = bhr.getBlockPos();
            String registryName = this.getItem().getItem().getRegistryName().toString();
            String animalItemName = registryName.substring(registryName.indexOf(":") + 1, registryName.indexOf("_"));
            var entityType = EntityType.byString(animalItemName);
            var newPokemon = entityType.get().spawn((ServerLevel) this.getLevel(), null, null, bp.above(), MobSpawnType.SPAWN_EGG, false, false);
            if (newPokemon == null) {
                System.out.println("spawn did not work");
            } else {
                System.out.println(newPokemon.getPosition(0).toString());
            }
        }
    }
    System.out.println(r.toString());
}
Also used : BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 82 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MobZReborn by rikka0w0.

the class FrozenMeal method useOn.

@Override
public InteractionResult useOn(UseOnContext context) {
    Level world = context.getLevel();
    Player player = context.getPlayer();
    ItemStack handItem = player.getMainHandItem();
    BlockPos pos = context.getClickedPos();
    BlockState blockState = world.getBlockState(pos);
    BlockHitResult blockHitResult = getPlayerPOVHitResult(world, player, ClipContext.Fluid.SOURCE_ONLY);
    BlockPos blockPos = blockHitResult.getBlockPos();
    BlockState blockBlock = world.getBlockState(blockPos);
    if (blockBlock.getBlock() instanceof BucketPickup && !world.isClientSide) {
        world.setBlock(blockPos, Blocks.ICE.defaultBlockState(), 3);
        handItem.shrink(1);
        return InteractionResult.SUCCESS;
    }
    Random random = world.getRandom();
    if (((blockState.getBlock() == Blocks.WATER || blockState.getBlock() == Blocks.ICE) && world.isClientSide)) {
        for (int i = 0; i < 16; ++i) {
            double d = random.nextGaussian() * 0.02D;
            double e = random.nextGaussian() * 0.02D;
            double f = random.nextGaussian() * 0.02D;
            world.addParticle(ParticleTypes.HAPPY_VILLAGER, (double) ((float) pos.getX() + random.nextFloat()), (double) pos.getY() + (double) random.nextFloat() * blockState.getShape(world, pos).max(Direction.Axis.Y), (double) ((float) pos.getZ() + random.nextFloat()), d, e, f);
        }
        return InteractionResult.SUCCESS;
    }
    if (blockState.getBlock() == Blocks.ICE && !world.isClientSide) {
        if (world.getBlockState(pos.east()).getBlock().equals(Blocks.ICE)) {
            world.setBlock(pos.east(), Blocks.BLUE_ICE.defaultBlockState(), 3);
        }
        if (world.getBlockState(pos.south()).getBlock().equals(Blocks.ICE)) {
            world.setBlock(pos.south(), Blocks.BLUE_ICE.defaultBlockState(), 3);
        }
        if (world.getBlockState(pos.north()).getBlock().equals(Blocks.ICE)) {
            world.setBlock(pos.north(), Blocks.BLUE_ICE.defaultBlockState(), 3);
        }
        if (world.getBlockState(pos.west()).getBlock().equals(Blocks.ICE)) {
            world.setBlock(pos.west(), Blocks.BLUE_ICE.defaultBlockState(), 3);
        }
        world.setBlock(pos, Blocks.BLUE_ICE.defaultBlockState(), 3);
        handItem.shrink(1);
        return InteractionResult.SUCCESS;
    } else {
        return InteractionResult.PASS;
    }
}
Also used : BucketPickup(net.minecraft.world.level.block.BucketPickup) Player(net.minecraft.world.entity.player.Player) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 83 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method handleCollideImpactEvent.

public static boolean handleCollideImpactEvent(final net.minecraft.world.entity.Entity projectile, @Nullable final ProjectileSource projectileSource, final HitResult movingObjectPosition) {
    final HitResult.Type movingObjectType = movingObjectPosition.getType();
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(projectile);
        frame.addContext(EventContextKeys.PROJECTILE_SOURCE, projectileSource == null ? UnknownProjectileSource.UNKNOWN : projectileSource);
        final Optional<UUID> creator = PhaseTracker.getInstance().getPhaseContext().getCreator();
        creator.ifPresent(user -> frame.addContext(EventContextKeys.CREATOR, user));
        final ServerLocation impactPoint = ServerLocation.of((ServerWorld) projectile.level, VecHelper.toVector3d(movingObjectPosition.getLocation()));
        boolean cancelled = false;
        if (movingObjectType == HitResult.Type.BLOCK) {
            final BlockHitResult blockMovingObjectPosition = (BlockHitResult) movingObjectPosition;
            final BlockPos blockPos = blockMovingObjectPosition.getBlockPos();
            if (blockPos.getY() <= 0) {
                return false;
            }
            final BlockSnapshot targetBlock = ((ServerWorld) projectile.level).createSnapshot(blockPos.getX(), blockPos.getY(), blockPos.getZ());
            final Direction side = DirectionFacingProvider.INSTANCE.getKey(blockMovingObjectPosition.getDirection()).get();
            final CollideBlockEvent.Impact event = SpongeEventFactory.createCollideBlockEventImpact(frame.currentCause(), impactPoint, targetBlock.state(), targetBlock.location().get(), side);
            cancelled = SpongeCommon.post(event);
            // Track impact block if event is not cancelled
            if (!cancelled && creator.isPresent()) {
                final BlockPos targetPos = VecHelper.toBlockPos(impactPoint.blockPosition());
                final LevelChunkBridge spongeChunk = (LevelChunkBridge) projectile.level.getChunkAt(targetPos);
                spongeChunk.bridge$addTrackedBlockPosition((Block) targetBlock.state().type(), targetPos, creator.get(), PlayerTracker.Type.NOTIFIER);
            }
        } else if (movingObjectType == HitResult.Type.ENTITY) {
            // entity
            final EntityHitResult entityMovingObjectPosition = (EntityHitResult) movingObjectPosition;
            final ArrayList<Entity> entityList = new ArrayList<>();
            entityList.add((Entity) entityMovingObjectPosition.getEntity());
            final CollideEntityEvent.Impact event = SpongeEventFactory.createCollideEntityEventImpact(frame.currentCause(), entityList, impactPoint);
            cancelled = SpongeCommon.post(event);
        }
        return cancelled;
    }
}
Also used : JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) Entity(org.spongepowered.api.entity.Entity) LivingEntity(net.minecraft.world.entity.LivingEntity) ServerLocation(org.spongepowered.api.world.server.ServerLocation) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) ArrayList(java.util.ArrayList) Direction(org.spongepowered.api.util.Direction) CollideBlockEvent(org.spongepowered.api.event.block.CollideBlockEvent) EntityHitResult(net.minecraft.world.phys.EntityHitResult) BlockHitResult(net.minecraft.world.phys.BlockHitResult) EntityHitResult(net.minecraft.world.phys.EntityHitResult) HitResult(net.minecraft.world.phys.HitResult) ServerWorld(org.spongepowered.api.world.server.ServerWorld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) UUID(java.util.UUID) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 84 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project SpongeCommon by SpongePowered.

the class AbstractArrowMixin method onProjectileHit.

/**
 * Collide impact event post for plugins to cancel impact.
 */
@Inject(method = "onHitBlock", at = @At("HEAD"), cancellable = true)
private void onProjectileHit(final BlockHitResult hitResult, final CallbackInfo ci) {
    if (!((LevelBridge) this.level).bridge$isFake() && hitResult.getType() != HitResult.Type.MISS) {
        if (SpongeCommonEventFactory.handleCollideImpactEvent((AbstractArrow) (Object) this, this.impl$getProjectileSource(), hitResult)) {
            this.shadow$playSound(SoundEvents.ARROW_HIT, 1.0F, 1.2F / (this.random.nextFloat() * 0.2F + 0.9F));
            // Make it almost look like it collided with something
            final BlockHitResult blockraytraceresult = (BlockHitResult) hitResult;
            final BlockState blockstate = this.level.getBlockState(blockraytraceresult.getBlockPos());
            this.lastState = blockstate;
            final Vec3 vec3d = blockraytraceresult.getLocation().subtract(this.shadow$getX(), this.shadow$getY(), this.shadow$getZ());
            this.shadow$setDeltaMovement(vec3d);
            final Vec3 vec3d1 = vec3d.normalize().scale(0.05F);
            this.shadow$setPos(this.shadow$getX() - vec3d1.x, this.shadow$getY() - vec3d1.y, this.shadow$getZ() - vec3d1.z);
            this.inGround = true;
            this.shakeTime = 7;
            this.shadow$setCritArrow(false);
            this.shadow$setPierceLevel((byte) 0);
            this.shadow$setShotFromCrossbow(false);
            this.resetPiercedEntities();
            ci.cancel();
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Vec3(net.minecraft.world.phys.Vec3) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 85 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method forceInteraction.

@Override
public void forceInteraction(Player player, Location location) {
    CraftPlayer craftPlayer = (CraftPlayer) player;
    BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    ((CraftBlock) location.getBlock()).getNMS().use(((CraftWorld) location.getWorld()).getHandle(), craftPlayer != null ? craftPlayer.getHandle() : null, InteractionHand.MAIN_HAND, new BlockHitResult(new Vec3(0, 0, 0), null, pos, false));
}
Also used : Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)181 BlockPos (net.minecraft.core.BlockPos)117 Vec3 (net.minecraft.world.phys.Vec3)79 BlockState (net.minecraft.world.level.block.state.BlockState)66 ItemStack (net.minecraft.world.item.ItemStack)63 HitResult (net.minecraft.world.phys.HitResult)51 Direction (net.minecraft.core.Direction)46 Level (net.minecraft.world.level.Level)41 ClipContext (net.minecraft.world.level.ClipContext)33 Player (net.minecraft.world.entity.player.Player)32 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