Combos now ignore unused keys

This commit is contained in:
Jules 2023-10-22 00:42:39 +02:00
parent 88d02f140f
commit e784230ce7
9 changed files with 41 additions and 40 deletions

View File

@ -240,9 +240,8 @@ public class MMOCore extends JavaPlugin {
// Skill casting
try {
SkillCastingMode mode = SkillCastingMode.valueOf(UtilityMethods.enumName(getConfig().getString("skill-casting.mode")));
final SkillCastingMode mode = SkillCastingMode.valueOf(UtilityMethods.enumName(getConfig().getString("skill-casting.mode")));
mode.setCurrent(getConfig().getConfigurationSection("skill-casting"));
} catch (RuntimeException exception) {
getLogger().log(Level.WARNING, "Could not load skill casting: " + exception.getMessage());
}

View File

@ -3,6 +3,7 @@ package net.Indyuce.mmocore.skill.cast;
import io.lumine.mythic.lib.UtilityMethods;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.HashSet;
@ -20,7 +21,7 @@ public class ComboMap {
/**
* All the keys that are at the start of a combo.
*/
private final Set<PlayerKey> firstKeys = new HashSet<>();
private final Set<PlayerKey> firstKeys = new HashSet<>(), keys = new HashSet<>();
private final int longestCombo;
@ -32,9 +33,12 @@ public class ComboMap {
final int spellSlot = Integer.valueOf(key);
Validate.isTrue(spellSlot >= 0, "Spell slot must be at least 0");
Validate.isTrue(!combos.values().contains(spellSlot), "There is already a key combo with the same skill slot");
KeyCombo combo = new KeyCombo();
for (String str : config.getStringList(key))
combo.registerKey(PlayerKey.valueOf(UtilityMethods.enumName(str)));
final KeyCombo combo = new KeyCombo();
for (String str : config.getStringList(key)) {
final PlayerKey newKey = PlayerKey.valueOf(UtilityMethods.enumName(str));
keys.add(newKey);
combo.registerKey(newKey);
}
combos.put(combo, spellSlot);
firstKeys.add(combo.getAt(0));
@ -46,6 +50,7 @@ public class ComboMap {
this.longestCombo = currentLongestCombo;
}
@NotNull
public Map<KeyCombo, Integer> getCombos() {
return combos;
}
@ -54,6 +59,10 @@ public class ComboMap {
return longestCombo;
}
public boolean isComboKey(PlayerKey key) {
return keys.contains(key);
}
public boolean isComboStart(PlayerKey key) {
return firstKeys.contains(key);
}

View File

@ -1,5 +1,7 @@
package net.Indyuce.mmocore.skill.cast;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -17,7 +19,7 @@ public class KeyCombo {
return keys.size();
}
public void registerKey(PlayerKey key) {
public void registerKey(@NotNull PlayerKey key) {
keys.add(key);
}

View File

@ -1,10 +1,10 @@
package net.Indyuce.mmocore.skill.cast;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.skill.cast.listener.KeyCombos;
import net.Indyuce.mmocore.skill.cast.listener.SkillBar;
import net.Indyuce.mmocore.skill.cast.listener.SkillCastingDisabled;
import net.Indyuce.mmocore.skill.cast.listener.SkillScroller;
import net.Indyuce.mmocore.skill.cast.handler.KeyCombos;
import net.Indyuce.mmocore.skill.cast.handler.SkillBar;
import net.Indyuce.mmocore.skill.cast.handler.SkillCastingDisabled;
import net.Indyuce.mmocore.skill.cast.handler.SkillScroller;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
@ -74,9 +74,6 @@ public enum SkillCastingMode {
public void setCurrent(@NotNull ConfigurationSection config) {
Validate.isTrue(current == null, "Skill casting mode already initialized");
current = listenerLoader.apply(config);
if (this == NONE) return;
// Register listener
Bukkit.getPluginManager().registerEvents(current, MMOCore.plugin);
}

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.skill.cast.listener;
package net.Indyuce.mmocore.skill.cast.handler;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.api.event.skill.PlayerCastSkillEvent;
@ -16,12 +16,13 @@ import org.bukkit.GameMode;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.*;
public class KeyCombos extends SkillCastingHandler {
public class KeyCombos extends SkillCastingHandler implements Listener {
private final ComboMap comboMap;
/**
@ -71,8 +72,7 @@ public class KeyCombos extends SkillCastingHandler {
public void whenPressingKey(PlayerKeyPressEvent event) {
PlayerData playerData = event.getData();
Player player = playerData.getPlayer();
if (player.getGameMode() == GameMode.CREATIVE && !MMOCore.plugin.configManager.canCreativeCast)
return;
if (player.getGameMode() == GameMode.CREATIVE && !MMOCore.plugin.configManager.canCreativeCast) return;
// Don't start combos if no skills are bound
if (playerData.getBoundSkills().isEmpty()) return;
@ -85,28 +85,27 @@ public class KeyCombos extends SkillCastingHandler {
if (event.getPressed().shouldCancelEvent()) event.setCancelled(true);
// Start combo
if (playerData.setSkillCasting() && beginComboSound != null)
beginComboSound.playTo(player);
if (playerData.setSkillCasting() && beginComboSound != null) beginComboSound.playTo(player);
}
return;
}
@Nullable CustomSkillCastingInstance casting = null;
// Make sure key is part of the combo map
final ComboMap comboMap = Objects.requireNonNullElse(playerData.getProfess().getComboMap(), this.comboMap);
if (!comboMap.isComboKey(event.getPressed())) return;
// Player is already casting
// Player already casting
final CustomSkillCastingInstance casting;
if (event.getData().isCasting()) casting = (CustomSkillCastingInstance) playerData.getSkillCasting();
// Start combo when there is NO initializer key
else {
final @NotNull ComboMap comboMap = Objects.requireNonNullElse(playerData.getProfess().getComboMap(), this.comboMap);
if (comboMap.isComboStart(event.getPressed()) && playerData.setSkillCasting()) {
casting = (CustomSkillCastingInstance) playerData.getSkillCasting();
if (beginComboSound != null) beginComboSound.playTo(player);
}
else if (comboMap.isComboStart(event.getPressed()) && playerData.setSkillCasting()) {
casting = (CustomSkillCastingInstance) playerData.getSkillCasting();
if (beginComboSound != null) beginComboSound.playTo(player);
}
if (casting == null) return;
// Just return
else return;
// Adding pressed key
casting.refreshTimeOut();
@ -159,12 +158,7 @@ public class KeyCombos extends SkillCastingHandler {
else getCaster().displayActionBar(actionBarOptions.format(this));
}
private static final List<TriggerType> IGNORED_WHEN_CASTING = Arrays.asList(
TriggerType.RIGHT_CLICK,
TriggerType.LEFT_CLICK,
TriggerType.SHIFT_RIGHT_CLICK,
TriggerType.SHIFT_LEFT_CLICK,
TriggerType.SNEAK);
private static final List<TriggerType> IGNORED_WHEN_CASTING = Arrays.asList(TriggerType.RIGHT_CLICK, TriggerType.LEFT_CLICK, TriggerType.SHIFT_RIGHT_CLICK, TriggerType.SHIFT_LEFT_CLICK, TriggerType.SNEAK);
/**
* This makes sure NO skills are cast when in casting mode so that

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.skill.cast.listener;
package net.Indyuce.mmocore.skill.cast.handler;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.api.player.EquipmentSlot;

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.skill.cast.listener;
package net.Indyuce.mmocore.skill.cast.handler;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.skill.cast.SkillCastingHandler;

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.skill.cast.listener;
package net.Indyuce.mmocore.skill.cast.handler;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.api.player.EquipmentSlot;

View File

@ -33,7 +33,7 @@ public class PlayerPressKeyListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void registerClickKey(PlayerInteractEvent event) {
if (event.useItemInHand() != Event.Result.DENY && event.getAction().name().contains("CLICK")&&event.getHand().equals(EquipmentSlot.HAND)) {
if (event.useItemInHand() != Event.Result.DENY && event.getAction().name().contains("CLICK") && event.getHand().equals(EquipmentSlot.HAND)) {
boolean rightClick = event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK;
PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), rightClick ? PlayerKey.RIGHT_CLICK : PlayerKey.LEFT_CLICK, event);
Bukkit.getPluginManager().callEvent(called);