Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Binary storage for player data #4126

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@
<!-- Shaded packages -->
<!-- TODO: Revert changes back to maven when dough 1.3 released -->
<dependency>
<groupId>com.github.baked-libs.dough</groupId>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough-api</artifactId>
<version>1108163a49</version>
<version>1.3.0_local_nbt_v7</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -528,10 +528,12 @@
<version>2.6</version>
<scope>compile</scope>
</dependency>
<!-- Also pinning Spigot since they did a breaking change -->
<!-- in this PR: https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/pull-requests/829/overview -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>${spigot.version}-R0.1-SNAPSHOT</version>
<version>${spigot.version}-R0.1-20240209.081002-77</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.annotation.Nullable;

import io.github.thebusybiscuit.slimefun4.storage.Storage;
import io.github.thebusybiscuit.slimefun4.storage.backend.binary.BinaryStorage;
import io.github.thebusybiscuit.slimefun4.storage.backend.legacy.LegacyStorage;

import org.apache.commons.lang.Validate;
Expand Down Expand Up @@ -306,8 +307,21 @@ private void onPluginStart() {
networkManager = new NetworkManager(networkSize, config.getBoolean("networks.enable-visualizer"), config.getBoolean("networks.delete-excess-items"));

// Data storage
playerStorage = new LegacyStorage();
logger.log(Level.INFO, "Using legacy storage for player data");
String storageBackend = config.getString("storage.player-data");

if (storageBackend.equals("legacy")) {
playerStorage = new LegacyStorage();
logger.info("Using legacy storage for player data");
} else if (storageBackend.equals("binary")) {
playerStorage = new BinaryStorage();
StartupWarnings.experimentalStorage(logger, storageBackend);

// TODO(future): Run migration if needed
} else {
playerStorage = new LegacyStorage();
logger.warning("Unknown storage backend for player data: " + storageBackend);
logger.warning("Defaulting to legacy storage instead");
}

// Setting up bStats
new Thread(metricsService::start, "Slimefun Metrics").start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ static void oldJavaVersion(Logger logger, int recommendedJavaVersion) {
logger.log(Level.WARNING, BORDER);
}

static void experimentalStorage(Logger logger, String backend) {
logger.log(Level.WARNING, BORDER);
logger.log(Level.WARNING, PREFIX + "You have enabled an experimental storage backend!");
logger.log(Level.WARNING, PREFIX);
logger.log(Level.WARNING, PREFIX + "\"{0}\" storage is still in development.", backend);
logger.log(Level.WARNING, PREFIX + "It might not work as expected and it might");
logger.log(Level.WARNING, PREFIX + "break your Slimefun data. Use at your own risk!");
logger.log(Level.WARNING, PREFIX);
logger.log(Level.WARNING, PREFIX + "You can revert at any time but be aware you will");
logger.log(Level.WARNING, PREFIX + "lose progress that happened during the time you switched.");
logger.log(Level.WARNING, PREFIX);
logger.log(Level.WARNING, PREFIX + "If you encounter any issues, please report them to us!");
logger.log(Level.WARNING, BORDER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package io.github.thebusybiscuit.slimefun4.storage.backend.binary;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;

import io.github.bakedlibs.dough.nbt.TagType;
import io.github.bakedlibs.dough.nbt.streams.CompressionType;
import io.github.bakedlibs.dough.nbt.streams.TagInputStream;
import io.github.bakedlibs.dough.nbt.streams.TagOutputStream;
import io.github.bakedlibs.dough.nbt.tags.CompoundTag;
import io.github.bakedlibs.dough.nbt.tags.ListTag;
import io.github.bakedlibs.dough.nbt.tags.StringTag;
import io.github.bakedlibs.dough.nbt.tags.Tag;
import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.storage.Storage;
import io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers.LocationSerializer;
import io.github.thebusybiscuit.slimefun4.storage.data.PlayerData;

public class BinaryStorage implements Storage {

private static final NamespacedKey PLAYER_DATA = new NamespacedKey(Slimefun.instance(), "player_data");
private static final NamespacedKey RESEARCHES_KEY = new NamespacedKey(Slimefun.instance(), "researches");
private static final NamespacedKey BACKPACKS_KEY = new NamespacedKey(Slimefun.instance(), "backpacks");
private static final NamespacedKey WAYPOINTS_KEY = new NamespacedKey(Slimefun.instance(), "waypoints");

// TODO: Move to lz4 or zstd
private static final CompressionType compression = CompressionType.GZIP;

/*
* Structure as JSON:
* {
* "slimefun:researches": ["slimefun:some_research", "slimefun:some_other_research"],
* "slimefun:backpacks": [
* {
* "id": x,
* "item": y
* }
* ],
* "slimefun:waypoints": {
* "<ID>": {
* "name": "",
* "location": {}
* }
* }
* }
*/

@Override
public PlayerData loadPlayerData(UUID uuid) {
Debug.log(TestCase.PLAYER_PROFILE_DATA, "Loading player data from binary storage for {}", uuid);
File file = new File("data-storage/Slimefun/Players/" + uuid + ".dat");

if (!file.exists()) {
return new PlayerData(Set.of(), Map.of(), Set.of());
}

CompoundTag root;
try {
try (TagInputStream stream = new TagInputStream(new FileInputStream(file), compression)) {
root = (CompoundTag) stream.readTag();
}
} catch(IOException e) {
throw new IllegalStateException("Failed to read Player data for " + uuid, e);
}

// Load researches
ListTag<StringTag> list = root.getList(RESEARCHES_KEY, TagType.STRING);

Set<Research> researches = new HashSet<>();
for (Research research : Slimefun.getRegistry().getResearches()) {
for (StringTag tag : list) {
if (tag.getValue().equals(research.getKey().toString())) {
researches.add(research);
}
}
}

// Load backpacks
Map<Integer, PlayerBackpack> backpacks = Map.of();
CompoundTag backpackMap = root.getCompound(BACKPACKS_KEY);
// TODO: Item serialization

// Load waypoints
Set<Waypoint> waypoints = new HashSet<>();
CompoundTag waypointMap = root.getCompound(WAYPOINTS_KEY);
for (Map.Entry<NamespacedKey, Tag<?>> key : waypointMap) {
CompoundTag waypoint = (CompoundTag) key.getValue();

String name = waypoint.getString(CommonKeys.NAME);
Location location = LocationSerializer.INSTANCE.deserialize(waypoint.getCompound(CommonKeys.LOCATION));

waypoints.add(new Waypoint(uuid, key.getKey().getKey(), location, name));
}

return new PlayerData(researches, backpacks, waypoints);
}

@Override
public void savePlayerData(UUID uuid, PlayerData data) {
Debug.log(TestCase.PLAYER_PROFILE_DATA, "Saving player data from binary storage for {}", uuid);
File file = new File("data-storage/Slimefun/Players/" + uuid + ".dat");

CompoundTag root = new CompoundTag(PLAYER_DATA);

// Save researches
ListTag<StringTag> list = new ListTag<>();
for (Research research : data.getResearches()) {
list.add(new StringTag(research.getKey().toString()));
}

root.putList(RESEARCHES_KEY, list);

// Save backpacks
CompoundTag backpackMap = new CompoundTag();

// Save waypoints
CompoundTag waypointMap = new CompoundTag();
for (Waypoint waypoint : data.getWaypoints()) {
CompoundTag waypointTag = new CompoundTag();

waypointTag.putString(CommonKeys.ID, waypoint.getId());
waypointTag.putString(CommonKeys.NAME, waypoint.getName());
waypointTag.putCompound(CommonKeys.LOCATION, LocationSerializer.INSTANCE.serialize(waypoint.getLocation()));

waypointMap.putCompound(new NamespacedKey(Slimefun.instance(), waypoint.getId()), waypointTag);
Comment on lines +135 to +139
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use a BinarySerializer for this?

}
root.putCompound(WAYPOINTS_KEY, waypointMap);

try {
try (TagOutputStream stream = new TagOutputStream(new FileOutputStream(file), compression)) {
stream.writeTag(root);
}
} catch(IOException e) {
throw new IllegalStateException("Failed to read Player data for " + uuid, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.thebusybiscuit.slimefun4.storage.backend.binary;

import org.bukkit.NamespacedKey;

import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

public class CommonKeys {

public static final NamespacedKey ID = new NamespacedKey(Slimefun.instance(), "id");
public static final NamespacedKey NAME = new NamespacedKey(Slimefun.instance(), "name");
// Location stuff
public static final NamespacedKey LOCATION = new NamespacedKey(Slimefun.instance(), "location");
public static final NamespacedKey WORLD = new NamespacedKey(Slimefun.instance(), "world");
public static final NamespacedKey POSITION = new NamespacedKey(Slimefun.instance(), "position");
public static final NamespacedKey PITCH = new NamespacedKey(Slimefun.instance(), "pitch");
public static final NamespacedKey YAW = new NamespacedKey(Slimefun.instance(), "yaw");
Comment on lines +13 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't these in LocationSerializer?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers;

import io.github.bakedlibs.dough.nbt.tags.Tag;

public interface BinarySerialize<O, T extends Tag<?>> {

public T serialize(O obj);

public O deserialize(T obj);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers;

import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Location;

import io.github.bakedlibs.dough.blocks.BlockPosition;
import io.github.bakedlibs.dough.nbt.tags.CompoundTag;
import io.github.bakedlibs.dough.nbt.tags.IntArrayTag;
import io.github.thebusybiscuit.slimefun4.storage.backend.binary.CommonKeys;

public class LocationSerializer implements BinarySerialize<Location, CompoundTag> {

public static final LocationSerializer INSTANCE = new LocationSerializer();

private LocationSerializer() {}

@Override
public CompoundTag serialize(Location location) {
CompoundTag tag = new CompoundTag();

if (location.getWorld() != null) {
tag.put(CommonKeys.WORLD, UUIDSerializer.INSTANCE.serialize(location.getWorld().getUID()));
}

tag.putDouble(CommonKeys.POSITION, BlockPosition.getAsLong(location));

if (location.getPitch() != 0.0F) {
tag.putFloat(CommonKeys.PITCH, location.getPitch());
}
if (location.getYaw() != 0.0F) {
tag.putFloat(CommonKeys.YAW, location.getYaw());
}

return tag;
}

@Override
public Location deserialize(CompoundTag location) {
UUID worldUuid = null;
IntArrayTag worldTag = (IntArrayTag) location.get(CommonKeys.WORLD);
if (worldTag != null) {
worldUuid = UUIDSerializer.INSTANCE.deserialize(worldTag);
}

BlockPosition position = new BlockPosition(null, location.getLong(CommonKeys.POSITION).getAsLong());

double x = position.getX();
double y = position.getY();
double z = position.getZ();
float pitch = location.getFloat(CommonKeys.PITCH).orElse(0.0f);
float yaw = location.getFloat(CommonKeys.YAW).orElse(0.0f);

return new Location(
worldUuid != null ? Bukkit.getWorld(worldUuid) : null,
x, y, z, yaw, pitch
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers;

import java.util.UUID;

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;

import io.github.bakedlibs.dough.nbt.tags.IntArrayTag;

public class UUIDSerializer implements BinarySerialize<UUID, IntArrayTag> {

public static final UUIDSerializer INSTANCE = new UUIDSerializer();

private UUIDSerializer() {}

@Override
public IntArrayTag serialize(@Nonnull UUID uuid) {
Validate.notNull(uuid, "The provided UUID cannot be null");

long mostSig = uuid.getMostSignificantBits();
long leastSig = uuid.getLeastSignificantBits();
int[] ints = new int[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NBT has a long array type, I suggest we use that instead of an int array

(int) (mostSig >> 32),
(int) mostSig,
(int) (leastSig >> 32),
(int) leastSig
};

return new IntArrayTag(ints);
}

@Override
public UUID deserialize(@Nonnull IntArrayTag uuid) {
Validate.notNull(uuid, "The provided UUID cannot be null");

int[] ints = uuid.getValue();
return new UUID(
(long) ints[0] << 32L | ints[1] & 0xFFFFFFFFL,
(long) ints[2] << 32L | ints[3] & 0xFFFFFFFFL
);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ options:
backup-data: true
drop-block-creative: true

storage:
player-data: legacy

guide:
show-vanilla-recipes: true
receive-on-first-join: true
Expand Down
Loading