Better migration of databases relates to #2404 (#2411)

Also, adds support for 1.21.
This commit is contained in:
tastybento 2024-06-19 09:08:07 -07:00 committed by GitHub
parent 09c5cbb449
commit 2f9e3c42fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 117 additions and 7 deletions

View File

@ -235,6 +235,12 @@
<version>${spigot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc.....</groupId>
<artifactId>spigot</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc....</groupId>
<artifactId>spigot</artifactId>

View File

@ -1,8 +1,13 @@
package world.bentobox.bentobox.commands;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
@ -20,6 +25,8 @@ import world.bentobox.bentobox.database.objects.DataObject;
public class BentoBoxMigrateCommand extends ConfirmableCommand {
private static final String MIGRATED = "commands.bentobox.migrate.migrated";
private Queue<Class<? extends DataObject>> classQueue;
private BukkitTask task;
/**
* Reloads settings, addons and localization command
@ -41,11 +48,21 @@ public class BentoBoxMigrateCommand extends ConfirmableCommand {
user.sendMessage("commands.bentobox.migrate.addons");
Set<Class<? extends DataObject>> classSet = getPlugin().getAddonsManager().getDataObjects();
classSet.addAll(Database.getDataobjects());
classSet.forEach(t -> {
user.sendMessage("commands.bentobox.migrate.class", TextVariables.DESCRIPTION, BentoBox.getInstance().getSettings().getDatabasePrefix() + t.getCanonicalName());
new Database<>(getPlugin(), t).loadObjects();
user.sendMessage(MIGRATED);
});
// Put classSet into classQueue
classQueue = new LinkedList<>(classSet);
// Start a scheduler to step through these in a reasonable time
task = Bukkit.getScheduler().runTaskTimer(getPlugin(), () -> {
Class<? extends DataObject> t = classQueue.poll();
if (t != null) {
user.sendMessage("commands.bentobox.migrate.class", TextVariables.DESCRIPTION,
BentoBox.getInstance().getSettings().getDatabasePrefix() + t.getCanonicalName());
new Database<>(getPlugin(), t).loadObjects();
user.sendMessage(MIGRATED);
} else {
user.sendMessage("commands.bentobox.migrate.completed");
task.cancel();
}
}, 0, 20L);
});
return true;
}

View File

@ -44,8 +44,16 @@ public class TransitionDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
List<T> listTo = toHandler.loadObjects();
// If source database has objects, then delete and save them in the destination database
for (T object : listFrom) {
toHandler.saveObject(object);
fromHandler.deleteObject(object);
toHandler.saveObject(object).thenAccept(b -> {
// Only delete if save was successful
if (b) {
try {
fromHandler.deleteObject(object);
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
plugin.logStacktrace(e);
}
}
});
}
// Merge results
listTo.addAll(listFrom);

View File

@ -0,0 +1,52 @@
package world.bentobox.bentobox.nms.v1_21_R0_1_SNAPSHOT;
import java.util.concurrent.CompletableFuture;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_21_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_21_R1.block.data.CraftBlockData;
import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.nms.PasteHandler;
import world.bentobox.bentobox.util.DefaultPasteUtil;
import world.bentobox.bentobox.util.Util;
public class PasteHandlerImpl implements PasteHandler {
protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState();
/**
* Set the block to the location
*
* @param island - island
* @param location - location
* @param bpBlock - blueprint block
*/
@Override
public CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
return Util.getChunkAtAsync(location).thenRun(() -> {
Block block = location.getBlock();
// Set the block data - default is AIR
BlockData bd = DefaultPasteUtil.createBlockData(bpBlock);
CraftBlockData craft = (CraftBlockData) bd;
net.minecraft.world.level.World nmsWorld = ((CraftWorld) location.getWorld()).getHandle();
Chunk nmsChunk = nmsWorld.d(location.getBlockX() >> 4, location.getBlockZ() >> 4);
BlockPosition bp = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
// Setting the block to air before setting to another state prevents some console errors
nmsChunk.a(bp, AIR, false);
nmsChunk.a(bp, craft.getState(), false);
block.setBlockData(bd, false);
DefaultPasteUtil.setBlockState(island, block, bpBlock);
// Set biome
if (bpBlock.getBiome() != null) {
block.setBiome(bpBlock.getBiome());
}
});
}
}

View File

@ -0,0 +1,26 @@
package world.bentobox.bentobox.nms.v1_21_R0_1_SNAPSHOT;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_21_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_21_R1.block.data.CraftBlockData;
import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.World;
import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.nms.CopyWorldRegenerator;
public class WorldRegeneratorImpl extends CopyWorldRegenerator {
@Override
public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData,
boolean applyPhysics) {
CraftBlockData craft = (CraftBlockData) blockData;
World nmsWorld = ((CraftWorld) chunk.getWorld()).getHandle();
Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ());
BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z);
// Setting the block to air before setting to another state prevents some console errors
nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics);
nmsChunk.a(bp, craft.getState(), applyPhysics);
}
}

View File

@ -514,6 +514,7 @@ commands:
addons: '[prefix_bentobox]&6 Migrating addons'
class: '[prefix_bentobox]&6 Migrating [description]'
migrated: '[prefix_bentobox]&a Migrated'
completed: '[prefix_bentobox]&a Completed'
rank:
description: 'list, add, or remove ranks'
parameters: '&a [list | add | remove] [rank reference] [rank value]'