Skip to content

Commit 89e8272

Browse files
Initial commit
0 parents  commit 89e8272

22 files changed

+831
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package me.illusion.skyblockcore;
2+
3+
import lombok.Getter;
4+
import me.illusion.skyblockcore.data.PlayerManager;
5+
import me.illusion.skyblockcore.file.IslandConfig;
6+
import me.illusion.skyblockcore.island.IslandManager;
7+
import me.illusion.skyblockcore.listener.JoinListener;
8+
import me.illusion.skyblockcore.listener.LeaveListener;
9+
import me.illusion.skyblockcore.sql.SQLUtil;
10+
import me.illusion.skyblockcore.world.EmptyWorldGenerator;
11+
import org.bukkit.Bukkit;
12+
import org.bukkit.WorldCreator;
13+
import org.bukkit.plugin.java.JavaPlugin;
14+
15+
import java.io.File;
16+
import java.sql.Connection;
17+
18+
@Getter
19+
public class CorePlugin extends JavaPlugin {
20+
21+
private Connection mySQLConnection;
22+
private IslandConfig islandConfig;
23+
private IslandManager islandManager;
24+
private PlayerManager playerManager;
25+
26+
private File startSchematic;
27+
28+
@Override
29+
public void onEnable() {
30+
startSchematic = new File(getDataFolder(), "skyblock-schematic.schematic");
31+
32+
if(!startSchematic.exists())
33+
saveResource("skyblock-schematic.schematic", false);
34+
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+
}
41+
42+
islandConfig = new IslandConfig(this);
43+
islandManager = new IslandManager();
44+
playerManager = new PlayerManager();
45+
46+
Bukkit.getScheduler().scheduleSyncDelayedTask(this, this::setupWorld, 1L);
47+
48+
Bukkit.getPluginManager().registerEvents(new JoinListener(this), this);
49+
Bukkit.getPluginManager().registerEvents(new LeaveListener(this), this);
50+
}
51+
52+
private void setupWorld() {
53+
new WorldCreator(islandConfig.getOverworldSettings().getWorld())
54+
.generator(new EmptyWorldGenerator())
55+
.generateStructures(false)
56+
.createWorld();
57+
}
58+
private boolean setupSQL()
59+
{
60+
saveDefaultConfig();
61+
62+
String host = getConfig().getString("database.host");
63+
String database = getConfig().getString("database.database");
64+
String username = getConfig().getString("database.username");
65+
String password = getConfig().getString("database.password");
66+
int port = getConfig().getInt("database.port");
67+
68+
SQLUtil sql = new SQLUtil(this, host, database, username, password, port);
69+
70+
if(!sql.openConnection())
71+
return false;
72+
73+
sql.createTable();
74+
mySQLConnection = sql.getConnection();
75+
return true;
76+
}
77+
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package me.illusion.skyblockcore.data;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.bukkit.Location;
6+
import org.bukkit.inventory.ItemStack;
7+
8+
import java.io.File;
9+
import java.util.UUID;
10+
11+
@Getter
12+
@Setter
13+
public class PlayerData {
14+
15+
private File islandSchematic = null;
16+
private double money = 0;
17+
private ItemStack[] inventory = null;
18+
private Location islandLocation;
19+
private Location lastLocation;
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package me.illusion.skyblockcore.data;
2+
3+
import org.bukkit.entity.Player;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.UUID;
8+
9+
public class PlayerManager {
10+
11+
private final Map<UUID, SkyblockPlayer> players = new HashMap<>();
12+
13+
void register(UUID uuid, SkyblockPlayer player) {
14+
players.put(uuid, player);
15+
}
16+
17+
public void unregister(UUID uuid) {
18+
players.remove(uuid);
19+
}
20+
21+
public SkyblockPlayer get(UUID uuid) {
22+
return players.get(uuid);
23+
}
24+
25+
public SkyblockPlayer get(Player player) {
26+
return get(player.getUniqueId());
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package me.illusion.skyblockcore.file;
2+
3+
import lombok.Getter;
4+
import me.illusion.skyblockcore.file.path.ExtendedYMLBase;
5+
import me.illusion.skyblockcore.file.settings.WorldSettings;
6+
import me.illusion.utilities.storage.YMLBase;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
9+
@Getter
10+
public class IslandConfig extends ExtendedYMLBase {
11+
12+
private final WorldSettings overworldSettings;
13+
private final WorldSettings netherSettings;
14+
15+
public IslandConfig(JavaPlugin plugin) {
16+
super(plugin, "island-config.yml");
17+
18+
overworldSettings = load(new WorldSettings(), getConfiguration().getConfigurationSection("island.overworld"));
19+
netherSettings = load(new WorldSettings(), getConfiguration().getConfigurationSection("island.nether"));
20+
}
21+
}

0 commit comments

Comments
 (0)