Skip to content

Commit

Permalink
enhance: 使SkillHandler使用DisguiseState中的技能和技能配置
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Oct 11, 2024
1 parent cd38792 commit 2e664ed
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
30 changes: 21 additions & 9 deletions src/main/java/xyz/nifeather/morph/misc/DisguiseState.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
import xyz.nifeather.morph.network.server.MorphClientHandler;
import xyz.nifeather.morph.providers.animation.SingleAnimation;
import xyz.nifeather.morph.providers.disguise.DisguiseProvider;
import xyz.nifeather.morph.skills.IMorphSkill;
import xyz.nifeather.morph.skills.MorphSkillHandler;
import xyz.nifeather.morph.skills.SkillCooldownInfo;
import xyz.nifeather.morph.skills.SkillType;
import xyz.nifeather.morph.skills.*;
import xyz.nifeather.morph.skills.impl.NoneMorphSkill;
import xyz.nifeather.morph.storage.playerdata.PlayerMeta;
import xyz.nifeather.morph.storage.skill.SkillAbilityConfiguration;
import xyz.nifeather.morph.utilities.ItemUtils;
import xyz.nifeather.morph.utilities.NbtUtils;
import xyz.nifeather.morph.utilities.PermissionUtils;
Expand Down Expand Up @@ -556,22 +554,36 @@ public void skillLookupIdentifier(@NotNull String newSkillID)
this.skillLookupIdentifier = newSkillID;
}

@Nullable
private SkillAbilityConfiguration skillAbilityConfiguration;

public void setSkillAbilityConfiguration(@Nullable SkillAbilityConfiguration newInstance)
{
this.skillAbilityConfiguration = newInstance;
}

@Nullable
public SkillAbilityConfiguration getSkillAbilityConfiguration()
{
return skillAbilityConfiguration;
}

@NotNull
private IMorphSkill<?> skill = NoneMorphSkill.instance;

/**
* 设置此伪装的技能
* @param s 目标技能
* @param newSkill 目标技能
* @apiNote 如果目标技能是null,则会fallback到 {@link NoneMorphSkill#instance}
*/
public void setSkill(@Nullable IMorphSkill<?> s)
public void setSkill(@Nullable IMorphSkill<?> newSkill)
{
if (s == null) s = NoneMorphSkill.instance;
if (newSkill == null) newSkill = NoneMorphSkill.instance;

this.skill.onDeEquip(this);

s.onInitialEquip(this);
this.skill = s;
newSkill.onInitialEquip(this);
this.skill = newSkill;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import xyz.nifeather.morph.skills.SkillType;
import xiamomc.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Messages.MessageStore;
import xyz.nifeather.morph.storage.skill.ISkillOption;

import java.util.List;

Expand Down Expand Up @@ -202,6 +203,11 @@ public void postConstructDisguise(DisguiseState state, @Nullable Entity targetEn
var abilityOptions = abilityHandler.getOptionsFor(state.skillLookupIdentifier());
abilityOptions.forEach((id, config) -> state.getAbilityUpdater().setAbilityConfig(id.asString(), config));

state.setSkill(skillHandler.lookupDisguiseSkill(state.skillLookupIdentifier()));
var skillEntry = skillHandler.getSkillEntry(state.skillLookupIdentifier());
if (skillEntry != null)
{
state.setSkillAbilityConfiguration(skillEntry.key());
state.setSkill(skillEntry.value());
}
}
}
37 changes: 12 additions & 25 deletions src/main/java/xyz/nifeather/morph/skills/MorphSkillHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import xyz.nifeather.morph.messages.MorphStrings;
import xyz.nifeather.morph.messages.SkillStrings;
import xyz.nifeather.morph.misc.permissions.CommonPermissions;
import xyz.nifeather.morph.providers.disguise.DefaultDisguiseProvider;
import xyz.nifeather.morph.skills.impl.*;
import xyz.nifeather.morph.storage.skill.ISkillOption;
import xyz.nifeather.morph.storage.skill.SkillAbilityConfiguration;
Expand Down Expand Up @@ -149,7 +150,7 @@ private void update()
* @return 对应的技能和技能配置,如果没找到则是null
*/
@Nullable
private Pair<SkillAbilityConfiguration, IMorphSkill<?>> getSkillEntry(String identifier)
public Pair<SkillAbilityConfiguration, IMorphSkill<?>> getSkillEntry(String identifier)
{
if (identifier == null) return null;

Expand Down Expand Up @@ -215,11 +216,9 @@ public void executeDisguiseSkill(Player player, boolean bypassPermission)
return;
}

var skillEntry= getSkillEntry(state.skillLookupIdentifier());
var skill = state.getSkill();

if (player.getGameMode() == GameMode.SPECTATOR
|| skillEntry == null
|| skillEntry.key().getSkillIdentifier().equals(SkillType.NONE))
if (player.getGameMode() == GameMode.SPECTATOR || skill == NoneMorphSkill.instance)
{
player.sendMessage(MessageUtils.prefixes(player, SkillStrings.skillNotAvaliableString()));

Expand All @@ -230,11 +229,9 @@ public void executeDisguiseSkill(Player player, boolean bypassPermission)
}

var cdInfo = getCooldownInfo(player.getUniqueId(), state.skillLookupIdentifier());
assert cdInfo != null;

//logger.info("Permission is " + CommonPermissions.skillPermissionOf(skillEntry.getKey().getSkillIdentifier().asString(), state.getDisguiseIdentifier()));

var singleSkillPerm = CommonPermissions.skillPermissionOf(skillEntry.key().getSkillIdentifier().asString(), state.getDisguiseIdentifier());
var singleSkillPerm = CommonPermissions.skillPermissionOf(skill.getIdentifier().asString(), state.getDisguiseIdentifier());
var hasSkillPerm = PermissionUtils.hasPermission(player, singleSkillPerm, true);

if (!bypassPermission && !hasSkillPerm)
Expand Down Expand Up @@ -269,28 +266,18 @@ public void executeDisguiseSkill(Player player, boolean bypassPermission)
return;
}

var skill = skillEntry.right();
var config = skillEntry.left();

ISkillOption option;

try
{
option = skill.getOptionInstance().fromMap(config.getSkillOptions(skill));
}
catch (Throwable t)
SkillAbilityConfiguration config = state.getSkillAbilityConfiguration();
if (config == null)
{
if (t instanceof ClassCastException)
logger.error(state.getDisguiseIdentifier() + " -> " + skill.getIdentifier() + " has an invalid setting, please check your skill configurations.");
else
logger.error("Error occurred while parsing skill configuration");

t.printStackTrace();

logger.warn("Disguise have a skill but don't have a skill configuration?!");
player.sendMessage(MessageUtils.prefixes(player, SkillStrings.exceptionOccurredString()));
state.setSkillCooldown(20, true);

return;
}

var option = skill.getOptionInstance().fromMap(config.getSkillOptions(skill));

var cd = skill.executeSkillGeneric(player, state, config, option);
cdInfo.setLastInvoke(plugin.getCurrentTick());

Expand Down

0 comments on commit 2e664ed

Please sign in to comment.