Make client net handling run on main thread

This commit is contained in:
Sensei authored and gnembon committed 2022-10-10 20:30:25 +02:00
1 parent a50a0d7328
commit 059fda49a4
4 files changed
+12 -36

No files matched your search

@@ -7,6 +7,7 @@ import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket; import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.game.ClientboundLoginPacket; import net.minecraft.network.protocol.game.ClientboundLoginPacket;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@@ -16,9 +17,9 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientPacketListener.class) @Mixin(ClientPacketListener.class)
public abstract class ClientPacketListener_customPacketsMixin public abstract class ClientPacketListener_customPacketsMixin
{ {
@Shadow private Minecraft minecraft; @Final @Shadow private Minecraft minecraft;
@Inject(method = "handleCustomPayload", at = @At("HEAD"), cancellable = true) @Inject(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;getIdentifier()Lnet/minecraft/resources/ResourceLocation;"), cancellable = true)
private void onOnCustomPayload(ClientboundCustomPayloadPacket packet, CallbackInfo ci) private void onOnCustomPayload(ClientboundCustomPayloadPacket packet, CallbackInfo ci)
{ {
if (CarpetClient.CARPET_CHANNEL.equals(packet.getIdentifier())) if (CarpetClient.CARPET_CHANNEL.equals(packet.getIdentifier()))
@@ -39,5 +40,4 @@ public abstract class ClientPacketListener_customPacketsMixin
{ {
CarpetClient.disconnect(); CarpetClient.disconnect();
} }
} }
@@ -12,7 +12,6 @@ import net.minecraft.resources.ResourceLocation;
public class CarpetClient public class CarpetClient
{ {
public static final Object sync = new Object();
public static final int HI = 69; public static final int HI = 69;
public static final int HELLO = 420; public static final int HELLO = 420;
public static final int DATA = 1; public static final int DATA = 1;
@@ -24,15 +23,8 @@ public class CarpetClient
public static final ResourceLocation CARPET_CHANNEL = new ResourceLocation("carpet:hello"); public static final ResourceLocation CARPET_CHANNEL = new ResourceLocation("carpet:hello");
public static void gameJoined(LocalPlayer player) public static void gameJoined(LocalPlayer player)
{
synchronized (sync)
{ {
clientPlayer = player; clientPlayer = player;
// client didn't say hi back yet
if (isServerCarpet)
ClientNetworkHandler.respondHello();
}
} }
public static void disconnect() public static void disconnect()
@@ -87,6 +87,7 @@ public class ClientNetworkHandler
}); });
}; };
// Ran on the Main Minecraft Thread
public static void handleData(FriendlyByteBuf data, LocalPlayer player) public static void handleData(FriendlyByteBuf data, LocalPlayer player)
{ {
if (data != null) if (data != null)
@@ -100,8 +101,6 @@ public class ClientNetworkHandler
} }
private static void onHi(FriendlyByteBuf data) private static void onHi(FriendlyByteBuf data)
{
synchronized (CarpetClient.sync)
{ {
CarpetClient.setCarpet(); CarpetClient.setCarpet();
CarpetClient.serverCarpetVersion = data.readUtf(64); CarpetClient.serverCarpetVersion = data.readUtf(64);
@@ -113,10 +112,9 @@ public class ClientNetworkHandler
{ {
CarpetSettings.LOG.warn("Joined carpet server with another carpet version: "+CarpetClient.serverCarpetVersion); CarpetSettings.LOG.warn("Joined carpet server with another carpet version: "+CarpetClient.serverCarpetVersion);
} }
if (CarpetClient.getPlayer() != null) // We can ensure that this packet is
// processed AFTER the player has joined
respondHello(); respondHello();
}
} }
public static void respondHello() public static void respondHello()
@@ -113,7 +113,7 @@ public class ShapesRenderer
double cameraZ = camera.getPosition().z; double cameraZ = camera.getPosition().z;
boolean entityBoxes = client.getEntityRenderDispatcher().shouldRenderHitBoxes(); boolean entityBoxes = client.getEntityRenderDispatcher().shouldRenderHitBoxes();
if (shapes.size() != 0) { synchronized (shapes) { if (shapes.size() != 0) {
shapes.get(dimensionType).long2ObjectEntrySet().removeIf( shapes.get(dimensionType).long2ObjectEntrySet().removeIf(
entry -> entry.getValue().isExpired(currentTime) entry -> entry.getValue().isExpired(currentTime)
); );
@@ -136,16 +136,15 @@ public class ShapesRenderer
matrixStack.popPose(); matrixStack.popPose();
RenderSystem.applyModelViewMatrix(); RenderSystem.applyModelViewMatrix();
}} }
if (labels.size() != 0) { synchronized (labels) if (labels.size() != 0) {
{
labels.get(dimensionType).long2ObjectEntrySet().removeIf( labels.get(dimensionType).long2ObjectEntrySet().removeIf(
entry -> entry.getValue().isExpired(currentTime) entry -> entry.getValue().isExpired(currentTime)
); );
labels.get(dimensionType).values().forEach(s -> { labels.get(dimensionType).values().forEach(s -> {
if ( (!s.shape.debug || entityBoxes) && s.shouldRender(dimensionType)) s.renderLines(matrices, tessellator, bufferBuilder, cameraX, cameraY, cameraZ, partialTick); if ( (!s.shape.debug || entityBoxes) && s.shouldRender(dimensionType)) s.renderLines(matrices, tessellator, bufferBuilder, cameraX, cameraY, cameraZ, partialTick);
}); });
}} }
RenderSystem.enableCull(); RenderSystem.enableCull();
RenderSystem.depthMask(true); RenderSystem.depthMask(true);
RenderSystem.enableBlend(); RenderSystem.enableBlend();
@@ -181,8 +180,6 @@ public class ShapesRenderer
long key = rshape.key(); long key = rshape.key();
Map<ResourceKey<Level>, Long2ObjectOpenHashMap<RenderedShape<? extends ShapeDispatcher.ExpiringShape>>> container = Map<ResourceKey<Level>, Long2ObjectOpenHashMap<RenderedShape<? extends ShapeDispatcher.ExpiringShape>>> container =
rshape.stageDeux()?labels:shapes; rshape.stageDeux()?labels:shapes;
synchronized (container)
{
RenderedShape<?> existing = container.computeIfAbsent(dim, d -> new Long2ObjectOpenHashMap<>()).get(key); RenderedShape<?> existing = container.computeIfAbsent(dim, d -> new Long2ObjectOpenHashMap<>()).get(key);
if (existing != null) if (existing != null)
{ // promoting previous shape { // promoting previous shape
@@ -192,32 +189,21 @@ public class ShapesRenderer
{ {
container.get(dim).put(key, rshape); container.get(dim).put(key, rshape);
} }
}
} }
} }
public void reset() public void reset()
{
synchronized (shapes)
{ {
shapes.values().forEach(Long2ObjectOpenHashMap::clear); shapes.values().forEach(Long2ObjectOpenHashMap::clear);
}
synchronized (labels)
{
labels.values().forEach(Long2ObjectOpenHashMap::clear); labels.values().forEach(Long2ObjectOpenHashMap::clear);
} }
}
public void renewShapes() public void renewShapes()
{ {
CarpetProfiler.ProfilerToken token = CarpetProfiler.start_section(null, "Scarpet client", CarpetProfiler.TYPE.GENERAL); CarpetProfiler.ProfilerToken token = CarpetProfiler.start_section(null, "Scarpet client", CarpetProfiler.TYPE.GENERAL);
synchronized (shapes)
{
shapes.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++)); shapes.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++));
}
synchronized (labels)
{
labels.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++)); labels.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++));
}
CarpetProfiler.end_current_section(token); CarpetProfiler.end_current_section(token);
} }