use of net.minecraft.world.level.ChunkPos in project MinecraftForge by MinecraftForge.
the class ForgeChunkManager method forceChunk.
/**
* Forces a chunk to be loaded for the given mod with the given "owner".
*
* @param add {@code true} to force the chunk, {@code false} to unforce the chunk.
*
* @implNote Based on {@link ServerLevel#setChunkForced(int, int, boolean)}
*/
private static <T extends Comparable<? super T>> boolean forceChunk(ServerLevel world, String modId, T owner, int chunkX, int chunkZ, boolean add, boolean ticking, TicketType<TicketOwner<T>> type, Function<ForcedChunksSavedData, TicketTracker<T>> ticketGetter) {
if (!ModList.get().isLoaded(modId)) {
LOGGER.warn("A mod attempted to force a chunk for an unloaded mod of id: {}", modId);
return false;
}
ForcedChunksSavedData saveData = world.getDataStorage().computeIfAbsent(ForcedChunksSavedData::load, ForcedChunksSavedData::new, "chunks");
ChunkPos pos = new ChunkPos(chunkX, chunkZ);
long chunk = pos.toLong();
TicketTracker<T> tickets = ticketGetter.apply(saveData);
TicketOwner<T> ticketOwner = new TicketOwner<>(modId, owner);
boolean success;
if (add) {
success = tickets.add(ticketOwner, chunk, ticking);
if (success)
world.getChunk(chunkX, chunkZ);
} else {
success = tickets.remove(ticketOwner, chunk, ticking);
}
if (success) {
saveData.setDirty(true);
forceChunk(world, pos, type, ticketOwner, add, ticking);
}
return success;
}
use of net.minecraft.world.level.ChunkPos in project MinecraftForge by MinecraftForge.
the class ModelDataManager method requestModelDataRefresh.
public static void requestModelDataRefresh(BlockEntity te) {
Preconditions.checkNotNull(te, "Tile entity must not be null");
Level world = te.getLevel();
cleanCaches(world);
needModelDataRefresh.computeIfAbsent(new ChunkPos(te.getBlockPos()), $ -> Collections.synchronizedSet(new HashSet<>())).add(te.getBlockPos());
}
use of net.minecraft.world.level.ChunkPos in project SpongeCommon by SpongePowered.
the class VolumeStreamUtils method getElementByPosition.
private static <T> Function<ChunkAccess, Stream<Map.Entry<BlockPos, T>>> getElementByPosition(final TriFunction<ChunkAccess, LevelChunkSection, BlockPos, T> elementAccessor, final Vector3i min, final Vector3i max) {
// Build the min and max
final ChunkCursor minCursor = new ChunkCursor(min);
final ChunkCursor maxCursor = new ChunkCursor(max);
return chunk -> {
final ChunkPos pos = chunk.getPos();
final int xStart = pos.x == minCursor.chunkX ? minCursor.xOffset : 0;
// 16 because IntStream.range is upper range exclusive
final int xEnd = pos.x == maxCursor.chunkX ? maxCursor.xOffset + 1 : 16;
final int zStart = pos.z == minCursor.chunkZ ? minCursor.zOffset : 0;
// 16 because IntStream.range is upper range exclusive
final int zEnd = pos.z == maxCursor.chunkZ ? maxCursor.zOffset + 1 : 16;
final int chunkMinX = pos.x << 4;
final int chunkMinZ = pos.z << 4;
return Arrays.stream(chunk.getSections()).filter(Objects::nonNull).filter(chunkSection -> chunkSection.bottomBlockY() >= minCursor.ySection && chunkSection.bottomBlockY() <= maxCursor.ySection).flatMap(chunkSection -> IntStream.range(zStart, zEnd).mapToObj(z -> IntStream.range(xStart, xEnd).mapToObj(x -> {
final int sectionY = chunkSection.bottomBlockY();
final int yStart = sectionY == minCursor.ySection ? minCursor.yOffset : 0;
// plus 1 because of IntStream range exclusive
final int yEnd = sectionY == maxCursor.ySection ? maxCursor.yOffset + 1 : 16;
return IntStream.range(yStart, yEnd).mapToObj(y -> {
final int adjustedX = x + chunkMinX;
final int adjustedY = y + sectionY;
final int adjustedZ = z + chunkMinZ;
final BlockPos blockPos = new BlockPos(adjustedX, adjustedY, adjustedZ);
final T apply = Objects.requireNonNull(elementAccessor.apply(chunk, chunkSection, blockPos), "Element cannot be null");
return new AbstractMap.SimpleEntry<>(blockPos, apply);
});
})).flatMap(Function.identity()).flatMap(Function.identity()));
};
}
use of net.minecraft.world.level.ChunkPos in project SpongeCommon by SpongePowered.
the class ServerPlayerMixin method teleportTo.
/*
@Inject(method = "markPlayerActive()V", at = @At("HEAD"))
private void impl$onPlayerActive(final CallbackInfo ci) {
((ServerPlayNetHandlerBridge) this.connection).bridge$resendLatestResourcePackRequest();
}
*/
/**
* @author zidane - November 21st, 2020 - Minecraft 1.15
* @reason Ensure that the teleport hook honors our events
*/
@Overwrite
public void teleportTo(final net.minecraft.server.level.ServerLevel world, final double x, final double y, final double z, final float yaw, final float pitch) {
final net.minecraft.server.level.ServerPlayer player = (net.minecraft.server.level.ServerPlayer) (Object) this;
double actualX = x;
double actualY = y;
double actualZ = z;
double actualYaw = yaw;
double actualPitch = pitch;
final boolean hasMovementContext = PhaseTracker.getCauseStackManager().currentContext().containsKey(EventContextKeys.MOVEMENT_TYPE);
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
if (!hasMovementContext) {
frame.addContext(EventContextKeys.MOVEMENT_TYPE, MovementTypes.PLUGIN);
}
if (world == player.level) {
@Nullable final Vector3d destination = this.impl$fireMoveEvent(PhaseTracker.SERVER, new Vector3d(x, y, z));
if (destination == null) {
return;
}
actualX = destination.x();
actualY = destination.y();
actualZ = destination.z();
if (ShouldFire.ROTATE_ENTITY_EVENT) {
final RotateEntityEvent rotateEvent = SpongeEventFactory.createRotateEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) player, new Vector3d(actualPitch, actualYaw, 0), new Vector3d(pitch, yaw, 0));
SpongeCommon.post(rotateEvent);
actualYaw = rotateEvent.isCancelled() ? player.yRot : rotateEvent.toRotation().y();
actualPitch = rotateEvent.isCancelled() ? player.xRot : rotateEvent.toRotation().x();
}
this.shadow$setCamera(player);
this.shadow$stopRiding();
if (player.isSleeping()) {
player.stopSleepInBed(true, true);
}
player.connection.teleport(actualX, actualY, actualZ, (float) actualYaw, (float) actualPitch);
player.setYHeadRot((float) actualYaw);
final ChunkPos chunkpos = new ChunkPos(new BlockPos(actualX, actualY, actualZ));
world.getChunkSource().addRegionTicket(TicketType.POST_TELEPORT, chunkpos, 1, player.getId());
} else {
final ChangeEntityWorldEvent.Pre preEvent = PlatformHooks.INSTANCE.getEventHooks().callChangeEntityWorldEventPre(player, world);
if (SpongeCommon.post(preEvent)) {
return;
}
if (ShouldFire.MOVE_ENTITY_EVENT) {
final MoveEntityEvent posEvent = SpongeEventFactory.createChangeEntityWorldEventReposition(frame.currentCause(), (org.spongepowered.api.entity.Entity) player, preEvent.originalWorld(), VecHelper.toVector3d(player.position()), new Vector3d(x, y, z), preEvent.originalDestinationWorld(), new Vector3d(x, y, z), preEvent.destinationWorld());
if (SpongeCommon.post(posEvent)) {
return;
}
actualX = posEvent.destinationPosition().x();
actualY = posEvent.destinationPosition().y();
actualZ = posEvent.destinationPosition().z();
}
this.shadow$setPos(actualX, actualY, actualZ);
if (ShouldFire.ROTATE_ENTITY_EVENT) {
final RotateEntityEvent rotateEvent = SpongeEventFactory.createRotateEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) player, new Vector3d(actualYaw, actualPitch, 0), new Vector3d(yaw, pitch, 0));
if (!SpongeCommon.post(rotateEvent)) {
actualYaw = (float) rotateEvent.toRotation().x();
actualPitch = (float) rotateEvent.toRotation().y();
}
}
this.yRot = (float) actualYaw;
this.xRot = (float) actualPitch;
EntityUtil.performPostChangePlayerWorldLogic(player, (net.minecraft.server.level.ServerLevel) preEvent.originalWorld(), (net.minecraft.server.level.ServerLevel) preEvent.originalDestinationWorld(), (net.minecraft.server.level.ServerLevel) preEvent.destinationWorld(), false);
}
}
}
use of net.minecraft.world.level.ChunkPos in project SpongeCommon by SpongePowered.
the class ServerPlayerMixin method impl$setLocation.
@Override
protected final boolean impl$setLocation(final boolean isChangeOfWorld, final ServerLevel originalDestination, final ServerLevel destinationWorld, final Vector3d destinationPosition) {
final net.minecraft.server.level.ServerPlayer player = ((net.minecraft.server.level.ServerPlayer) (Object) this);
player.stopRiding();
if (player.isSleeping()) {
player.stopSleepInBed(true, true);
}
final ChunkPos chunkPos = VecHelper.toChunkPos(Sponge.server().chunkLayout().forceToChunk(destinationPosition.toInt()));
destinationWorld.getChunkSource().addRegionTicket(TicketType.POST_TELEPORT, chunkPos, 1, player.getId());
if (isChangeOfWorld) {
this.shadow$absMoveTo(destinationPosition.x(), destinationPosition.y(), destinationPosition.z(), this.yRot, this.xRot);
EntityUtil.performPostChangePlayerWorldLogic(player, this.shadow$getLevel(), destinationWorld, destinationWorld, false);
} else {
this.connection.teleport(destinationPosition.x(), destinationPosition.y(), destinationPosition.z(), this.yRot, this.xRot, new HashSet<>());
this.connection.resetPosition();
}
return true;
}
Aggregations