Skip to content

Commit

Permalink
Fixes a Level addon crash on startup.
Browse files Browse the repository at this point in the history
Level addon crashed at the startup if Visit or Warps addon were not installed. It happened because Level addon main class were implementing Listener interface.
To avoid it and fix the crash, I moved migration listener to a separate class.

Fixes #2012
  • Loading branch information
BONNe committed Aug 21, 2022
1 parent dae3db6 commit 2ca4e0a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 43 deletions.
46 changes: 6 additions & 40 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
Expand All @@ -38,6 +35,7 @@
import world.bentobox.level.config.ConfigSettings;
import world.bentobox.level.listeners.IslandActivitiesListeners;
import world.bentobox.level.listeners.JoinLeaveListener;
import world.bentobox.level.listeners.MigrationListener;
import world.bentobox.level.objects.LevelsData;
import world.bentobox.level.objects.TopTenData;
import world.bentobox.level.requests.LevelRequestHandler;
Expand All @@ -50,7 +48,7 @@
* @author tastybento
*
*/
public class Level extends Addon implements Listener {
public class Level extends Addon {

// The 10 in top ten
public static final int TEN = 10;
Expand Down Expand Up @@ -112,7 +110,8 @@ public void onEnable() {
// Register listeners
this.registerListener(new IslandActivitiesListeners(this));
this.registerListener(new JoinLeaveListener(this));
this.registerListener(this);
this.registerListener(new MigrationListener(this));

// Register commands for GameModes
registeredGameModes.clear();
getPlugin().getAddonsManager().getGameModeAddons().stream()
Expand Down Expand Up @@ -174,7 +173,7 @@ private void hookExtensions()
this.getAddonByName("Visit").ifPresentOrElse(addon ->
{
this.visitHook = (VisitAddon) addon;
this.log("Likes Addon hooked into Visit addon.");
this.log("Level Addon hooked into Visit addon.");
}, () ->
{
this.visitHook = null;
Expand All @@ -184,7 +183,7 @@ private void hookExtensions()
this.getAddonByName("Warps").ifPresentOrElse(addon ->
{
this.warpHook = (Warp) addon;
this.log("Likes Addon hooked into Warps addon.");
this.log("Level Addon hooked into Warps addon.");
}, () ->
{
this.warpHook = null;
Expand Down Expand Up @@ -217,39 +216,6 @@ public static int compareVersions(String version1, String version2) {
return comparisonResult;
}

@EventHandler
public void onBentoBoxReady(BentoBoxReadyEvent e) {
// Perform upgrade check
manager.migrate();
// Load TopTens
manager.loadTopTens();
/*
* DEBUG code to generate fake islands and then try to level them all.
Bukkit.getScheduler().runTaskLater(getPlugin(), () -> {
getPlugin().getAddonsManager().getGameModeAddons().stream()
.filter(gm -> !settings.getGameModes().contains(gm.getDescription().getName()))
.forEach(gm -> {
for (int i = 0; i < 1000; i++) {
try {
NewIsland.builder().addon(gm).player(User.getInstance(UUID.randomUUID())).name("default").reason(Reason.CREATE).noPaste().build();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
// Queue all islands DEBUG
getIslands().getIslands().stream().filter(Island::isOwned).forEach(is -> {
this.getManager().calculateLevel(is.getOwner(), is).thenAccept(r ->
log("Result for island calc " + r.getLevel() + " at " + is.getCenter()));
});
}, 60L);*/
}


private void registerPlaceholders(GameModeAddon gm) {
if (getPlugin().getPlaceholdersManager() == null) return;
// Island Level
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
public class LevelsManager {
private static final String INTOPTEN = "intopten";
private static final TreeMap<BigInteger, String> LEVELS;
private static final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25};
private static final BigInteger THOUSAND = BigInteger.valueOf(1000);
static {
LEVELS = new TreeMap<>();
Expand All @@ -45,7 +44,7 @@ public class LevelsManager {
LEVELS.put(THOUSAND.pow(3), "G");
LEVELS.put(THOUSAND.pow(4), "T");
}
private Level addon;
private final Level addon;

// Database handler for level data
private final Database<IslandLevels> handler;
Expand Down Expand Up @@ -341,7 +340,7 @@ boolean hasTopTenPerm(@NonNull World world, @NonNull UUID targetPlayer) {
/**
* Loads all the top tens from the database
*/
void loadTopTens() {
public void loadTopTens() {
topTenLists.clear();
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> {
addon.log("Generating rankings");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Created by BONNe
// Copyright - 2022
//


package world.bentobox.level.listeners;


import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
import world.bentobox.level.Level;


/**
* This listener checks when BentoBox is ready and then tries to migrate Levels addon database, if it is required.
*/
public class MigrationListener implements Listener
{
public MigrationListener(Level addon)
{
this.addon = addon;
}

@EventHandler
public void onBentoBoxReady(BentoBoxReadyEvent e) {
// Perform upgrade check
this.addon.getManager().migrate();
// Load TopTens
this.addon.getManager().loadTopTens();
/*
* DEBUG code to generate fake islands and then try to level them all.
Bukkit.getScheduler().runTaskLater(getPlugin(), () -> {
getPlugin().getAddonsManager().getGameModeAddons().stream()
.filter(gm -> !settings.getGameModes().contains(gm.getDescription().getName()))
.forEach(gm -> {
for (int i = 0; i < 1000; i++) {
try {
NewIsland.builder().addon(gm).player(User.getInstance(UUID.randomUUID())).name("default").reason(Reason.CREATE).noPaste().build();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
// Queue all islands DEBUG
getIslands().getIslands().stream().filter(Island::isOwned).forEach(is -> {
this.getManager().calculateLevel(is.getOwner(), is).thenAccept(r ->
log("Result for island calc " + r.getLevel() + " at " + is.getCenter()));
});
}, 60L);*/
}


private final Level addon;
}

0 comments on commit 2ca4e0a

Please sign in to comment.