use of net.minecraft.util.hit.BlockHitResult in project Paradise-Lost by devs-immortal.
the class VialItem method use.
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack itemStack = user.getStackInHand(hand);
BlockHitResult hitResult = raycast(world, user, this.fluid == Fluids.EMPTY ? RaycastContext.FluidHandling.SOURCE_ONLY : RaycastContext.FluidHandling.NONE);
if (hitResult.getType() == HitResult.Type.BLOCK) {
BlockPos hitPos = hitResult.getBlockPos();
if (this.fluid != Fluids.EMPTY && world.getBlockState(hitPos.up()).canReplace(new ItemPlacementContext(user, hand, itemStack, hitResult))) {
world.setBlockState(hitPos.up(), fluid.getDefaultState().getBlockState());
if (!user.isCreative()) {
itemStack.decrement(1);
}
return TypedActionResult.success(itemStack, world.isClient());
}
}
return TypedActionResult.fail(itemStack);
}
use of net.minecraft.util.hit.BlockHitResult in project LittleMaidReBirth-Fabric by SistrScarlet.
the class TorcherMode method tick.
@Override
public void tick() {
// 5秒経過しても置けない、または明るい地点を無視
if (100 < ++this.timeToIgnore || 8 < mob.world.getLightLevel(placePos.up())) {
this.placePos = null;
this.timeToIgnore = 0;
return;
}
// 距離が遠すぎる場合は無視
if (distance * 2F < placePos.getManhattanDistance(mob.getBlockPos())) {
this.placePos = null;
return;
}
Item item = mob.getMainHandStack().getItem();
if (!(item instanceof BlockItem)) {
return;
}
if (3 * 3 < this.mob.squaredDistanceTo(placePos.getX(), placePos.getY(), placePos.getZ())) {
if (--timeToRecalcPath < 0) {
timeToRecalcPath = 20;
Path path = this.mob.getNavigation().findPathTo(placePos.getX(), placePos.getY(), placePos.getZ(), 3);
this.mob.getNavigation().startMovingAlong(path, 1);
}
return;
}
Vec3d start = mob.getCameraPosVec(1F);
// 終端はブロックの上面
Vec3d end = new Vec3d(placePos.getX() + 0.5D, placePos.getY() + 1D, placePos.getZ() + 0.5D);
BlockHitResult result = mob.world.raycast(new RaycastContext(start, end, RaycastContext.ShapeType.OUTLINE, RaycastContext.FluidHandling.NONE, this.mob));
FakePlayer fakePlayer = mob.getFakePlayer();
if (((BlockItem) item).place(new ItemPlacementContext(new ItemUsageContext(fakePlayer, Hand.MAIN_HAND, result))).shouldSwingHand()) {
mob.swingHand(Hand.MAIN_HAND);
if (mob instanceof SoundPlayable) {
((SoundPlayable) mob).play(LMSounds.INSTALLATION);
}
}
this.placePos = null;
}
use of net.minecraft.util.hit.BlockHitResult in project LittleMaidReBirth-Fabric by SistrScarlet.
the class IFFCopyBookItem method use.
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
if (world.isClient) {
return super.use(world, user, hand);
}
ItemStack stack = user.getStackInHand(hand);
Vec3d start = user.getCameraPosVec(1F);
Vec3d end = start.add(user.getRotationVector().multiply(4D));
BlockHitResult bResult = world.raycast(new RaycastContext(start, end, RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.NONE, user));
if (bResult.getType() != HitResult.Type.MISS) {
end = bResult.getPos();
}
Box box = new Box(start, end).expand(1);
EntityHitResult eResult = ProjectileUtil.getEntityCollision(world, user, start, end, box, entity -> entity instanceof HasIFF);
if (eResult == null || eResult.getType() == HitResult.Type.MISS)
return super.use(world, user, hand);
Entity target = eResult.getEntity();
if (user.isSneaking()) {
ListTag list = new ListTag();
((HasIFF) target).getIFFs().forEach(iff -> list.add(iff.writeTag()));
CompoundTag tag = stack.getOrCreateTag();
tag.put("IFFs", list);
user.sendMessage(new TranslatableText("item.littlemaidrebirth.iff_copy_book.message_written"), true);
} else {
CompoundTag tag = stack.getOrCreateTag();
if (!tag.contains("IFFs")) {
return super.use(world, user, hand);
}
ListTag list = tag.getList("IFFs", 10);
((HasIFF) target).setIFFs(list.stream().map(t -> (CompoundTag) t).map(t -> IFFTypeManager.getINSTANCE().loadIFF(t)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()));
user.sendMessage(new TranslatableText("item.littlemaidrebirth.iff_copy_book.message_apply"), true);
}
user.world.playSound(null, user.getX(), user.getY(), user.getZ(), SoundEvents.BLOCK_NOTE_BLOCK_PLING, SoundCategory.PLAYERS, 1F, 1F);
return TypedActionResult.success(stack);
}
Aggregations