Skip to content

Commit

Permalink
Update Immersive Portals compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Oct 17, 2024
1 parent e8bf139 commit 8451816
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import eu.pb4.polymer.core.impl.networking.BlockPacketUtil;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.WorldChunk;
import qouteall.imm_ptl.core.api.PortalAPI;
import qouteall.imm_ptl.core.chunk_loading.ImmPtlChunkTracking;
import qouteall.imm_ptl.core.network.PacketRedirection;

import java.util.List;
import java.util.Objects;

public class ImmersivePortalsUtils {
Expand All @@ -20,4 +27,12 @@ public static void sendBlockPackets(ServerPlayNetworkHandler handler, Packet<?>
BlockPacketUtil.sendFromPacket(packet, handler);
}
}

public static List<ServerPlayerEntity> getPlayerTracking(WorldChunk chunk) {
return ImmPtlChunkTracking.getPlayersViewingChunk(chunk.getWorld().getRegistryKey(), chunk.getPos().x, chunk.getPos().z, false);
}

public static List<ServerPlayerEntity> getPlayerTracking(RegistryKey<World> worldRegistryKey, ChunkPos pos) {
return ImmPtlChunkTracking.getPlayersViewingChunk(worldRegistryKey, pos.x, pos.z, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,20 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.common.impl.CommonImplUtils;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.ChunkDataS2CPacket;
import net.minecraft.server.network.ChunkDataSender;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Unit;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.chunk.light.LightingProvider;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.BitSet;

@Mixin(ChunkDataSender.class)
@Mixin(value = ChunkDataSender.class, priority = 1001)
public class ChunkDataSenderMixin {
@WrapOperation(method = "sendChunkData", at = @At(value = "NEW", target = "(Lnet/minecraft/world/chunk/WorldChunk;Lnet/minecraft/world/chunk/light/LightingProvider;Ljava/util/BitSet;Ljava/util/BitSet;)Lnet/minecraft/network/packet/s2c/play/ChunkDataS2CPacket;"), require = 0)
private static ChunkDataS2CPacket addContext(WorldChunk chunk, LightingProvider lightProvider, @Nullable BitSet skyBits, @Nullable BitSet blockBits, Operation<ChunkDataS2CPacket> call,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package eu.pb4.polymer.core.mixin.block;

import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.core.impl.compat.ImmersivePortalsUtils;
import eu.pb4.polymer.core.impl.interfaces.PolymerBlockPosStorage;
import it.unimi.dsi.fastutil.objects.Object2LongArrayMap;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
Expand All @@ -12,6 +14,7 @@
import net.minecraft.server.world.ServerLightingProvider;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.server.world.ServerChunkLoadingManager;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.ChunkSectionPos;
import net.minecraft.world.LightType;
import net.minecraft.world.chunk.WorldChunk;
Expand Down Expand Up @@ -83,9 +86,17 @@ public abstract class ServerChunkManagerMixin {
});
}

private List<ServerPlayerEntity> getPlayersWatchingChunk(ChunkPos chunkPos) {
if (CompatStatus.IMMERSIVE_PORTALS) {
return ImmersivePortalsUtils.getPlayerTracking(this.world.getRegistryKey(), chunkPos);
} else {
return this.chunkLoadingManager.getPlayersWatchingChunk(chunkPos, false);
}
}

@Unique
private void polymer$broadcastBlockLightForSection(ChunkSectionPos pos) {
List<ServerPlayerEntity> players = this.chunkLoadingManager.getPlayersWatchingChunk(pos.toChunkPos(), false);
List<ServerPlayerEntity> players = getPlayersWatchingChunk(pos.toChunkPos());
if (players.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package eu.pb4.polymer.virtualentity.api.attachment;

import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.virtualentity.api.ElementHolder;
import eu.pb4.polymer.virtualentity.api.VirtualEntityUtils;
import eu.pb4.polymer.virtualentity.impl.HolderAttachmentHolder;
import eu.pb4.polymer.virtualentity.impl.compat.ImmersivePortalsUtils;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerChunkManager;
Expand Down Expand Up @@ -94,7 +96,8 @@ public boolean shouldTick() {
@Override
public void updateCurrentlyTracking(Collection<ServerPlayNetworkHandler> currentlyTracking) {
List<ServerPlayNetworkHandler> watching = new ArrayList<>();
for (ServerPlayerEntity x : ((ServerChunkManager) this.chunk.getWorld().getChunkManager()).chunkLoadingManager.getPlayersWatchingChunk(this.chunk.getPos(), false)) {

for (ServerPlayerEntity x : getPlayersWatchingChunk(chunk)) {
ServerPlayNetworkHandler networkHandler = x.networkHandler;
watching.add(networkHandler);
}
Expand All @@ -110,10 +113,18 @@ public void updateCurrentlyTracking(Collection<ServerPlayNetworkHandler> current
}
}

private static List<ServerPlayerEntity> getPlayersWatchingChunk(WorldChunk chunk) {
if (CompatStatus.IMMERSIVE_PORTALS) {
return ImmersivePortalsUtils.getPlayerTracking(chunk);
} else {
return ((ServerChunkManager) chunk.getWorld().getChunkManager()).chunkLoadingManager.getPlayersWatchingChunk(chunk.getPos(), false);
}
}

@Override
public void updateTracking(ServerPlayNetworkHandler tracking) {
if (tracking.player.isDead() || !VirtualEntityUtils.isPlayerTracking(tracking.getPlayer(), this.chunk)) {
VirtualEntityUtils.wrapCallWithContext(this.getWorld(), () -> this.stopWatching(tracking));
this.stopWatching(tracking);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.pb4.polymer.virtualentity.api.attachment;

import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.virtualentity.api.ElementHolder;
import eu.pb4.polymer.virtualentity.api.VirtualEntityUtils;
import eu.pb4.polymer.virtualentity.impl.SimpleUpdateType;
import eu.pb4.polymer.virtualentity.impl.VoidUpdateType;
import net.minecraft.network.listener.ClientPlayPacketListener;
Expand All @@ -23,33 +25,53 @@ public interface HolderAttachment {

default void startWatching(ServerPlayerEntity handler) {
if (this.holder().getAttachment() == this) {
this.holder().startWatching(handler);
if (CompatStatus.IMMERSIVE_PORTALS) {
VirtualEntityUtils.wrapCallWithContext(this.getWorld(), () -> this.holder().startWatching(handler));
} else {
this.holder().startWatching(handler);
}
}
}

default void startWatching(ServerPlayNetworkHandler handler) {
if (this.holder().getAttachment() == this) {
this.holder().startWatching(handler);
if (CompatStatus.IMMERSIVE_PORTALS) {
VirtualEntityUtils.wrapCallWithContext(this.getWorld(), () -> this.holder().startWatching(handler));
} else {
this.holder().startWatching(handler);
}
}
}

default void startWatchingExtraPackets(ServerPlayNetworkHandler handler, Consumer<Packet<ClientPlayPacketListener>> packetConsumer) {};

default void stopWatching(ServerPlayerEntity handler) {
if (this.holder().getAttachment() == this) {
this.holder().stopWatching(handler);
if (CompatStatus.IMMERSIVE_PORTALS) {
VirtualEntityUtils.wrapCallWithContext(this.getWorld(), () -> this.holder().stopWatching(handler));
} else {
this.holder().stopWatching(handler);
}
}
}

default void stopWatching(ServerPlayNetworkHandler handler) {
if (this.holder().getAttachment() == this) {
this.holder().stopWatching(handler);
if (CompatStatus.IMMERSIVE_PORTALS) {
VirtualEntityUtils.wrapCallWithContext(this.getWorld(), () -> this.holder().stopWatching(handler));
} else {
this.holder().stopWatching(handler);
}
}
}

default void tick() {
if (this.holder().getAttachment() == this) {
this.holder().tick();
if (CompatStatus.IMMERSIVE_PORTALS) {
VirtualEntityUtils.wrapCallWithContext(this.getWorld(), () -> this.holder().tick());
} else {
this.holder().tick();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import qouteall.imm_ptl.core.chunk_loading.ImmPtlChunkTracking;
import qouteall.imm_ptl.core.network.PacketRedirection;

import java.util.List;


@ApiStatus.Internal
public class ImmersivePortalsUtils {
public static boolean isPlayerTracking(ServerPlayerEntity player, WorldChunk chunk) {
return ImmPtlChunkTracking.isPlayerWatchingChunk(player, chunk.getWorld().getRegistryKey(), chunk.getPos().x, chunk.getPos().z);
}

public static List<ServerPlayerEntity> getPlayerTracking(WorldChunk chunk) {
return ImmPtlChunkTracking.getPlayersViewingChunk(chunk.getWorld().getRegistryKey(), chunk.getPos().x, chunk.getPos().z, false);
}

public static void callRedirected(ServerWorld world, Runnable runnable) {
PacketRedirection.withForceRedirect(world, runnable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.pb4.polymer.virtualentity.mixin.compat;

import com.llamalad7.mixinextras.sugar.Local;
import eu.pb4.polymer.virtualentity.api.attachment.ChunkAttachment;
import eu.pb4.polymer.virtualentity.impl.HolderHolder;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import net.minecraft.registry.RegistryKey;
Expand All @@ -9,6 +10,7 @@
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import qouteall.imm_ptl.core.chunk_loading.ImmPtlChunkTracking;

Expand All @@ -18,20 +20,22 @@
@Pseudo
@Mixin(ImmPtlChunkTracking.class)
public class ip_ImmPtlChunkTracking {
@Inject(method = "lambda$purge$5", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;)V"), require = 0)
@Inject(method = "lambda$purge$5", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;)V"))
private static void polymerVE$chunkUnload(Map.Entry e, CallbackInfoReturnable<Boolean> cir, @Local ServerPlayerEntity player, @Local ImmPtlChunkTracking.PlayerWatchRecord record) {
for (var holder : new ArrayList<>(((HolderHolder) player.networkHandler).polymer$getHolders())) {
if (holder.getAttachment() != null && holder.getChunkPos().toLong() == record.chunkPos) {
holder.getAttachment().updateTracking(player.networkHandler);
if (holder.getAttachment() instanceof ChunkAttachment chunkAttachment
&& chunkAttachment.getWorld().getRegistryKey().equals(record.dimension)
&& holder.getChunkPos().toLong() == record.chunkPos) {
holder.getAttachment().stopWatching(player.networkHandler);
}
}
}

@Inject(method = "lambda$forceRemovePlayer$16", at = @At(value = "INVOKE", target = "Lqouteall/imm_ptl/core/network/PacketRedirection;sendRedirectedMessage(Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/registry/RegistryKey;Lnet/minecraft/network/packet/Packet;)V"), require = 0)
private static void polymerVE$chunkUnload2(ServerPlayerEntity player, RegistryKey dim, Long2ObjectMap.Entry e, CallbackInfoReturnable<Boolean> cir, @Local ImmPtlChunkTracking.PlayerWatchRecord rec) {
for (var holder : new ArrayList<>(((HolderHolder) player.networkHandler).polymer$getHolders())) {
if (holder.getAttachment() != null && holder.getChunkPos().toLong() == rec.chunkPos) {
holder.getAttachment().updateTracking(player.networkHandler);
@Inject(method = "forceRemovePlayer", at = @At("TAIL"), require = 0)
private static void polymerVE$chunkUnload2(ServerPlayerEntity oldPlayer, CallbackInfo ci) {
for (var holder : new ArrayList<>(((HolderHolder) oldPlayer.networkHandler).polymer$getHolders())) {
if (holder.getAttachment() instanceof ChunkAttachment chunkAttachment) {
holder.getAttachment().stopWatching(oldPlayer.networkHandler);
}
}
}
Expand Down

0 comments on commit 8451816

Please sign in to comment.