Finished new combo system (untested)

This commit is contained in:
jayoevans 2019-10-25 22:11:50 +10:00
parent af0cf17b0b
commit b9db7a3d68
5 changed files with 224 additions and 104 deletions

View file

@ -4,7 +4,6 @@ import co.aikar.timings.lib.MCTiming;
import com.projectkorra.projectkorra.ProjectKorra;
import com.projectkorra.projectkorra.ability.api.PassiveAbility;
import com.projectkorra.projectkorra.ability.loader.*;
import com.projectkorra.projectkorra.ability.util.ComboManager;
import com.projectkorra.projectkorra.ability.util.MultiAbilityManager;
import com.projectkorra.projectkorra.configuration.ConfigManager;
import com.projectkorra.projectkorra.configuration.configs.abilities.AbilityConfig;
@ -13,6 +12,7 @@ import com.projectkorra.projectkorra.element.SubElement;
import com.projectkorra.projectkorra.event.AbilityProgressEvent;
import com.projectkorra.projectkorra.firebending.FireBlast;
import com.projectkorra.projectkorra.module.Module;
import com.projectkorra.projectkorra.module.ModuleManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
@ -24,12 +24,16 @@ import java.util.stream.Collectors;
public class AbilityManager extends Module {
private final ComboManager comboManager;
private final Set<Ability> abilitySet = new HashSet<>();
private final Map<UUID, Map<Class<? extends Ability>, LinkedList<Ability>>> abilityMap = new HashMap<>();
public AbilityManager() {
super("Ability");
this.comboManager = ModuleManager.getModule(ComboManager.class);
runTimer(() -> {
for (Ability ability : abilitySet) {
if (ability instanceof PassiveAbility) {
@ -128,15 +132,16 @@ public class AbilityManager extends Module {
if (abilityLoader instanceof ComboAbilityLoader) {
ComboAbilityLoader comboAbilityLoader = (ComboAbilityLoader) abilityLoader;
if (comboAbilityLoader.getCombination() == null) {
if (comboAbilityLoader.getCombination() == null || comboAbilityLoader.getCombination().size() < 2) {
getPlugin().getLogger().info(abilityName + " has no combination");
return;
}
// TODO Register Combo Ability
this.comboManager.registerAbility(abilityClass, abilityData, comboAbilityLoader);
// ComboManager.getComboAbilities().put(abilityName, new ComboManager.ComboAbilityInfo(abilityName, comboAbilityLoader.getCombination(), ));
ComboManager.getDescriptions().put(abilityName, abilityConfig.Description);
ComboManager.getInstructions().put(abilityName, abilityConfig.Instructions);
// ComboManager.getDescriptions().put(abilityName, abilityConfig.Description);
// ComboManager.getInstructions().put(abilityName, abilityConfig.Instructions);
}
if (abilityLoader instanceof MultiAbilityLoader) {

View file

@ -1,128 +1,223 @@
package com.projectkorra.projectkorra.ability;
import com.projectkorra.projectkorra.ability.loader.ComboAbilityLoader;
import com.projectkorra.projectkorra.module.Module;
import com.projectkorra.projectkorra.module.ModuleManager;
import com.projectkorra.projectkorra.player.BendingPlayer;
import com.projectkorra.projectkorra.player.BendingPlayerManager;
import com.projectkorra.projectkorra.util.ClickType;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
public class ComboManager extends Module {
private final Map<UUID, List<AbilityInformation>> recentlyUsed = new HashMap<>();
private final BendingPlayerManager bendingPlayerManager;
private final Map<UUID, LinkedList<Combination>> recentlyUsed = new HashMap<>();
private final List<ComboAbilityInfo> comboAbilities = new ArrayList<>();
private final long combinationMax = 8;
private ComboManager() {
super("Combo Ability");
this.bendingPlayerManager = ModuleManager.getModule(BendingPlayerManager.class);
this.comboAbilities.clear();
}
/**
* Contains information on an ability used in a combo.
*
* @author kingbirdy
*
*/
public static class AbilityInformation {
private String abilityName;
private ClickType clickType;
private long time;
public void registerAbility(Class<? extends Ability> abilityClass, AbilityData abilityData, ComboAbilityLoader comboAbilityLoader) {
ComboAbilityInfo comboAbilityInfo = new ComboAbilityInfo(abilityClass, abilityData.name(), comboAbilityLoader.getCombination());
public AbilityInformation(final String name, final ClickType type) {
this(name, type, 0);
this.comboAbilities.add(comboAbilityInfo);
}
private void processComboAbility(Player player, ClickType clickType) {
BendingPlayer bendingPlayer = this.bendingPlayerManager.getBendingPlayer(player);
String abilityName = bendingPlayer.getBoundAbility();
LinkedList<Combination> recentlyUsed = this.recentlyUsed.computeIfAbsent(player.getUniqueId(), k -> new LinkedList<>());
recentlyUsed.addFirst(new Combination(abilityName, clickType));
if (recentlyUsed.size() > this.combinationMax) {
recentlyUsed.removeLast();
}
public AbilityInformation(final String name, final ClickType type, final long time) {
this.abilityName = name;
this.clickType = type;
this.time = time;
ComboAbilityInfo comboAbilityInfo = getComboAbiblity(recentlyUsed);
if (comboAbilityInfo == null) {
return;
}
/**
* Compares if two {@link com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation}'s are equal without
* respect to {@link com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation#time time}.
*
* @param info The AbilityInformation to compare against
* @return True if they are equal without respect to time
*/
public boolean equalsWithoutTime(final com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation info) {
return this.getAbilityName().equals(info.getAbilityName()) && this.getClickType().equals(info.getClickType());
if (!player.hasPermission("bending.ability." + comboAbilityInfo.abilityName)) {
return;
}
/**
* Gets the name of the ability.
*
* @return The name of the ability.
*/
public String getAbilityName() {
return this.abilityName;
try {
Class<? extends Ability> abilityClass = comboAbilityInfo.abilityClass;
Constructor<? extends Ability> constructor = abilityClass.getDeclaredConstructor(Player.class);
Ability ability = constructor.newInstance(player);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
/**
* Gets the {@link ClickType} of the {@link com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation}.
*
* @return The ClickType
*/
public ClickType getClickType() {
return this.clickType;
}
private ComboAbilityInfo getComboAbiblity(LinkedList<Combination> recentlyUsed) {
for (ComboAbilityInfo comboAbilityInfo : this.comboAbilities) {
LinkedList<Combination> abilityCombinations = comboAbilityInfo.combinations;
int comboSize = abilityCombinations.size();
if (recentlyUsed.size() < comboSize) {
continue;
}
if (recentlyUsed.subList(0, comboSize).equals(abilityCombinations)) {
return comboAbilityInfo;
}
}
public long getTime() {
return this.time;
return null;
}
@EventHandler
public void onInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
BendingPlayer bendingPlayer = this.bendingPlayerManager.getBendingPlayer(player);
if (event.getHand() != EquipmentSlot.HAND) {
return;
}
public void setAbilityName(final String abilityName) {
if (!bendingPlayer.canCurrentlyBendWithWeapons()) {
return;
}
boolean rightClick = event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR;
boolean leftClick = event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR;
if (leftClick) {
processComboAbility(player, ClickType.LEFT_CLICK);
return;
}
if (rightClick) {
ClickType clickType = event.getClickedBlock() != null ? ClickType.RIGHT_CLICK_BLOCK : ClickType.RIGHT_CLICK;
processComboAbility(player, clickType);
return;
}
}
@EventHandler
public void onDamage(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof LivingEntity)) {
return;
}
Player player = (Player) event.getDamager();
BendingPlayer bendingPlayer = this.bendingPlayerManager.getBendingPlayer(player);
if (!bendingPlayer.canCurrentlyBendWithWeapons()) {
return;
}
processComboAbility(player, ClickType.LEFT_CLICK_ENTITY);
}
@EventHandler
public void onInteractEntity(PlayerInteractEntityEvent event) {
Player player = event.getPlayer();
BendingPlayer bendingPlayer = this.bendingPlayerManager.getBendingPlayer(player);
if (!bendingPlayer.canCurrentlyBendWithWeapons()) {
return;
}
processComboAbility(player, ClickType.RIGHT_CLICK_ENTITY);
}
@EventHandler
public void onSneak(PlayerToggleSneakEvent event) {
Player player = event.getPlayer();
BendingPlayer bendingPlayer = this.bendingPlayerManager.getBendingPlayer(player);
if (!bendingPlayer.canCurrentlyBendWithWeapons()) {
return;
}
ClickType clickType = event.isSneaking() ? ClickType.SHIFT_DOWN : ClickType.SHIFT_UP;
processComboAbility(player, clickType);
}
@EventHandler
public void onSwapHandItems(PlayerSwapHandItemsEvent event) {
Player player = event.getPlayer();
BendingPlayer bendingPlayer = this.bendingPlayerManager.getBendingPlayer(player);
ItemStack mainHand = event.getMainHandItem();
ItemStack offHand = event.getOffHandItem();
if (mainHand.getType() != Material.AIR) {
return;
}
if (offHand != null || offHand.getType() != Material.AIR) {
return;
}
processComboAbility(player, ClickType.OFFHAND_TRIGGER);
}
public class Combination {
private final String abilityName;
private final ClickType clickType;
Combination(String abilityName, ClickType clickType) {
this.abilityName = abilityName;
}
public void setClickType(final ClickType clickType) {
this.clickType = clickType;
}
public void setTime(final long time) {
this.time = time;
}
@Override
public String toString() {
return this.abilityName + " " + this.clickType + " " + this.time;
public boolean equals(Object obj) {
if (!(obj instanceof Combination)) {
return false;
}
Combination combination = (Combination) obj;
return this.abilityName.equals(combination.abilityName) && this.clickType == combination.clickType;
}
}
public static class ComboAbilityInfo {
private String name;
private ArrayList<com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation> abilities;
private Object comboType;
private class ComboAbilityInfo {
private final Class<? extends Ability> abilityClass;
private final String abilityName;
private final LinkedList<Combination> combinations;
public ComboAbilityInfo(final String name, final ArrayList<com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation> abilities, final Object comboType) {
this.name = name;
this.abilities = abilities;
this.comboType = comboType;
}
public ArrayList<com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation> getAbilities() {
return this.abilities;
}
public Object getComboType() {
return this.comboType;
}
public String getName() {
return this.name;
}
public void setAbilities(final ArrayList<com.projectkorra.projectkorra.ability.util.ComboManager.AbilityInformation> abilities) {
this.abilities = abilities;
}
public void setComboType(final Object comboType) {
this.comboType = comboType;
}
public void setName(final String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
ComboAbilityInfo(Class<? extends Ability> abilityClass, String abilityName, LinkedList<Combination> combinations) {
this.abilityClass = abilityClass;
this.abilityName = abilityName;
this.combinations = combinations;
}
}
}

View file

@ -1,15 +1,15 @@
package com.projectkorra.projectkorra.ability.loader;
import com.projectkorra.projectkorra.ability.util.ComboManager;
import com.projectkorra.projectkorra.ability.ComboManager;
import java.util.ArrayList;
import java.util.LinkedList;
public abstract class ComboAbilityLoader extends AbilityLoader {
/**
* Returns the list of abilities which constitute the combo.
*
* @return An ArrayList containing the combo's steps.
* @return A {@link LinkedList} containing the combo's steps.
*/
public abstract ArrayList<ComboManager.AbilityInformation> getCombination();
public abstract LinkedList<ComboManager.Combination> getCombination();
}

View file

@ -1,6 +1,8 @@
package com.projectkorra.projectkorra.player;
import com.projectkorra.projectkorra.GeneralMethods;
import com.projectkorra.projectkorra.ability.Ability;
import com.projectkorra.projectkorra.ability.AbilityManager;
import com.projectkorra.projectkorra.ability.bind.AbilityBindManager;
import com.projectkorra.projectkorra.ability.api.ChiAbility;
import com.projectkorra.projectkorra.ability.util.PassiveManager;
@ -16,6 +18,7 @@ public class BendingPlayer {
private final BendingPlayerManager manager;
private final ElementManager elementManager;
private final AbilityManager abilityManager;
private final AbilityBindManager abilityBindManager;
private final CooldownManager cooldownManager;
@ -40,6 +43,7 @@ public class BendingPlayer {
public BendingPlayer(int playerId, UUID uuid, String playerName, long firstLogin) {
this.manager = ModuleManager.getModule(BendingPlayerManager.class);
this.elementManager = ModuleManager.getModule(ElementManager.class);
this.abilityManager = ModuleManager.getModule(AbilityManager.class);
this.abilityBindManager = ModuleManager.getModule(AbilityBindManager.class);
this.cooldownManager = ModuleManager.getModule(CooldownManager.class);
@ -134,11 +138,7 @@ public class BendingPlayer {
}
}
public Ability getBoundAbility() {
return Ability.getAbility(getBoundAbilityName());
}
public String getBoundAbilityName() {
public String getBoundAbility() {
int slot = this.player.getInventory().getHeldItemSlot();
return this.abilities[slot];
}
@ -195,6 +195,26 @@ public class BendingPlayer {
this.cooldownManager.removeCooldown(this.player, abilityName);
}
public boolean canCurrentlyBendWithWeapons() {
if (getBoundAbility() == null) {
return false;
}
if (this.player.getInventory().getItemInMainHand() == null) {
return true;
}
boolean noWeaponElement = true; // GeneralMethods.getElementsWithNoWeaponBending().contains(this.abilityManager.getAbility())
if (!noWeaponElement) {
return true;
}
boolean hasWeapon = GeneralMethods.isWeapon(this.player.getInventory().getItemInMainHand().getType());
return !hasWeapon;
}
public ChiAbility getStance() {
return this.stance;
}

View file

@ -61,7 +61,7 @@ public class BendingPlayerManager extends DatabaseModule<BendingPlayerRepository
Player player = event.getPlayer();
BendingPlayer bendingPlayer = this.players.get(player.getUniqueId());
String ability = bendingPlayer.getBoundAbilityName();
String ability = bendingPlayer.getBoundAbility();
if (ability != null && ability.equals(event.getAbility())) {
GeneralMethods.displayMovePreview(player);