|
| 1 | +package me.illusion.skyblockcore.data; |
| 2 | + |
| 3 | +import com.boydti.fawe.FaweAPI; |
| 4 | +import com.boydti.fawe.object.schematic.Schematic; |
| 5 | +import com.sk89q.worldedit.Vector; |
| 6 | +import com.sk89q.worldedit.extent.clipboard.Clipboard; |
| 7 | +import com.sk89q.worldedit.extent.clipboard.ClipboardFormats; |
| 8 | +import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; |
| 9 | +import com.sk89q.worldedit.regions.CuboidRegion; |
| 10 | +import me.illusion.skyblockcore.CorePlugin; |
| 11 | +import me.illusion.skyblockcore.island.Island; |
| 12 | +import me.illusion.skyblockcore.island.IslandData; |
| 13 | +import me.illusion.skyblockcore.sql.SQLSerializer; |
| 14 | +import org.bukkit.Bukkit; |
| 15 | +import org.bukkit.Location; |
| 16 | +import org.bukkit.World; |
| 17 | +import org.bukkit.entity.Player; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.sql.PreparedStatement; |
| 22 | +import java.sql.ResultSet; |
| 23 | +import java.sql.SQLException; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | + |
| 28 | +public class SkyblockPlayer { |
| 29 | + |
| 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 | + |
| 35 | + private final CorePlugin main; |
| 36 | + |
| 37 | + private final UUID uuid; |
| 38 | + private PlayerData data; |
| 39 | + |
| 40 | + private Island island; |
| 41 | + |
| 42 | + public SkyblockPlayer(CorePlugin main, UUID uuid) { |
| 43 | + this.main = main; |
| 44 | + this.uuid = uuid; |
| 45 | + |
| 46 | + CompletableFuture.runAsync(() -> { |
| 47 | + IslandData islandData = null; |
| 48 | + if (!this.load()) { |
| 49 | + System.out.println("Inicializing Data"); |
| 50 | + data = new PlayerData(); |
| 51 | + System.out.println("Loading cache"); |
| 52 | + |
| 53 | + System.out.println("Creating island data"); |
| 54 | + islandData = new IslandData(Arrays.asList(uuid), uuid); |
| 55 | + |
| 56 | + System.out.println("Setting final data"); |
| 57 | + data.setIslandSchematic(main.getStartSchematic()); |
| 58 | + data.setMoney(0); |
| 59 | + data.setInventory(getPlayer().getInventory().getContents()); |
| 60 | + } |
| 61 | + |
| 62 | + IslandData finalIslandData = islandData; |
| 63 | + Bukkit.getScheduler().runTask(main, () -> { |
| 64 | + |
| 65 | + System.out.println("Loading island"); |
| 66 | + island = loadIsland(finalIslandData, main.getIslandConfig().getNetherSettings().getBukkitWorld(), data.getIslandSchematic()); |
| 67 | + |
| 68 | + System.out.println("Running update task"); |
| 69 | + |
| 70 | + data.setLastLocation(island.getCenter()); |
| 71 | + checkTeleport(); |
| 72 | + updateInventory(); |
| 73 | + saveIsland(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + main.getPlayerManager().register(uuid, this); |
| 78 | + } |
| 79 | + |
| 80 | + public Player getPlayer() { |
| 81 | + return Bukkit.getPlayer(uuid); |
| 82 | + } |
| 83 | + // ----- DATA LOADING ----- |
| 84 | + |
| 85 | + private boolean load() { |
| 86 | + data = (PlayerData) load(GET_SERIALIZED); |
| 87 | + |
| 88 | + if (data == null) |
| 89 | + return false; |
| 90 | + |
| 91 | + IslandData islandData = (IslandData) load(LOAD_ISLAND); |
| 92 | + |
| 93 | + getPlayer().teleport(data.getLastLocation()); |
| 94 | + |
| 95 | + for (UUID uuid : islandData.getUsers()) |
| 96 | + if (Bukkit.getPlayer(uuid) != null) { |
| 97 | + island = main.getPlayerManager().get(uuid).island; |
| 98 | + checkTeleport(); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + World world = main.getIslandConfig().getOverworldSettings().getBukkitWorld(); |
| 103 | + |
| 104 | + island = loadIsland(islandData, world, data.getIslandSchematic()); |
| 105 | + checkTeleport(); |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + private void checkTeleport() { |
| 110 | + System.out.println("Teleporting"); |
| 111 | + |
| 112 | + World world = main.getIslandConfig().getOverworldSettings().getBukkitWorld(); |
| 113 | + |
| 114 | + String worldName = world.getName(); |
| 115 | + String targetName = data.getLastLocation().getWorld().getName(); |
| 116 | + |
| 117 | + if (worldName.equalsIgnoreCase(targetName)) |
| 118 | + getPlayer().teleport(data.getLastLocation().add(island.getCenter())); |
| 119 | + else |
| 120 | + getPlayer().teleport(data.getLastLocation()); |
| 121 | + } |
| 122 | + |
| 123 | + private Island loadIsland(IslandData data, World world, File schem) { |
| 124 | + Location one = null; |
| 125 | + Location two = null; |
| 126 | + Location center = null; |
| 127 | + try { |
| 128 | + Schematic schematic = ClipboardFormat.SCHEMATIC.load(schem); |
| 129 | + Clipboard clipboard = schematic.getClipboard(); |
| 130 | + |
| 131 | + one = new Location(world, clipboard.getMinimumPoint().getBlockX(), clipboard.getMinimumPoint().getBlockY(), clipboard.getMinimumPoint().getBlockZ()); |
| 132 | + two = new Location(world, clipboard.getMaximumPoint().getBlockX(), clipboard.getMaximumPoint().getBlockY(), clipboard.getMaximumPoint().getBlockZ()); |
| 133 | + center = new Location(world, clipboard.getOrigin().getBlockX(), clipboard.getOrigin().getBlockY(), clipboard.getOrigin().getBlockZ()); |
| 134 | + |
| 135 | + schematic.paste(FaweAPI.getWorld(world.getName()), clipboard.getOrigin(), false); |
| 136 | + |
| 137 | + } catch (IOException e) { |
| 138 | + e.printStackTrace(); |
| 139 | + } |
| 140 | + |
| 141 | + return new Island(one, two, center, data); |
| 142 | + } |
| 143 | + |
| 144 | + private Object load(String sql) { |
| 145 | + try { |
| 146 | + PreparedStatement statement = main.getMySQLConnection().prepareStatement(sql); |
| 147 | + |
| 148 | + statement.setString(1, uuid.toString()); |
| 149 | + |
| 150 | + ResultSet result = statement.executeQuery(); |
| 151 | + |
| 152 | + if(!result.first()) |
| 153 | + return null; |
| 154 | + |
| 155 | + long serialized = result.getLong("id"); |
| 156 | + return SQLSerializer.deserialize(main.getMySQLConnection(), serialized); |
| 157 | + } catch (SQLException | IOException | ClassNotFoundException e) { |
| 158 | + e.printStackTrace(); |
| 159 | + } |
| 160 | + return null; |
| 161 | + } |
| 162 | + // ----- DATA SAVING ----- |
| 163 | + |
| 164 | + public void save() { |
| 165 | + data.setInventory(getPlayer().getInventory().getContents()); |
| 166 | + saveIsland(); |
| 167 | + CompletableFuture.runAsync(() -> saveObject(data, SAVE_SERIALIZED)); |
| 168 | + |
| 169 | + if (island.getData().getUsers().stream().noneMatch(uuid -> !this.uuid.equals(uuid) && Bukkit.getPlayer(uuid) == null)) |
| 170 | + island.cleanIsland(); |
| 171 | + } |
| 172 | + |
| 173 | + private void saveObject(Object object, String SQL) { |
| 174 | + |
| 175 | + try { |
| 176 | + long id = SQLSerializer.serialize(main.getMySQLConnection(), object); |
| 177 | + PreparedStatement statement = main.getMySQLConnection().prepareStatement(SQL); |
| 178 | + |
| 179 | + statement.setString(1, uuid.toString()); |
| 180 | + statement.setLong(2, id); |
| 181 | + |
| 182 | + statement.execute(); |
| 183 | + } catch (SQLException e) { |
| 184 | + e.printStackTrace(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // ----- DATA POST-LOAD ----- |
| 189 | + |
| 190 | + private void updateInventory() { |
| 191 | + if (data == null) |
| 192 | + getPlayer().getInventory().clear(); |
| 193 | + |
| 194 | + getPlayer().getInventory().setContents(data.getInventory()); |
| 195 | + } |
| 196 | + |
| 197 | + // ----- DATA PRE-SAVE ----- |
| 198 | + |
| 199 | + public void saveIsland() { |
| 200 | + CuboidRegion region = new CuboidRegion(FaweAPI.getWorld(island.getCenter().getWorld().getName()), |
| 201 | + new Vector(island.getPointOne().getBlockX(), island.getPointOne().getBlockY(), island.getPointOne().getBlockZ()), |
| 202 | + new Vector(island.getPointTwo().getBlockX(), island.getPointTwo().getBlockY(), island.getPointTwo().getBlockZ())); |
| 203 | + |
| 204 | + Schematic schematic = new Schematic(region); |
| 205 | + Clipboard board = schematic.getClipboard(); |
| 206 | + |
| 207 | + board.setOrigin(new Vector(island.getCenter().getBlockX(), island.getCenter().getBlockY(), island.getCenter().getBlockZ())); |
| 208 | + |
| 209 | + try { |
| 210 | + schematic.save(data.getIslandSchematic(), ClipboardFormats.findByFile(data.getIslandSchematic())); |
| 211 | + |
| 212 | + CompletableFuture.runAsync(() -> saveObject(island.getData(), SAVE_ISLAND)); |
| 213 | + } catch (IOException e) { |
| 214 | + e.printStackTrace(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | +} |
0 commit comments