Skip to content

Commit a502533

Browse files
Island serialization, SQL re-organizing
1 parent 89e8272 commit a502533

10 files changed

+215
-57
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Project exclude paths
2+
/.gradle/
3+
/build/
4+
/build/classes/java/main/

src/main/java/me/illusion/skyblockcore/CorePlugin.java

+21-17
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
import me.illusion.skyblockcore.data.PlayerManager;
55
import me.illusion.skyblockcore.file.IslandConfig;
66
import me.illusion.skyblockcore.island.IslandManager;
7+
import me.illusion.skyblockcore.island.grid.IslandGrid;
8+
import me.illusion.skyblockcore.island.world.EmptyWorldGenerator;
79
import me.illusion.skyblockcore.listener.JoinListener;
810
import me.illusion.skyblockcore.listener.LeaveListener;
911
import me.illusion.skyblockcore.sql.SQLUtil;
10-
import me.illusion.skyblockcore.world.EmptyWorldGenerator;
1112
import org.bukkit.Bukkit;
1213
import org.bukkit.WorldCreator;
1314
import org.bukkit.plugin.java.JavaPlugin;
1415

1516
import java.io.File;
1617
import java.sql.Connection;
18+
import java.util.concurrent.CompletableFuture;
1719

1820
@Getter
1921
public class CorePlugin extends JavaPlugin {
@@ -22,25 +24,22 @@ public class CorePlugin extends JavaPlugin {
2224
private IslandConfig islandConfig;
2325
private IslandManager islandManager;
2426
private PlayerManager playerManager;
27+
private IslandGrid grid;
2528

2629
private File startSchematic;
2730

2831
@Override
2932
public void onEnable() {
3033
startSchematic = new File(getDataFolder(), "skyblock-schematic.schematic");
3134

32-
if(!startSchematic.exists())
35+
if (!startSchematic.exists())
3336
saveResource("skyblock-schematic.schematic", false);
3437

35-
if(!setupSQL()) {
36-
getLogger().warning("Could not load SQL Database.");
37-
getLogger().warning("This plugin requires a valid SQL database to work.");
38-
getPluginLoader().disablePlugin(this);
39-
return;
40-
}
38+
setupSQL();
4139

4240
islandConfig = new IslandConfig(this);
43-
islandManager = new IslandManager();
41+
grid = new IslandGrid(5, 5);
42+
islandManager = new IslandManager(this);
4443
playerManager = new PlayerManager();
4544

4645
Bukkit.getScheduler().scheduleSyncDelayedTask(this, this::setupWorld, 1L);
@@ -55,8 +54,8 @@ private void setupWorld() {
5554
.generateStructures(false)
5655
.createWorld();
5756
}
58-
private boolean setupSQL()
59-
{
57+
58+
private void setupSQL() {
6059
saveDefaultConfig();
6160

6261
String host = getConfig().getString("database.host");
@@ -65,14 +64,19 @@ private boolean setupSQL()
6564
String password = getConfig().getString("database.password");
6665
int port = getConfig().getInt("database.port");
6766

68-
SQLUtil sql = new SQLUtil(this, host, database, username, password, port);
67+
CompletableFuture.runAsync(() -> {
68+
SQLUtil sql = new SQLUtil(this, host, database, username, password, port);
6969

70-
if(!sql.openConnection())
71-
return false;
70+
if (!sql.openConnection()) {
71+
getLogger().warning("Could not load SQL Database.");
72+
getLogger().warning("This plugin requires a valid SQL database to work.");
73+
getPluginLoader().disablePlugin(this);
74+
return;
75+
}
7276

73-
sql.createTable();
74-
mySQLConnection = sql.getConnection();
75-
return true;
77+
sql.createTable();
78+
mySQLConnection = sql.getConnection();
79+
});
7680
}
7781

7882
}

src/main/java/me/illusion/skyblockcore/data/PlayerData.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
import lombok.Getter;
44
import lombok.Setter;
5-
import org.bukkit.Location;
6-
import org.bukkit.inventory.ItemStack;
5+
import me.illusion.skyblockcore.sql.serialized.SerializedItemStackArray;
6+
import me.illusion.skyblockcore.sql.serialized.SerializedLocation;
77

88
import java.io.File;
9-
import java.util.UUID;
9+
import java.io.Serializable;
1010

1111
@Getter
1212
@Setter
13-
public class PlayerData {
13+
public class PlayerData implements Serializable {
1414

1515
private File islandSchematic = null;
1616
private double money = 0;
17-
private ItemStack[] inventory = null;
18-
private Location islandLocation;
19-
private Location lastLocation;
17+
private SerializedItemStackArray inventory = new SerializedItemStackArray();
18+
private SerializedLocation islandLocation = new SerializedLocation();
19+
private SerializedLocation lastLocation = new SerializedLocation();
2020

2121
}

src/main/java/me/illusion/skyblockcore/data/SkyblockPlayer.java

+54-21
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
import com.boydti.fawe.FaweAPI;
44
import com.boydti.fawe.object.schematic.Schematic;
5+
import com.google.common.io.Files;
56
import com.sk89q.worldedit.Vector;
67
import com.sk89q.worldedit.extent.clipboard.Clipboard;
78
import com.sk89q.worldedit.extent.clipboard.ClipboardFormats;
89
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
910
import com.sk89q.worldedit.regions.CuboidRegion;
11+
import lombok.AccessLevel;
12+
import lombok.Getter;
1013
import me.illusion.skyblockcore.CorePlugin;
1114
import me.illusion.skyblockcore.island.Island;
1215
import me.illusion.skyblockcore.island.IslandData;
16+
import me.illusion.skyblockcore.island.grid.GridCell;
1317
import me.illusion.skyblockcore.sql.SQLSerializer;
1418
import org.bukkit.Bukkit;
1519
import org.bukkit.Location;
@@ -21,17 +25,16 @@
2125
import java.sql.PreparedStatement;
2226
import java.sql.ResultSet;
2327
import java.sql.SQLException;
24-
import java.util.Arrays;
28+
import java.util.ArrayList;
2529
import java.util.UUID;
2630
import java.util.concurrent.CompletableFuture;
2731

28-
public class SkyblockPlayer {
32+
import static me.illusion.skyblockcore.sql.SQLOperation.*;
2933

30-
private static final String SAVE_SERIALIZED = "INSERT INTO uuid_data(uuid, id) VALUES (?, ?)";
31-
private static final String GET_SERIALIZED = "SELECT id FROM uuid_data WHERE uuid = ?";
32-
private static final String LOAD_ISLAND = "SELECT id FROM island_data WHERE uuid = ?";
33-
private static final String SAVE_ISLAND = "INSERT INTO island_data(uuid, id) VALUES (?, ?)";
34+
@Getter
35+
public class SkyblockPlayer {
3436

37+
@Getter(AccessLevel.NONE)
3538
private final CorePlugin main;
3639

3740
private final UUID uuid;
@@ -44,30 +47,41 @@ public SkyblockPlayer(CorePlugin main, UUID uuid) {
4447
this.uuid = uuid;
4548

4649
CompletableFuture.runAsync(() -> {
50+
File target = null;
4751
IslandData islandData = null;
4852
if (!this.load()) {
4953
System.out.println("Inicializing Data");
5054
data = new PlayerData();
5155
System.out.println("Loading cache");
56+
target = new File(main.getDataFolder() + File.separator + "cache", uuid.toString() + ".schematic");
57+
try {
58+
target.getParentFile().mkdirs();
59+
target.createNewFile();
60+
Files.copy(main.getStartSchematic(), target);
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
5264

5365
System.out.println("Creating island data");
54-
islandData = new IslandData(Arrays.asList(uuid), uuid);
66+
islandData = new IslandData(null, uuid.toString(), uuid, new ArrayList<>());
5567

5668
System.out.println("Setting final data");
57-
data.setIslandSchematic(main.getStartSchematic());
69+
data.setIslandSchematic(target);
5870
data.setMoney(0);
59-
data.setInventory(getPlayer().getInventory().getContents());
71+
data.getInventory().updateArray(getPlayer().getInventory().getContents());
6072
}
6173

6274
IslandData finalIslandData = islandData;
75+
File finalTarget = target;
6376
Bukkit.getScheduler().runTask(main, () -> {
6477

6578
System.out.println("Loading island");
66-
island = loadIsland(finalIslandData, main.getIslandConfig().getNetherSettings().getBukkitWorld(), data.getIslandSchematic());
79+
island = loadIsland(finalIslandData, main.getIslandConfig().getOverworldSettings().getBukkitWorld(), finalTarget);
80+
finalIslandData.setIsland(island);
6781

6882
System.out.println("Running update task");
6983

70-
data.setLastLocation(island.getCenter());
84+
data.getLastLocation().update(island.getCenter());
7185
checkTeleport();
7286
updateInventory();
7387
saveIsland();
@@ -90,7 +104,7 @@ private boolean load() {
90104

91105
IslandData islandData = (IslandData) load(LOAD_ISLAND);
92106

93-
getPlayer().teleport(data.getLastLocation());
107+
getPlayer().teleport(data.getLastLocation().getLocation());
94108

95109
for (UUID uuid : islandData.getUsers())
96110
if (Bukkit.getPlayer(uuid) != null) {
@@ -112,18 +126,22 @@ private void checkTeleport() {
112126
World world = main.getIslandConfig().getOverworldSettings().getBukkitWorld();
113127

114128
String worldName = world.getName();
115-
String targetName = data.getLastLocation().getWorld().getName();
129+
String targetName = data.getLastLocation().getLocation().getWorld().getName();
116130

117131
if (worldName.equalsIgnoreCase(targetName))
118-
getPlayer().teleport(data.getLastLocation().add(island.getCenter()));
132+
getPlayer().teleport(data.getLastLocation().getLocation().add(island.getCenter()));
119133
else
120-
getPlayer().teleport(data.getLastLocation());
134+
getPlayer().teleport(data.getLastLocation().getLocation());
121135
}
122136

123137
private Island loadIsland(IslandData data, World world, File schem) {
124138
Location one = null;
125139
Location two = null;
126140
Location center = null;
141+
GridCell cell = main.getGrid().getFirstCell();
142+
143+
cell.setOccupied(true);
144+
127145
try {
128146
Schematic schematic = ClipboardFormat.SCHEMATIC.load(schem);
129147
Clipboard clipboard = schematic.getClipboard();
@@ -132,13 +150,26 @@ private Island loadIsland(IslandData data, World world, File schem) {
132150
two = new Location(world, clipboard.getMaximumPoint().getBlockX(), clipboard.getMaximumPoint().getBlockY(), clipboard.getMaximumPoint().getBlockZ());
133151
center = new Location(world, clipboard.getOrigin().getBlockX(), clipboard.getOrigin().getBlockY(), clipboard.getOrigin().getBlockZ());
134152

135-
schematic.paste(FaweAPI.getWorld(world.getName()), clipboard.getOrigin(), false);
153+
int distance = main.getIslandConfig().getOverworldSettings().getDistance();
154+
center = center.add(cell.getXPos() * distance, 0, cell.getYPos() * distance);
155+
one = one.add(cell.getXPos() * distance, 0, cell.getYPos() * distance);
156+
two = two.add(cell.getXPos() * distance, 0, cell.getYPos() * distance);
136157

137-
} catch (IOException e) {
158+
Bukkit.getLogger().info(String.format("Pasting island at %s %s %s (%s)", center.getX(), center.getY(), center.getZ(), center.getWorld().getName()));
159+
schematic.paste(
160+
FaweAPI.getWorld(
161+
world.getName()),
162+
clipboard.getOrigin(),
163+
false);
164+
165+
166+
} catch (Exception e) {
138167
e.printStackTrace();
139168
}
140169

141-
return new Island(one, two, center, data);
170+
Bukkit.broadcastMessage("post-paste");
171+
172+
return new Island(one, two, center, data, cell);
142173
}
143174

144175
private Object load(String sql) {
@@ -162,12 +193,14 @@ private Object load(String sql) {
162193
// ----- DATA SAVING -----
163194

164195
public void save() {
165-
data.setInventory(getPlayer().getInventory().getContents());
196+
data.getInventory().updateArray(getPlayer().getInventory().getContents());
166197
saveIsland();
167198
CompletableFuture.runAsync(() -> saveObject(data, SAVE_SERIALIZED));
168199

169200
if (island.getData().getUsers().stream().noneMatch(uuid -> !this.uuid.equals(uuid) && Bukkit.getPlayer(uuid) == null))
170201
island.cleanIsland();
202+
203+
data.getIslandSchematic().delete();
171204
}
172205

173206
private void saveObject(Object object, String SQL) {
@@ -179,7 +212,7 @@ private void saveObject(Object object, String SQL) {
179212
statement.setString(1, uuid.toString());
180213
statement.setLong(2, id);
181214

182-
statement.execute();
215+
statement.executeUpdate();
183216
} catch (SQLException e) {
184217
e.printStackTrace();
185218
}
@@ -191,7 +224,7 @@ private void updateInventory() {
191224
if (data == null)
192225
getPlayer().getInventory().clear();
193226

194-
getPlayer().getInventory().setContents(data.getInventory());
227+
getPlayer().getInventory().setContents(data.getInventory().getArray());
195228
}
196229

197230
// ----- DATA PRE-SAVE -----

src/main/java/me/illusion/skyblockcore/listener/LeaveListener.java

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public LeaveListener(CorePlugin main) {
1616
@EventHandler
1717
private void onLeave(PlayerQuitEvent e) {
1818
main.getPlayerManager().get(e.getPlayer()).save();
19+
e.getPlayer().getInventory().clear();
1920
main.getPlayerManager().unregister(e.getPlayer().getUniqueId());
2021
}
2122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package me.illusion.skyblockcore.sql;
2+
3+
public class SQLOperation {
4+
5+
public static final String SAVE_SERIALIZED = "INSERT INTO uuid_data(uuid, id) VALUES (?, ?)";
6+
public static final String GET_SERIALIZED = "SELECT id FROM uuid_data WHERE uuid = ?";
7+
public static final String LOAD_ISLAND = "SELECT id FROM island_data WHERE uuid = ?";
8+
public static final String SAVE_ISLAND = "INSERT INTO island_data(uuid, id) VALUES (?, ?)";
9+
10+
public static final String CREATE_UUID_TABLE = "CREATE TABLE IF NOT EXISTS uuid_data (uuid VARCHAR(36), id LONG);";
11+
public static final String CREATE_ISLAND_TABLE = "CREATE TABLE IF NOT EXISTS island_data (uuid VARCHAR(36), id LONG);";
12+
public static final String CREATE_DATA_TABLE = "CREATE TABLE IF NOT EXISTS serialized_java_objects (serialized_id int(11) NOT NULL auto_increment, object_name varchar(80) default NULL, serialized_object mediumblob, PRIMARY KEY (serialized_id)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
13+
14+
15+
}

src/main/java/me/illusion/skyblockcore/sql/SQLSerializer.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package me.illusion.skyblockcore.sql;
22

3-
import org.bukkit.event.EventHandler;
3+
import org.bukkit.Bukkit;
44

55
import java.io.ByteArrayInputStream;
66
import java.io.IOException;
77
import java.io.ObjectInputStream;
8-
import java.sql.Connection;
9-
import java.sql.PreparedStatement;
10-
import java.sql.ResultSet;
11-
import java.sql.SQLException;
8+
import java.sql.*;
129

1310
public class SQLSerializer {
1411

@@ -19,9 +16,10 @@ public static long serialize(Connection connection,
1916
Object objectToSerialize) throws SQLException {
2017

2118
PreparedStatement pstmt = connection
22-
.prepareStatement(SQL_SERIALIZE_OBJECT);
19+
.prepareStatement(SQL_SERIALIZE_OBJECT, Statement.RETURN_GENERATED_KEYS);
2320

2421
// just setting the class name
22+
System.out.println(objectToSerialize.getClass().getName().length());
2523
pstmt.setString(1, objectToSerialize.getClass().getName());
2624
pstmt.setObject(2, objectToSerialize);
2725
pstmt.executeUpdate();
@@ -32,8 +30,7 @@ public static long serialize(Connection connection,
3230
}
3331
rs.close();
3432
pstmt.close();
35-
System.out.println("Java object serialized to database. Object: "
36-
+ objectToSerialize);
33+
Bukkit.getLogger().info("Serialized object with id " + serialized_id);
3734
return serialized_id;
3835
}
3936

@@ -55,6 +52,7 @@ public static Object deserialize(Connection connection,
5552

5653
// Object object = rs.getObject(1);
5754

55+
Bukkit.getLogger().info("Deserializing object with id " + serialized_id);
5856
byte[] buf = rs.getBytes(1);
5957
ObjectInputStream objectIn = null;
6058
if (buf != null)

0 commit comments

Comments
 (0)