Skip to content

Commit

Permalink
Fixes to runWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 25, 2024
1 parent 0dafd7c commit ca57b0e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
org.gradle.jvmargs=-Xmx1G

# Fabric Properties
minecraft_version=1.20.6
yarn_mappings=1.20.6+build.1
minecraft_version=1.21
yarn_mappings=1.21+build.1
loader_version=0.15.10

# Mod Properties
mod_version=0.5.4
mod_version=0.5.5
maven_group=xyz.nucleoid
archives_base_name=packet-tweaker
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

public interface ContextProvidingPacketListener {
ContextProvidingPacketListener EMPTY = new ContextProvidingPacketListener() {};

@Nullable
default ServerPlayerEntity getPlayerForPacketTweaker() {
return null;
Expand All @@ -31,29 +32,32 @@ default GameProfile getGameProfileForPacketTweaker() {
@Nullable
default ClientConnection getClientConnectionForPacketTweaker() { return null; };

static ContextProvidingPacketListener of(@Nullable PacketListener listener) {
return listener != null ? ((ContextProvidingPacketListener) listener) : EMPTY;
}

@Nullable
static ServerPlayerEntity getPlayer(PacketListener listener) {
return ((ContextProvidingPacketListener) listener).getPlayerForPacketTweaker();
static ServerPlayerEntity getPlayer(@Nullable PacketListener listener) {
return of(listener).getPlayerForPacketTweaker();
}

@Nullable
static SyncedClientOptions getClientOptions(PacketListener listener) {
return ((ContextProvidingPacketListener) listener).getClientOptionsForPacketTweaker();
static SyncedClientOptions getClientOptions(@Nullable PacketListener listener) {
return of(listener).getClientOptionsForPacketTweaker();
}

@Nullable
static GameProfile getGameProfile(PacketListener listener) {
return ((ContextProvidingPacketListener) listener).getGameProfileForPacketTweaker();
static GameProfile getGameProfile(@Nullable PacketListener listener) {
return of(listener).getGameProfileForPacketTweaker();
}

@Nullable
static RegistryWrapper.WrapperLookup getWrapperLookup(PacketListener listener) {
return ((ContextProvidingPacketListener) listener).getWrapperLookupForPacketTweaker();
static RegistryWrapper.WrapperLookup getWrapperLookup(@Nullable PacketListener listener) {
return of(listener).getWrapperLookupForPacketTweaker();
}

@Nullable
static ClientConnection getClientConnection(PacketListener listener) {
return ((ContextProvidingPacketListener) listener).getClientConnectionForPacketTweaker();
static ClientConnection getClientConnection(@Nullable PacketListener listener) {
return of(listener).getClientConnectionForPacketTweaker();
}
}
14 changes: 7 additions & 7 deletions src/main/java/xyz/nucleoid/packettweaker/PacketContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ public static PacketContext get() {
}

public static void runWithContext(@Nullable ClientConnection connection, @Nullable PacketListener networkHandler, @Nullable Packet<?> packet, Runnable runnable) {
if (networkHandler == null) {
runnable.run();
return;
}

PacketContext context = PacketContext.get();
var oldTarget = context.target;
var oldPacket = context.encodedPacket;
var oldConnection = context.connection;
context.target = (ContextProvidingPacketListener) networkHandler;
context.target = ContextProvidingPacketListener.of(networkHandler);
context.encodedPacket = packet;
context.connection = connection;
runnable.run();
Expand All @@ -44,7 +39,7 @@ public static void runWithContext(@Nullable ClientConnection connection, @Nullab
context.connection = oldConnection;
}
public static void runWithContext(@Nullable PacketListener networkHandler, @Nullable Packet<?> packet, Runnable runnable) {
runWithContext(((ContextProvidingPacketListener) networkHandler).getClientConnectionForPacketTweaker(), networkHandler, packet, runnable);
runWithContext(ContextProvidingPacketListener.getClientConnection(networkHandler), networkHandler, packet, runnable);
}

public static void runWithContext(@Nullable PacketListener networkHandler, Runnable runnable) {
Expand Down Expand Up @@ -122,6 +117,11 @@ public ContextProvidingPacketListener getPacketListener() {
return this.target;
}

@Nullable
public PacketListener getBackingPacketListener() {
return this.target != ContextProvidingPacketListener.EMPTY ? (PacketListener) this.target : null;
}

@Nullable
public ClientConnection getClientConnection() {
return this.connection;
Expand Down

0 comments on commit ca57b0e

Please sign in to comment.