Skip to content

Commit 14e6a89

Browse files
committed
Add PlayerRespawnEvent
1 parent 2131de4 commit 14e6a89

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023-2025 Fox2Code
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.fox2code.foxloader.event.player;
25+
26+
import net.minecraft.common.entity.player.EntityPlayer;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
public final class PlayerRespawnEvent extends PlayerEvent {
30+
public PlayerRespawnEvent(@NotNull EntityPlayer entityPlayer) {
31+
super(entityPlayer);
32+
}
33+
}

loader/src/main/java/com/fox2code/foxloader/internal/InternalPlayerHooks.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.fox2code.foxloader.event.player.PlayerChatEvent;
2828
import com.fox2code.foxloader.event.player.PlayerJoinEvent;
2929
import com.fox2code.foxloader.event.player.PlayerLeaveEvent;
30+
import com.fox2code.foxloader.event.player.PlayerRespawnEvent;
3031
import net.minecraft.common.entity.player.EntityPlayer;
3132
import net.minecraft.common.networking.Packet3Chat;
3233
import net.minecraft.server.MinecraftServer;
@@ -38,6 +39,8 @@ public final class InternalPlayerHooks {
3839
EventHolder.getHolderFromEvent(PlayerJoinEvent.class);
3940
private static final EventHolder<PlayerLeaveEvent> PLAYER_LEAVE_EVENT =
4041
EventHolder.getHolderFromEvent(PlayerLeaveEvent.class);
42+
private static final EventHolder<PlayerRespawnEvent> PLAYER_RESPAWN_EVENT =
43+
EventHolder.getHolderFromEvent(PlayerRespawnEvent.class);
4144

4245
private InternalPlayerHooks() {}
4346

@@ -65,4 +68,9 @@ public static void sendPlayerLeaveEvent(MinecraftServer minecraftServer, EntityP
6568
minecraftServer.configManager.sendPacketToAllPlayers(new Packet3Chat(playerJoinEvent.getLeaveMessage()));
6669
}
6770
}
71+
72+
public static void sendPlayerRespawnEvent(EntityPlayer entityPlayer) {
73+
if (PLAYER_RESPAWN_EVENT.isEmpty()) return;
74+
PLAYER_RESPAWN_EVENT.callEvent(new PlayerRespawnEvent(entityPlayer));
75+
}
6876
}

patching/src/main/java/com/fox2code/foxloader/patching/game/ClientGameDirectoryPatch.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ public ClassNode transform(ClassNode classNode) {
136136
changeWorldSetPlayer.add(notNewPlayer);
137137
changeWorld.instructions.insert(setPlayer, changeWorldSetPlayer);
138138
}
139+
MethodNode respawnPlayer = TransformerUtils.getMethod(classNode, "respawn");
140+
InsnList respawnPlayerAppend = new InsnList();
141+
respawnPlayerAppend.add(new VarInsnNode(ALOAD, 0));
142+
respawnPlayerAppend.add(new FieldInsnNode(GETFIELD,
143+
Minecraft, "thePlayer", "L" + EntityPlayerSP + ";"));
144+
respawnPlayerAppend.add(new MethodInsnNode(INVOKESTATIC, InternalPlayerHooks,
145+
"sendPlayerRespawnEvent", "(L" + EntityPlayer + ";)V"));
146+
TransformerUtils.insertToEndOfCode(respawnPlayer, respawnPlayerAppend);
139147
}
140148
return classNode;
141149
}

patching/src/main/java/com/fox2code/foxloader/patching/game/NetworkConnectionPatch.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public ClassNode transform(ClassNode classNode) {
8080
return transformPacket(classNode);
8181
case Packet2ClientProtocol:
8282
return transformPacket2ClientProtocol(classNode);
83+
case ServerConfigurationManager:
84+
return transformServerConfigurationManager(classNode);
8385
case NetClientHandler:
8486
return transformNetHandlers(classNode, true, false, false);
8587
case NetLoginHandler:
@@ -449,6 +451,21 @@ private static ClassNode transformPacket2ClientProtocol(ClassNode classNode) {
449451
return classNode;
450452
}
451453

454+
private ClassNode transformServerConfigurationManager(ClassNode classNode) {
455+
MethodNode methodNode = TransformerUtils.getMethod(classNode, "respawnPlayer");
456+
AbstractInsnNode aReturn = TransformerUtils.previousCodeInsn(TransformerUtils.getEndingLabelNode(methodNode));
457+
VarInsnNode aLoad = (VarInsnNode) TransformerUtils.previousCodeInsn(aReturn);
458+
if (aLoad.getOpcode() != Opcodes.ALOAD) {
459+
throw new RuntimeException("Not ALOAD");
460+
}
461+
InsnList append = new InsnList();
462+
append.add(new VarInsnNode(ALOAD, aLoad.var));
463+
append.add(new MethodInsnNode(INVOKESTATIC, InternalPlayerHooks,
464+
"sendPlayerRespawnEvent", "(L" + EntityPlayer + ";)V"));
465+
methodNode.instructions.insertBefore(aLoad, append);
466+
return classNode;
467+
}
468+
452469
public static ClassNode transformNetHandlers(ClassNode classNode, boolean client, boolean login, boolean server) {
453470
String owner = classNode.name;
454471
FieldNode networkManagerField =

0 commit comments

Comments
 (0)