Improve Packet Handling
All checks were successful
Gestus/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-08-14 16:54:11 -04:00
parent 248f925b99
commit 26492891a6
9 changed files with 62 additions and 72 deletions

View File

@ -6,7 +6,7 @@ org.gradle.jvmargs = -Xmx1G
minecraft_version = 1.16.2 minecraft_version = 1.16.2
curseforge_id = 401707 curseforge_id = 401707
simple_minecraft_version = 1.16.2 simple_minecraft_version = 1.16.2
yarn_build = 1 yarn_build = 12
fabric_loader_version = 0.9.0+build.204 fabric_loader_version = 0.9.0+build.204
# Mod Properties # Mod Properties

View File

@ -180,16 +180,16 @@ public class FakePlayerEntity extends ArmorStandEntity {
if (emote != null) { if (emote != null) {
layers[AIM_LAYER].play(emote, (hand == Hand.OFF_HAND) != isLeftHanded()); layers[AIM_LAYER].play(emote, (hand == Hand.OFF_HAND) != isLeftHanded());
return true;
} else {
return false; return false;
} else {
return true;
} }
} }
private void updateAim() { private void updateAim() {
Hand hand = player.getActiveHand(); Hand hand = player.getActiveHand();
if (!updateAim(hand, true)) { if (updateAim(hand, true) && updateAim(hand == Hand.MAIN_HAND ? Hand.OFF_HAND : Hand.MAIN_HAND, false)) {
updateAim(hand == Hand.MAIN_HAND ? Hand.OFF_HAND : Hand.MAIN_HAND, false); layers[AIM_LAYER].play(null, false);
} }
} }

View File

@ -0,0 +1,24 @@
package com.thebrokenrail.gestus.mixin;
import com.mojang.datafixers.util.Pair;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;
import java.util.List;
@Mixin(EntityEquipmentUpdateS2CPacket.class)
public interface EntityEquipmentUpdateS2CPacketAccessor {
@Accessor
int getId();
@Accessor
List<Pair<EquipmentSlot, ItemStack>> getEquipmentList();
@Mutable
@Accessor
void setEquipmentList(List<Pair<EquipmentSlot, ItemStack>> equipmentList);
}

View File

@ -1,27 +0,0 @@
package com.thebrokenrail.gestus.mixin;
import com.mojang.datafixers.util.Pair;
import com.thebrokenrail.gestus.util.Util;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.EntityTrackerEntry;
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.ModifyArg;
import java.util.List;
@Mixin(EntityTrackerEntry.class)
public class MixinEntityTrackerEntry {
@Shadow
@Final
private Entity entity;
@ModifyArg(at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/EntityEquipmentUpdateS2CPacket;<init>(ILjava/util/List;)V"), method = "*", index = 1)
public List<Pair<EquipmentSlot, ItemStack>> modifyEquipment(List<Pair<EquipmentSlot, ItemStack>> list) {
return Util.modifyEquipment(entity, list);
}
}

View File

@ -1,20 +0,0 @@
package com.thebrokenrail.gestus.mixin;
import com.mojang.datafixers.util.Pair;
import com.thebrokenrail.gestus.util.Util;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import java.util.List;
@Mixin(LivingEntity.class)
public class MixinLivingEntity {
@ModifyArg(at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/EntityEquipmentUpdateS2CPacket;<init>(ILjava/util/List;)V"), method = "*", index = 1)
public List<Pair<EquipmentSlot, ItemStack>> modifyEquipment(List<Pair<EquipmentSlot, ItemStack>> list) {
return Util.modifyEquipment((LivingEntity) (Object) this, list);
}
}

View File

@ -5,7 +5,9 @@ import com.thebrokenrail.gestus.util.Util;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.GenericFutureListener;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.Packet; import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket; import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.MobSpawnS2CPacket; import net.minecraft.network.packet.s2c.play.MobSpawnS2CPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayNetworkHandler;
@ -24,19 +26,25 @@ public class MixinServerPlayNetworkHandler {
@Inject(at = @At("HEAD"), method = "sendPacket(Lnet/minecraft/network/Packet;Lio/netty/util/concurrent/GenericFutureListener;)V", cancellable = true) @Inject(at = @At("HEAD"), method = "sendPacket(Lnet/minecraft/network/Packet;Lio/netty/util/concurrent/GenericFutureListener;)V", cancellable = true)
public void sendPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> listener, CallbackInfo info) { public void sendPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> listener, CallbackInfo info) {
if (packet instanceof MobSpawnS2CPacket) { if (packet == null) {
MobSpawnS2CPacketAccessor packetData = (MobSpawnS2CPacketAccessor) packet;
Entity entity = player.getEntityWorld().getEntityById(packetData.getId());
if (entity instanceof FakePlayerEntity && ((FakePlayerEntity) entity).player.getUuid().equals(player.getUuid())) {
info.cancel(); info.cancel();
} }
} }
}
@ModifyVariable(at = @At("HEAD"), method = "sendPacket(Lnet/minecraft/network/Packet;Lio/netty/util/concurrent/GenericFutureListener;)V", argsOnly = true) @ModifyVariable(at = @At("HEAD"), method = "sendPacket(Lnet/minecraft/network/Packet;Lio/netty/util/concurrent/GenericFutureListener;)V", argsOnly = true)
public Packet<?> modifyPacket(Packet<?> packet) { public Packet<?> modifyPacket(Packet<?> packet) {
if (packet instanceof EntityTrackerUpdateS2CPacket && ((EntityTrackerUpdateS2CPacketAccessor) packet).getId() == player.getEntityId()) { if (packet instanceof MobSpawnS2CPacket) {
MobSpawnS2CPacketAccessor packetData = (MobSpawnS2CPacketAccessor) packet;
Entity entity = player.getEntityWorld().getEntityById(packetData.getId());
if (entity instanceof FakePlayerEntity && ((FakePlayerEntity) entity).player.getUuid().equals(player.getUuid())) {
return null;
} else {
return packet;
}
} else if (packet instanceof EntityTrackerUpdateS2CPacket && ((EntityTrackerUpdateS2CPacketAccessor) packet).getId() == player.getEntityId()) {
return Util.modifyTracker(player, (EntityTrackerUpdateS2CPacket) packet); return Util.modifyTracker(player, (EntityTrackerUpdateS2CPacket) packet);
} else if (packet instanceof EntityEquipmentUpdateS2CPacket && player.getEntityWorld().getEntityById(((EntityEquipmentUpdateS2CPacketAccessor) packet).getId()) instanceof PlayerEntity) {
return Util.modifyEquipment((EntityEquipmentUpdateS2CPacket) packet);
} else { } else {
return packet; return packet;
} }

View File

@ -18,6 +18,8 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Objects;
@Mixin(ServerPlayerEntity.class) @Mixin(ServerPlayerEntity.class)
public abstract class MixinServerPlayerEntity extends PlayerEntity implements ServerPlayerEntityExtension { public abstract class MixinServerPlayerEntity extends PlayerEntity implements ServerPlayerEntityExtension {
@Unique @Unique
@ -50,7 +52,14 @@ public abstract class MixinServerPlayerEntity extends PlayerEntity implements Se
shadow.refreshPositionAndAngles(getX(), getY(), getZ(), yaw, pitch); shadow.refreshPositionAndAngles(getX(), getY(), getZ(), yaw, pitch);
shadow.setHeadRotation(new EulerAngle(pitch, getHeadYaw() - yaw, 0f)); shadow.setHeadRotation(new EulerAngle(pitch, getHeadYaw() - yaw, 0f));
shadow.setVelocity(getVelocity());
Vec3d olVVelocity = shadow.getVelocity();
Vec3d velocity = getVelocity();
if (!Objects.equals(olVVelocity, velocity)) {
shadow.setVelocity(velocity);
shadow.velocityDirty = true;
shadow.velocityModified = true;
}
shadow.setCustomNameVisible(!invisible && !isSneaking()); shadow.setCustomNameVisible(!invisible && !isSneaking());

View File

@ -2,30 +2,27 @@ package com.thebrokenrail.gestus.util;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import com.thebrokenrail.gestus.mixin.EntityAccessor; import com.thebrokenrail.gestus.mixin.EntityAccessor;
import com.thebrokenrail.gestus.mixin.EntityEquipmentUpdateS2CPacketAccessor;
import com.thebrokenrail.gestus.mixin.EntityTrackerUpdateS2CPacketAccessor; import com.thebrokenrail.gestus.mixin.EntityTrackerUpdateS2CPacketAccessor;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.effect.StatusEffects; import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket; import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Util { public class Util {
public static List<Pair<EquipmentSlot, ItemStack>> modifyEquipment(Entity entity, List<Pair<EquipmentSlot, ItemStack>> list) { public static EntityEquipmentUpdateS2CPacket modifyEquipment(EntityEquipmentUpdateS2CPacket packet) {
if (entity instanceof PlayerEntity) {
List<Pair<EquipmentSlot, ItemStack>> newList = new ArrayList<>(); List<Pair<EquipmentSlot, ItemStack>> newList = new ArrayList<>();
for (EquipmentSlot slot : EquipmentSlot.values()) { for (EquipmentSlot slot : EquipmentSlot.values()) {
newList.add(Pair.of(slot, ItemStack.EMPTY)); newList.add(Pair.of(slot, ItemStack.EMPTY));
} }
return newList; return new EntityEquipmentUpdateS2CPacket(((EntityEquipmentUpdateS2CPacketAccessor) packet).getId(), newList);
} else {
return list;
}
} }
private static final int INVISIBILITY_FLAG = 5; private static final int INVISIBILITY_FLAG = 5;

View File

@ -6,9 +6,8 @@
"ArmorStandEntityAccessor", "ArmorStandEntityAccessor",
"DataTrackerAccessor", "DataTrackerAccessor",
"EntityAccessor", "EntityAccessor",
"EntityEquipmentUpdateS2CPacketAccessor",
"EntityTrackerUpdateS2CPacketAccessor", "EntityTrackerUpdateS2CPacketAccessor",
"MixinEntityTrackerEntry",
"MixinLivingEntity",
"MixinMobSpawnS2CPacket", "MixinMobSpawnS2CPacket",
"MixinServerPlayerEntity", "MixinServerPlayerEntity",
"MixinServerPlayNetworkHandler", "MixinServerPlayNetworkHandler",