Skip to content

Commit

Permalink
Fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jun 14, 2024
1 parent 79aa037 commit 751ca1b
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 56 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ org.gradle.jvmargs=-Xmx4G
# Fabric Properties
# check these on https://fabricmc.net/use

minecraft_version=1.21-rc1
yarn_mappings=1.21-rc1+build.1
minecraft_version=1.21
yarn_mappings=1.21+build.1
loader_version=0.15.11

# Fabric API
fabric_version=0.100.0+1.21

maven_group = eu.pb4

mod_version = 0.9.0
mod_version = 0.9.1

minecraft_version_supported = ">=1.20.5-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import java.util.function.Consumer;

public final class PolymerCommonUtils {
private static final ThreadLocal<Unit> FORCE_NETWORKING = new ThreadLocal<>();
private static final ThreadLocal<LogicOverride> FORCE_NETWORKING = ThreadLocal.withInitial(() -> LogicOverride.DEFAULT);

private PolymerCommonUtils(){}
private PolymerCommonUtils() {}

public static final SimpleEvent<ResourcePackChangeCallback> ON_RESOURCE_PACK_STATUS_CHANGE = new SimpleEvent<>();
private static Path cachedClientPath;
private final static String SAFE_CLIENT_SHA1 = "05b6f1c6b46a29d6ea82b4e0d42190e42402030f";
private final static String SAFE_CLIENT_SHA1 = "0e9a07b9bb3390602f977073aa12884a4ce12431";
private final static String SAFE_CLIENT_URL = "https://piston-data.mojang.com/v1/objects/" + SAFE_CLIENT_SHA1 + "/client.jar";
private static Path cachedClientJarRoot;

Expand Down Expand Up @@ -113,18 +114,25 @@ public static Path getClientJar() {

public static void executeWithNetworkingLogic(Runnable runnable) {
var val = FORCE_NETWORKING.get();
FORCE_NETWORKING.set(Unit.INSTANCE);
FORCE_NETWORKING.set(LogicOverride.TRUE);
runnable.run();
FORCE_NETWORKING.set(val);
}

public static void executeWithNetworkingLogic(PacketListener listener, Runnable runnable) {
var val = FORCE_NETWORKING.get();
FORCE_NETWORKING.set(Unit.INSTANCE);
FORCE_NETWORKING.set(LogicOverride.TRUE);
PacketContext.runWithContext(listener, runnable);
FORCE_NETWORKING.set(val);
}

public static void executeWithoutNetworkingLogic(Runnable runnable) {
var val = FORCE_NETWORKING.get();
FORCE_NETWORKING.set(LogicOverride.FALSE);
runnable.run();
FORCE_NETWORKING.set(val);
}

/**
* Allows to execute code with selected player being returned for {@link PolymerCommonUtils#getPlayerContext()}
* calls. Useful for custom packets using writeItemStack and similar methods.
Expand All @@ -146,6 +154,20 @@ public static void executeWithPlayerContext(ServerPlayerEntity player, Runnable
PacketContext.setContext(oldTarget, oldPacket);
}

public static void executeWithPlayerContext(ServerPlayerEntity player, Runnable runnable, Consumer<Runnable> runnableConsumer) {
var oldPlayer = CommonImplUtils.getPlayer();
var oldTarget = PacketContext.get().getClientConnection();
var oldPacket = PacketContext.get().getEncodedPacket();

CommonImplUtils.setPlayer(player);
PacketContext.setContext(player != null ? ((CommonNetworkHandlerExt)player.networkHandler).polymerCommon$getConnection() : null, null);

runnableConsumer.accept(runnable);

CommonImplUtils.setPlayer(oldPlayer);
PacketContext.setContext(oldTarget, oldPacket);
}


public static World getFakeWorld() {
return FakeWorld.INSTANCE;
Expand Down Expand Up @@ -176,15 +198,19 @@ public static ServerPlayerEntity getPlayerContextNoClient() {
}

public static boolean isNetworkingThread() {
return FORCE_NETWORKING.get() == Unit.INSTANCE || Thread.currentThread().getName().startsWith("Netty");
return FORCE_NETWORKING.get().value(Thread.currentThread().getName().startsWith("Netty"));
}

public static boolean isServerNetworkingThread() {
return FORCE_NETWORKING.get() == Unit.INSTANCE || (isNetworkingThread() && Thread.currentThread().getName().contains("Server"));
return FORCE_NETWORKING.get().value(
Thread.currentThread().getName().startsWith("Netty") && Thread.currentThread().getName().contains("Server")
);
}

public static boolean isClientNetworkingThread() {
return CommonImpl.IS_CLIENT && (FORCE_NETWORKING.get() == Unit.INSTANCE || isNetworkingThread()) && Thread.currentThread().getName().contains("Client");
return CommonImpl.IS_CLIENT && FORCE_NETWORKING.get().value(
Thread.currentThread().getName().startsWith("Netty") && Thread.currentThread().getName().contains("Client")
);
}

public static boolean isBedrockPlayer(ServerPlayerEntity player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package eu.pb4.polymer.common.impl;

public interface LogicOverride {
LogicOverride TRUE = (v) -> true;
LogicOverride FALSE = (v) -> false;
LogicOverride DEFAULT = (v) -> v;

boolean value(boolean fallback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public static ItemStack createMinimalItemStack(ItemStack itemStack, RegistryWrap
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(
(NbtCompound) POLYMER_STACK_CODEC.encoder().encodeStart(RegistryOps.of(NbtOps.INSTANCE, lookup), itemStack).getOrThrow()
));
});
}, PolymerCommonUtils::executeWithoutNetworkingLogic);
} catch (Throwable e) {
out.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT.with(RegistryOps.of(NbtOps.INSTANCE, lookup), POLYMER_STACK_ID_CODEC, Registries.ITEM.getId(itemStack.getItem())).getOrThrow());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class PotionContentsComponentMixin implements TransformingDataCo

@Override
public boolean polymer$requireModification(ServerPlayerEntity player) {
if (this.potion.isPresent() && this.potion.get() instanceof PolymerObject) {
if (this.potion.isPresent() && this.potion.get().value() instanceof PolymerObject) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.function.Function;

@Mixin(ComponentMap.class)
public abstract class ComponentMapMixin {
public interface ComponentMapMixin {
@ModifyVariable(method = "createCodecFromValueMap", at = @At("HEAD"), argsOnly = true)
private static Codec<Map<ComponentType<?>, Object>> patchCodec(Codec<Map<ComponentType<?>, Object>> codec) {
return codec.xmap(Function.identity(), content -> { // Encode
Expand Down
1 change: 1 addition & 0 deletions polymer-core/src/main/resources/polymer-core.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"item.packet.SynchronizeRecipesS2CPacketMixin",
"item.packet.TradedItemMixin",
"other.BootstrapMixin",
"other.ComponentMapMixin",
"other.HoverEventMixin",
"other.IdListAccessor",
"other.IdListMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void onResourcePackStatus(ResourcePackStatusC2SPacket packet) {

@Override
public void onClientOptions(ClientOptionsC2SPacket packet) {
this.context.options().setValue(packet.options());
this.context.options().set(packet.options());
}

@Override
Expand Down Expand Up @@ -235,7 +235,7 @@ public GameProfile getGameProfileForPacketTweaker() {

@Override
public SyncedClientOptions getClientOptionsForPacketTweaker() {
return this.context.options().getValue();
return this.context.options().get();
}

protected final ServerLoginNetworkHandler getLoginNetworkHandler() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package eu.pb4.polymer.networking.impl;

import com.mojang.authlib.GameProfile;
import eu.pb4.polymer.common.impl.FakeRegistry;
import eu.pb4.polymer.networking.api.server.EarlyConfigurationNetworkHandler;
import eu.pb4.polymer.networking.api.server.EarlyPlayNetworkHandler;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.network.NetworkPhase;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
import net.minecraft.network.state.ConfigurationStates;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import org.apache.commons.lang3.mutable.MutableObject;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;

Expand All @@ -27,17 +25,21 @@ public class EarlyConfigurationConnectionMagic {
public static void handle(GameProfile profile, SyncedClientOptions options, ServerLoginNetworkHandler loginHandler, MinecraftServer server, ClientConnection connection, Consumer<ContextImpl> finish) {
var iterator = new ArrayList<>(CONSTRUCTORS).iterator();

var context = new ContextImpl(server, profile, connection, loginHandler, new ArrayList<>(), (c) -> {
var ctx = new ContextImpl(server, profile, connection, loginHandler, new ArrayList<>(), (c) -> {
while (iterator.hasNext()) {
var handler = iterator.next().apply(c);
if (handler != null) {
return;
}
}
finish.accept(c);
}, new MutableObject<>(options));
}, new AtomicReference<>(options));

context.continueRunning.accept(context);

connection.transitionInbound(ConfigurationStates.C2S,
new FallbackServerPacketHandler(NetworkPhase.CONFIGURATION, ctx.options()::set, ctx.storedPackets()::add, loginHandler::onDisconnected));

ctx.continueRunning().accept(ctx);
}

public static void register(Function<EarlyConfigurationNetworkHandler.Context, @Nullable EarlyConfigurationNetworkHandler> constructor) {
Expand All @@ -53,9 +55,9 @@ public record ContextImpl(
GameProfile profile,
ClientConnection connection,
ServerLoginNetworkHandler loginHandler,
List<CustomPayloadC2SPacket> storedPackets,
List<Packet<?>> storedPackets,
Consumer<ContextImpl> continueRunning,
MutableObject<SyncedClientOptions> options
AtomicReference<SyncedClientOptions> options
) implements EarlyConfigurationNetworkHandler.Context {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import eu.pb4.polymer.networking.api.server.EarlyPlayNetworkHandler;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.common.ClientOptionsC2SPacket;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -48,9 +50,14 @@ public record ContextImpl(
ServerPlayerEntity player,
ClientConnection connection,
ServerConfigurationNetworkHandler loginHandler,
List<CustomPayloadC2SPacket> storedPackets,
List<Packet<?>> storedPackets,
Consumer<ContextImpl> continueRunning,
MutableObject<SyncedClientOptions> options
) implements EarlyPlayNetworkHandler.Context {
public void addStoredPacket(Packet<?> packet) {
if (packet instanceof CustomPayloadC2SPacket || packet instanceof ClientOptionsC2SPacket) {
this.storedPackets.add(packet);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.util.Identifier;
Expand All @@ -28,7 +29,7 @@ static ExtClientConnection of(ServerCommonNetworkHandler networkHandler) {
Object2IntMap<Identifier> polymerNet$getSupportMap();
Object2ObjectMap<Identifier, NbtElement> polymerNet$getMetadataMap();

void polymerNet$wrongPacketConsumer(Consumer<CustomPayloadC2SPacket> consumer);
void polymerNet$wrongPacketConsumer(Consumer<Packet<?>> consumer);
Channel polymerNet$getChannel();

static ExtClientConnection of(ClientConnection connection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import net.minecraft.network.packet.c2s.config.SelectKnownPacksC2SPacket;
import net.minecraft.network.packet.c2s.play.*;
import net.minecraft.network.packet.c2s.query.QueryPingC2SPacket;
import net.minecraft.text.Text;

public record EmptyServerPacketHandler(NetworkPhase phase) implements ServerConfigurationPacketListener, ServerPlayPacketListener {
public static EmptyServerPacketHandler CONFIGURATION = new EmptyServerPacketHandler(NetworkPhase.CONFIGURATION);
public static EmptyServerPacketHandler PLAY = new EmptyServerPacketHandler(NetworkPhase.PLAY);
import java.util.function.Consumer;

public record FallbackServerPacketHandler(NetworkPhase phase,
Consumer<SyncedClientOptions> optionsConsumer,
Consumer<CustomPayloadC2SPacket> payloadConsumer,
Consumer<DisconnectionInfo> disconnectionInfoConsumer
) implements ServerConfigurationPacketListener, ServerPlayPacketListener {
@Override
public NetworkPhase getPhase() {
return phase;
Expand Down Expand Up @@ -42,7 +44,7 @@ public void onPong(CommonPongC2SPacket packet) {

@Override
public void onCustomPayload(CustomPayloadC2SPacket packet) {

this.payloadConsumer.accept(packet);
}

@Override
Expand All @@ -52,7 +54,7 @@ public void onResourcePackStatus(ResourcePackStatusC2SPacket packet) {

@Override
public void onClientOptions(ClientOptionsC2SPacket packet) {

optionsConsumer.accept(packet.options());
}

@Override
Expand All @@ -62,7 +64,7 @@ public void onCookieResponse(CookieResponseC2SPacket packet) {

@Override
public void onDisconnected(DisconnectionInfo info) {

this.disconnectionInfoConsumer.accept(info);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.listener.PacketListener;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -37,7 +33,7 @@ public class ClientConnectionMixin implements ExtClientConnection {
@Unique
private final Object2ObjectMap<Identifier, NbtElement> polymerNet$metadata = new Object2ObjectOpenHashMap<>();
@Unique
private Consumer<CustomPayloadC2SPacket> polymerNet$packetConsumer;
private Consumer<Packet<?>> polymerNet$packetConsumer;
@Override
public boolean polymerNet$hasPolymer() {
return !this.polymerNet$version.isEmpty();
Expand Down Expand Up @@ -76,16 +72,14 @@ public class ClientConnectionMixin implements ExtClientConnection {
@Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;)V", at = @At("HEAD"), cancellable = true)
private void polymerNet$handlePacket(ChannelHandlerContext channelHandlerContext, Packet<?> packet, CallbackInfo ci) {
if (this.polymerNet$packetConsumer != null) {
if (packet instanceof CustomPayloadC2SPacket c) {
this.polymerNet$packetConsumer.accept(c);
}
this.polymerNet$packetConsumer.accept(packet);
ci.cancel();
this.packetsReceivedCounter++;
}
}

@Override
public void polymerNet$wrongPacketConsumer(Consumer<CustomPayloadC2SPacket> consumer) {
public void polymerNet$wrongPacketConsumer(Consumer<Packet<?>> consumer) {
this.polymerNet$packetConsumer = consumer;
}

Expand Down
Loading

0 comments on commit 751ca1b

Please sign in to comment.