Make fake players compatible with adventure platform lib and minor refactor around them (#1235)

* Make fake players compatible with adventure platform and minor refactor around them

Cleaner approach

* Remove no longer needed enderpearl mixin for fake players

Their ClientConnection now has an open channel so isOpen returns true
This commit is contained in:
altrisi authored and GitHub committed 2022-01-19 21:28:36 +01:00
1 parent c0bba54799
commit ec47dea267
9 files changed
+49 -93

No files matched your search

@@ -1,18 +1,21 @@
package carpet.mixins;
import carpet.fakes.ClientConnectionInterface;
import carpet.logging.logHelpers.PacketCounter;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.Packet;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientConnection.class)
public abstract class ClientConnectionMixin
public abstract class ClientConnectionMixin implements ClientConnectionInterface
{
// Add to the packet counter whenever a packet is received.
@Inject(method = "channelRead0", at = @At("HEAD"))
@@ -28,4 +31,8 @@ public abstract class ClientConnectionMixin
{
PacketCounter.totalOut++;
}
@Override
@Accessor //Compat with adventure-platform-fabric
public abstract void setChannel(Channel channel);
}
@@ -1,9 +1,7 @@
package carpet.mixins;
import com.mojang.authlib.GameProfile;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.ClientConnection;
import net.minecraft.text.TranslatableText;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayNetworkHandler;
@@ -15,14 +13,9 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import carpet.patches.NetHandlerPlayServerFake;
import carpet.patches.EntityPlayerMPFake;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
@Mixin(PlayerManager.class)
public abstract class PlayerManager_fakePlayersMixin
{
@@ -42,38 +35,13 @@ public abstract class PlayerManager_fakePlayersMixin
@Redirect(method = "onPlayerConnect", at = @At(value = "NEW", target = "net/minecraft/server/network/ServerPlayNetworkHandler"))
private ServerPlayNetworkHandler replaceNetworkHandler(MinecraftServer server, ClientConnection clientConnection, ServerPlayerEntity playerIn)
{
boolean isServerPlayerEntity = playerIn instanceof EntityPlayerMPFake;
if (isServerPlayerEntity)
if (playerIn instanceof EntityPlayerMPFake fake)
{
return new NetHandlerPlayServerFake(this.server, clientConnection, playerIn);
return new NetHandlerPlayServerFake(this.server, clientConnection, fake);
}
else
{
return new ServerPlayNetworkHandler(this.server, clientConnection, playerIn);
}
}
@Redirect(method = "createPlayer", at = @At(value = "INVOKE", target = "Ljava/util/Iterator;hasNext()Z"))
private boolean cancelWhileLoop(Iterator iterator)
{
return false;
}
@Inject(method = "createPlayer", at = @At(value = "INVOKE", shift = At.Shift.BEFORE,
target = "Ljava/util/Iterator;hasNext()Z"), locals = LocalCapture.CAPTURE_FAILHARD)
private void newWhileLoop(GameProfile gameProfile_1, CallbackInfoReturnable<ServerPlayerEntity> cir, UUID uUID_1,
List list_1, ServerPlayerEntity serverPlayerEntity, Iterator var5)
{
while (var5.hasNext())
{
ServerPlayerEntity serverPlayerEntity_3 = (ServerPlayerEntity) var5.next();
if(serverPlayerEntity_3 instanceof EntityPlayerMPFake)
{
((EntityPlayerMPFake)serverPlayerEntity_3).kill(new TranslatableText("multiplayer.disconnect.duplicate_login"));
continue;
}
serverPlayerEntity_3.networkHandler.disconnect(new TranslatableText("multiplayer.disconnect.duplicate_login"));
}
}
}
@@ -1,29 +0,0 @@
package carpet.mixins;
import carpet.patches.EntityPlayerMPFake;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.projectile.thrown.EnderPearlEntity;
import net.minecraft.entity.projectile.thrown.ThrownItemEntity;
import net.minecraft.network.ClientConnection;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(EnderPearlEntity.class)
public abstract class ThrownEnderpearlEntity_fakePlayersMixin extends ThrownItemEntity
{
public ThrownEnderpearlEntity_fakePlayersMixin(EntityType<? extends ThrownItemEntity> entityType_1, World world_1)
{
super(entityType_1, world_1);
}
@Redirect(method = "onCollision", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/ClientConnection;isOpen()Z"
))
private boolean isConnectionGood(ClientConnection clientConnection)
{
return clientConnection.isOpen() || getOwner() instanceof EntityPlayerMPFake;
}
}