Skip to content

Commit

Permalink
Fix human spawn packets injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Sep 27, 2024
1 parent 44dfdab commit 5e985b7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "org.spongepowered.common.mixin.core",
"priority": 1301,
"mixins": [
"server.level.ServerEntityMixin_Shared",
"world.entity.projectile.FishingHookMixin_Shared"
],
"overwrites": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.neoforge.mixin.core.server.level;

import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.server.level.ServerEntity;
import net.minecraft.world.entity.Entity;
import net.neoforged.neoforge.network.bundle.PacketAndPayloadAcceptor;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.common.entity.living.human.HumanEntity;

import java.util.EnumSet;
import java.util.List;

@Mixin(ServerEntity.class)
public class ServerEntityMixin_Neo {
@Shadow @Final private Entity entity;

/**
* @author gabizou
* @reason Because the entity spawn packet is just a lone packet, we have to actually
* do some hackery to create the player list packet first, then the spawn packet,
* then perform the remove packet.
*/
@Redirect(
method = "sendPairingData",
at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/network/bundle/PacketAndPayloadAcceptor;accept(Lnet/minecraft/network/protocol/Packet;)Lnet/neoforged/neoforge/network/bundle/PacketAndPayloadAcceptor;", ordinal = 0)
)
public PacketAndPayloadAcceptor impl$sendHumanSpawnPacket(PacketAndPayloadAcceptor consumer, Packet spawnPacket) {
if (!(this.entity instanceof final HumanEntity human)) {
return consumer.accept(spawnPacket);
}
// Adds the GameProfile to the client
consumer.accept(human.createPlayerListPacket(EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)));
// Actually spawn the human (a player)
consumer.accept(spawnPacket);
// Remove from the player map
final ClientboundPlayerInfoRemovePacket removePacket = new ClientboundPlayerInfoRemovePacket(List.of(human.getUUID()));
if (human.canRemoveFromListImmediately()) {
consumer.accept(removePacket);
} else {
// Human is a Player entity on the client and needs to tick once for the skin to render
human.removeFromTabListDelayed(null, removePacket);
}
return consumer;
}
}
1 change: 1 addition & 0 deletions neoforge/src/mixins/resources/mixins.spongeneo.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"server.BootstrapMixin_Neo",
"server.MinecraftServerMixin_Neo",
"server.commands.SpreadPlayersCommandMixin_Neo",
"server.level.ServerEntityMixin_Neo",
"server.level.ServerPlayerMixin_Neo",
"server.network.ServerGamePacketListenerImplMixin_Neo",
"server.network.ServerLoginPacketListenerImplMixin_Neo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.List;
import java.util.function.Consumer;

// TODO NeoForge
// Forge and Vanilla
@Mixin(ServerEntity.class)
public class ServerEntityMixin_Shared {
Expand All @@ -54,7 +53,7 @@ public class ServerEntityMixin_Shared {
*/
@Redirect(
method = "sendPairingData",
at = @At(value = "INVOKE", remap = false, target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V", ordinal = 0)
at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V", ordinal = 0)
)
public void impl$sendHumanSpawnPacket(final Consumer<Packet<?>> consumer, final Object spawnPacket) {
if (!(this.entity instanceof final HumanEntity human)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "org.spongepowered.common.mixin.core",
"priority": 1301,
"mixins": [
"server.level.ServerEntityMixin_Shared",
"world.entity.projectile.FishingHookMixin_Shared"
],
"overwrites": {
Expand Down

0 comments on commit 5e985b7

Please sign in to comment.