Cleaning up the events, removing lambok from events, and making leaveCastingMode and setCastingMode as booleans to reflect the events they fire!

This commit is contained in:
Rosenthalk0 2023-06-24 15:06:17 -05:00
parent 96ac7a9c00
commit 51f4f7be1b
6 changed files with 26 additions and 21 deletions

View File

@ -1,13 +1,11 @@
package net.Indyuce.mmocore.api.event;
import lombok.Getter;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
@Getter
public class PlayerEnterCastingModeEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlerList = new HandlerList();
private boolean cancelled = false;

View File

@ -1,13 +1,12 @@
package net.Indyuce.mmocore.api.event;
import lombok.Getter;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
@Getter
public class PlayerExitCastingModeEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlerList = new HandlerList();
private boolean cancelled = false;
@ -16,6 +15,7 @@ public class PlayerExitCastingModeEvent extends PlayerDataEvent implements Cance
super(PlayerData.get(who.getUniqueId()));
}
@Override
public boolean isCancelled() {
return cancelled;

View File

@ -1003,16 +1003,21 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
return skillCasting != null;
}
public void setSkillCasting(@NotNull SkillCastingInstance skillCasting) {
/**
* @return true if the PlayerEnterCastingModeEvent successfully put the player into casting mode, otherwise if the event is cancelled, returns false.
* @apiNote Changed to a boolean to reflect the cancellation state of the event being fired
*/
public boolean setSkillCasting(@NotNull SkillCastingInstance skillCasting) {
Validate.isTrue(!isCasting(), "Player already in casting mode");
PlayerEnterCastingModeEvent event = new PlayerEnterCastingModeEvent(getPlayer());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()){
skillCasting.close();
return;
return false;
}
this.skillCasting = skillCasting;
return true;
}
/**
@ -1030,27 +1035,31 @@ public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerD
/**
* API Method to leave casting mode and fire the PlayerExitCastingModeEvent
* @return true if the skill casting mode was left, or false if the event was cancelled, keeping the player in casting mode.
*/
public void leaveSkillCasting(){
this.leaveSkillCasting(false);
public boolean leaveSkillCasting(){
return this.leaveSkillCasting(false);
}
/**
* @param skipEvent Skip the PlayerExitCastingModeEvent
* @param skipEvent Skip Firing the PlayerExitCastingModeEvent
* @return true if the PlayerExitCastingModeEvent is not cancelled, or if the event is skipped.
*
*/
public void leaveSkillCasting(boolean skipEvent) {
public boolean leaveSkillCasting(boolean skipEvent) {
Validate.isTrue(isCasting(), "Player not in casting mode");
if (!skipEvent) {
PlayerExitCastingModeEvent event = new PlayerExitCastingModeEvent(getPlayer());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
return false;
}
}
skillCasting.close();
this.skillCasting = null;
setLastActivity(PlayerActivity.ACTION_BAR_MESSAGE, 0); // Reset action bar
return true;
}
public void displayActionBar(String message) {

View File

@ -79,8 +79,9 @@ public class KeyCombos implements SkillCastingListener {
if (event.getPressed().shouldCancelEvent()) event.setCancelled(true);
// Start combo
playerData.setSkillCasting(new CustomSkillCastingInstance(playerData));
if (beginComboSound != null) beginComboSound.playTo(player);
if (playerData.setSkillCasting(new CustomSkillCastingInstance(playerData))) {
if (beginComboSound != null) beginComboSound.playTo(player);
}
}
return;
}
@ -95,8 +96,7 @@ public class KeyCombos implements SkillCastingListener {
final @NotNull ComboMap comboMap = Objects.requireNonNullElse(playerData.getProfess().getComboMap(), this.comboMap);
if (comboMap.isComboStart(event.getPressed())) {
casting = new CustomSkillCastingInstance(playerData);
playerData.setSkillCasting(casting);
if (!playerData.isCasting()){
if (!playerData.setSkillCasting(casting)) {
return;
}
if (beginComboSound != null) beginComboSound.playTo(player);

View File

@ -56,11 +56,9 @@ public class SkillBar implements SkillCastingListener {
// Enter spell casting
final PlayerData playerData = event.getData();
if (player.getGameMode() != GameMode.SPECTATOR && (MMOCore.plugin.configManager.canCreativeCast || player.getGameMode() != GameMode.CREATIVE) && !playerData.isCasting() && !playerData.getBoundSkills().isEmpty()) {
playerData.setSkillCasting(new CustomSkillCastingInstance(playerData));
if (!playerData.isCasting()){
return;
if (playerData.setSkillCasting(new CustomSkillCastingInstance(playerData))) {
MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_BEGIN).playTo(player);
}
MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_BEGIN).playTo(player);
}
}

View File

@ -77,10 +77,10 @@ public class SkillScroller implements SkillCastingListener {
if (event.getPressed().shouldCancelEvent()) event.setCancelled(true);
// Enter casting mode
playerData.setSkillCasting(new CustomSkillCastingInstance(playerData));
if (!playerData.isCasting()){
if (!playerData.setSkillCasting(new CustomSkillCastingInstance(playerData))) {
return;
}
if (enterSound != null) enterSound.playTo(player);
}