Skip to content

Commit

Permalink
misc: 一些调整
Browse files Browse the repository at this point in the history
add: 添加史莱姆/岩浆怪Watcher
  • Loading branch information
MATRIX-feather committed Dec 14, 2023
1 parent 365521a commit daec845
Show file tree
Hide file tree
Showing 20 changed files with 413 additions and 90 deletions.
10 changes: 7 additions & 3 deletions src/main/java/xiamomc/morph/backends/server/ServerBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static ServerBackend getInstance()

private static ServerBackend instance;

private final ServerRenderer serverRenderer;
public final ServerRenderer serverRenderer;

public ServerBackend()
{
Expand Down Expand Up @@ -144,7 +144,11 @@ public boolean disguise(Player player, DisguiseWrapper<?> wrapper)
if (disguiseWrapperMap.containsKey(player.getUniqueId())) return false;

disguiseWrapperMap.put(player.getUniqueId(), serverDisguiseWrapper);
serverRenderer.renderEntity(player, wrapper.getEntityType(), wrapper.getDisguiseName());

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

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

Expand All @@ -157,7 +161,7 @@ public boolean disguise(Player player, DisguiseWrapper<?> wrapper)
@Override
public boolean unDisguise(Player player)
{
serverRenderer.unRenderEntity(player);
serverRenderer.unRegisterEntity(player);

var uuid = player.getUniqueId();
var wrapper = disguiseWrapperMap.getOrDefault(uuid, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.TagType;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
Expand All @@ -15,9 +14,10 @@
import org.slf4j.Logger;
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.backends.DisguiseWrapper;
import xiamomc.morph.backends.fallback.NilBackend;
import xiamomc.morph.backends.fallback.NilDisguise;
import xiamomc.morph.backends.fallback.NilWrapper;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
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 @@ -41,6 +41,16 @@ public void mergeCompound(CompoundTag compoundTag)

if (this.getEntityType() == EntityType.MAGMA_CUBE || this.getEntityType() == EntityType.SLIME)
resetDimensions();

if (bindingParameters != null)
{
var watcher = bindingParameters.watcher();
if (watcher instanceof SlimeWatcher slimeWatcher)
{
var size = Math.max(1, getCompound().getInt("Size"));
slimeWatcher.write(ValueIndex.SLIME_MAGMA.SIZE, size);
}
}
}

@Override
Expand Down Expand Up @@ -98,6 +108,9 @@ public void setDisplayingEquipments(@NotNull EntityEquipment newEquipment)
this.equipment.setArmorContents(newEquipment.getArmorContents());

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

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

@Override
Expand Down Expand Up @@ -138,6 +151,9 @@ public String getDisguiseName()
public void setDisguiseName(String name)
{
this.instance.name = name;

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

@Override
Expand Down Expand Up @@ -181,6 +197,9 @@ public void applySkin(GameProfile profile)
if (this.getEntityType() != EntityType.PLAYER) return;

this.instance.profile = profile;

if (bindingParameters != null)
bindingParameters.setProfile(profile);
}

@Override
Expand Down Expand Up @@ -210,4 +229,45 @@ public boolean isSaddled()
{
return instance.saddled;
}

private Player bindingPlayer;

public Player getBindingPlayer()
{
return bindingPlayer;
}

private RegistryParameters bindingParameters;

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

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)
{
logger.warn("Have a bindingPlayer but no registry parameters?!");
Thread.dumpStack();
return;
}

bindingParameters.setProfile(this.instance.profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import xiamomc.morph.backends.server.renderer.network.*;
import xiamomc.morph.backends.server.renderer.network.datawatcher.WatcherIndex;
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.RenderRegistry;
import xiamomc.morph.backends.server.renderer.skins.SkinStore;

public class ServerRenderer extends MorphPluginObject
{
private final ProtocolHandler protocolHandler;

private final RenderRegistry registry = new RenderRegistry();
public final RenderRegistry registry = new RenderRegistry();

private final SkinStore skinStore = new SkinStore();

Expand All @@ -30,13 +32,24 @@ public ServerRenderer()
dependencies.cache(protocolHandler = new ProtocolHandler());
}

public void renderEntity(Player player, EntityType entityType, String name)
/**
* 向后端渲染器注册玩家
* @param player 目标玩家
* @param entityType 目标类型
* @param name 伪装名称
*/
public RegistryParameters registerEntity(Player player, EntityType entityType, String name)
{
registry.register(player.getUniqueId(),
new RegistryParameters(entityType, name, WatcherIndex.getInstance().getWatcherForType(player, entityType)));
var parameters = new RegistryParameters(
entityType, name,
WatcherIndex.getInstance().getWatcherForType(player, entityType),
null);

registry.register(player.getUniqueId(), parameters);
return parameters;
}

public void unRenderEntity(Player player)
public void unRegisterEntity(Player player)
{
registry.unregister(player.getUniqueId());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 @@ -26,7 +27,7 @@ public class PacketFactory extends MorphPluginObject
{
public static final String MORPH_PACKET_METAKEY = "fm";

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

Expand All @@ -51,8 +52,9 @@ public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters
{
//logger.info("Building player info packet!");

Objects.requireNonNull(parameters.gameProfile(), "Null game profile!");
var gameProfile = new MorphGameProfile(parameters.gameProfile());
var parametersProfile = parameters.getProfile();
Objects.requireNonNull(parametersProfile, "Null game profile!");
var gameProfile = new MorphGameProfile(parametersProfile);

//todo: Get random UUID from world
//玩家在客户端的UUID会根据其GameProfile中的UUID设定,我们需要避免伪装的UUID和某一玩家自己的UUID冲突
Expand Down Expand Up @@ -91,7 +93,10 @@ public List<PacketContainer> buildSpawnPackets(Player player, DisplayParameters
var equipmentPacket = new ClientboundSetEquipmentPacket(player.getEntityId(),
ProtocolEquipment.toPairs(player.getEquipment()));
packets.add(PacketContainer.fromPacket(equipmentPacket));
packets.add(buildMetaPacket(player, parameters.watcher()));

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

return packets;
}
Expand All @@ -112,8 +117,6 @@ public PacketContainer buildMetaPacket(Player player, SingleWatcher watcher)

List<WrappedDataValue> wrappedDataValues = new ObjectArrayList<>();

watcher.sync();

Map<SingleValue<?>, Object> valuesToSent = new Object2ObjectOpenHashMap<>();
valuesToSent.putAll(watcher.getDirty());
watcher.getRegistry().forEach((single, v) ->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

public class ValueIndex
{
public static final EntityValues BASE_ENTITY = new EntityValues();
public static final LivingEntityValues BASE_LIVING = new LivingEntityValues();

public static final ArmorStandValues ARMOR_STAND = new ArmorStandValues();
public static final AllayValues ALLAY = new AllayValues();
public static final PlayerValues PLAYER = new PlayerValues();
public static final LivingEntityValues BASE_LIVING = new LivingEntityValues();
public static final EntityValues BASE_ENTITY = new EntityValues();
public static final SlimeValues SLIME_MAGMA = new SlimeValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.bukkit.entity.Player;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.ArmorStandValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.AllayWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.ArmorStandWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.LivingEntityWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.PlayerWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.*;

import java.util.Map;
import java.util.function.Function;
Expand All @@ -30,6 +27,8 @@ public WatcherIndex()
setTypeWatcher(EntityType.PLAYER, PlayerWatcher::new);
setTypeWatcher(EntityType.ALLAY, AllayWatcher::new);
setTypeWatcher(EntityType.ARMOR_STAND, ArmorStandWatcher::new);
setTypeWatcher(EntityType.SLIME, SlimeWatcher::new);
setTypeWatcher(EntityType.MAGMA_CUBE, MagmaWatcher::new);
}

private void setTypeWatcher(EntityType type, Function<Player, SingleWatcher> func)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ public class PlayerValues extends LivingEntityValues
public final SingleValue<Integer> SCORE = getSingle(0);
public final SingleValue<Byte> SKIN = getSingle((byte)0); //127
public final SingleValue<Byte> MAINHAND = getSingle((byte)1);
public final SingleValue<Object> LEFT_SHOULDER_PARROT_COMPOUND = getSingle(new Object());
public final SingleValue<Object> RIGHT_SHOULDER_PARROT_COMPOUND = getSingle(new Object());

public PlayerValues()
{
super();

System.out.println("SKIN is on index " + SKIN.index());

registerSingle(ABSORPTION_AMOUNT, SCORE, SKIN, MAINHAND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

public class SlimeValues extends MobValues
{
public final SingleValue<Integer> SIZE = getSingle(1);

public SlimeValues()
{
super();

registerSingle(SIZE);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
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.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Exceptions.NullDependencyException;

import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand Down Expand Up @@ -49,6 +55,7 @@ public SingleWatcher(Player bindingPlayer, EntityType entityType)
this.bindingPlayer = bindingPlayer;

this.entityType = entityType;
sync();
}

//region Value Registry
Expand Down
Loading

0 comments on commit daec845

Please sign in to comment.