Skip to content

Commit

Permalink
Changed serialization method to NBT
Browse files Browse the repository at this point in the history
- Is waaaaay more portable
- Cleaner code
- Fixes #6
  • Loading branch information
BrainStone committed Apr 24, 2017
1 parent 0450408 commit df721d3
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 212 deletions.
52 changes: 13 additions & 39 deletions src/main/java/world/jnc/invsync/DataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import java.util.Optional;
import java.util.UUID;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;

import lombok.Cleanup;
import lombok.NonNull;
import world.jnc.invsync.util.PlayerData;
import world.jnc.invsync.util.database.DatabaseConnection;
import world.jnc.invsync.util.database.H2DatabaseConnection;
import world.jnc.invsync.util.database.MysqlDatabaseConnection;
Expand All @@ -27,10 +23,7 @@ public class DataSource {
private final String tableInventories;
private final String tableInventoriesColumnUUID;
private final String tableInventoriesColumnActive;
private final String tableInventoriesColumnGamemode;
private final String tableInventoriesColumnExperience;
private final String tableInventoriesColumnInventory;
private final String tableInventoriesColumnEnderchest;
private final String tableInventoriesColumnData;

@NonNull
private PreparedStatement insertInventory;
Expand Down Expand Up @@ -71,17 +64,14 @@ public DataSource() throws SQLException {

tableInventories = getTableName("inventories");
tableInventoriesColumnUUID = "UUID";
tableInventoriesColumnGamemode = "Gamemode";
tableInventoriesColumnExperience = "Experience";
tableInventoriesColumnActive = "Active";
tableInventoriesColumnInventory = "Inventory";
tableInventoriesColumnEnderchest = "Enderchest";
tableInventoriesColumnData = "Data";

prepareTable();
prepareStatements();
}

public void saveInventory(Player player, PlayerData data) {
public void saveInventory(Player player, byte[] data) {
if (!connection.verifyConnection()) {
prepareStatements();
}
Expand All @@ -92,10 +82,7 @@ public void saveInventory(Player player, PlayerData data) {
InventorySync.getLogger().debug("Saving inventory for player " + playerName);

insertInventory.setBytes(1, getBytesFromUUID(player.getUniqueId()));
insertInventory.setString(2, data.getGameMode().getId());
insertInventory.setInt(3, data.getExperience());
insertInventory.setBytes(4, data.getInventory());
insertInventory.setBytes(5, data.getEnderChest());
insertInventory.setBytes(2, data);

insertInventory.executeUpdate();
insertInventory.clearParameters();
Expand All @@ -104,7 +91,7 @@ public void saveInventory(Player player, PlayerData data) {
}
}

public Optional<PlayerData> loadInventory(Player player) {
public Optional<byte[]> loadInventory(Player player) {
if (!connection.verifyConnection()) {
prepareStatements();
}
Expand All @@ -121,14 +108,7 @@ public Optional<PlayerData> loadInventory(Player player) {
loadInventory.clearParameters();

if (result.next())
// TODO Transform the string from gamemode into an actual
// gamemode
return Optional.of(PlayerData.of(
Sponge.getRegistry().getType(GameMode.class, result.getString(tableInventoriesColumnGamemode))
.orElse(GameModes.SURVIVAL),
result.getInt(tableInventoriesColumnExperience),
result.getBytes(tableInventoriesColumnInventory),
result.getBytes(tableInventoriesColumnEnderchest)));
return Optional.of(result.getBytes(tableInventoriesColumnData));
else
return Optional.empty();
} catch (SQLException e) {
Expand Down Expand Up @@ -214,12 +194,9 @@ private void prepareTable() {

createTable.append("CREATE TABLE IF NOT EXISTS ").append(tableInventories).append(" (")
.append(tableInventoriesColumnUUID).append(" BINARY(16) NOT NULL, ")
.append(tableInventoriesColumnActive).append(" BOOL NOT NULL, ")
.append(tableInventoriesColumnGamemode).append(" TINYTEXT NOT NULL, ")
.append(tableInventoriesColumnExperience).append(" INT NOT NULL, ")
.append(tableInventoriesColumnInventory).append(" MEDIUMBLOB NOT NULL, ")
.append(tableInventoriesColumnEnderchest).append(" MEDIUMBLOB NOT NULL, PRIMARY KEY ("
+ tableInventoriesColumnUUID + ")) DEFAULT CHARSET=utf8");
.append(tableInventoriesColumnActive).append(" BOOL NOT NULL, ").append(tableInventoriesColumnData)
.append(" MEDIUMBLOB NOT NULL, PRIMARY KEY (").append(tableInventoriesColumnUUID)
.append(")) DEFAULT CHARSET=utf8");

connection.executeStatement(createTable.toString());

Expand All @@ -238,13 +215,10 @@ private void prepareStatements() {

insertInventoryStr.append("REPLACE INTO ").append(tableInventories).append(" (")
.append(tableInventoriesColumnUUID).append(", ").append(tableInventoriesColumnActive).append(", ")
.append(tableInventoriesColumnGamemode).append(", ").append(tableInventoriesColumnExperience)
.append(", ").append(tableInventoriesColumnInventory).append(", ")
.append(tableInventoriesColumnEnderchest).append(") VALUES (?, FALSE, ?, ?, ?, ?)");
getInventoryStr.append("SELECT ").append(tableInventoriesColumnGamemode).append(", ")
.append(tableInventoriesColumnExperience).append(", ").append(tableInventoriesColumnInventory)
.append(", ").append(tableInventoriesColumnEnderchest).append(" FROM ").append(tableInventories)
.append(" WHERE ").append(tableInventoriesColumnUUID).append(" = ? LIMIT 1");
.append(tableInventoriesColumnData).append(") VALUES (?, FALSE, ?)");
getInventoryStr.append("SELECT ").append(tableInventoriesColumnData).append(" FROM ")
.append(tableInventories).append(" WHERE ").append(tableInventoriesColumnUUID)
.append(" = ? LIMIT 1");
setActiveStr.append("UPDATE ").append(tableInventories).append(" SET ").append(tableInventoriesColumnActive)
.append(" = TRUE WHERE ").append(tableInventoriesColumnUUID).append(" = ? LIMIT 1");
isActiveStr.append("SELECT ").append(tableInventoriesColumnActive).append(" FROM ").append(tableInventories)
Expand Down
30 changes: 5 additions & 25 deletions src/main/java/world/jnc/invsync/PlayerEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
import java.util.zip.DataFormatException;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.item.inventory.ChangeInventoryEvent;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.scheduler.Task;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import world.jnc.invsync.util.InventorySerializer;
import world.jnc.invsync.util.PlayerData;

@RequiredArgsConstructor
public class PlayerEvents implements AutoCloseable {
Expand All @@ -45,7 +42,7 @@ public void onPlayerJoin(ClientConnectionEvent.Join event)
}

@Listener
public void onPlayerLeave(ClientConnectionEvent.Disconnect event) throws IOException, DataFormatException {
public void onPlayerLeave(ClientConnectionEvent.Disconnect event) throws IOException {
@NonNull
Player player = event.getTargetEntity();
UUID uuid = player.getUniqueId();
Expand Down Expand Up @@ -82,36 +79,19 @@ public void close() throws IOException, DataFormatException {
}

private void loadPlayer(@NonNull Player player) throws ClassNotFoundException, IOException, DataFormatException {
@NonNull
Inventory inventory = player.getInventory();
@NonNull
Inventory enderInventory = player.getEnderChestInventory();
Optional<PlayerData> result = dataSource.loadInventory(player);
Optional<byte[]> result = dataSource.loadInventory(player);

if (result.isPresent()) {
PlayerData playerData = result.get();

player.offer(Keys.GAME_MODE, playerData.getGameMode());
player.offer(Keys.TOTAL_EXPERIENCE, playerData.getExperience());
InventorySerializer.deserializeInventory(playerData.getInventory(), inventory);
InventorySerializer.deserializeInventory(playerData.getEnderChest(), enderInventory);
InventorySerializer.deserializePlayer(player, result.get());
} else {
savePlayer(player);
}

dataSource.setActive(player);
}

private void savePlayer(@NonNull Player player) throws IOException, DataFormatException {
@NonNull
Inventory inventory = player.getInventory();
@NonNull
Inventory enderInventory = player.getEnderChestInventory();
PlayerData data = PlayerData.of(player.get(Keys.GAME_MODE).get(), player.get(Keys.TOTAL_EXPERIENCE).get(),
InventorySerializer.serializeInventory(inventory),
InventorySerializer.serializeInventory(enderInventory));

dataSource.saveInventory(player, data);
private void savePlayer(@NonNull Player player) throws IOException {
dataSource.saveInventory(player, InventorySerializer.serializePlayer(player));
}

private class WaitingForOtherServerToFinish implements Consumer<Task> {
Expand Down
71 changes: 0 additions & 71 deletions src/main/java/world/jnc/invsync/util/CompressionUtils.java

This file was deleted.

Loading

0 comments on commit df721d3

Please sign in to comment.