Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 8437561

Browse files
authored
Implement ClientPlayerNetworkEvent (#177)
* Implement ClientPlayerNetworkEvent * Only use MixinMinecraftClient on the client * Move ClientHooks to patchwork-god-classes
1 parent e936b61 commit 8437561

8 files changed

Lines changed: 267 additions & 0 deletions

File tree

patchwork-god-classes/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ dependencies {
1111
implementation project(path: ':patchwork-events-rendering', configuration: 'dev')
1212
implementation project(path: ':patchwork-events-world', configuration: 'dev')
1313
implementation project(path: ':patchwork-loot', configuration: 'dev')
14+
implementation project(path: ':patchwork-networking', configuration: 'dev')
1415
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.fml.client;
21+
22+
import net.minecraft.client.network.ClientPlayerEntity;
23+
import net.minecraft.client.network.ClientPlayerInteractionManager;
24+
import net.minecraft.network.ClientConnection;
25+
26+
import net.patchworkmc.impl.networking.ClientNetworkingEvents;
27+
28+
public class ClientHooks {
29+
public static void firePlayerLogin(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity player, final ClientConnection clientConnection) {
30+
ClientNetworkingEvents.firePlayerLogin(interactionManager, player, clientConnection);
31+
}
32+
33+
public static void firePlayerLogout(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity player) {
34+
ClientNetworkingEvents.firePlayerLogout(interactionManager, player);
35+
}
36+
37+
public static void firePlayerRespawn(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity oldPlayer, final ClientPlayerEntity newPlayer, final ClientConnection clientConnection) {
38+
ClientNetworkingEvents.firePlayerRespawn(interactionManager, oldPlayer, newPlayer, clientConnection);
39+
}
40+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.client.event;
21+
22+
import net.minecraftforge.eventbus.api.Event;
23+
24+
import net.minecraft.client.network.ClientPlayerEntity;
25+
import net.minecraft.client.network.ClientPlayerInteractionManager;
26+
import net.minecraft.network.ClientConnection;
27+
28+
/**
29+
* Client-side events fired on changes to player connectivity.
30+
*/
31+
public class ClientPlayerNetworkEvent extends Event {
32+
private final ClientPlayerInteractionManager playerInteractionManager;
33+
private final ClientPlayerEntity player;
34+
private final ClientConnection clientConnection;
35+
36+
ClientPlayerNetworkEvent(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity player, final ClientConnection clientConnection) {
37+
this.playerInteractionManager = interactionManager;
38+
this.player = player;
39+
this.clientConnection = clientConnection;
40+
}
41+
42+
/**
43+
* @return The ClientPlayerInteractionManager instance for the client side.
44+
*/
45+
public ClientPlayerInteractionManager getController() {
46+
return this.playerInteractionManager;
47+
}
48+
49+
/**
50+
* @return The player instance, if present (otherwise, returns null).
51+
*/
52+
public ClientPlayerEntity getPlayer() {
53+
return this.player;
54+
}
55+
56+
/**
57+
* @return The network connection, if present (otherwise, returns null).
58+
*/
59+
public ClientConnection getNetworkManager() {
60+
return this.clientConnection;
61+
}
62+
63+
/**
64+
* Fired when the player logs out.
65+
*
66+
* <p>Note: This might also be fired when a new integrated server is being created.</p>
67+
*/
68+
public static class LoggedInEvent extends ClientPlayerNetworkEvent {
69+
public LoggedInEvent(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity player, final ClientConnection clientConnection) {
70+
super(interactionManager, player, clientConnection);
71+
}
72+
}
73+
74+
/**
75+
* Fired when the player logs out.
76+
*
77+
* <p>Note: This might also fire when a new integrated server is being created.</p>
78+
*/
79+
public static class LoggedOutEvent extends ClientPlayerNetworkEvent {
80+
public LoggedOutEvent(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity player, final ClientConnection clientConnection) {
81+
super(interactionManager, player, clientConnection);
82+
}
83+
}
84+
85+
/**
86+
* Fired when the player object respawns, such as dimension changes.
87+
*/
88+
public static class RespawnEvent extends ClientPlayerNetworkEvent {
89+
private final ClientPlayerEntity oldPlayer;
90+
91+
public RespawnEvent(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity oldPlayer, final ClientPlayerEntity newPlayer, final ClientConnection clientConnection) {
92+
super(interactionManager, newPlayer, clientConnection);
93+
this.oldPlayer = oldPlayer;
94+
}
95+
96+
public ClientPlayerEntity getOldPlayer() {
97+
return this.oldPlayer;
98+
}
99+
100+
public ClientPlayerEntity getNewPlayer() {
101+
return super.getPlayer();
102+
}
103+
}
104+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.impl.networking;
21+
22+
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
23+
import net.minecraftforge.common.MinecraftForge;
24+
25+
import net.minecraft.client.network.ClientPlayerEntity;
26+
import net.minecraft.client.network.ClientPlayerInteractionManager;
27+
import net.minecraft.network.ClientConnection;
28+
29+
public class ClientNetworkingEvents {
30+
public static void firePlayerLogin(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity player, final ClientConnection clientConnection) {
31+
MinecraftForge.EVENT_BUS.post(new ClientPlayerNetworkEvent.LoggedInEvent(interactionManager, player, clientConnection));
32+
}
33+
34+
public static void firePlayerLogout(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity player) {
35+
MinecraftForge.EVENT_BUS.post(new ClientPlayerNetworkEvent.LoggedOutEvent(interactionManager, player, player != null ? player.networkHandler != null ? player.networkHandler.getConnection() : null : null));
36+
}
37+
38+
public static void firePlayerRespawn(final ClientPlayerInteractionManager interactionManager, final ClientPlayerEntity oldPlayer, final ClientPlayerEntity newPlayer, final ClientConnection clientConnection) {
39+
MinecraftForge.EVENT_BUS.post(new ClientPlayerNetworkEvent.RespawnEvent(interactionManager, oldPlayer, newPlayer, clientConnection));
40+
}
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.networking.client;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.Shadow;
24+
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.Inject;
26+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
27+
28+
import net.minecraft.client.MinecraftClient;
29+
import net.minecraft.client.network.ClientPlayerEntity;
30+
import net.minecraft.client.network.ClientPlayerInteractionManager;
31+
32+
import net.patchworkmc.impl.networking.ClientNetworkingEvents;
33+
34+
@Mixin(MinecraftClient.class)
35+
public class MixinMinecraftClient {
36+
@Shadow
37+
public ClientPlayerInteractionManager interactionManager;
38+
39+
@Shadow
40+
public ClientPlayerEntity player;
41+
42+
@Inject(method = "disconnect(Lnet/minecraft/client/gui/screen/Screen;)V",
43+
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/GameRenderer;reset()V", shift = At.Shift.AFTER))
44+
public void patchwork$hookDisconnect(CallbackInfo ci) {
45+
ClientNetworkingEvents.firePlayerLogout(this.interactionManager, this.player);
46+
}
47+
}

patchwork-networking/src/main/java/net/patchworkmc/mixin/networking/handler/MixinClientPlayNetworkHandler.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,23 @@
2626
import org.spongepowered.asm.mixin.injection.At;
2727
import org.spongepowered.asm.mixin.injection.Inject;
2828
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
29+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
2930

31+
import net.minecraft.client.MinecraftClient;
3032
import net.minecraft.client.network.ClientPlayNetworkHandler;
33+
import net.minecraft.client.network.ClientPlayerEntity;
3134
import net.minecraft.network.ClientConnection;
3235
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
36+
import net.minecraft.network.packet.s2c.play.PlayerRespawnS2CPacket;
37+
import net.minecraft.world.dimension.DimensionType;
38+
39+
import net.patchworkmc.impl.networking.ClientNetworkingEvents;
3340

3441
@Mixin(ClientPlayNetworkHandler.class)
3542
public abstract class MixinClientPlayNetworkHandler {
43+
@Shadow
44+
private MinecraftClient client;
45+
3646
@Shadow
3747
public abstract ClientConnection getConnection();
3848

@@ -42,4 +52,16 @@ public abstract class MixinClientPlayNetworkHandler {
4252
callback.cancel();
4353
}
4454
}
55+
56+
@Inject(method = "onGameJoin", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/GameJoinS2CPacket;getEntityId()I"))
57+
public void patchwork$hookGameJoin(CallbackInfo ci) {
58+
ClientNetworkingEvents.firePlayerLogin(this.client.interactionManager, this.client.player, this.client.getNetworkHandler().getConnection());
59+
}
60+
61+
@Inject(method = "onPlayerRespawn",
62+
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/world/ClientWorld;addPlayer(ILnet/minecraft/client/network/AbstractClientPlayerEntity;)V"),
63+
locals = LocalCapture.CAPTURE_FAILHARD)
64+
public void patchwork$hookPlayerRespawn(PlayerRespawnS2CPacket packet, CallbackInfo ci, DimensionType dimensionType, ClientPlayerEntity clientPlayerEntity, int i, String string, ClientPlayerEntity clientPlayerEntity2) {
65+
ClientNetworkingEvents.firePlayerRespawn(this.client.interactionManager, clientPlayerEntity, clientPlayerEntity2, clientPlayerEntity2.networkHandler.getConnection());
66+
}
4567
}

patchwork-networking/src/main/resources/fabric.mod.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"mixins": [
2020
"patchwork-networking.accessor.mixins.json",
21+
"patchwork-networking.client.mixins.json",
2122
"patchwork-networking.handler.mixins.json",
2223
"patchwork-networking.packet.mixins.json"
2324
],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"required": true,
3+
"package": "net.patchworkmc.mixin.networking.client",
4+
"compatibilityLevel": "JAVA_8",
5+
"client": [
6+
"MixinMinecraftClient"
7+
],
8+
"injectors": {
9+
"defaultRequire": 1
10+
}
11+
}

0 commit comments

Comments
 (0)