Skip to content

Commit

Permalink
enhance: 修改Meta包时只剔除不属于BASE_LIVING范围的值而非构建重新构建完整Meta包
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 27, 2023
1 parent 2c361cb commit 0347572
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
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.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.registries.EntryIndex;
Expand Down Expand Up @@ -137,6 +139,38 @@ public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters
return packets;
}

/**
* 从给定的meta包中移除不属于给定AbstractValues的数据
* @return
*/
public PacketContainer removeNonLivingValues(AbstractValues av, PacketContainer originalPacket)
{
if (originalPacket.getType() != PacketType.Play.Server.ENTITY_METADATA)
throw new IllegalArgumentException("Original packet is not a valid metadata packet!");

var livingValues = av.getValues();
var modifier = originalPacket.getDataValueCollectionModifier();

//获取原Meta包中的数据
var wrappedData = modifier.read(0);

//剔除不属于BASE_LIVING的数据
wrappedData.removeIf(w ->
{
var rawValue = w.getRawValue();

var match = livingValues.stream().filter(sv ->
w.getIndex() == sv.index() && (rawValue == null || rawValue.getClass() == sv.defaultValue().getClass())
).findFirst().orElse(null);

return match == null;
});

modifier.write(0, wrappedData);

return originalPacket;
}

public PacketContainer buildDiffMetaPacket(Player player, SingleWatcher watcher)
{
var metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.injector.GamePhase;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.backends.server.renderer.network.PacketFactory;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
import xiamomc.morph.backends.server.renderer.network.registries.RenderRegistry;
import xiamomc.morph.utilities.NmsUtils;
import xiamomc.pluginbase.Annotations.Resolved;
Expand Down Expand Up @@ -64,12 +66,14 @@ private void onMetaPacket(ClientboundSetEntityDataPacket packet, PacketEvent pac
return;

//不要二次处理来自我们自己的包
//并且不要处理同为玩家的Meta包
var packetContainer = packetEvent.getPacket();
var meta = packetContainer.getMeta(PacketFactory.MORPH_PACKET_METAKEY);
if (meta.isEmpty())
if (meta.isEmpty() && watcher.getEntityType() != EntityType.PLAYER)
{
//取得来源玩家的伪装后的Meta,发送给目标玩家
packetEvent.setPacket(getFactory().buildFullMetaPacket(sourcePlayer, watcher));
//从包里移除玩家meta中不属于BASE_LIVING的部分
packetEvent.setPacket(getFactory().removeNonLivingValues(ValueIndex.BASE_LIVING, packetContainer));
}
}

Expand Down

0 comments on commit 0347572

Please sign in to comment.