Skip to content

Commit

Permalink
feat: Add PlayerConnectedEvent that fires earlier than Forge's Player…
Browse files Browse the repository at this point in the history
…Login, move Fabric's PlayerLogin to fire same time as Forge's PlayerLogin for consistency
  • Loading branch information
BlayTheNinth committed Dec 9, 2023
1 parent e036411 commit b2514b0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void registerEvents(FabricBalmEvents events) {
});
});

events.registerEvent(PlayerLoginEvent.class, () -> ServerPlayConnectionEvents.JOIN.register((listener, sender, server) -> {
events.registerEvent(PlayerConnectedEvent.class, () -> ServerPlayConnectionEvents.JOIN.register((listener, sender, server) -> {
final PlayerLoginEvent event = new PlayerLoginEvent(listener.player);
events.fireEventHandlers(event);
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.blay09.mods.balm.mixin;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.event.PlayerLoginEvent;
import net.minecraft.network.Connection;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerList.class)
public class PlayerListMixin {
@Inject(method = "placeNewPlayer", at = @At(value = "RETURN"))
private void handlePlayerConnection(Connection connection, ServerPlayer player, CommonListenerCookie commonListenerCookie, CallbackInfo callbackInfo) {
final PlayerLoginEvent event = new PlayerLoginEvent(player);
Balm.getEvents().fireEvent(event);
}
}
3 changes: 2 additions & 1 deletion fabric/src/main/resources/balm.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"ServerPlayerMixin",
"SlotAccessor",
"StemBlockMixin",
"MinecraftServerMixin"
"MinecraftServerMixin",
"PlayerListMixin"
],
"client": [
"AbstractClientPlayerMixin",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.blay09.mods.balm.mixin;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.event.PlayerConnectedEvent;
import net.minecraft.network.Connection;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerList.class)
public class PlayerListMixin {
@Inject(method = "placeNewPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientboundPlayerAbilitiesPacket;<init>(Lnet/minecraft/world/entity/player/Abilities;)V"))
private void handlePlayerConnection(Connection connection, ServerPlayer player, CommonListenerCookie commonListenerCookie, CallbackInfo callbackInfo) {
final PlayerConnectedEvent event = new PlayerConnectedEvent(player);
Balm.getEvents().fireEvent(event);
}
}
3 changes: 2 additions & 1 deletion forge/src/main/resources/balm.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"ReloadableServerResourcesMixin",
"SlotAccessor",
"MinecraftServerMixin",
"EntityMixin"
"EntityMixin",
"PlayerListMixin"
],
"client": [
"AbstractContainerScreenAccessor",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.blay09.mods.balm.api.event;

import net.minecraft.server.level.ServerPlayer;

public class PlayerConnectedEvent extends BalmEvent {
private final ServerPlayer player;

public PlayerConnectedEvent(ServerPlayer player) {
this.player = player;
}

public ServerPlayer getPlayer() {
return player;
}
}

0 comments on commit b2514b0

Please sign in to comment.