Skip to content

Commit

Permalink
only build diffs when setting meta
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 14, 2023
1 parent b20f9c3 commit d634984
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters

var watcher = parameters.getWatcher();
watcher.sync();
packets.add(buildMetaPacket(player, watcher));
packets.add(buildFullMetaPacket(player, watcher));

for (PacketContainer packet : packets)
{
Expand All @@ -107,7 +107,41 @@ public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters
return packets;
}

public PacketContainer buildMetaPacket(Player player, SingleWatcher watcher)
public PacketContainer buildDiffMetaPacket(Player player, SingleWatcher watcher)
{
var metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
metaPacket.getIntegers().write(0, player.getEntityId());

var modifier = metaPacket.getDataValueCollectionModifier();

List<WrappedDataValue> wrappedDataValues = new ObjectArrayList<>();
Map<SingleValue<?>, Object> valuesToSent = watcher.getDirty();

valuesToSent.forEach((single, v) ->
{
WrappedDataWatcher.Serializer serializer;

try
{
serializer = ProtocolRegistryUtils.getSerializer(single.defaultValue());
}
catch (Throwable t)
{
logger.warn("Error occurred while generating meta packet with id '%s': %s".formatted(single.index(), t.getMessage()));
return;
}

var value = new WrappedDataValue(single.index(), serializer, v);
wrappedDataValues.add(value);
});

modifier.write(0, wrappedDataValues);
metaPacket.setMeta(MORPH_PACKET_METAKEY, true);

return metaPacket;
}

public PacketContainer buildFullMetaPacket(Player player, SingleWatcher watcher)
{
var metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
metaPacket.getIntegers().write(0, player.getEntityId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.world.level.GameType;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.backends.server.renderer.network.PacketFactory;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.AbstractValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.SingleValue;
import xiamomc.morph.backends.server.renderer.network.queue.PacketQueue;
import xiamomc.morph.backends.server.renderer.network.queue.QueueEntry;
import xiamomc.morph.backends.server.renderer.network.registries.EntryIndex;
import xiamomc.morph.backends.server.renderer.network.registries.RegistryKey;
import xiamomc.morph.misc.DisguiseEquipment;
import xiamomc.morph.misc.NmsRecord;
import xiamomc.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Exceptions.NullDependencyException;
Expand Down Expand Up @@ -165,7 +157,7 @@ public void write(int index, Object value)
dirtySingles.put(single, value);

onTrackerWrite(index, prev, value);
sendPacketToAffectedPlayers(packetFactory.buildMetaPacket(getBindingPlayer(), this));
sendPacketToAffectedPlayers(packetFactory.buildDiffMetaPacket(getBindingPlayer(), this));
}

@Resolved(shouldSolveImmediately = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListeningWhitelist;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.injector.GamePhase;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -53,7 +52,7 @@ private void onMetaPacket(ClientboundSetEntityDataPacket packet, PacketEvent pac

//取得来源玩家的伪装后的Meta,发送给目标玩家
watcher.sync();
packetEvent.setPacket(getFactory().buildMetaPacket(sourcePlayer, watcher));
packetEvent.setPacket(getFactory().buildFullMetaPacket(sourcePlayer, watcher));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.comphenix.protocol.events.ListeningWhitelist;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.injector.GamePhase;
import com.mojang.authlib.GameProfile;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
Expand All @@ -14,7 +13,6 @@
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.GameType;
import org.bukkit.Bukkit;
Expand All @@ -26,7 +24,6 @@
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.backends.server.renderer.network.DisplayParameters;
import xiamomc.morph.backends.server.renderer.network.PacketFactory;
import xiamomc.morph.backends.server.renderer.network.ProtocolEquipment;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.PlayerWatcher;
import xiamomc.morph.backends.server.renderer.network.registries.EntryIndex;
import xiamomc.morph.backends.server.renderer.network.registries.RenderRegistry;
Expand Down Expand Up @@ -104,7 +101,7 @@ private void unDisguiseForPlayer(@Nullable Player player)

var watcher = new PlayerWatcher(player);
packets.add(getFactory().getEquipmentPacket(player, watcher));
packets.add(getFactory().buildMetaPacket(player, watcher));
packets.add(getFactory().buildFullMetaPacket(player, watcher));

affectedPlayers.forEach(p ->
{
Expand Down

0 comments on commit d634984

Please sign in to comment.