From 66b098ec738fe5fbb13a5673cf1e06fdfeac3565 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 3 Jul 2020 12:16:13 -0700 Subject: [PATCH] Adds more API to enable Level manipulation from plugins --- .../world/bentobox/level/LevelsManager.java | 19 +++++++++++++++++-- .../bentobox/level/calculators/Results.java | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/level/LevelsManager.java b/src/main/java/world/bentobox/level/LevelsManager.java index c79fd59..665c2b3 100644 --- a/src/main/java/world/bentobox/level/LevelsManager.java +++ b/src/main/java/world/bentobox/level/LevelsManager.java @@ -21,6 +21,7 @@ import org.bukkit.World; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; +import world.bentobox.bentobox.api.events.addon.AddonBaseEvent; import world.bentobox.bentobox.api.events.addon.AddonEvent; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; @@ -127,10 +128,18 @@ public class LevelsManager { return result; } + /** + * Fires the IslandLevelCalculatedEvent and returns true if it is canceled + * @param targetPlayer - target player + * @param island - island + * @param results - results set + * @return true if canceled + */ private boolean fireIslandLevelCalcEvent(UUID targetPlayer, Island island, Results results) { // Fire post calculation event IslandLevelCalculatedEvent ilce = new IslandLevelCalculatedEvent(targetPlayer, island, results); Bukkit.getPluginManager().callEvent(ilce); + if (ilce.isCancelled()) return true; // This exposes these values to plugins via the event Map keyValues = new HashMap<>(); keyValues.put("eventName", "IslandLevelCalculatedEvent"); @@ -140,8 +149,14 @@ public class LevelsManager { keyValues.put("pointsToNextLevel", results.getPointsToNextLevel()); keyValues.put("deathHandicap", results.getDeathHandicap()); keyValues.put("initialLevel", results.getInitialLevel()); - new AddonEvent().builder().addon(addon).keyValues(keyValues).build(); - return ilce.isCancelled(); + keyValues.put("isCancelled", false); + AddonBaseEvent e = new AddonEvent().builder().addon(addon).keyValues(keyValues).build(); + // Set the values if they were altered + results.setLevel((Long)e.getKeyValues().getOrDefault("level", results.getLevel())); + results.setInitialLevel((Long)e.getKeyValues().getOrDefault("initialLevel", results.getInitialLevel())); + results.setDeathHandicap((int)e.getKeyValues().getOrDefault("deathHandicap", results.getDeathHandicap())); + results.setPointsToNextLevel((Long)e.getKeyValues().getOrDefault("pointsToNextLevel", results.getPointsToNextLevel())); + return ((Boolean)e.getKeyValues().getOrDefault("isCancelled", false)); } /** diff --git a/src/main/java/world/bentobox/level/calculators/Results.java b/src/main/java/world/bentobox/level/calculators/Results.java index faec036..4bbf453 100644 --- a/src/main/java/world/bentobox/level/calculators/Results.java +++ b/src/main/java/world/bentobox/level/calculators/Results.java @@ -29,6 +29,13 @@ public class Results { public int getDeathHandicap() { return deathHandicap.get(); } + /** + * Set the death handicap + * @param handicap + */ + public void setDeathHandicap(int handicap) { + deathHandicap.set(handicap); + } /** * @return the report @@ -56,6 +63,14 @@ public class Results { return pointsToNextLevel.get(); } + /** + * Set the points to next level + * @param points + */ + public void setPointsToNextLevel(long points) { + pointsToNextLevel.set(points); + } + public long getInitialLevel() { return initialLevel.get(); }