Adds more API to enable Level manipulation from plugins

This commit is contained in:
tastybento 2020-07-03 12:16:13 -07:00
parent 071d3d06d5
commit 66b098ec73
2 changed files with 32 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; 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.events.addon.AddonEvent;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
@ -127,10 +128,18 @@ public class LevelsManager {
return result; 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) { private boolean fireIslandLevelCalcEvent(UUID targetPlayer, Island island, Results results) {
// Fire post calculation event // Fire post calculation event
IslandLevelCalculatedEvent ilce = new IslandLevelCalculatedEvent(targetPlayer, island, results); IslandLevelCalculatedEvent ilce = new IslandLevelCalculatedEvent(targetPlayer, island, results);
Bukkit.getPluginManager().callEvent(ilce); Bukkit.getPluginManager().callEvent(ilce);
if (ilce.isCancelled()) return true;
// This exposes these values to plugins via the event // This exposes these values to plugins via the event
Map<String, Object> keyValues = new HashMap<>(); Map<String, Object> keyValues = new HashMap<>();
keyValues.put("eventName", "IslandLevelCalculatedEvent"); keyValues.put("eventName", "IslandLevelCalculatedEvent");
@ -140,8 +149,14 @@ public class LevelsManager {
keyValues.put("pointsToNextLevel", results.getPointsToNextLevel()); keyValues.put("pointsToNextLevel", results.getPointsToNextLevel());
keyValues.put("deathHandicap", results.getDeathHandicap()); keyValues.put("deathHandicap", results.getDeathHandicap());
keyValues.put("initialLevel", results.getInitialLevel()); keyValues.put("initialLevel", results.getInitialLevel());
new AddonEvent().builder().addon(addon).keyValues(keyValues).build(); keyValues.put("isCancelled", false);
return ilce.isCancelled(); 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));
} }
/** /**

View File

@ -29,6 +29,13 @@ public class Results {
public int getDeathHandicap() { public int getDeathHandicap() {
return deathHandicap.get(); return deathHandicap.get();
} }
/**
* Set the death handicap
* @param handicap
*/
public void setDeathHandicap(int handicap) {
deathHandicap.set(handicap);
}
/** /**
* @return the report * @return the report
@ -56,6 +63,14 @@ public class Results {
return pointsToNextLevel.get(); return pointsToNextLevel.get();
} }
/**
* Set the points to next level
* @param points
*/
public void setPointsToNextLevel(long points) {
pointsToNextLevel.set(points);
}
public long getInitialLevel() { public long getInitialLevel() {
return initialLevel.get(); return initialLevel.get();
} }