Skip to content

Commit

Permalink
fix: 对于克隆出来的伪装,MorphManager仍然会对其设定随机的伪装属性
Browse files Browse the repository at this point in the history
misc: 使用DisguieProperty替换原本的WrapperAttribute
  • Loading branch information
MATRIX-feather committed Oct 27, 2024
1 parent ebe9c3a commit ac0ff84
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 196 deletions.
26 changes: 15 additions & 11 deletions src/main/java/xyz/nifeather/morph/MorphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nifeather.morph.abilities.AbilityManager;
import xyz.nifeather.morph.backends.DisguiseBackend;
import xyz.nifeather.morph.backends.DisguiseWrapper;
import xyz.nifeather.morph.backends.WrapperAttribute;
import xyz.nifeather.morph.backends.WrapperProperties;
import xyz.nifeather.morph.backends.fallback.NilBackend;
import xyz.nifeather.morph.backends.server.ServerBackend;
import xyz.nifeather.morph.events.api.gameplay.*;
Expand Down Expand Up @@ -824,7 +823,7 @@ private DisguiseBuildResult buildDisguise(MorphParameters parameters, DisguiseMe
assert wrapper != null;

// 向Wrapper写入伪装ID
wrapper.write(WrapperAttribute.disguiseIdentifier, disguiseIdentifier);
wrapper.writeProperty(WrapperProperties.DISGUISE_ID, disguiseIdentifier);

// 获取此伪装将用来显示的目标装备
EntityEquipment equipment = null;
Expand Down Expand Up @@ -853,6 +852,9 @@ private DisguiseBuildResult buildDisguise(MorphParameters parameters, DisguiseMe
wrapper, provider, equipment,
clientHandler.getPlayerOption(player, true), playerMorphConfig);

if (result.isCopy())
outComingState.setSessionData(DATAKEY_SKIP_PROPERTIES, true);

return DisguiseBuildResult.of(outComingState, provider, disguiseMeta, targetEntity);
}
catch (IllegalArgumentException iae)
Expand All @@ -879,8 +881,7 @@ private DisguiseBuildResult buildDisguise(MorphParameters parameters, DisguiseMe
}
}

@Resolved
private AbilityManager abilityManager;
public static final String DATAKEY_SKIP_PROPERTIES = "skip_properties_init";

private void postBuildDisguise(DisguiseBuildResult result,
MorphParameters parameters,
Expand All @@ -896,13 +897,16 @@ private void postBuildDisguise(DisguiseBuildResult result,
var state = result.state();
var wrapper = state.getDisguiseWrapper();

// 同步伪装属性
var propertyHandler = state.disguisePropertyHandler();
propertyHandler.setProperties(disguiseProperties.get(state.getEntityType()));
propertyHandler.getAll().forEach((property, value) ->
if (!state.getSessionDataOr(DATAKEY_SKIP_PROPERTIES, Boolean.class, false))
{
wrapper.writeProperty((SingleProperty<Object>) property, value);
});
// 同步伪装属性
var propertyHandler = state.disguisePropertyHandler();
propertyHandler.setProperties(disguiseProperties.get(state.getEntityType()));
propertyHandler.getAll().forEach((property, value) ->
{
wrapper.writeProperty((SingleProperty<Object>) property, value);
});
}

// 初始化nbt
var wrapperCompound = provider.getInitialNbtCompound(state, targetEntity, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public boolean revokeFromPlayer(Player player, DisguiseState state)
{
if (!super.revokeFromPlayer(player, state)) return false;

state.removeCustomData(PROPERTY_ID);
state.removeSessionData(PROPERTY_ID);

return true;
}
Expand All @@ -92,7 +92,7 @@ public boolean handle(Player player, DisguiseState state)
//Find or refresh entity
var nmsRecord = NmsRecord.of(player);

var beamTarget = state.getCustomData(PROPERTY_ID, Entity.class);
var beamTarget = state.getSessionData(PROPERTY_ID, Entity.class);
if (beamTarget != null)
{
if (beamTarget.isAlive())
Expand Down Expand Up @@ -129,7 +129,7 @@ public boolean handle(Player player, DisguiseState state)
nmsRecord.nmsPlayer().hurt(source, option.damageWhenDestroyed);
}

state.removeCustomData(PROPERTY_ID);
state.removeSessionData(PROPERTY_ID);
}
}

Expand All @@ -147,7 +147,7 @@ public boolean handle(Player player, DisguiseState state)

if (beamTarget != newEntity)
{
state.setCustomData(PROPERTY_ID, newEntity);
state.setSessionData(PROPERTY_ID, newEntity);

if (state.getEntityType() == org.bukkit.entity.EntityType.ENDER_DRAGON)
clientHandler.sendCommand(player, this.getBeamCommand(state));
Expand All @@ -168,7 +168,7 @@ public void onClientInit(DisguiseState state)

public S2CSetSNbtCommand getBeamCommand(DisguiseState state)
{
var entity = state.getCustomData(PROPERTY_ID, Entity.class);
var entity = state.getSessionData(PROPERTY_ID, Entity.class);

var compound = new CompoundTag();

Expand Down
88 changes: 13 additions & 75 deletions src/main/java/xyz/nifeather/morph/backends/DisguiseWrapper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package xyz.nifeather.morph.backends;

import com.mojang.authlib.GameProfile;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.TagType;
Expand All @@ -11,7 +10,6 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand All @@ -23,7 +21,6 @@
import xyz.nifeather.morph.utilities.EntityTypeUtils;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
Expand Down Expand Up @@ -76,7 +73,7 @@ public TInstance getInstance()
*/
public boolean getDisplayingFakeEquipments()
{
return readOrDefault(WrapperAttribute.displayFakeEquip);
return readProperty(WrapperProperties.DISPLAY_FAKE_EQUIP);
}

/**
Expand All @@ -85,7 +82,7 @@ public boolean getDisplayingFakeEquipments()
*/
public void setDisplayingFakeEquipments(boolean newVal)
{
write(WrapperAttribute.displayFakeEquip, newVal);
writeProperty(WrapperProperties.DISPLAY_FAKE_EQUIP, newVal);
}

/**
Expand Down Expand Up @@ -125,7 +122,7 @@ public void onPlayerOffline()
*/
public String getDisguiseName()
{
return readOrDefault(WrapperAttribute.disguiseName);
return readProperty(WrapperProperties.DISGUISE_NAME);
}

/**
Expand All @@ -134,7 +131,7 @@ public String getDisguiseName()
*/
public void setDisguiseName(String name)
{
write(WrapperAttribute.disguiseName, name);
writeProperty(WrapperProperties.DISGUISE_NAME, name);
}

/**
Expand Down Expand Up @@ -262,7 +259,7 @@ protected int getSlimeSize()
*/
public void applySkin(GameProfile profile)
{
write(WrapperAttribute.profile, Optional.of(profile));
writeProperty(WrapperProperties.PROFILE, Optional.of(profile));
}

/**
Expand All @@ -272,7 +269,7 @@ public void applySkin(GameProfile profile)
@Nullable
public GameProfile getSkin()
{
return readOrDefault(WrapperAttribute.profile, Optional.empty()).orElse(null);
return readPropertyOr(WrapperProperties.PROFILE, Optional.empty()).orElse(null);
}

/**
Expand Down Expand Up @@ -334,12 +331,12 @@ public void dispose()

public void setSaddled(boolean saddled)
{
write(WrapperAttribute.saddled, saddled);
writeProperty(WrapperProperties.SADDLED, saddled);
}

public boolean isSaddled()
{
return readOrDefault(WrapperAttribute.saddled);
return readProperty(WrapperProperties.SADDLED);
}

public void setAggressive(boolean aggressive)
Expand All @@ -354,76 +351,17 @@ public void playAttackAnimation()

public abstract <X> void writeProperty(SingleProperty<X> property, X value);

public abstract <X> X readProperty(SingleProperty<X> property);

public abstract <X> X readPropertyOr(SingleProperty<X> property, X defaultVal);

private final Map<String, Object> attributes = new Object2ObjectArrayMap<>();

protected <T> void onAttributeWrite(WrapperAttribute<T> attribute, T value)
{
}

public Map<String, Object> getAttributes()
{
return new Object2ObjectArrayMap<>(attributes);
}

/**
* 仅用作克隆使用!
* @return 与此Property对应的值,如果没有设定则返回默认值
*/
@ApiStatus.Internal
protected void writeInternal(String id, Object val)
{
attributes.put(id, val);
}

public <T> void write(WrapperAttribute<T> attribute, T value)
{
attributes.put(attribute.getIdentifier(), value);

try
{
onAttributeWrite(attribute, value);
}
catch (Throwable t)
{
var logger = MorphPlugin.getInstance().getSLF4JLogger();

logger.error("Error invoking onAttributeWrite: " + t.getMessage());
t.printStackTrace();
}
}

@Nullable
public <T> T read(WrapperAttribute<T> attribute)
{
return readOrDefault(attribute, null);
}

@NotNull
public <T> T readOrThrow(WrapperAttribute<T> attribute)
{
var obj = readOrDefault(attribute, null);
public abstract <X> X readProperty(SingleProperty<X> property);

Objects.requireNonNull(obj, "Null value for attribute '%s'!".formatted(attribute.getIdentifier()));

return obj;
}

public <T> T readOrDefault(WrapperAttribute<T> attribute)
{
return readOrDefault(attribute, attribute.createDefault());
}

public <T> T readOrDefault(WrapperAttribute<T> attribute, T defaultVal)
{
var val = attributes.getOrDefault(attribute.getIdentifier(), null);
public abstract <X> X readPropertyOr(SingleProperty<X> property, X defaultVal);

if (val == null) return defaultVal;
public abstract <X> X readPropertyOrThrow(SingleProperty<X> property);

return (T) val;
}
public abstract Map<SingleProperty<?>, Object> getProperties();

public void playAnimation(String animationId)
{
Expand Down
48 changes: 0 additions & 48 deletions src/main/java/xyz/nifeather/morph/backends/WrapperAttribute.java

This file was deleted.

17 changes: 17 additions & 0 deletions src/main/java/xyz/nifeather/morph/backends/WrapperProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xyz.nifeather.morph.backends;

import com.mojang.authlib.GameProfile;
import net.minecraft.nbt.CompoundTag;
import xyz.nifeather.morph.misc.disguiseProperty.SingleProperty;

import java.util.Optional;

public class WrapperProperties
{
public static final SingleProperty<String> DISGUISE_ID = SingleProperty.of("wrapper_disguiseIdentifier", "nil");
public static final SingleProperty<Optional<GameProfile>> PROFILE = SingleProperty.of("wrapper_profile", Optional.empty());
public static final SingleProperty<CompoundTag> NBT = SingleProperty.of("wrapper_nbt", new CompoundTag());
public static final SingleProperty<Boolean> DISPLAY_FAKE_EQUIP = SingleProperty.of("wrapper_display_fake_equip", false);
public static final SingleProperty<String> DISGUISE_NAME = SingleProperty.of("wrapper_disguise_name", "");
public static final SingleProperty<Boolean> SADDLED = SingleProperty.of("wrapper_saddled", false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import xiamomc.pluginbase.Messages.FormattableMessage;
import xyz.nifeather.morph.backends.DisguiseBackend;
import xyz.nifeather.morph.backends.DisguiseWrapper;
import xyz.nifeather.morph.backends.WrapperAttribute;
import xyz.nifeather.morph.backends.WrapperProperties;
import xyz.nifeather.morph.messages.BackendStrings;
import xyz.nifeather.morph.misc.NetworkingHelper;
import xyz.nifeather.morph.network.server.MorphClientHandler;
Expand Down Expand Up @@ -138,7 +138,7 @@ public boolean disguise(Player player, DisguiseWrapper<?> rawWrapper)

var players = new ObjectArrayList<>(Bukkit.getOnlinePlayers());
players.remove(player);
var cmd = new S2CRenderMapAddCommand(player.getEntityId(), wrapper.readOrThrow(WrapperAttribute.disguiseIdentifier));
var cmd = new S2CRenderMapAddCommand(player.getEntityId(), wrapper.readPropertyOrThrow(WrapperProperties.DISGUISE_ID));
players.forEach(p -> clientHandler.sendCommand(p, cmd));

networkingHelper.prepareMeta(player)
Expand Down
Loading

0 comments on commit ac0ff84

Please sign in to comment.