fixed resource update events

This commit is contained in:
Indyuce 2021-08-12 18:35:33 +02:00
parent e85ce8ea79
commit baf8c468b5
13 changed files with 322 additions and 192 deletions

View File

@ -1,62 +0,0 @@
package net.Indyuce.mmocore.api.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
public class PlayerRegenResourceEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
/*
* type of resource being regenerated. this way all three re
*/
private final PlayerResource resource;
/*
* amount of resource regenerated. whole point of the event is to be able to
* change it.
*/
private double amount;
private boolean cancelled = false;
public PlayerRegenResourceEvent(PlayerData playerData, PlayerResource resource, double amount) {
super(playerData);
this.resource = resource;
this.amount = amount;
}
public PlayerResource getResource() {
return resource;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,121 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
public class PlayerResourceUpdateEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
/**
* Type of resource being regenerated, this way
* this event handles all four resources.
*/
private final PlayerResource resource;
private final UpdateReason reason;
/**
* Amount of resource regenerated. The whole point of the event is
* being able to modify it, for instance to apply the mana regeneration stat.
*/
private double amount;
private boolean cancelled = false;
/**
* Called when a player gains some resource back. This can
* be used to handle stats like health or mana regeneration.
* <p>
* Example use: {@link net.Indyuce.mmocore.skill.Neptune_Gift} which is a skill
* that temporarily increases resource regeneration for a short amount of time.
*
* @param playerData Player regenerating
* @param resource Resource being increased
* @param amount Amount being taken away/regenerated
* @param reason The reason why this event was called
*/
public PlayerResourceUpdateEvent(PlayerData playerData, PlayerResource resource, double amount, UpdateReason reason) {
super(playerData);
this.resource = resource;
this.amount = amount;
this.reason = reason;
}
public PlayerResource getResource() {
return resource;
}
public double getAmount() {
return amount;
}
/**
* Changes the amount of resource given/taken away
*
* @param amount New amount
*/
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public enum UpdateReason {
/**
* When resource is being regenerated
*/
REGENERATION,
/**
* When some resource is gained, or consumed by some skills
*/
SKILL_REGENERATION,
/**
* When some resource is gained, or consumed by some skills
*/
SKILL_COST,
/**
* Used by quests triggers
* - {@link net.Indyuce.mmocore.api.quest.trigger.ManaTrigger}
* - {@link net.Indyuce.mmocore.api.quest.trigger.StaminaTrigger}
* - {@link net.Indyuce.mmocore.api.quest.trigger.StelliumTrigger}
*/
TRIGGER,
/**
* When using the resource command {@link net.Indyuce.mmocore.command.rpg.admin.ResourceCommandTreeNode}
*/
COMMAND,
/**
* Anything else
*/
OTHER;
public boolean isSkill() {
return this == SKILL_COST || this == SKILL_REGENERATION;
}
}
}

View File

@ -317,19 +317,30 @@ public class PlayerData extends OfflinePlayerData {
lastLootChest = System.currentTimeMillis(); lastLootChest = System.currentTimeMillis();
} }
/**
* @deprecated Provide a heal reason with {@link #heal(double, PlayerResourceUpdateEvent.UpdateReason)}
*/
@Deprecated
public void heal(double heal) { public void heal(double heal) {
this.heal(heal, PlayerResourceUpdateEvent.UpdateReason.OTHER);
}
public void heal(double heal, PlayerResourceUpdateEvent.UpdateReason reason) {
if (!isOnline()) if (!isOnline())
return; return;
// Avoid calling an useless event
double newest = Math.max(0, Math.min(getPlayer().getHealth() + heal, getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue())); double newest = Math.max(0, Math.min(getPlayer().getHealth() + heal, getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
if (getPlayer().getHealth() == newest) if (getPlayer().getHealth() == newest)
return; return;
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.HEALTH, heal); PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.HEALTH, heal, reason);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())
return; return;
getPlayer().setHealth(newest); // Use updated amount from event
getPlayer().setHealth(getPlayer().getHealth() + event.getAmount());
} }
public void addFriend(UUID uuid) { public void addFriend(UUID uuid) {
@ -373,6 +384,12 @@ public class PlayerData extends OfflinePlayerData {
MMOCore.plugin.requestManager.registerRequest(request); MMOCore.plugin.requestManager.registerRequest(request);
} }
/**
* Teleports the player to a specific waypoint. This applies
* the stellium waypoint cost and plays the teleport animation.
*
* @param waypoint Target waypoint
*/
public void warp(Waypoint waypoint) { public void warp(Waypoint waypoint) {
if (!isOnline()) if (!isOnline())
return; return;
@ -384,7 +401,7 @@ public class PlayerData extends OfflinePlayerData {
*/ */
lastWaypoint = System.currentTimeMillis(); lastWaypoint = System.currentTimeMillis();
giveStellium(-waypoint.getStelliumCost()); giveStellium(-waypoint.getStelliumCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_COST);
new BukkitRunnable() { new BukkitRunnable() {
final int x = getPlayer().getLocation().getBlockX(); final int x = getPlayer().getLocation().getBlockX();
@ -399,7 +416,7 @@ public class PlayerData extends OfflinePlayerData {
|| getPlayer().getLocation().getBlockZ() != z) { || getPlayer().getLocation().getBlockZ() != z) {
MMOCore.plugin.soundManager.play(getPlayer(), SoundManager.SoundEvent.WARP_CANCELLED); MMOCore.plugin.soundManager.play(getPlayer(), SoundManager.SoundEvent.WARP_CANCELLED);
MMOCore.plugin.configManager.getSimpleMessage("warping-canceled").send(getPlayer()); MMOCore.plugin.configManager.getSimpleMessage("warping-canceled").send(getPlayer());
giveStellium(waypoint.getStelliumCost()); giveStellium(waypoint.getStelliumCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_REGENERATION);
cancel(); cancel();
return; return;
} }
@ -501,44 +518,76 @@ public class PlayerData extends OfflinePlayerData {
return profess == null ? MMOCore.plugin.classManager.getDefaultClass() : profess; return profess == null ? MMOCore.plugin.classManager.getDefaultClass() : profess;
} }
/**
* @deprecated Provide reason with {@link #giveMana(double, PlayerResourceUpdateEvent.UpdateReason)}
*/
@Deprecated
public void giveMana(double amount) { public void giveMana(double amount) {
giveMana(amount, PlayerResourceUpdateEvent.UpdateReason.OTHER);
}
public void giveMana(double amount, PlayerResourceUpdateEvent.UpdateReason reason) {
// Avoid calling useless event
double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_MANA), mana + amount)); double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_MANA), mana + amount));
if (mana == newest) if (mana == newest)
return; return;
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.MANA, amount); PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.MANA, amount, reason);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())
return; return;
mana = newest; // Use updated amount from Bukkit event
setMana(mana + event.getAmount());
} }
/**
* @deprecated Provide reason with {@link #giveStamina(double, PlayerResourceUpdateEvent.UpdateReason)}
*/
@Deprecated
public void giveStamina(double amount) { public void giveStamina(double amount) {
giveStamina(amount, PlayerResourceUpdateEvent.UpdateReason.OTHER);
}
public void giveStamina(double amount, PlayerResourceUpdateEvent.UpdateReason reason) {
// Avoid calling useless event
double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STAMINA), stamina + amount)); double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STAMINA), stamina + amount));
if (stamina == newest) if (stamina == newest)
return; return;
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.STAMINA, amount); PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.STAMINA, amount, reason);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())
return; return;
stamina = newest; // Use updated amount from Bukkit event
setStamina(stamina + event.getAmount());
} }
/**
* @deprecated Provide reason with {@link #giveStellium(double, PlayerResourceUpdateEvent.UpdateReason)}
*/
@Deprecated
public void giveStellium(double amount) { public void giveStellium(double amount) {
giveStellium(amount, PlayerResourceUpdateEvent.UpdateReason.OTHER);
}
public void giveStellium(double amount, PlayerResourceUpdateEvent.UpdateReason reason) {
// Avoid calling useless event
double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STELLIUM), stellium + amount)); double newest = Math.max(0, Math.min(getStats().getStat(StatType.MAX_STELLIUM), stellium + amount));
if (stellium == newest) if (stellium == newest)
return; return;
PlayerRegenResourceEvent event = new PlayerRegenResourceEvent(this, PlayerResource.STELLIUM, amount); PlayerResourceUpdateEvent event = new PlayerResourceUpdateEvent(this, PlayerResource.STELLIUM, amount, reason);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())
return; return;
stellium = newest; // Use updated amount from Bukkit event
setStellium(stellium + event.getAmount());
} }
public double getMana() { public double getMana() {
@ -794,8 +843,8 @@ public class PlayerData extends OfflinePlayerData {
flatCooldownReduction *= flatCooldownReduction > 0 ? skill.getModifier("cooldown", getSkillLevel(skill.getSkill())) * 1000 : 0; flatCooldownReduction *= flatCooldownReduction > 0 ? skill.getModifier("cooldown", getSkillLevel(skill.getSkill())) * 1000 : 0;
skillData.setLastCast(cast.getSkill(), System.currentTimeMillis() - (long) flatCooldownReduction); skillData.setLastCast(cast.getSkill(), System.currentTimeMillis() - (long) flatCooldownReduction);
giveMana(-cast.getManaCost()); giveMana(-cast.getManaCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_COST);
giveStamina(-cast.getStaminaCost()); giveStamina(-cast.getStaminaCost(), PlayerResourceUpdateEvent.UpdateReason.SKILL_COST);
} }
PlayerPostCastSkillEvent postEvent = new PlayerPostCastSkillEvent(this, skill, cast); PlayerPostCastSkillEvent postEvent = new PlayerPostCastSkillEvent(this, skill, cast);

View File

@ -1,36 +1,72 @@
package net.Indyuce.mmocore.api.player.profess.resource; package net.Indyuce.mmocore.api.player.profess.resource;
import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.ClassOption;
import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.api.quest.trigger.ManaTrigger;
import org.bukkit.attribute.Attribute;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import org.bukkit.attribute.Attribute;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.ClassOption;
import net.Indyuce.mmocore.api.player.stats.StatType;
public enum PlayerResource { public enum PlayerResource {
HEALTH(StatType.HEALTH_REGENERATION, ClassOption.OFF_COMBAT_HEALTH_REGEN, (data) -> data.getPlayer().getHealth(), HEALTH(StatType.HEALTH_REGENERATION, ClassOption.OFF_COMBAT_HEALTH_REGEN,
(data) -> data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(), PlayerData::heal), (data) -> data.getPlayer().getHealth(),
MANA(StatType.MANA_REGENERATION, ClassOption.OFF_COMBAT_MANA_REGEN, PlayerData::getMana, data -> data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(),
(data) -> data.getStats().getStat(StatType.MAX_MANA), PlayerData::giveMana), (data, amount) -> data.heal(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
STAMINA(StatType.STAMINA_REGENERATION, ClassOption.OFF_COMBAT_STAMINA_REGEN, PlayerData::getStamina, (data, amount) -> data.heal(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
(data) -> data.getStats().getStat(StatType.MAX_STAMINA), PlayerData::giveStamina), (data, amount) -> data.heal(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
STELLIUM(StatType.STELLIUM_REGENERATION, ClassOption.OFF_COMBAT_STELLIUM_REGEN, PlayerData::getStellium, (data, amount) -> data.getPlayer().setHealth(amount)),
(data) -> data.getStats().getStat(StatType.MAX_STELLIUM), PlayerData::giveStellium);
MANA(StatType.MANA_REGENERATION, ClassOption.OFF_COMBAT_MANA_REGEN,
PlayerData::getMana,
data -> data.getStats().getStat(StatType.MAX_MANA),
(data, amount) -> data.giveMana(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
(data, amount) -> data.giveMana(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
(data, amount) -> data.giveMana(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
(data, amount) -> data.setMana(amount)),
STAMINA(StatType.STAMINA_REGENERATION, ClassOption.OFF_COMBAT_STAMINA_REGEN,
PlayerData::getStamina,
data -> data.getStats().getStat(StatType.MAX_STAMINA),
(data, amount) -> data.giveStamina(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
(data, amount) -> data.giveStamina(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
(data, amount) -> data.giveStamina(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
(data, amount) -> data.setStamina(amount)),
STELLIUM(StatType.STELLIUM_REGENERATION, ClassOption.OFF_COMBAT_STELLIUM_REGEN,
PlayerData::getStellium,
data -> data.getStats().getStat(StatType.MAX_STELLIUM),
(data, amount) -> data.giveStellium(amount, PlayerResourceUpdateEvent.UpdateReason.REGENERATION),
(data, amount) -> data.giveStellium(amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
(data, amount) -> data.giveStellium(-amount, PlayerResourceUpdateEvent.UpdateReason.COMMAND),
(data, amount) -> data.setStellium(amount));
private final StatType regenStat; private final StatType regenStat;
private final ClassOption offCombatRegen; private final ClassOption offCombatRegen;
private final Function<PlayerData, Double> current, max; private final Function<PlayerData, Double> current, max;
private final BiConsumer<PlayerData, Double> regen; private final BiConsumer<PlayerData, Double> regen;
PlayerResource(StatType regenStat, ClassOption offCombatRegen, Function<PlayerData, Double> current, Function<PlayerData, Double> max, BiConsumer<PlayerData, Double> regen) { // Used for MMOCore commands
private final BiConsumer<PlayerData, Double> set, give, take;
PlayerResource(StatType regenStat, ClassOption offCombatRegen,
Function<PlayerData, Double> current,
Function<PlayerData, Double> max,
BiConsumer<PlayerData, Double> regen,
BiConsumer<PlayerData, Double> give,
BiConsumer<PlayerData, Double> take,
BiConsumer<PlayerData, Double> set) {
this.regenStat = regenStat; this.regenStat = regenStat;
this.offCombatRegen = offCombatRegen; this.offCombatRegen = offCombatRegen;
this.current = current; this.current = current;
this.max = max; this.max = max;
this.regen = regen; this.regen = regen;
this.give = give;
this.take = take;
this.set = set;
} }
/** /**
@ -65,12 +101,26 @@ public enum PlayerResource {
/** /**
* Regens a player resource. Whatever resource, a bukkit event is triggered * Regens a player resource. Whatever resource, a bukkit event is triggered
* *
* @param player * @param player Player to regen
* Player to regen * @param amount Amount to regen
* @param amount
* Amount to regen
*/ */
public void regen(PlayerData player, double amount) { public void regen(PlayerData player, double amount) {
regen.accept(player, amount); regen.accept(player, amount);
} }
/**
* Used by MMOCore admin commands here: {@link net.Indyuce.mmocore.command.rpg.admin.ResourceCommandTreeNode}
*/
public BiConsumer<PlayerData, Double> getConsumer(ManaTrigger.Operation operation) {
switch (operation) {
case SET:
return set;
case TAKE:
return take;
case GIVE:
return give;
default:
throw new IllegalArgumentException("Operation cannot be null");
}
}
} }

View File

@ -1,8 +1,9 @@
package net.Indyuce.mmocore.api.quest.trigger; package net.Indyuce.mmocore.api.quest.trigger;
import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.math.formula.RandomAmount; import net.Indyuce.mmocore.api.util.math.formula.RandomAmount;
import io.lumine.mythic.lib.api.MMOLineConfig;
public class ManaTrigger extends Trigger { public class ManaTrigger extends Trigger {
private final RandomAmount amount; private final RandomAmount amount;
@ -19,23 +20,17 @@ public class ManaTrigger extends Trigger {
@Override @Override
public void apply(PlayerData player) { public void apply(PlayerData player) {
/* // Give mana
* give mana
*/
if (operation == Operation.GIVE) if (operation == Operation.GIVE)
player.giveMana(amount.calculate()); player.giveMana(amount.calculate(), PlayerResourceUpdateEvent.UpdateReason.TRIGGER);
/* // Set mana
* set mana
*/
else if (operation == Operation.SET) else if (operation == Operation.SET)
player.setMana(amount.calculate()); player.setMana(amount.calculate());
/* // Take mana
* take mana
*/
else else
player.giveMana(-amount.calculate()); player.giveMana(-amount.calculate(), PlayerResourceUpdateEvent.UpdateReason.TRIGGER);
} }
public enum Operation { public enum Operation {

View File

@ -1,8 +1,10 @@
package net.Indyuce.mmocore.api.quest.trigger; package net.Indyuce.mmocore.api.quest.trigger;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.math.formula.RandomAmount;
import io.lumine.mythic.lib.api.MMOLineConfig; import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.quest.trigger.ManaTrigger.Operation;
import net.Indyuce.mmocore.api.util.math.formula.RandomAmount;
public class StaminaTrigger extends Trigger { public class StaminaTrigger extends Trigger {
private final RandomAmount amount; private final RandomAmount amount;
@ -19,28 +21,16 @@ public class StaminaTrigger extends Trigger {
@Override @Override
public void apply(PlayerData player) { public void apply(PlayerData player) {
/* // Give stamina
* give mana
*/
if (operation == Operation.GIVE) if (operation == Operation.GIVE)
player.giveStamina(amount.calculate()); player.giveStamina(amount.calculate(), PlayerResourceUpdateEvent.UpdateReason.TRIGGER);
/* // Set stamina
* set mana
*/
else if (operation == Operation.SET) else if (operation == Operation.SET)
player.setStamina(amount.calculate()); player.setStamina(amount.calculate());
/* // Take stamina
* take mana
*/
else else
player.giveStamina(-amount.calculate()); player.giveStamina(-amount.calculate(), PlayerResourceUpdateEvent.UpdateReason.TRIGGER);
}
public enum Operation {
GIVE,
SET,
TAKE
} }
} }

View File

@ -1,9 +1,10 @@
package net.Indyuce.mmocore.api.quest.trigger; package net.Indyuce.mmocore.api.quest.trigger;
import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.quest.trigger.ManaTrigger.Operation; import net.Indyuce.mmocore.api.quest.trigger.ManaTrigger.Operation;
import net.Indyuce.mmocore.api.util.math.formula.RandomAmount; import net.Indyuce.mmocore.api.util.math.formula.RandomAmount;
import io.lumine.mythic.lib.api.MMOLineConfig;
public class StelliumTrigger extends Trigger { public class StelliumTrigger extends Trigger {
private final RandomAmount amount; private final RandomAmount amount;
@ -20,22 +21,16 @@ public class StelliumTrigger extends Trigger {
@Override @Override
public void apply(PlayerData player) { public void apply(PlayerData player) {
/* // Give stellium
* give mana
*/
if (operation == Operation.GIVE) if (operation == Operation.GIVE)
player.giveStellium(amount.calculate()); player.giveStellium(amount.calculate(), PlayerResourceUpdateEvent.UpdateReason.TRIGGER);
/* // Set stellium
* set mana
*/
else if (operation == Operation.SET) else if (operation == Operation.SET)
player.setStellium(amount.calculate()); player.setStellium(amount.calculate());
/* // Take stellium
* take mana
*/
else else
player.giveStellium(-amount.calculate()); player.giveStellium(-amount.calculate(), PlayerResourceUpdateEvent.UpdateReason.TRIGGER);
} }
} }

View File

@ -1,10 +1,9 @@
package net.Indyuce.mmocore.api.quest.trigger; package net.Indyuce.mmocore.api.quest.trigger;
import org.bukkit.Bukkit; import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import io.lumine.mythic.lib.api.MMOLineConfig; import org.bukkit.Bukkit;
public abstract class Trigger { public abstract class Trigger {
private final long delay; private final long delay;
@ -28,10 +27,10 @@ public abstract class Trigger {
Bukkit.getScheduler().scheduleSyncDelayedTask(MMOCore.plugin, () -> apply(player), delay); Bukkit.getScheduler().scheduleSyncDelayedTask(MMOCore.plugin, () -> apply(player), delay);
} }
/* /**
* this method must not be used directly when executing triggers after quest * This method must not be used directly when executing triggers after quest
* objectives for example, because this method does NOT take into account * objectives for example, because this method does NOT take into account
* trigger delay * trigger delay, {@link #schedule(PlayerData)} is used instead.
*/ */
public abstract void apply(PlayerData player); public abstract void apply(PlayerData player);
} }

View File

@ -1,9 +1,9 @@
package net.Indyuce.mmocore.command.rpg.admin; package net.Indyuce.mmocore.command.rpg.admin;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.command.CommandSender;
import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode; import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import org.bukkit.command.CommandSender;
public class AdminCommandTreeNode extends CommandTreeNode { public class AdminCommandTreeNode extends CommandTreeNode {
public AdminCommandTreeNode(CommandTreeNode parent) { public AdminCommandTreeNode(CommandTreeNode parent) {
@ -25,10 +25,8 @@ public class AdminCommandTreeNode extends CommandTreeNode {
addChild(new PointsCommandTreeNode("attribute", this, PlayerData::setAttributePoints, PlayerData::giveAttributePoints, PlayerData::getAttributePoints)); addChild(new PointsCommandTreeNode("attribute", this, PlayerData::setAttributePoints, PlayerData::giveAttributePoints, PlayerData::getAttributePoints));
addChild(new PointsCommandTreeNode("attr-realloc", this, PlayerData::setAttributeReallocationPoints, PlayerData::giveAttributeReallocationPoints, PlayerData::getAttributeReallocationPoints)); addChild(new PointsCommandTreeNode("attr-realloc", this, PlayerData::setAttributeReallocationPoints, PlayerData::giveAttributeReallocationPoints, PlayerData::getAttributeReallocationPoints));
addChild(new ResourceCommandTreeNode("health", this, (data, value) -> data.getPlayer().setHealth(value), PlayerData::heal, (data, value) -> data.heal(-value), (data) -> data.getPlayer().getHealth())); for (PlayerResource res : PlayerResource.values())
addChild(new ResourceCommandTreeNode("mana", this, PlayerData::setMana, PlayerData::giveMana, (data, value) -> data.giveMana(-value), PlayerData::getMana)); addChild(new ResourceCommandTreeNode(res.name().toLowerCase(), this, res));
addChild(new ResourceCommandTreeNode("stamina", this, PlayerData::setStamina, PlayerData::giveStamina, (data, value) -> data.giveStamina(-value), PlayerData::getStamina));
addChild(new ResourceCommandTreeNode("stellium", this, PlayerData::setStellium, PlayerData::giveStellium, (data, value) -> data.giveStellium(-value), PlayerData::getStellium));
} }
@Override @Override

View File

@ -1,38 +1,35 @@
package net.Indyuce.mmocore.command.rpg.admin; package net.Indyuce.mmocore.command.rpg.admin;
import java.util.function.BiConsumer; import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode;
import java.util.function.Function; import io.lumine.mythic.lib.mmolibcommands.api.Parameter;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.api.quest.trigger.ManaTrigger;
import net.Indyuce.mmocore.command.CommandVerbose; import net.Indyuce.mmocore.command.CommandVerbose;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import net.Indyuce.mmocore.api.player.PlayerData;
import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode;
import io.lumine.mythic.lib.mmolibcommands.api.Parameter;
public class ResourceCommandTreeNode extends CommandTreeNode { public class ResourceCommandTreeNode extends CommandTreeNode {
private final String type; private final String type;
private final Function<PlayerData, Double> get; private final PlayerResource resource;
public ResourceCommandTreeNode(String type, CommandTreeNode parent, BiConsumer<PlayerData, Double> set, BiConsumer<PlayerData, Double> give, public ResourceCommandTreeNode(String type, CommandTreeNode parent, PlayerResource resource) {
BiConsumer<PlayerData, Double> take, Function<PlayerData, Double> get) {
super(parent, "resource-" + type); super(parent, "resource-" + type);
this.type = type; this.type = type;
this.get = get; this.resource = resource;
addChild(new ActionCommandTreeNode(this, "set", set)); addChild(new ActionCommandTreeNode(this, "set", ManaTrigger.Operation.SET));
addChild(new ActionCommandTreeNode(this, "give", give)); addChild(new ActionCommandTreeNode(this, "give", ManaTrigger.Operation.GIVE));
addChild(new ActionCommandTreeNode(this, "take", take)); addChild(new ActionCommandTreeNode(this, "take", ManaTrigger.Operation.TAKE));
} }
public class ActionCommandTreeNode extends CommandTreeNode { public class ActionCommandTreeNode extends CommandTreeNode {
private final BiConsumer<PlayerData, Double> action; private final ManaTrigger.Operation action;
public ActionCommandTreeNode(CommandTreeNode parent, String type, BiConsumer<PlayerData, Double> action) { public ActionCommandTreeNode(CommandTreeNode parent, String type, ManaTrigger.Operation action) {
super(parent, type); super(parent, type);
this.action = action; this.action = action;
@ -61,9 +58,9 @@ public class ResourceCommandTreeNode extends CommandTreeNode {
} }
PlayerData data = PlayerData.get(player); PlayerData data = PlayerData.get(player);
action.accept(data, amount); resource.getConsumer(action).accept(data, amount);
CommandVerbose.verbose(sender, CommandVerbose.CommandType.RESOURCE, CommandVerbose.verbose(sender, CommandVerbose.CommandType.RESOURCE,
ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " now has " + ChatColor.GOLD + get.apply(data) ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " now has " + ChatColor.GOLD + resource.getCurrent(data)
+ ChatColor.YELLOW + " " + type + " points."); + ChatColor.YELLOW + " " + type + " points.");
return CommandResult.SUCCESS; return CommandResult.SUCCESS;
} }

View File

@ -2,7 +2,7 @@
package net.Indyuce.mmocore.listener; package net.Indyuce.mmocore.listener;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent; import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource; import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.gui.api.PluginInventory; import net.Indyuce.mmocore.gui.api.PluginInventory;
@ -19,7 +19,6 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
@ -70,16 +69,20 @@ public class PlayerListener implements Listener {
} }
/** /**
* Warning: this really is not the best way to interface with MMOCore * Using the Bukkit health update event is not a good way of interacting
* generation. Use instead PlayerRegenResourceEvent to be able to access * with MMOCore health regeneration. The PlayerResourceUpdateEvent
* directly the PlayerData without an extra map lookup. * should be heavily prioritized if possible.
* <p>
* This method makes sure that all the plugins which utilize this event
* can also communicate with MMOCore
*/ */
@Deprecated
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
public void g(PlayerRegenResourceEvent event) { public void g(PlayerResourceUpdateEvent event) {
if (event.getResource() == PlayerResource.HEALTH) { if (event.getResource() == PlayerResource.HEALTH) {
EntityRegainHealthEvent bukkitEvent = new EntityRegainHealthEvent(event.getPlayer(), event.getAmount(), RegainReason.CUSTOM); EntityRegainHealthEvent bukkitEvent = new EntityRegainHealthEvent(event.getPlayer(), event.getAmount(), RegainReason.CUSTOM);
Bukkit.getPluginManager().callEvent(bukkitEvent); Bukkit.getPluginManager().callEvent(bukkitEvent);
// Update event values
event.setCancelled(bukkitEvent.isCancelled()); event.setCancelled(bukkitEvent.isCancelled());
event.setAmount(bukkitEvent.getAmount()); event.setAmount(bukkitEvent.getAmount());
} }

View File

@ -1,24 +1,19 @@
package net.Indyuce.mmocore.skill; package net.Indyuce.mmocore.skill;
import org.bukkit.Bukkit; import io.lumine.mythic.lib.api.DamageType;
import org.bukkit.Color; import io.lumine.mythic.lib.api.event.PlayerAttackEvent;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.stats.StatType; import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.api.skill.Skill; import net.Indyuce.mmocore.api.skill.Skill;
import net.Indyuce.mmocore.api.skill.SkillResult; import net.Indyuce.mmocore.api.skill.SkillResult;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue; import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
import net.Indyuce.mmocore.api.util.math.particle.ParabolicProjectile; import net.Indyuce.mmocore.api.util.math.particle.ParabolicProjectile;
import io.lumine.mythic.lib.api.DamageType; import org.bukkit.*;
import io.lumine.mythic.lib.api.event.PlayerAttackEvent; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
public class Ambers extends Skill implements Listener { public class Ambers extends Skill implements Listener {
public Ambers() { public Ambers() {
@ -79,7 +74,7 @@ public class Ambers extends Skill implements Listener {
data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1, 1); data.getPlayer().playSound(data.getPlayer().getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1, 1);
// data.getSkillData().ambers++; // data.getSkillData().ambers++;
data.giveMana((data.getStats().getStat(StatType.MAX_MANA) - data.getMana()) * percent); data.giveMana((data.getStats().getStat(StatType.MAX_MANA) - data.getMana()) * percent, PlayerResourceUpdateEvent.UpdateReason.SKILL_REGENERATION);
cancel(); cancel();
return; return;

View File

@ -8,7 +8,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent; import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.skill.Skill; import net.Indyuce.mmocore.api.skill.Skill;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue; import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
@ -29,7 +29,7 @@ public class Neptune_Gift extends Skill implements Listener {
} }
@EventHandler @EventHandler
public void a(PlayerRegenResourceEvent event) { public void a(PlayerResourceUpdateEvent event) {
PlayerData data = event.getData(); PlayerData data = event.getData();
if (event.getPlayer().getLocation().getBlock().getType() == Material.WATER) { if (event.getPlayer().getLocation().getBlock().getType() == Material.WATER) {
Optional<SkillInfo> skill = data.getProfess().findSkill(this); Optional<SkillInfo> skill = data.getProfess().findSkill(this);