Skip to content

Commit

Permalink
Adds more API to enable Level manipulation from plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 3, 2020
1 parent 071d3d0 commit 66b098e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -127,10 +128,18 @@ public CompletableFuture<Results> calculateLevel(UUID targetPlayer, Island islan
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<String, Object> keyValues = new HashMap<>();
keyValues.put("eventName", "IslandLevelCalculatedEvent");
Expand All @@ -140,8 +149,14 @@ private boolean fireIslandLevelCalcEvent(UUID targetPlayer, Island island, Resul
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));
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/world/bentobox/level/calculators/Results.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +63,14 @@ public long getPointsToNextLevel() {
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();
}
Expand Down

0 comments on commit 66b098e

Please sign in to comment.