Fixes a Level addon crash on startup.

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
This commit is contained in:
BONNe 2022-08-21 17:31:42 +03:00
parent dae3db6c98
commit 2ca4e0a070
3 changed files with 69 additions and 43 deletions

View File

@ -13,8 +13,6 @@ import org.bukkit.Bukkit;
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;
@ -22,7 +20,6 @@ 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;
@ -38,6 +35,7 @@ import world.bentobox.level.config.BlockConfig;
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;
@ -50,7 +48,7 @@ import world.bentobox.warps.Warp;
* @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;
@ -112,7 +110,8 @@ public class Level extends Addon implements Listener {
// 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()
@ -174,7 +173,7 @@ public class Level extends Addon implements Listener {
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;
@ -184,7 +183,7 @@ public class Level extends Addon implements Listener {
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;
@ -217,39 +216,6 @@ public class Level extends Addon implements Listener {
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

View File

@ -35,7 +35,6 @@ import world.bentobox.level.objects.TopTenData;
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<>();
@ -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;
@ -341,7 +340,7 @@ public class LevelsManager {
/**
* Loads all the top tens from the database
*/
void loadTopTens() {
public void loadTopTens() {
topTenLists.clear();
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> {
addon.log("Generating rankings");

View File

@ -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;
}