!api for better skill casting

This commit is contained in:
Jules 2022-01-15 18:36:54 +01:00
parent ea44c42233
commit feeb3aea78
6 changed files with 198 additions and 24 deletions

View File

@ -27,6 +27,7 @@ import net.Indyuce.mmocore.comp.region.WorldGuardRegionHandler;
import net.Indyuce.mmocore.comp.vault.VaultEconomy;
import net.Indyuce.mmocore.comp.vault.VaultMMOLoader;
import net.Indyuce.mmocore.listener.*;
import net.Indyuce.mmocore.listener.event.PlayerPressKeyListener;
import net.Indyuce.mmocore.listener.option.*;
import net.Indyuce.mmocore.listener.profession.FishingListener;
import net.Indyuce.mmocore.listener.profession.PlayerCollectStats;
@ -241,6 +242,7 @@ public class MMOCore extends LuminePlugin {
Bukkit.getPluginManager().registerEvents(new GuildListener(), this);
Bukkit.getPluginManager().registerEvents(new FishingListener(), this);
Bukkit.getPluginManager().registerEvents(new PlayerCollectStats(), this);
Bukkit.getPluginManager().registerEvents(new PlayerPressKeyListener(), this);
/*
* Initialize player data from all online players. This is very important to do

View File

@ -0,0 +1,51 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.skill.cast.PlayerKey;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
public class PlayerKeyPressEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Cancellable cancellable;
private final PlayerKey pressed;
/**
* Called when a player presses some key. This event is
* only fired if the user has chosen the 'key combos' casting
* method
*
* @param playerData Player pressing the key
* @param pressed Key being pressed
*/
public PlayerKeyPressEvent(PlayerData playerData, PlayerKey pressed, Cancellable cancellable) {
super(playerData);
this.pressed = pressed;
this.cancellable = cancellable;
}
public PlayerKey getPressed() {
return pressed;
}
@Override
public boolean isCancelled() {
return cancellable.isCancelled();
}
@Override
public void setCancelled(boolean b) {
cancellable.setCancelled(b);
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,50 @@
package net.Indyuce.mmocore.listener.event;
import net.Indyuce.mmocore.api.event.PlayerKeyPressEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.skill.cast.PlayerKey;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
/**
* This registers all the KeyPress events
*/
public class PlayerPressKeyListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void registerCrouchKey(PlayerToggleSneakEvent event) {
if (event.isSneaking()) {
PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.CROUCH, event);
Bukkit.getPluginManager().callEvent(called);
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void registerClickKey(PlayerInteractEvent event) {
if (event.useItemInHand() != Event.Result.DENY && event.getAction().name().contains("CLICK")) {
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);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void registerDropKey(PlayerDropItemEvent event) {
PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.DROP, event);
Bukkit.getPluginManager().callEvent(called);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void registerSwapHandsKey(PlayerSwapHandItemsEvent event) {
PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.SWAP_HANDS, event);
Bukkit.getPluginManager().callEvent(called);
}
}

View File

@ -0,0 +1,48 @@
package net.Indyuce.mmocore.skill.cast;
import net.Indyuce.mmocore.skill.cast.listener.SkillBar;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.Listener;
import java.util.function.Function;
public enum CastingMethod {
/**
* The first ever casting method to be implemented in MMOCore.
* <p>
* When pressing a key, the list of bound skills display on the
* action bar
*/
SKILL_BAR(config-> new SkillBar(config)),
SKILL_SCROLL;
/**
* Initialize your skill combo by pressing some key
*/
KEY_COMBOS(),
/**
* Not implemented yet.
* <p>
* This would allow players to cast skills by opening
* a book with all the skills displayed into it and click
* some clickable text to cast the skill.
*/
SPELL_BOOK(),
/**
* Not implemented yet.
* <p>
* Much like the spell book but using a custom GUI instead
* of a spell book to display the available skills.
*/
SPELL_GUI();
private final Function<ConfigurationSection, Listener> listenerLoader;
CastingMethod(Function<ConfigurationSection, Listener> listenerLoader) {
this.listenerLoader = listenerLoader;
}
}

View File

@ -0,0 +1,29 @@
package net.Indyuce.mmocore.skill.cast;
public enum PlayerKey {
/**
* When a player left clicks
*/
LEFT_CLICK,
/**
* When a player right clicks
*/
RIGHT_CLICK,
/**
* When a player drops the item they are holding
*/
DROP,
/**
* When a player swaps their hand items
*/
SWAP_HANDS,
/**
* When a player sneaks (doesn't trigger when unsneaking)
*/
CROUCH;
}

View File

@ -1,15 +1,19 @@
package net.Indyuce.mmocore.listener;
package net.Indyuce.mmocore.skill.cast.listener;
import io.lumine.mythic.lib.api.player.EquipmentSlot;
import io.lumine.mythic.lib.player.PlayerMetadata;
import io.lumine.mythic.lib.skill.trigger.TriggerMetadata;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerKeyPressEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.listener.event.PlayerPressKeyListener;
import net.Indyuce.mmocore.manager.ConfigManager;
import net.Indyuce.mmocore.manager.SoundManager;
import net.Indyuce.mmocore.skill.ClassSkill;
import net.Indyuce.mmocore.skill.cast.PlayerKey;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
@ -19,36 +23,26 @@ import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
public class SpellCast implements Listener {
import java.util.Objects;
public class SkillBar implements Listener {
private final PlayerKey mainKey;
public SkillBar(ConfigurationSection config) {
mainKey = PlayerKey.valueOf(Objects.requireNonNull(config.getString("open"), "Could not find open key"));
}
@EventHandler
public void a(PlayerSwapHandItemsEvent event) {
Player player = event.getPlayer();
ConfigManager.SwapAction action = player.isSneaking() ? MMOCore.plugin.configManager.sneakingSwapAction : MMOCore.plugin.configManager.normalSwapAction;
// Vanilla action does nothing
if (action == ConfigManager.SwapAction.VANILLA)
public void a(PlayerKeyPressEvent event) {
if (event.getPressed() != mainKey)
return;
// Always cancel event if it's not the vanilla action
// Always cancel event
event.setCancelled(true);
/*
* Hotbar swap feature, this entirely switches the
* player's hotbar with their 9 lowest inventory slots
*/
if (action == ConfigManager.SwapAction.HOTBAR_SWAP) {
MMOCore.plugin.soundManager.play(player, SoundManager.SoundEvent.HOTBAR_SWAP);
for (int j = 0; j < 9; j++) {
ItemStack replaced = player.getInventory().getItem(j + 9 * 3);
player.getInventory().setItem(j + 9 * 3, player.getInventory().getItem(j));
player.getInventory().setItem(j, replaced);
}
return;
}
// Enter spell casting
PlayerData playerData = PlayerData.get(player);
Player player = event.getData().getPlayer();
PlayerData playerData = event.getData();
if (player.getGameMode() != GameMode.SPECTATOR
&& (MMOCore.plugin.configManager.canCreativeCast || player.getGameMode() != GameMode.CREATIVE)
&& !playerData.isCasting()