Skip to content

Commit

Permalink
Merge branch 'server-renderer' into 1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 28, 2023
2 parents 25ce685 + dc51271 commit 57eb3ee
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 156 deletions.
3 changes: 0 additions & 3 deletions src/main/java/xiamomc/morph/MorphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,10 +661,7 @@ else if (!provider.isValid(key))
var compound = provider.getNbtCompound(outComingState, targetEntity);

if (compound != null)
{
outComingState.setCachedNbtString(NbtUtils.getCompoundString(compound));
outComingState.getDisguiseWrapper().mergeCompound(compound);
}

// 如果此伪装可以同步给客户端,那么初始化客户端状态
if (provider.validForClient(outComingState))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters
gameProfile.setName(subStr);
}

if (gameProfile.getName().isBlank())
throw new IllegalArgumentException("GameProfile name is empty!");

var packetPlayerInfo = new ClientboundPlayerInfoUpdatePacket(
EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER),
new ClientboundPlayerInfoUpdatePacket.Entry(
Expand All @@ -88,6 +91,17 @@ public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters

spawnUUID = uuid;
packets.add(PacketContainer.fromPacket(packetPlayerInfo));

var watcher = parameters.getWatcher();
var lastUUID = watcher.getOrDefault(EntryIndex.TABLIST_UUID, null);

if (lastUUID != null)
{
var packetTabRemove = new ClientboundPlayerInfoRemovePacket(List.of(lastUUID));
packets.add(PacketContainer.fromPacket(packetTabRemove));
}

parameters.getWatcher().write(EntryIndex.TABLIST_UUID, uuid);
}

var pitch = player.getPitch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
import com.comphenix.protocol.injector.GamePhase;
import com.mojang.authlib.GameProfile;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket;
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.GameType;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spigotmc.SpigotWorldConfig;
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.datawatcher.watchers.SingleWatcher;
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 @@ -51,15 +48,16 @@ public SpawnPacketHandler()
registry.onRegister(this, ep ->
refreshStateForPlayer(ep.player(), getAffectedPlayers(ep.player())));

registry.onUnRegister(this, this::unDisguiseForPlayer);
registry.onUnRegister(this, ep ->
unDisguiseForPlayer(ep.player(), ep.parameters()));
}

private List<Player> getAffectedPlayers(Player sourcePlayer)
{
return WatcherUtils.getAffectedPlayers(sourcePlayer);
}

private void unDisguiseForPlayer(@Nullable Player player)
private void unDisguiseForPlayer(@Nullable Player player, SingleWatcher disguiseWatcher)
{
if (player == null) return;

Expand All @@ -75,6 +73,15 @@ private void unDisguiseForPlayer(@Nullable Player player)

var removePacket = new ClientboundRemoveEntitiesPacket(player.getEntityId());
var rmPacketContainer = PacketContainer.fromPacket(removePacket);

var lastUUID = disguiseWatcher.getOrDefault(EntryIndex.TABLIST_UUID, null);
if (lastUUID != null)
{
spawnPackets.add(PacketContainer.fromPacket(
new ClientboundPlayerInfoRemovePacket(List.of(lastUUID))
));
}

affectedPlayers.forEach(p ->
{
protocolManager.sendServerPacket(p, rmPacketContainer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xiamomc.morph.backends.server.renderer.network.registries;

import com.mojang.authlib.GameProfile;
import net.minecraft.Util;
import org.bukkit.entity.EntityType;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.misc.DisguiseEquipment;
Expand All @@ -14,4 +15,6 @@ public class EntryIndex

public static final RegistryKey<DisguiseEquipment> EQUIPMENT = RegistryKey.of("equip", new DisguiseEquipment());
public static final RegistryKey<Boolean> DISPLAY_FAKE_EQUIPMENT = RegistryKey.of("display_fake_equip", false);

public static final RegistryKey<UUID> TABLIST_UUID = RegistryKey.of("tablist_uuid", Util.NIL_UUID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public record EventParameters(Player player, SingleWatcher parameters)
}

private final Map<Object, Consumer<EventParameters>> onRegisterConsumers = new Object2ObjectOpenHashMap<>();
private final Map<Object, Consumer<Player>> unRegisterConsumers = new Object2ObjectOpenHashMap<>();
private final Map<Object, Consumer<EventParameters>> unRegisterConsumers = new Object2ObjectOpenHashMap<>();
private final Map<Object, Consumer<EventParameters>> onRegistryChangeConsumers = new Object2ObjectOpenHashMap<>();

public void onRegistryChange(Object source, Consumer<EventParameters> consumer)
Expand All @@ -41,14 +41,14 @@ private void callRegister(Player player, SingleWatcher watcher)
onRegisterConsumers.forEach((source, consumer) -> consumer.accept(ep));
}

public void onUnRegister(Object source, Consumer<Player> consumer)
public void onUnRegister(Object source, Consumer<EventParameters> consumer)
{
unRegisterConsumers.put(source, consumer);
}

private void callUnregister(Player player)
private void callUnregister(Player player, SingleWatcher watcher)
{
unRegisterConsumers.forEach((source, consumer) -> consumer.accept(player));
unRegisterConsumers.forEach((source, consumer) -> consumer.accept(new EventParameters(player, watcher)));
}

//region Registry
Expand All @@ -74,8 +74,8 @@ public void unregister(Player player)

public void unregister(UUID uuid)
{
watcherMap.remove(uuid);
callUnregister(Bukkit.getPlayer(uuid));
var watcher = watcherMap.remove(uuid);
callUnregister(Bukkit.getPlayer(uuid), watcher);
}

/**
Expand Down Expand Up @@ -112,10 +112,12 @@ public void register(@NotNull UUID uuid, @NotNull SingleWatcher watcher)

public void reset()
{
var players = watcherMap.keySet().stream().toList();
watcherMap.clear();
watcherMap.forEach((uuid, watcher) ->
{
callRegister(Bukkit.getPlayer(uuid), watcher);
});

players.forEach(uuid -> callUnregister(Bukkit.getPlayer(uuid)));
watcherMap.clear();
}

//endregion Registry
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/xiamomc/morph/events/CommonEventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,6 @@ public void onPlayerJoin(PlayerJoinEvent e)
state.getSkillLookupIdentifier(), wrapper, state.shouldHandlePose(), false,
state.getDisguisedItems());

state.setCachedNbtString(nbt);
state.setCachedProfileNbtString(profile);

if (customName != null)
{
state.entityCustomName = customName;
Expand Down
37 changes: 7 additions & 30 deletions src/main/java/xiamomc/morph/misc/DisguiseState.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@
import xiamomc.morph.skills.SkillType;
import xiamomc.morph.skills.impl.NoneMorphSkill;
import xiamomc.morph.storage.playerdata.PlayerMeta;
import xiamomc.morph.utilities.EntityTypeUtils;
import xiamomc.morph.utilities.ItemUtils;
import xiamomc.morph.utilities.MathUtils;
import xiamomc.morph.utilities.SoundUtils;
import xiamomc.morph.utilities.*;
import xiamomc.pluginbase.Annotations.Resolved;

import java.util.Arrays;
Expand Down Expand Up @@ -424,41 +421,27 @@ public boolean shouldHandlePose()

//region NBT

private String cachedNbtString = "{}";

public String getCachedNbtString()
{
return cachedNbtString;
}

public void setCachedNbtString(String newNbt)
{
if (newNbt == null || newNbt.isEmpty() || newNbt.isBlank()) newNbt = "{}";

this.cachedNbtString = newNbt;
return NbtUtils.getCompoundString(disguiseWrapper.getCompound());
}

//endregion

//region ProfileNBT

private String cachedProfileNbtString = "{}";

public String getProfileNbtString()
{
return cachedProfileNbtString;
}

public void setCachedProfileNbtString(String newNbt)
{
if (newNbt == null || newNbt.isEmpty() || newNbt.isBlank()) newNbt = "{}";
if (!haveProfile())
return "{}";

this.cachedProfileNbtString = newNbt;
var s = NbtUtils.getCompoundString(NbtUtils.toCompoundTag(disguiseWrapper.getSkin()));
return s;
}

public boolean haveProfile()
{
return !cachedProfileNbtString.equals("{}");
return disguiseWrapper.getSkin() != null;
}

//endregion ProfileNBT
Expand Down Expand Up @@ -495,9 +478,6 @@ public void setDisguise(@NotNull String identifier, @NotNull String skillIdentif
{
if (disguiseWrapper == wrapper) return;

setCachedProfileNbtString(null);
setCachedNbtString(null);

this.entityCustomName = null;

this.disguiseWrapper = wrapper;
Expand Down Expand Up @@ -788,9 +768,6 @@ public DisguiseState createCopy()
var state = new DisguiseState(player, this.disguiseIdentifier, this.skillLookupIdentifier,
disguise, shouldHandlePose, provider, getDisguisedItems(), this.playerOptions, morphConfiguration);

state.setCachedProfileNbtString(this.cachedProfileNbtString);
state.setCachedNbtString(this.cachedNbtString);

return state;
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/xiamomc/morph/misc/DisguiseStateGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.backends.DisguiseBackend;
import xiamomc.morph.network.PlayerOptions;
import xiamomc.morph.providers.PlayerDisguiseProvider;
import xiamomc.morph.skills.MorphSkillHandler;
import xiamomc.morph.skills.SkillType;
import xiamomc.morph.storage.offlinestore.OfflineDisguiseState;
Expand Down Expand Up @@ -80,18 +79,20 @@ public static DisguiseState fromOfflineState(OfflineDisguiseState offlineState,
wrapper, true, provider,
null, playerOptions, playerMeta);

//设置NBT和皮肤
state.setCachedProfileNbtString(offlineState.profileString);
state.setCachedNbtString(offlineState.snbt);

try
{
var profileCompound = NbtUtils.toCompoundTag(offlineState.profileString);

if (profileCompound != null)
{
var profile = net.minecraft.nbt.NbtUtils.readGameProfile(profileCompound);
wrapper.applySkin(profile);
var rawProfile = net.minecraft.nbt.NbtUtils.readGameProfile(profileCompound);

if (rawProfile != null)
{
var profile = new MorphGameProfile(rawProfile);
profile.setName(wrapper.getDisguiseName());
wrapper.applySkin(profile);
}
}
}
catch (Throwable t)
Expand Down
Loading

0 comments on commit 57eb3ee

Please sign in to comment.