Skip to content

Commit

Permalink
misc: 使RegistryParameters仅作为注册时的参数用
Browse files Browse the repository at this point in the history
    - 将处理重心放到Watcher上
  • Loading branch information
MATRIX-feather committed Dec 14, 2023
1 parent daec845 commit e502e3a
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public boolean disguise(Player player, DisguiseWrapper<?> wrapper)

disguiseWrapperMap.put(player.getUniqueId(), serverDisguiseWrapper);

var parameters = serverRenderer.registerEntity(
var watcher = serverRenderer.registerEntity(
player, wrapper.getEntityType(), wrapper.getDisguiseName());

serverDisguiseWrapper.setRenderParameters(player, parameters);
serverDisguiseWrapper.setRenderParameters(player, watcher);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.backends.DisguiseWrapper;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.SlimeWatcher;
import xiamomc.morph.backends.server.renderer.network.registries.EntryIndex;
import xiamomc.morph.backends.server.renderer.network.registries.RegistryParameters;
import xiamomc.morph.misc.DisguiseEquipment;
import xiamomc.morph.misc.DisguiseState;
import xiamomc.morph.utilities.NbtUtils;
Expand All @@ -42,10 +42,9 @@ public void mergeCompound(CompoundTag compoundTag)
if (this.getEntityType() == EntityType.MAGMA_CUBE || this.getEntityType() == EntityType.SLIME)
resetDimensions();

if (bindingParameters != null)
if (bindingWatcher != null)
{
var watcher = bindingParameters.watcher();
if (watcher instanceof SlimeWatcher slimeWatcher)
if (bindingWatcher instanceof SlimeWatcher slimeWatcher)
{
var size = Math.max(1, getCompound().getInt("Size"));
slimeWatcher.write(ValueIndex.SLIME_MAGMA.SIZE, size);
Expand Down Expand Up @@ -109,8 +108,8 @@ public void setDisplayingEquipments(@NotNull EntityEquipment newEquipment)

this.equipment.setHandItems(newEquipment.getItemInMainHand(), newEquipment.getItemInOffHand());

if (bindingParameters != null)
bindingParameters.open().write(EntryIndex.EQUIPMENT, this.equipment).close();
if (bindingWatcher != null)
bindingWatcher.write(EntryIndex.EQUIPMENT, this.equipment);
}

@Override
Expand Down Expand Up @@ -152,8 +151,8 @@ public void setDisguiseName(String name)
{
this.instance.name = name;

if (bindingParameters != null)
bindingParameters.open().write(EntryIndex.CUSTOM_NAME, name).close();
if (bindingWatcher != null)
bindingWatcher.write(EntryIndex.CUSTOM_NAME, name);
}

@Override
Expand Down Expand Up @@ -198,8 +197,8 @@ public void applySkin(GameProfile profile)

this.instance.profile = profile;

if (bindingParameters != null)
bindingParameters.setProfile(profile);
if (bindingWatcher != null)
bindingWatcher.write(EntryIndex.PROFILE, this.instance.profile);
}

@Override
Expand Down Expand Up @@ -237,37 +236,29 @@ public Player getBindingPlayer()
return bindingPlayer;
}

private RegistryParameters bindingParameters;
private SingleWatcher bindingWatcher;

public void setRenderParameters(Player newBinding, RegistryParameters bindingParameters)
public void setRenderParameters(Player newBinding, SingleWatcher bindingWatcher)
{
bindingPlayer = newBinding;
this.bindingParameters = bindingParameters;
this.bindingWatcher = bindingWatcher;

refreshRegistry();
}

private void refreshRegistry()
{
/*
if (!(getBackend() instanceof ServerBackend serverBackend))
{
logger.warn("ServerDisguiseWrapper applied without ServerBackend?!");
Thread.dumpStack();
return;
}
*/

if (bindingPlayer == null)
return;

if (bindingParameters == null)
if (bindingWatcher == null)
{
logger.warn("Have a bindingPlayer but no registry parameters?!");
logger.warn("Have a bindingPlayer but no bindingWatcher?!");
Thread.dumpStack();
return;
}

bindingParameters.setProfile(this.instance.profile);
//todo: 激活刷新时也刷新到玩家
bindingWatcher.write(EntryIndex.PROFILE, this.instance.profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.backends.server.renderer.network.*;
import xiamomc.morph.backends.server.renderer.network.datawatcher.WatcherIndex;
import xiamomc.morph.backends.server.renderer.network.PacketFactory;
import xiamomc.morph.backends.server.renderer.network.ProtocolHandler;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.queue.PacketQueue;
import xiamomc.morph.backends.server.renderer.network.registries.RegistryParameters;
import xiamomc.morph.backends.server.renderer.network.registries.RegisterParameters;
import xiamomc.morph.backends.server.renderer.network.registries.RenderRegistry;
import xiamomc.morph.backends.server.renderer.skins.SkinStore;

Expand Down Expand Up @@ -38,15 +39,9 @@ public ServerRenderer()
* @param entityType 目标类型
* @param name 伪装名称
*/
public RegistryParameters registerEntity(Player player, EntityType entityType, String name)
public SingleWatcher registerEntity(Player player, EntityType entityType, String name)
{
var parameters = new RegistryParameters(
entityType, name,
WatcherIndex.getInstance().getWatcherForType(player, entityType),
null);

registry.register(player.getUniqueId(), parameters);
return parameters;
return registry.register(player, new RegisterParameters(entityType, name));
}

public void unRegisterEntity(Player player)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package xiamomc.morph.backends.server.renderer.network;

import com.mojang.authlib.GameProfile;
import org.bukkit.entity.EntityType;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;

public class DisplayParameters
{
public EntityType getEntityType()
{
return entityType;
}

public SingleWatcher getWatcher()
{
return singleWatcher;
}

public GameProfile getProfile()
{
return gameProfile;
}

private final EntityType entityType;
private final SingleWatcher singleWatcher;
private final GameProfile gameProfile;

public DisplayParameters(EntityType bindingType, SingleWatcher watcher, GameProfile profile)
{
this.entityType = bindingType;
this.singleWatcher = watcher;
this.gameProfile = profile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import xiamomc.morph.MorphPluginObject;
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.RegistryParameters;
import xiamomc.morph.backends.server.renderer.utilties.ProtocolRegistryUtils;
import xiamomc.morph.misc.MorphGameProfile;
import xiamomc.morph.misc.NmsRecord;
Expand All @@ -27,13 +26,13 @@ public class PacketFactory extends MorphPluginObject
{
public static final String MORPH_PACKET_METAKEY = "fm";

public List<PacketContainer> buildSpawnPackets(Player player, RegistryParameters parameters)
public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters parameters)
{
List<PacketContainer> packets = new ObjectArrayList<>();

//logger.info("Build spawn packets, player is " + player.getName() + " :: parameters are " + parameters);

var playerType = parameters.bukkitType();
var playerType = parameters.getEntityType();
var nmsType = EntityTypeUtils.getNmsType(playerType);
if (nmsType == null)
{
Expand Down Expand Up @@ -94,7 +93,7 @@ public List<PacketContainer> buildSpawnPackets(Player player, RegistryParameters
ProtocolEquipment.toPairs(player.getEquipment()));
packets.add(PacketContainer.fromPacket(equipmentPacket));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
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.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Exceptions.NullDependencyException;

Expand All @@ -21,6 +26,10 @@

public abstract class SingleWatcher extends MorphPluginObject
{
protected void initRegistry()
{
}

public final UUID bindingUUID;

private Player bindingPlayer;
Expand Down Expand Up @@ -55,9 +64,54 @@ public SingleWatcher(Player bindingPlayer, EntityType entityType)
this.bindingPlayer = bindingPlayer;

this.entityType = entityType;
initRegistry();

sync();
}

//region Custom Registry

protected final Map<String, Object> customRegistry = new Object2ObjectOpenHashMap<>();

public <X> void write(RegistryKey<X> key, X value)
{
customRegistry.put(key.name, value);

onCustomWrite(key, value);
}

protected void onCustomWrite(RegistryKey<?> key, Object val)
{
}

public <X> X getOrDefault(RegistryKey<X> key, X defaultValue)
{
var val = get(key);

return val == null ? defaultValue : val;
}

@Nullable
public <X> X get(RegistryKey<X> key)
{
var val = customRegistry.getOrDefault(key.name, null);

if (val == null) return null;

if (key.type.isInstance(val))
{
return (X)val;
}
else
{
logger.warn("Find incompatible value '%s' for key '%s'!".formatted(val, key));

return null;
}
}

//endregion Custom Registry

//region Value Registry

protected final Map<SingleValue<?>, Object> registry = new Object2ObjectOpenHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

public class AllayWatcher extends LivingEntityWatcher
{
public AllayWatcher(Player bindingPlayer)
@Override
protected void initRegistry()
{
super(bindingPlayer, EntityType.ALLAY);
super.initRegistry();

register(ValueIndex.ALLAY);
}

public AllayWatcher(Player bindingPlayer)
{
super(bindingPlayer, EntityType.ALLAY);
}

@Override
protected void doSync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

public class ArmorStandWatcher extends LivingEntityWatcher
{
public ArmorStandWatcher(Player bindingPlayer)
@Override
protected void initRegistry()
{
super(bindingPlayer, EntityType.ARMOR_STAND);
super.initRegistry();

register(ValueIndex.ARMOR_STAND);
}

public ArmorStandWatcher(Player bindingPlayer)
{
super(bindingPlayer, EntityType.ARMOR_STAND);
}

@Override
protected void doSync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@

public class EntityWatcher extends SingleWatcher
{
public EntityWatcher(Player bindingPlayer, EntityType entityType)
@Override
protected void initRegistry()
{
super(bindingPlayer, entityType);
super.initRegistry();

register(ValueIndex.BASE_ENTITY);
}

public EntityWatcher(Player bindingPlayer, EntityType entityType)
{
super(bindingPlayer, entityType);
}

@Override
protected void doSync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

public class LivingEntityWatcher extends EntityWatcher
{
public LivingEntityWatcher(Player bindingPlayer, EntityType entityType)
@Override
protected void initRegistry()
{
super(bindingPlayer, entityType);
super.initRegistry();

register(ValueIndex.BASE_LIVING);
}

public LivingEntityWatcher(Player bindingPlayer, EntityType entityType)
{
super(bindingPlayer, entityType);
}

@Override
protected void doSync()
{
Expand Down
Loading

0 comments on commit e502e3a

Please sign in to comment.