use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.
the class ItemSextant method use.
@Nonnull
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, @Nonnull InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (!player.isShiftKeyDown()) {
BlockHitResult rtr = ToolCommons.raytraceFromEntity(player, 128, false);
if (rtr.getType() == HitResult.Type.BLOCK) {
if (!world.isClientSide) {
BlockPos pos = rtr.getBlockPos();
ItemNBTHelper.setInt(stack, TAG_SOURCE_X, pos.getX());
ItemNBTHelper.setInt(stack, TAG_SOURCE_Y, pos.getY());
ItemNBTHelper.setInt(stack, TAG_SOURCE_Z, pos.getZ());
}
player.startUsingItem(hand);
}
} else {
reset(world, stack);
}
return InteractionResultHolder.success(stack);
}
use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.
the class PlayerHelper method substituteUseTrackPos.
/**
* Temporarily swap <code>toUse</code> into the hand of the player, use it, then swap it back out.
* This is to ensure that any code in mods' onItemUse that relies on player.getHeldItem(ctx.hand) == ctx.stack
* will work as intended.
* Properly handles null players, as long as the Item's onItemUse also handles them.
*
* @return The usage result, as well as a properly offset block position pointing at where the block was placed
* (if the item was a BlockItem)
*/
public static Pair<InteractionResult, BlockPos> substituteUseTrackPos(UseOnContext ctx, ItemStack toUse) {
ItemStack save = ItemStack.EMPTY;
BlockHitResult hit = new BlockHitResult(ctx.getClickLocation(), ctx.getClickedFace(), ctx.getClickedPos(), ctx.isInside());
UseOnContext newCtx;
if (ctx.getPlayer() != null) {
save = ctx.getPlayer().getItemInHand(ctx.getHand());
ctx.getPlayer().setItemInHand(ctx.getHand(), toUse);
// Need to construct a new one still to refresh the itemstack
newCtx = new UseOnContext(ctx.getPlayer(), ctx.getHand(), hit);
} else {
newCtx = new ItemUseContextWithNullPlayer(ctx.getLevel(), ctx.getHand(), toUse, hit);
}
BlockPos finalPos = new BlockPlaceContext(newCtx).getClickedPos();
InteractionResult result = toUse.useOn(newCtx);
if (ctx.getPlayer() != null) {
ctx.getPlayer().setItemInHand(ctx.getHand(), save);
}
return Pair.of(result, finalPos);
}
use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.
the class EntityBabylonWeapon method tick.
@Override
public void tick() {
Entity thrower = getOwner();
if (!level.isClientSide && (thrower == null || !(thrower instanceof Player) || !thrower.isAlive())) {
discard();
return;
}
Player player = (Player) thrower;
if (!level.isClientSide) {
ItemStack stack = PlayerHelper.getFirstHeldItem(player, ModItems.kingKey);
boolean newCharging = !stack.isEmpty() && ItemKingKey.isCharging(stack);
if (isCharging() != newCharging) {
setCharging(newCharging);
}
}
Vec3 mot = getDeltaMovement();
int liveTime = getLiveTicks();
int delay = getDelay();
boolean charging = isCharging() && liveTime == 0;
if (charging) {
setDeltaMovement(Vec3.ZERO);
int chargeTime = getChargeTicks();
setChargeTicks(chargeTime + 1);
if (level.random.nextInt(20) == 0) {
level.playSound(null, getX(), getY(), getZ(), ModSounds.babylonSpawn, SoundSource.PLAYERS, 0.1F, 1F + level.random.nextFloat() * 3F);
}
} else {
if (liveTime < delay) {
setDeltaMovement(Vec3.ZERO);
} else if (liveTime == delay && player != null) {
Vec3 playerLook;
BlockHitResult rtr = ToolCommons.raytraceFromEntity(player, 64, true);
if (rtr.getType() != HitResult.Type.BLOCK) {
playerLook = player.getLookAngle().scale(64).add(player.position());
} else {
playerLook = Vec3.atCenterOf(rtr.getBlockPos());
}
Vec3 thisVec = VecHelper.fromEntityCenter(this);
mot = playerLook.subtract(thisVec.x, thisVec.y, thisVec.z).normalize().scale(2);
level.playSound(null, getX(), getY(), getZ(), ModSounds.babylonAttack, SoundSource.PLAYERS, 2F, 0.1F + level.random.nextFloat() * 3F);
}
if (!level.isClientSide) {
setLiveTicks(liveTime + 1);
AABB axis = new AABB(getX(), getY(), getZ(), xOld, yOld, zOld).inflate(2);
List<LivingEntity> entities = level.getEntitiesOfClass(LivingEntity.class, axis);
for (LivingEntity living : entities) {
if (living == thrower) {
continue;
}
if (living.hurtTime == 0) {
if (player != null) {
living.hurt(DamageSource.playerAttack(player), 20);
} else {
living.hurt(DamageSource.GENERIC, 20);
}
onHit(new EntityHitResult(living));
return;
}
}
}
}
super.tick();
// Apply after super tick so drag is not applied by super
setDeltaMovement(mot);
if (level.isClientSide && liveTime > delay) {
WispParticleData data = WispParticleData.wisp(0.3F, 1F, 1F, 0F, 1);
level.addParticle(data, getX(), getY(), getZ(), 0, -0F, 0);
}
if (!level.isClientSide && liveTime > 200 + delay) {
discard();
}
}
use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.
the class LensWarp method collideBurst.
@Override
public boolean collideBurst(IManaBurst burst, HitResult pos, boolean isManaBlock, boolean shouldKill, ItemStack stack) {
Entity entity = burst.entity();
if (entity.level.isClientSide || burst.isFake() || pos.getType() != HitResult.Type.BLOCK) {
return shouldKill;
}
BlockPos hit = ((BlockHitResult) pos).getBlockPos();
if (entity.level.getBlockState(hit).is(ModBlocks.pistonRelay)) {
BlockPistonRelay.WorldData data = BlockPistonRelay.WorldData.get(entity.level);
BlockPos dest = data.mapping.get(hit);
if (dest != null) {
entity.setPos(dest.getX() + 0.5, dest.getY() + 0.5, dest.getZ() + 0.5);
burst.setCollidedAt(dest);
burst.setWarped(true);
return false;
}
}
return shouldKill;
}
use of net.minecraft.world.phys.BlockHitResult in project refinedstorage by refinedmods.
the class StorageMonitorBlock method attack.
@Override
@SuppressWarnings("deprecation")
public void attack(BlockState state, Level level, BlockPos pos, Player player) {
super.attack(state, level, pos, player);
if (!level.isClientSide) {
HitResult result = LevelUtils.rayTracePlayer(level, player);
if (!(result instanceof BlockHitResult)) {
return;
}
((StorageMonitorBlockEntity) level.getBlockEntity(pos)).getNode().extract(player, ((BlockHitResult) result).getDirection());
}
}
Aggregations