Migrate after BentoBox worlds have loaded.

This commit is contained in:
tastybento 2020-07-26 13:32:46 -07:00
parent 232360d165
commit 53d0bf4093
4 changed files with 43 additions and 18 deletions

View File

@ -98,6 +98,9 @@ public class Level extends Addon implements Listener {
@EventHandler
public void onBentoBoxReady(BentoBoxReadyEvent e) {
// Perform upgrade check
manager.migrate(this);
// Load TopTens
manager.loadTopTens();
/*
* DEBUG code to generate fake islands and then try to level them all.

View File

@ -85,11 +85,9 @@ public class LevelsManager {
topTenLists = new HashMap<>();
// Background
background = new PanelItemBuilder().icon(Material.BLACK_STAINED_GLASS_PANE).name(" ").build();
// Perform upgrade check
migrate(addon);
}
private void migrate(Level addon2) {
public void migrate(Level addon2) {
Database<LevelsData> oldDb = new Database<>(addon, LevelsData.class);
oldDb.loadObjects().forEach(ld -> {
try {
@ -115,7 +113,7 @@ public class LevelsManager {
// Now delete the old database entry
oldDb.deleteID(ld.getUniqueId());
} catch (Exception e) {
addon.logError("Could not migrate level data database! ");
addon.logError("Could not migrate level data database! " + e.getMessage());
e.printStackTrace();
return;
}
@ -389,9 +387,7 @@ public class LevelsManager {
public String getPointsToNextString(@NonNull World world, @Nullable UUID targetPlayer) {
if (targetPlayer == null) return "";
Island island = addon.getIslands().getIsland(world, targetPlayer);
if (island == null) return "";
IslandLevels ld = getLevelsData(island);
return ld == null ? "" : String.valueOf(ld.getPointsToNextLevel());
return island == null ? "" : String.valueOf(getLevelsData(island).getPointsToNextLevel());
}
/**
@ -495,7 +491,9 @@ public class LevelsManager {
Island island = addon.getIslands().getIsland(world, targetPlayer);
if (island != null) {
String id = island.getUniqueId();
levelsCache.computeIfAbsent(id, IslandLevels::new).setLevel(lv);
IslandLevels il = levelsCache.computeIfAbsent(id, IslandLevels::new);
// Remove the initial level
il.setLevel(lv - il.getInitialLevel());
handler.saveObjectAsync(levelsCache.get(id));
// Update TopTen
addToTopTen(world, targetPlayer, levelsCache.get(id).getLevel());
@ -514,7 +512,7 @@ public class LevelsManager {
Island island = addon.getIslands().getIsland(world, owner);
if (island == null) return;
IslandLevels ld = levelsCache.computeIfAbsent(island.getUniqueId(), IslandLevels::new);
ld.setLevel(r.getLevel());
ld.setLevel(r.getLevel() - ld.getInitialLevel());
ld.setUwCount(Maps.asMap(r.getUwCount().elementSet(), elem -> r.getUwCount().count(elem)));
ld.setMdCount(Maps.asMap(r.getMdCount().elementSet(), elem -> r.getMdCount().count(elem)));
ld.setPointsToNextLevel(r.getPointsToNextLevel());
@ -524,4 +522,13 @@ public class LevelsManager {
addToTopTen(world, owner, ld.getLevel());
}
/**
* Removes island from cache when it is deleted
* @param uniqueId - id of island
*/
public void deleteIsland(String uniqueId) {
levelsCache.remove(uniqueId);
handler.deleteID(uniqueId);
}
}

View File

@ -8,6 +8,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandCreatedEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandDeleteEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandPreclearEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandRegisteredEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandResettedEvent;
@ -65,6 +66,12 @@ public class IslandActivitiesListeners implements Listener {
remove(world, uuid);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onIslandDeleted(IslandDeleteEvent e) {
// Remove island
addon.getManager().deleteIsland(e.getIsland().getUniqueId());
}
private void remove(World world, UUID uuid) {
if (uuid != null && world != null) {
addon.getManager().removeEntry(world, uuid);
@ -88,14 +95,14 @@ public class IslandActivitiesListeners implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(IslandUnregisteredEvent e) {
// Remove player from the top ten and level
// Remove player from the top ten
remove(e.getIsland().getWorld(), e.getPlayerUUID());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(IslandRegisteredEvent e) {
// Remove player from the top ten and level
// Remove player from the top ten
remove(e.getIsland().getWorld(), e.getPlayerUUID());
}

View File

@ -16,6 +16,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -58,7 +59,7 @@ import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.level.calculators.Pipeliner;
import world.bentobox.level.calculators.Results;
import world.bentobox.level.config.ConfigSettings;
import world.bentobox.level.objects.LevelsData;
import world.bentobox.level.objects.IslandLevels;
import world.bentobox.level.objects.TopTenData;
/**
@ -104,7 +105,7 @@ public class LevelsManagerTest {
@Mock
private PluginManager pim;
@Mock
private LevelsData levelsData;
private IslandLevels levelsData;
@Mock
private IslandsManager im;
@ -125,6 +126,7 @@ public class LevelsManagerTest {
/**
* @throws java.lang.Exception
*/
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
when(addon.getPlugin()).thenReturn(plugin);
@ -154,9 +156,11 @@ public class LevelsManagerTest {
when(island.getMemberSet()).thenReturn(iset);
when(island.getOwner()).thenReturn(uuid);
when(island.getWorld()).thenReturn(world);
when(island.getUniqueId()).thenReturn(UUID.randomUUID().toString());
// Default to uuid's being island owners
when(im.isOwner(eq(world), any())).thenReturn(true);
when(im.getOwner(any(), any(UUID.class))).thenAnswer(in -> in.getArgument(1, UUID.class));
when(im.getIsland(eq(world), eq(uuid))).thenReturn(island);
// Player
when(player.getUniqueId()).thenReturn(uuid);
@ -205,9 +209,10 @@ public class LevelsManagerTest {
// Include a known UUID
ttd.getTopTen().put(uuid, 456789L);
topTen.add(ttd);
when(handler.loadObjects()).thenReturn(topTen);
// Supply no island levels first, then topTen
when(handler.loadObjects()).thenReturn(Collections.emptyList(), topTen);
when(handler.objectExists(anyString())).thenReturn(true);
when(levelsData.getLevel(any())).thenReturn(-5L, -4L, -3L, -2L, -1L, 0L, 1L, 2L, 3L, 4L, 5L, 45678L);
when(levelsData.getLevel()).thenReturn(-5L, -4L, -3L, -2L, -1L, 0L, 1L, 2L, 3L, 4L, 5L, 45678L);
when(levelsData.getUniqueId()).thenReturn(uuid.toString());
when(handler.loadObject(anyString())).thenReturn(levelsData );
@ -220,6 +225,7 @@ public class LevelsManagerTest {
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock.");
lm = new LevelsManager(addon);
lm.migrate(addon);
}
/**
@ -252,7 +258,7 @@ public class LevelsManagerTest {
lm.calculateLevel(uuid, island);
cf.complete(results);
assertEquals(Long.valueOf(10000), lm.getLevelsData(uuid).getLevel(world));
assertEquals(10000L, lm.getLevelsData(island).getLevel());
//Map<UUID, Long> tt = lm.getTopTen(world, 10);
//assertEquals(1, tt.size());
//assertTrue(tt.get(uuid) == 10000);
@ -280,7 +286,9 @@ public class LevelsManagerTest {
*/
@Test
public void testGetPointsToNextString() {
assertEquals("0", lm.getPointsToNextString(world, UUID.randomUUID()));
// No island player
assertEquals("", lm.getPointsToNextString(world, UUID.randomUUID()));
// Player has island
assertEquals("0", lm.getPointsToNextString(world, uuid));
}
@ -297,7 +305,7 @@ public class LevelsManagerTest {
*/
@Test
public void testGetLevelsData() {
assertEquals(levelsData, lm.getLevelsData(uuid));
assertEquals(levelsData, lm.getLevelsData(island));
}