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());
}
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;
}
}
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;
}
}
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();
}
}
}
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));
}
Aggregations