getting rid of event triggers

This commit is contained in:
Indyuce 2022-01-01 12:18:51 +01:00
parent 1c5f73c754
commit 839af53c62
9 changed files with 22 additions and 294 deletions

View File

@ -7,7 +7,6 @@ import io.lumine.mythic.lib.api.MMOLineConfig;
import io.lumine.mythic.lib.api.util.PostLoadObject; import io.lumine.mythic.lib.api.util.PostLoadObject;
import io.lumine.mythic.lib.version.VersionMaterial; import io.lumine.mythic.lib.version.VersionMaterial;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.profess.event.EventTrigger;
import net.Indyuce.mmocore.api.player.profess.resource.ManaDisplayOptions; import net.Indyuce.mmocore.api.player.profess.resource.ManaDisplayOptions;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource; import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.api.player.profess.resource.ResourceRegeneration; import net.Indyuce.mmocore.api.player.profess.resource.ResourceRegeneration;
@ -19,8 +18,8 @@ import net.Indyuce.mmocore.experience.ExpCurve;
import net.Indyuce.mmocore.experience.provider.ExperienceDispenser; import net.Indyuce.mmocore.experience.provider.ExperienceDispenser;
import net.Indyuce.mmocore.experience.provider.MainExperienceDispenser; import net.Indyuce.mmocore.experience.provider.MainExperienceDispenser;
import net.Indyuce.mmocore.experience.source.type.ExperienceSource; import net.Indyuce.mmocore.experience.source.type.ExperienceSource;
import net.Indyuce.mmocore.skill.Skill; import net.Indyuce.mmocore.skill.ClassSkill;
import net.Indyuce.mmocore.skill.Skill.SkillInfo; import net.Indyuce.mmocore.skill.RegisteredSkill;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Material; import org.bukkit.Material;
@ -44,11 +43,10 @@ public class PlayerClass extends PostLoadObject {
private final ExpCurve expCurve; private final ExpCurve expCurve;
private final Map<StatType, LinearValue> stats = new HashMap<>(); private final Map<StatType, LinearValue> stats = new HashMap<>();
private final Map<String, SkillInfo> skills = new LinkedHashMap<>(); private final Map<String, ClassSkill> skills = new LinkedHashMap<>();
private final List<Subclass> subclasses = new ArrayList<>(); private final List<Subclass> subclasses = new ArrayList<>();
private final Map<PlayerResource, ResourceRegeneration> resourceHandlers = new HashMap<>(); private final Map<PlayerResource, ResourceRegeneration> resourceHandlers = new HashMap<>();
private final Map<String, EventTrigger> eventTriggers = new HashMap<>();
private final CastingParticle castParticle; private final CastingParticle castParticle;
@ -102,9 +100,8 @@ public class PlayerClass extends PostLoadObject {
if (config.contains("skills")) if (config.contains("skills"))
for (String key : config.getConfigurationSection("skills").getKeys(false)) for (String key : config.getConfigurationSection("skills").getKeys(false))
try { try {
Validate.isTrue(MMOCore.plugin.skillManager.has(key), "Could not find skill " + key); Validate.isTrue(MMOCore.plugin.skillManager.hasSkill(key), "Could not find skill " + key);
skills.put(key.toUpperCase(), MMOCore.plugin.skillManager.get(key) skills.put(key, new ClassSkill(MMOCore.plugin.skillManager.getSkill(key), config.getConfigurationSection("skills." + key)));
.newSkillInfo(config.getConfigurationSection("skills." + key)));
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load skill info '" + key + "' from class '" MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load skill info '" + key + "' from class '"
+ id + "': " + exception.getMessage()); + id + "': " + exception.getMessage());
@ -136,19 +133,9 @@ public class PlayerClass extends PostLoadObject {
} }
} }
if (config.contains("triggers"))
for (String key : config.getConfigurationSection("triggers").getKeys(false)) {
try {
String format = key.toLowerCase().replace("_", "-").replace(" ", "-");
eventTriggers.put(format, new EventTrigger(format, config.getStringList("triggers." + key)));
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load trigger '" + key + "' from class '" + id + "':" + exception.getMessage());
}
}
/* /*
* must make sure all the resourceHandlers are registered when the placer class * Must make sure all the resourceHandlers are registered
* is initialized. * when the placer class is initialized.
*/ */
for (PlayerResource resource : PlayerResource.values()) { for (PlayerResource resource : PlayerResource.values()) {
if (config.isConfigurationSection("resource." + resource.name().toLowerCase())) if (config.isConfigurationSection("resource." + resource.name().toLowerCase()))
@ -165,8 +152,12 @@ public class PlayerClass extends PostLoadObject {
} }
} }
/* /**
* used to generate display class * Used to generate the default Human class if no one is
* specified after loading all the player classes. This is
* a very basic class that will make sure MMOCore can still
* continue to run without having to stop the server because
* some option was not provided
*/ */
public PlayerClass(String id, String name, Material material) { public PlayerClass(String id, String name, Material material) {
super(null); super(null);
@ -281,16 +272,16 @@ public class PlayerClass extends PostLoadObject {
return false; return false;
} }
public boolean hasSkill(Skill skill) { public boolean hasSkill(RegisteredSkill skill) {
return hasSkill(skill.getId()); return hasSkill(skill.getHandler().getId());
} }
public boolean hasSkill(String id) { public boolean hasSkill(String id) {
return skills.containsKey(id); return skills.containsKey(id);
} }
public SkillInfo getSkill(Skill skill) { public ClassSkill getSkill(RegisteredSkill skill) {
return getSkill(skill.getId()); return getSkill(skill.getHandler().getId());
} }
/** /**
@ -299,30 +290,18 @@ public class PlayerClass extends PostLoadObject {
* <p> * <p>
* Examples: * Examples:
* - {@link net.Indyuce.mmocore.skill.list.Neptune_Gift} * - {@link net.Indyuce.mmocore.skill.list.Neptune_Gift}
* - {@link net.Indyuce.mmocore.skill.list.Fire_Berserker} * - {@link net.Indyuce.mmocore.skill.list.Ambers}
*/ */
public Optional<SkillInfo> findSkill(Skill skill) { public Optional<ClassSkill> findSkill(RegisteredSkill skill) {
SkillInfo found = skills.get(skill.getId()); ClassSkill found = skills.get(skill.getHandler().getId());
return found == null ? Optional.empty() : Optional.of(found); return found == null ? Optional.empty() : Optional.of(found);
} }
public SkillInfo getSkill(String id) { public ClassSkill getSkill(String id) {
return skills.get(id); return skills.get(id);
} }
public Set<String> getEventTriggers() { public Collection<ClassSkill> getSkills() {
return eventTriggers.keySet();
}
public boolean hasEventTriggers(String name) {
return eventTriggers.containsKey(name);
}
public EventTrigger getEventTriggers(String name) {
return eventTriggers.get(name);
}
public Collection<SkillInfo> getSkills() {
return skills.values(); return skills.values();
} }

View File

@ -1,44 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event;
import io.lumine.mythic.lib.api.MMOLineConfig;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.quest.trigger.Trigger;
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
import org.apache.commons.lang.Validate;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
/**
* @deprecated Replaced by {@link ExperienceTable} and will
* be removed in 1.8.4
*/
@Deprecated
public class EventTrigger {
private final String event;
private final Set<Trigger> triggers = new LinkedHashSet<>();
public EventTrigger(String event, List<String> list) {
Validate.notNull(list, "Could not load trigger list");
this.event = event;
for (String format : list)
try {
triggers.add(MMOCore.plugin.loadManager.loadTrigger(new MMOLineConfig(format)));
} catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING,
"Could not load trigger '" + format + "' from event trigger '" + event + "': " + exception.getMessage());
}
}
public String getEvent() {
return event;
}
public Set<Trigger> getTriggers() {
return triggers;
}
}

View File

@ -1,13 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event;
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
import org.bukkit.event.Listener;
/**
* @deprecated Replaced by {@link ExperienceTable} and will
* be removed in 1.8.4
*/
@Deprecated
public interface EventTriggerHandler extends Listener {
boolean handles(String event);
}

View File

@ -1,32 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event.trigger;
import io.lumine.mythic.lib.api.event.PlayerAttackEvent;
import io.lumine.mythic.lib.damage.DamageType;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
public class AttackEventTrigger implements EventTriggerHandler {
@Override
public boolean handles(String event) {
return event.endsWith("-damage");
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void a(PlayerAttackEvent event) {
// We don't want players dying by themselves when using an enderpearl.
if (event.getPlayer().equals(event.getEntity())) return;
PlayerData player = PlayerData.get(event.getData().getUniqueId());
PlayerClass profess = player.getProfess();
for (DamageType type : event.getAttack().getDamage().collectTypes()) {
String path = type.getPath() + "-damage";
if (profess.hasEventTriggers(path))
profess.getEventTriggers(path).getTriggers().forEach(trigger -> trigger.apply(player));
}
}
}

View File

@ -1,23 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event.trigger;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler;
public class BlockBrokenTrigger implements EventTriggerHandler {
@Override
public boolean handles(String event) {
return event.startsWith("break-block");
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void a(BlockBreakEvent event) {
PlayerData player = PlayerData.get(event.getPlayer());
if (player.getProfess().hasEventTriggers("break-block"))
player.getProfess().getEventTriggers("break-block").getTriggers().forEach(trigger -> trigger.apply(player));
}
}

View File

@ -1,23 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event.trigger;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockPlaceEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler;
public class BlockPlacedTrigger implements EventTriggerHandler {
@Override
public boolean handles(String event) {
return event.startsWith("place-block");
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void a(BlockPlaceEvent event) {
PlayerData player = PlayerData.get(event.getPlayer());
if (player.getProfess().hasEventTriggers("place-block"))
player.getProfess().getEventTriggers("place-block").getTriggers().forEach(trigger -> trigger.apply(player));
}
}

View File

@ -1,28 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event.trigger;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerChangeClassEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
public class ClassChosenEventTrigger implements EventTriggerHandler {
@Override
public boolean handles(String event) {
return event.startsWith("class-chosen");
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void a(PlayerChangeClassEvent event) {
PlayerData player = event.getData();
if (event.getNewClass().hasEventTriggers("class-chosen")) {
Bukkit.getScheduler().runTaskLater(MMOCore.plugin, () -> {
event.getNewClass().getEventTriggers("class-chosen").getTriggers().forEach(trigger -> trigger.apply(player));
}, 1);
}
}
}

View File

@ -1,41 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event.trigger;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import net.Indyuce.mmocore.api.event.PlayerLevelUpEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler;
public class LevelUpEventTrigger implements EventTriggerHandler {
@Override
public boolean handles(String event) {
return event.startsWith("level-up");
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void a(PlayerLevelUpEvent event) {
PlayerData player = event.getData();
PlayerClass profess = player.getProfess();
for(int i = event.getOldLevel(); i < event.getNewLevel(); i++) {
int level = i + 1;
if(event.hasProfession()) {
String prof = event.getProfession().getId().toLowerCase();
processTrigger(player, profess, "level-up-" + prof);
processTrigger(player, profess, "level-up-" + prof + "-" + level);
} else {
processTrigger(player, profess, "level-up");
processTrigger(player, profess, "level-up-" + level);
if(profess.getMaxLevel() == level)
processTrigger(player, profess, "level-up-max");
}
}
}
public void processTrigger(PlayerData player, PlayerClass profess, String trigger) {
if(profess.hasEventTriggers(trigger)) profess.getEventTriggers(trigger).getTriggers().forEach(t -> t.apply(player));
}
}

View File

@ -1,47 +0,0 @@
package net.Indyuce.mmocore.api.player.profess.event.trigger;
import net.Indyuce.mmocore.api.event.PlayerLevelUpEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.event.EventTriggerHandler;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import java.text.DecimalFormat;
public class MultipleLevelUpEventTrigger implements EventTriggerHandler {
@Override
public boolean handles(String event) {
return event.startsWith("level-up-multiple");
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void a(PlayerLevelUpEvent event) {
PlayerData player = event.getData();
PlayerClass profess = player.getProfess();
for(int i = event.getOldLevel(); i < event.getNewLevel(); i++) {
int level = i + 1;
for (String t : profess.getEventTriggers()){
if (t.startsWith("level-up-multiple")) {
String[] split = t.split("-");
double multiple = Double.parseDouble(split[split.length-1]);
if (level / multiple % 1 == 0) {
DecimalFormat f = new DecimalFormat("#");
if (event.hasProfession()) {
processTrigger(player, profess, "level-up-multiple-" + event.getProfession().getId().toLowerCase() + "-" + f.format(multiple));
} else {
processTrigger(player, profess, "level-up-multiple-" + f.format(multiple));
}
}
}
}
}
}
public void processTrigger(PlayerData player, PlayerClass profess, String trigger) {
if(profess.hasEventTriggers(trigger)) profess.getEventTriggers(trigger).getTriggers().forEach(t -> t.apply(player));
}
}