added a "no stamina" option to the casting action bar

This commit is contained in:
ASangarin 2022-03-01 15:16:59 +01:00
parent 600a2b1e3c
commit 4be7379ed2
2 changed files with 100 additions and 99 deletions

View File

@ -21,121 +21,121 @@ import org.bukkit.event.player.PlayerItemHeldEvent;
import java.util.Objects; import java.util.Objects;
public class SkillBar implements Listener { public class SkillBar implements Listener {
private final PlayerKey mainKey; private final PlayerKey mainKey;
public SkillBar(ConfigurationSection config) { public SkillBar(ConfigurationSection config) {
mainKey = PlayerKey.valueOf(UtilityMethods.enumName(Objects.requireNonNull(config.getString("open"), "Could not find open key"))); mainKey = PlayerKey.valueOf(UtilityMethods.enumName(Objects.requireNonNull(config.getString("open"), "Could not find open key")));
} }
@EventHandler @EventHandler
public void a(PlayerKeyPressEvent event) { public void a(PlayerKeyPressEvent event) {
if (event.getPressed() != mainKey) if (event.getPressed() != mainKey) return;
return;
// Always cancel event // Always cancel event
if (event.getPressed().shouldCancelEvent()) if (event.getPressed().shouldCancelEvent()) event.setCancelled(true);
event.setCancelled(true);
// Enter spell casting // Enter spell casting
Player player = event.getData().getPlayer(); Player player = event.getData().getPlayer();
PlayerData playerData = event.getData(); PlayerData playerData = event.getData();
if (player.getGameMode() != GameMode.SPECTATOR if (player.getGameMode() != GameMode.SPECTATOR && (MMOCore.plugin.configManager.canCreativeCast || player.getGameMode() != GameMode.CREATIVE) && !playerData.isCasting() && !playerData.getBoundSkills()
&& (MMOCore.plugin.configManager.canCreativeCast || player.getGameMode() != GameMode.CREATIVE) .isEmpty()) {
&& !playerData.isCasting() playerData.setSkillCasting(new CustomSkillCastingHandler(playerData));
&& !playerData.getBoundSkills().isEmpty()) { MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_BEGIN).playTo(player);
playerData.setSkillCasting(new CustomSkillCastingHandler(playerData)); }
MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_BEGIN).playTo(player); }
}
}
private class CustomSkillCastingHandler extends SkillCastingHandler { private class CustomSkillCastingHandler extends SkillCastingHandler {
private final String ready = MMOCore.plugin.configManager.getSimpleMessage("casting.action-bar.ready").message(); private final String ready = MMOCore.plugin.configManager.getSimpleMessage("casting.action-bar.ready").message();
private final String onCooldown = MMOCore.plugin.configManager.getSimpleMessage("casting.action-bar.on-cooldown").message(); private final String onCooldown = MMOCore.plugin.configManager.getSimpleMessage("casting.action-bar.on-cooldown").message();
private final String noMana = MMOCore.plugin.configManager.getSimpleMessage("casting.action-bar.no-mana").message(); private final String noMana = MMOCore.plugin.configManager.getSimpleMessage("casting.action-bar.no-mana").message();
private final String split = MMOCore.plugin.configManager.getSimpleMessage("casting.split").message(); private final String noStamina = MMOCore.plugin.configManager.getSimpleMessage("casting.action-bar.no-stamina").message();
private final String split = MMOCore.plugin.configManager.getSimpleMessage("casting.split").message();
private int j; private int j;
CustomSkillCastingHandler(PlayerData playerData) { CustomSkillCastingHandler(PlayerData playerData) {
super(playerData, 1); super(playerData, 1);
} }
@EventHandler @EventHandler
public void onSkillCast(PlayerItemHeldEvent event) { public void onSkillCast(PlayerItemHeldEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!getCaster().isOnline()) return; if (!getCaster().isOnline()) return;
if (!event.getPlayer().equals(getCaster().getPlayer())) if (!event.getPlayer().equals(getCaster().getPlayer())) return;
return;
/* /*
* When the event is cancelled, another playerItemHeldEvent is * When the event is cancelled, another playerItemHeldEvent is
* called and previous and next slots are equal. the event must not * called and previous and next slots are equal. the event must not
* listen to that non-player called event. * listen to that non-player called event.
*/ */
if (event.getPreviousSlot() == event.getNewSlot()) if (event.getPreviousSlot() == event.getNewSlot()) return;
return;
event.setCancelled(true); event.setCancelled(true);
int slot = event.getNewSlot() + (event.getNewSlot() >= player.getInventory().getHeldItemSlot() ? -1 : 0); int slot = event.getNewSlot() + (event.getNewSlot() >= player.getInventory().getHeldItemSlot() ? -1 : 0);
/* /*
* The event is called again soon after the first since when * The event is called again soon after the first since when
* cancelling the first one, the player held item slot must go back * cancelling the first one, the player held item slot must go back
* to the previous one. * to the previous one.
*/ */
if (slot >= 0 && getCaster().hasSkillBound(slot)) { if (slot >= 0 && getCaster().hasSkillBound(slot)) {
PlayerMetadata caster = getCaster().getMMOPlayerData().getStatMap().cache(EquipmentSlot.MAIN_HAND); PlayerMetadata caster = getCaster().getMMOPlayerData().getStatMap().cache(EquipmentSlot.MAIN_HAND);
getCaster().getBoundSkill(slot).toCastable(getCaster()).cast(new TriggerMetadata(caster, null, null)); getCaster().getBoundSkill(slot).toCastable(getCaster()).cast(new TriggerMetadata(caster, null, null));
} }
} }
@EventHandler @EventHandler
public void stopCasting(PlayerKeyPressEvent event) { public void stopCasting(PlayerKeyPressEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (event.getPressed() == mainKey && event.getPlayer().equals(getCaster().getPlayer())) { if (event.getPressed() == mainKey && event.getPlayer().equals(getCaster().getPlayer())) {
MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_END).playTo(player); MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_END).playTo(player);
MMOCore.plugin.configManager.getSimpleMessage("casting.no-longer").send(getCaster().getPlayer()); MMOCore.plugin.configManager.getSimpleMessage("casting.no-longer").send(getCaster().getPlayer());
PlayerData.get(player).leaveCastingMode(); PlayerData.get(player).leaveCastingMode();
} }
} }
private String getFormat(PlayerData data) { private String getFormat(PlayerData data) {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
if (!data.isOnline()) return str.toString(); if (!data.isOnline()) return str.toString();
for (int j = 0; j < data.getBoundSkills().size(); j++) { for (int j = 0; j < data.getBoundSkills().size(); j++) {
ClassSkill skill = data.getBoundSkill(j); ClassSkill skill = data.getBoundSkill(j);
str.append((str.length() == 0) ? "" : split).append((onCooldown(data, skill) str.append((str.length() == 0) ? "" : split).append((onCooldown(data, skill) ? onCooldown.replace("{cooldown}",
? onCooldown.replace("{cooldown}", String.valueOf(data.getCooldownMap().getInfo(skill).getRemaining() / 1000)) String.valueOf(data.getCooldownMap().getInfo(skill).getRemaining() / 1000)) : noMana(data, skill) ? noMana : (noStamina(
: noMana(data, skill) ? noMana : ready).replace("{index}", "" + (j + 1 + (data.getPlayer().getInventory().getHeldItemSlot() data, skill) ? noStamina : ready)).replace("{index}",
<= j ? 1 : 0))).replace("{skill}", data.getBoundSkill(j).getSkill().getName())); "" + (j + 1 + (data.getPlayer().getInventory().getHeldItemSlot() <= j ? 1 : 0)))
} .replace("{skill}", data.getBoundSkill(j).getSkill().getName()));
}
return str.toString(); return str.toString();
} }
/** /**
* We don't even need to check if the skill has the 'cooldown' * We don't even need to check if the skill has the 'cooldown'
* modifier. We just look for an entry in the cooldown map which * modifier. We just look for an entry in the cooldown map which
* won't be here if the skill has no cooldown. * won't be here if the skill has no cooldown.
*/ */
private boolean onCooldown(PlayerData data, ClassSkill skill) { private boolean onCooldown(PlayerData data, ClassSkill skill) {
return data.getCooldownMap().isOnCooldown(skill); return data.getCooldownMap().isOnCooldown(skill);
} }
private boolean noMana(PlayerData data, ClassSkill skill) { private boolean noMana(PlayerData data, ClassSkill skill) {
return skill.getSkill().hasModifier("mana") && skill.getModifier("mana", data.getSkillLevel(skill.getSkill())) > data.getMana(); return skill.getSkill().hasModifier("mana") && skill.getModifier("mana", data.getSkillLevel(skill.getSkill())) > data.getMana();
} }
@Override private boolean noStamina(PlayerData data, ClassSkill skill) {
public void onTick() { return skill.getSkill().hasModifier("stamina") && skill.getModifier("stamina",
if (j % 20 == 0) data.getSkillLevel(skill.getSkill())) > data.getStamina();
getCaster().displayActionBar(getFormat(getCaster())); }
for (int k = 0; k < 2; k++) { @Override
double a = (double) j++ / 5; public void onTick() {
getCaster().getProfess().getCastParticle() if (j % 20 == 0) getCaster().displayActionBar(getFormat(getCaster()));
.display(getCaster().getPlayer().getLocation().add(Math.cos(a), 1 + Math.sin(a / 3) / 1.3, Math.sin(a)));
} for (int k = 0; k < 2; k++) {
} double a = (double) j++ / 5;
} getCaster().getProfess().getCastParticle()
.display(getCaster().getPlayer().getLocation().add(Math.cos(a), 1 + Math.sin(a / 3) / 1.3, Math.sin(a)));
}
}
}
} }

View File

@ -54,6 +54,7 @@ casting:
ready: '&6[{index}] &a&l{skill}' ready: '&6[{index}] &a&l{skill}'
on-cooldown: '&6[{index}] &c&l{skill} &6(&c{cooldown}&6)' on-cooldown: '&6[{index}] &c&l{skill} &6(&c{cooldown}&6)'
no-mana: '&6[{index}] &9&l{skill}' no-mana: '&6[{index}] &9&l{skill}'
no-stamina: '&6[{index}] &9&l{skill}'
split: '&7 &7 - &7 ' split: '&7 &7 - &7 '
no-longer: '%&cYou cancelled skill casting.' no-longer: '%&cYou cancelled skill casting.'
no-mana: '&cYou do not have enough {mana}!' no-mana: '&cYou do not have enough {mana}!'