mirror of
https://github.com/songoda/EpicFurnaces.git
synced 2024-12-03 15:23:22 +01:00
Added SQLite support + conversion.
This commit is contained in:
parent
ae13452a20
commit
e8b96653fb
@ -5,6 +5,9 @@ import com.songoda.core.SongodaPlugin;
|
|||||||
import com.songoda.core.commands.CommandManager;
|
import com.songoda.core.commands.CommandManager;
|
||||||
import com.songoda.core.compatibility.CompatibleMaterial;
|
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||||
import com.songoda.core.configuration.Config;
|
import com.songoda.core.configuration.Config;
|
||||||
|
import com.songoda.core.database.DataMigrationManager;
|
||||||
|
import com.songoda.core.database.DatabaseConnector;
|
||||||
|
import com.songoda.core.database.SQLiteConnector;
|
||||||
import com.songoda.core.gui.GuiManager;
|
import com.songoda.core.gui.GuiManager;
|
||||||
import com.songoda.core.hooks.EconomyManager;
|
import com.songoda.core.hooks.EconomyManager;
|
||||||
import com.songoda.core.hooks.HologramManager;
|
import com.songoda.core.hooks.HologramManager;
|
||||||
@ -19,6 +22,8 @@ import com.songoda.epicfurnaces.commands.CommandReload;
|
|||||||
import com.songoda.epicfurnaces.commands.CommandRemote;
|
import com.songoda.epicfurnaces.commands.CommandRemote;
|
||||||
import com.songoda.epicfurnaces.commands.CommandSettings;
|
import com.songoda.epicfurnaces.commands.CommandSettings;
|
||||||
import com.songoda.epicfurnaces.compatibility.FabledSkyBlockLoader;
|
import com.songoda.epicfurnaces.compatibility.FabledSkyBlockLoader;
|
||||||
|
import com.songoda.epicfurnaces.database.DataManager;
|
||||||
|
import com.songoda.epicfurnaces.database.migrations._1_InitialMigration;
|
||||||
import com.songoda.epicfurnaces.furnace.Furnace;
|
import com.songoda.epicfurnaces.furnace.Furnace;
|
||||||
import com.songoda.epicfurnaces.furnace.FurnaceBuilder;
|
import com.songoda.epicfurnaces.furnace.FurnaceBuilder;
|
||||||
import com.songoda.epicfurnaces.furnace.FurnaceManager;
|
import com.songoda.epicfurnaces.furnace.FurnaceManager;
|
||||||
@ -37,6 +42,7 @@ import com.songoda.epicfurnaces.tasks.FurnaceTask;
|
|||||||
import com.songoda.epicfurnaces.tasks.HologramTask;
|
import com.songoda.epicfurnaces.tasks.HologramTask;
|
||||||
import com.songoda.epicfurnaces.utils.Methods;
|
import com.songoda.epicfurnaces.utils.Methods;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.block.BlockState;
|
import org.bukkit.block.BlockState;
|
||||||
@ -72,7 +78,8 @@ public class EpicFurnaces extends SongodaPlugin {
|
|||||||
|
|
||||||
private BlacklistHandler blacklistHandler;
|
private BlacklistHandler blacklistHandler;
|
||||||
|
|
||||||
private Storage storage;
|
private DatabaseConnector databaseConnector;
|
||||||
|
private DataManager dataManager;
|
||||||
|
|
||||||
public static EpicFurnaces getInstance() {
|
public static EpicFurnaces getInstance() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
@ -85,8 +92,7 @@ public class EpicFurnaces extends SongodaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPluginDisable() {
|
public void onPluginDisable() {
|
||||||
saveToFile();
|
this.databaseConnector.closeConnection();
|
||||||
this.storage.closeConnection();
|
|
||||||
HologramManager.removeAllHolograms();
|
HologramManager.removeAllHolograms();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,43 +136,38 @@ public class EpicFurnaces extends SongodaPlugin {
|
|||||||
this.boostManager = new BoostManager();
|
this.boostManager = new BoostManager();
|
||||||
this.blacklistHandler = new BlacklistHandler();
|
this.blacklistHandler = new BlacklistHandler();
|
||||||
|
|
||||||
// Load from file
|
// Database stuff.
|
||||||
dataFile.load();
|
this.databaseConnector = new SQLiteConnector(this);
|
||||||
this.storage = new StorageYaml(this);
|
this.getLogger().info("Data handler connected using SQLite.");
|
||||||
|
|
||||||
setupRecipies();
|
this.dataManager = new DataManager(this.databaseConnector, this);
|
||||||
|
DataMigrationManager dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager,
|
||||||
|
new _1_InitialMigration());
|
||||||
|
dataMigrationManager.runMigrations();
|
||||||
|
|
||||||
// Start Tasks
|
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
|
||||||
FurnaceTask.startTask(this);
|
// Legacy data! Yay!
|
||||||
HologramTask.startTask(this);
|
File folder = getDataFolder();
|
||||||
|
File dataFile = new File(folder, "data.yml");
|
||||||
|
|
||||||
// Register Listeners
|
boolean converted = false;
|
||||||
guiManager.init();
|
if (dataFile.exists()) {
|
||||||
PluginManager pluginManager = Bukkit.getPluginManager();
|
converted = true;
|
||||||
pluginManager.registerEvents(new BlockListeners(this), this);
|
Storage storage = new StorageYaml(this);
|
||||||
pluginManager.registerEvents(new FurnaceListeners(this), this);
|
|
||||||
pluginManager.registerEvents(new InteractListeners(this, guiManager), this);
|
|
||||||
pluginManager.registerEvents(new InventoryListeners(this), this);
|
|
||||||
pluginManager.registerEvents(new EntityListeners(this), this);
|
|
||||||
|
|
||||||
// Start auto save
|
|
||||||
int saveInterval = Settings.AUTOSAVE.getInt() * 60 * 20;
|
|
||||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, saveInterval, saveInterval);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataLoad() {
|
|
||||||
/*
|
|
||||||
* Register furnaces into FurnaceManger from configuration
|
|
||||||
*/
|
|
||||||
if (storage.containsGroup("charged")) {
|
if (storage.containsGroup("charged")) {
|
||||||
|
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.RED +
|
||||||
|
"Conversion process starting. Do NOT turn off your server." +
|
||||||
|
"EpicFurnaces hasn't fully loaded yet, so make sure users don't" +
|
||||||
|
"interact with the plugin until the conversion process is complete.");
|
||||||
|
|
||||||
|
List<Furnace> furnaces = new ArrayList<>();
|
||||||
for (StorageRow row : storage.getRowsByGroup("charged")) {
|
for (StorageRow row : storage.getRowsByGroup("charged")) {
|
||||||
Location location = Methods.unserializeLocation(row.getKey());
|
Location location = Methods.unserializeLocation(row.getKey());
|
||||||
if (location == null) continue;
|
if (location == null) continue;
|
||||||
|
|
||||||
if (row.get("level").asInt() == 0) continue;
|
if (row.get("level").asInt() == 0) continue;
|
||||||
|
|
||||||
String placedByStr = row.get("placedBy").asString();
|
String placedByStr = row.get("placedby").asString();
|
||||||
UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr);
|
UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr);
|
||||||
|
|
||||||
List<String> list = row.get("accesslist").asStringList();
|
List<String> list = row.get("accesslist").asStringList();
|
||||||
@ -186,43 +187,71 @@ public class EpicFurnaces extends SongodaPlugin {
|
|||||||
toLevel.put(CompatibleMaterial.getMaterial(split[0]), Integer.parseInt(split[1]));
|
toLevel.put(CompatibleMaterial.getMaterial(split[0]), Integer.parseInt(split[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
Furnace furnace = new FurnaceBuilder(location)
|
furnaces.add(new FurnaceBuilder(location)
|
||||||
.setLevel(levelManager.getLevel(row.get("level").asInt()))
|
.setLevel(levelManager.getLevel(row.get("level").asInt()))
|
||||||
.setNickname(row.get("nickname").asString())
|
.setNickname(row.get("nickname").asString())
|
||||||
.setUses(row.get("uses").asInt())
|
.setUses(row.get("uses").asInt())
|
||||||
.setToLevel(toLevel)
|
.setToLevel(toLevel)
|
||||||
.setAccessList(usableList)
|
.setAccessList(usableList)
|
||||||
.setPlacedBy(placedBy).build();
|
.setPlacedBy(placedBy).build());
|
||||||
|
|
||||||
furnaceManager.addFurnace(furnace);
|
|
||||||
}
|
}
|
||||||
|
dataManager.createFurnaces(furnaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adding in Boosts
|
// Adding in Boosts
|
||||||
if (storage.containsGroup("boosts")) {
|
if (storage.containsGroup("boosts")) {
|
||||||
for (StorageRow row : storage.getRowsByGroup("boosts")) {
|
for (StorageRow row : storage.getRowsByGroup("boosts")) {
|
||||||
if (row.getItems().get("uuid").asObject() != null)
|
if (row.get("uuid").asObject() == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
BoostData boostData = new BoostData(
|
dataManager.createBoost(new BoostData(
|
||||||
row.get("amount").asInt(),
|
row.get("amount").asInt(),
|
||||||
Long.parseLong(row.getKey()),
|
Long.parseLong(row.getKey()),
|
||||||
UUID.fromString(row.get("uuid").asString()));
|
UUID.fromString(row.get("uuid").asString())));
|
||||||
|
|
||||||
this.boostManager.addBoostToPlayer(boostData);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dataFile.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
final boolean finalConverted = converted;
|
||||||
|
dataManager.queueAsync(() -> {
|
||||||
|
if (finalConverted) {
|
||||||
|
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dataManager.getFurnaces((furnaces) -> {
|
||||||
|
this.furnaceManager.addFurnaces(furnaces.values());
|
||||||
|
this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts));
|
||||||
|
});
|
||||||
|
}, "create");
|
||||||
|
});
|
||||||
|
|
||||||
|
setupRecipies();
|
||||||
|
|
||||||
|
// Start Tasks
|
||||||
|
FurnaceTask.startTask(this);
|
||||||
|
HologramTask.startTask(this);
|
||||||
|
|
||||||
|
// Register Listeners
|
||||||
|
guiManager.init();
|
||||||
|
PluginManager pluginManager = Bukkit.getPluginManager();
|
||||||
|
pluginManager.registerEvents(new BlockListeners(this), this);
|
||||||
|
pluginManager.registerEvents(new FurnaceListeners(this), this);
|
||||||
|
pluginManager.registerEvents(new InteractListeners(this, guiManager), this);
|
||||||
|
pluginManager.registerEvents(new InventoryListeners(this), this);
|
||||||
|
pluginManager.registerEvents(new EntityListeners(this), this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDataLoad() {
|
||||||
// Register Hologram Plugin
|
// Register Hologram Plugin
|
||||||
|
|
||||||
if (Settings.HOLOGRAMS.getBoolean()) {
|
if (Settings.HOLOGRAMS.getBoolean()) {
|
||||||
for (Furnace furnace : getFurnaceManager().getFurnaces().values()) {
|
for (Furnace furnace : getFurnaceManager().getFurnaces().values()) {
|
||||||
if (furnace.getLocation() == null || furnace.getLocation().getWorld() == null)
|
if (furnace.getLocation() == null || furnace.getLocation().getWorld() == null)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save data initially so that if the person reloads again fast they don't lose all their data.
|
|
||||||
this.saveToFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -340,13 +369,6 @@ public class EpicFurnaces extends SongodaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Saves registered furnaces to file.
|
|
||||||
*/
|
|
||||||
private void saveToFile() {
|
|
||||||
storage.doSave();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupRecipies() {
|
private void setupRecipies() {
|
||||||
File config = new File(getDataFolder(), "Furnace Recipes.yml");
|
File config = new File(getDataFolder(), "Furnace Recipes.yml");
|
||||||
if (!config.exists()) {
|
if (!config.exists()) {
|
||||||
@ -438,4 +460,12 @@ public class EpicFurnaces extends SongodaPlugin {
|
|||||||
public LevelManager getLevelManager() {
|
public LevelManager getLevelManager() {
|
||||||
return levelManager;
|
return levelManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DatabaseConnector getDatabaseConnector() {
|
||||||
|
return databaseConnector;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataManager getDataManager() {
|
||||||
|
return dataManager;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,9 +1,8 @@
|
|||||||
package com.songoda.epicfurnaces.boost;
|
package com.songoda.epicfurnaces.boost;
|
||||||
|
|
||||||
import java.util.Collections;
|
import com.songoda.epicfurnaces.EpicFurnaces;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
import java.util.*;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class BoostManager {
|
public class BoostManager {
|
||||||
|
|
||||||
@ -13,6 +12,10 @@ public class BoostManager {
|
|||||||
this.registeredBoosts.add(data);
|
this.registeredBoosts.add(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addBoosts(List<BoostData> boosts) {
|
||||||
|
registeredBoosts.addAll(boosts);
|
||||||
|
}
|
||||||
|
|
||||||
public void removeBoostFromPlayer(BoostData data) {
|
public void removeBoostFromPlayer(BoostData data) {
|
||||||
this.registeredBoosts.remove(data);
|
this.registeredBoosts.remove(data);
|
||||||
}
|
}
|
||||||
@ -27,10 +30,12 @@ public class BoostManager {
|
|||||||
if (boostData.getPlayer().toString().equals(player.toString())) {
|
if (boostData.getPlayer().toString().equals(player.toString())) {
|
||||||
if (System.currentTimeMillis() >= boostData.getEndTime()) {
|
if (System.currentTimeMillis() >= boostData.getEndTime()) {
|
||||||
removeBoostFromPlayer(boostData);
|
removeBoostFromPlayer(boostData);
|
||||||
|
EpicFurnaces.getInstance().getDataManager().deleteBoost(boostData);
|
||||||
}
|
}
|
||||||
return boostData;
|
return boostData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ public class CommandBoost extends AbstractCommand {
|
|||||||
|
|
||||||
BoostData boostData = new BoostData(Integer.parseInt(args[1]), duration == 0L ? Long.MAX_VALUE : System.currentTimeMillis() + duration, player.getUniqueId());
|
BoostData boostData = new BoostData(Integer.parseInt(args[1]), duration == 0L ? Long.MAX_VALUE : System.currentTimeMillis() + duration, player.getUniqueId());
|
||||||
instance.getBoostManager().addBoostToPlayer(boostData);
|
instance.getBoostManager().addBoostToPlayer(boostData);
|
||||||
|
instance.getDataManager().createBoost(boostData);
|
||||||
instance.getLocale().newMessage("&7Successfully boosted &6" + Bukkit.getPlayer(args[0]).getName()
|
instance.getLocale().newMessage("&7Successfully boosted &6" + Bukkit.getPlayer(args[0]).getName()
|
||||||
+ "'s &7furnace reward amounts &6" + args[1] + "x" + (duration == 0L ? "" : (" for " + Methods.makeReadable(duration))) + "&7.").sendPrefixedMessage(sender);
|
+ "'s &7furnace reward amounts &6" + args[1] + "x" + (duration == 0L ? "" : (" for " + Methods.makeReadable(duration))) + "&7.").sendPrefixedMessage(sender);
|
||||||
return ReturnType.SUCCESS;
|
return ReturnType.SUCCESS;
|
||||||
|
290
src/main/java/com/songoda/epicfurnaces/database/DataManager.java
Normal file
290
src/main/java/com/songoda/epicfurnaces/database/DataManager.java
Normal file
@ -0,0 +1,290 @@
|
|||||||
|
package com.songoda.epicfurnaces.database;
|
||||||
|
|
||||||
|
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||||
|
import com.songoda.core.database.DataManagerAbstract;
|
||||||
|
import com.songoda.core.database.DatabaseConnector;
|
||||||
|
import com.songoda.epicfurnaces.EpicFurnaces;
|
||||||
|
import com.songoda.epicfurnaces.boost.BoostData;
|
||||||
|
import com.songoda.epicfurnaces.furnace.Furnace;
|
||||||
|
import com.songoda.epicfurnaces.furnace.FurnaceBuilder;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class DataManager extends DataManagerAbstract {
|
||||||
|
|
||||||
|
public DataManager(DatabaseConnector connector, Plugin plugin) {
|
||||||
|
super(connector, plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createBoost(BoostData boostData) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String createBoostedPlayer = "INSERT INTO " + this.getTablePrefix() + "boosted_players (player, multiplier, end_time) VALUES (?, ?, ?)";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(createBoostedPlayer)) {
|
||||||
|
statement.setString(1, boostData.getPlayer().toString());
|
||||||
|
statement.setInt(2, boostData.getMultiplier());
|
||||||
|
statement.setLong(3, boostData.getEndTime());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getBoosts(Consumer<List<BoostData>> callback) {
|
||||||
|
List<BoostData> boosts = new ArrayList<>();
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
String selectBoostedPlayers = "SELECT * FROM " + this.getTablePrefix() + "boosted_players";
|
||||||
|
ResultSet result = statement.executeQuery(selectBoostedPlayers);
|
||||||
|
while (result.next()) {
|
||||||
|
UUID player = UUID.fromString(result.getString("player"));
|
||||||
|
int multiplier = result.getInt("multiplier");
|
||||||
|
long endTime = result.getLong("end_time");
|
||||||
|
boosts.add(new BoostData(multiplier, endTime, player));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sync(() -> callback.accept(boosts));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteBoost(BoostData boostData) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(deleteBoost)) {
|
||||||
|
statement.setLong(1, boostData.getEndTime());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createFurnaces(List<Furnace> furnaces) {
|
||||||
|
for (Furnace furnace : furnaces) {
|
||||||
|
createFurnace(furnace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createFurnace(Furnace furnace) {
|
||||||
|
this.queueAsync(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String createFurnace = "INSERT INTO " + this.getTablePrefix() + "active_furnaces (level, uses, nickname, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(createFurnace)) {
|
||||||
|
statement.setInt(1, furnace.getLevel().getLevel());
|
||||||
|
statement.setInt(2, furnace.getUses());
|
||||||
|
statement.setString(3, furnace.getNickname());
|
||||||
|
statement.setString(4,
|
||||||
|
furnace.getPlacedBy() == null ? null : furnace.getPlacedBy().toString());
|
||||||
|
|
||||||
|
statement.setString(5, furnace.getLocation().getWorld().getName());
|
||||||
|
statement.setInt(6, furnace.getLocation().getBlockX());
|
||||||
|
statement.setInt(7, furnace.getLocation().getBlockY());
|
||||||
|
statement.setInt(8, furnace.getLocation().getBlockZ());
|
||||||
|
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
int furnaceId = this.lastInsertedId(connection, "active_furnaces");
|
||||||
|
furnace.setId(furnaceId);
|
||||||
|
|
||||||
|
String createAccessList = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(createAccessList)) {
|
||||||
|
for (UUID uuid : furnace.getAccessList()) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.setString(2, uuid.toString());
|
||||||
|
statement.addBatch();
|
||||||
|
}
|
||||||
|
statement.executeBatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
String createNewLevel = "INSERT INTO " + this.getTablePrefix() + "to_level_new (furnace_id, item, amount) VALUES (?, ?, ?)";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(createNewLevel)) {
|
||||||
|
for (Map.Entry<CompatibleMaterial, Integer> entry : furnace.getToLevel().entrySet()) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.setString(2, entry.getKey().name());
|
||||||
|
statement.setInt(3, entry.getValue());
|
||||||
|
statement.addBatch();
|
||||||
|
}
|
||||||
|
statement.executeBatch();
|
||||||
|
}
|
||||||
|
}), "create");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateFurnace(Furnace furnace) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String updateHopper = "UPDATE " + this.getTablePrefix() + "active_furnaces SET level = ?, nickname = ?, uses = ? WHERE id = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(updateHopper)) {
|
||||||
|
statement.setInt(1, furnace.getLevel().getLevel());
|
||||||
|
statement.setString(2, furnace.getNickname());
|
||||||
|
statement.setInt(3, furnace.getUses());
|
||||||
|
statement.setInt(4, furnace.getId());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteFurnace(Furnace furnace) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String deleteFurnace = "DELETE FROM " + this.getTablePrefix() + "active_furnaces WHERE id = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(deleteFurnace)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
String deleteAccessList = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(deleteAccessList)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
String deleteLevelupItems = "DELETE FROM " + this.getTablePrefix() + "to_level_new WHERE furnace_id = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(deleteLevelupItems)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createAccessPlayer(Furnace furnace, UUID uuid) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(createAccessPlayer)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.setString(2, uuid.toString());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// These will be used in the future when the access list gets revamped.
|
||||||
|
// Probably by me since I already have a custom version in my server.
|
||||||
|
public void deleteAccessPlayer(Furnace furnace, UUID uuid) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String deleteAccessPlayer = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ? AND uuid = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(deleteAccessPlayer)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.setString(2, uuid.toString());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateAccessPlayers(Furnace furnace) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String deletePlayers = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(deletePlayers)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(createAccessPlayer)) {
|
||||||
|
for (UUID uuid : furnace.getAccessList()) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.setString(2, uuid.toString());
|
||||||
|
statement.addBatch();
|
||||||
|
}
|
||||||
|
statement.executeBatch();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateLevelupItems(Furnace furnace, CompatibleMaterial material, int amount) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
String deleteLevelupItem = "DELETE FROM " + this.getTablePrefix() + "to_level_new WHERE furnace_id = ? AND item = ?";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(deleteLevelupItem)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.setString(2, material.name());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
String createLevelupItem = "INSERT INTO " + this.getTablePrefix() + "to_level_new (furnace_id, item, amount) VALUES (?, ?, ?)";
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(createLevelupItem)) {
|
||||||
|
statement.setInt(1, furnace.getId());
|
||||||
|
statement.setString(2, material.name());
|
||||||
|
statement.setInt(3, amount);
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getFurnaces(Consumer<Map<Integer, Furnace>> callback) {
|
||||||
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
|
Map<Integer, Furnace> furnaces = new HashMap<>();
|
||||||
|
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
String selectFurnaces = "SELECT * FROM " + this.getTablePrefix() + "active_furnaces";
|
||||||
|
ResultSet result = statement.executeQuery(selectFurnaces);
|
||||||
|
while (result.next()) {
|
||||||
|
World world = Bukkit.getWorld(result.getString("world"));
|
||||||
|
|
||||||
|
if (world == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int id = result.getInt("id");
|
||||||
|
int level = result.getInt("level");
|
||||||
|
int uses = result.getInt("uses");
|
||||||
|
|
||||||
|
String placedByStr = result.getString("placed_by");
|
||||||
|
UUID placedBy = placedByStr == null ? null : UUID.fromString(result.getString("placed_by"));
|
||||||
|
|
||||||
|
String nickname = result.getString("nickname");
|
||||||
|
|
||||||
|
int x = result.getInt("x");
|
||||||
|
int y = result.getInt("y");
|
||||||
|
int z = result.getInt("z");
|
||||||
|
Location location = new Location(world, x, y, z);
|
||||||
|
|
||||||
|
Furnace furnace = new FurnaceBuilder(location)
|
||||||
|
.setId(id)
|
||||||
|
.setLevel(EpicFurnaces.getInstance().getLevelManager().getLevel(level))
|
||||||
|
.setUses(uses)
|
||||||
|
.setPlacedBy(placedBy)
|
||||||
|
.setNickname(nickname)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
furnaces.put(id, furnace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
String selectAccessList = "SELECT * FROM " + this.getTablePrefix() + "access_list";
|
||||||
|
ResultSet result = statement.executeQuery(selectAccessList);
|
||||||
|
while (result.next()) {
|
||||||
|
int id = result.getInt("furnace_id");
|
||||||
|
UUID uuid = UUID.fromString(result.getString("uuid"));
|
||||||
|
|
||||||
|
Furnace furnace = furnaces.get(id);
|
||||||
|
if (furnace == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
furnace.addToAccessList(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
String selectLevelupItems = "SELECT * FROM " + this.getTablePrefix() + "to_level_new";
|
||||||
|
ResultSet result = statement.executeQuery(selectLevelupItems);
|
||||||
|
while (result.next()) {
|
||||||
|
int id = result.getInt("furnace_id");
|
||||||
|
CompatibleMaterial material = CompatibleMaterial.getMaterial(result.getString("item"));
|
||||||
|
int amount = result.getInt("amount");
|
||||||
|
|
||||||
|
Furnace furnace = furnaces.get(id);
|
||||||
|
if (furnace == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
furnace.addToLevel(material, amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.sync(() -> callback.accept(furnaces));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.songoda.epicfurnaces.database.migrations;
|
||||||
|
|
||||||
|
import com.songoda.core.database.DataMigration;
|
||||||
|
import com.songoda.core.database.MySQLConnector;
|
||||||
|
import com.songoda.epicfurnaces.EpicFurnaces;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class _1_InitialMigration extends DataMigration {
|
||||||
|
|
||||||
|
public _1_InitialMigration() {
|
||||||
|
super(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void migrate(Connection connection, String tablePrefix) throws SQLException {
|
||||||
|
String autoIncrement = EpicFurnaces.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
|
||||||
|
|
||||||
|
// Create furnaces table.
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.execute("CREATE TABLE " + tablePrefix + "active_furnaces (" +
|
||||||
|
"id INTEGER PRIMARY KEY" + autoIncrement + ", " +
|
||||||
|
"level INTEGER NOT NULL, " +
|
||||||
|
"uses INTEGER NOT NULL," +
|
||||||
|
"placed_by VARCHAR(36), " +
|
||||||
|
"nickname TEXT, " +
|
||||||
|
"world TEXT NOT NULL, " +
|
||||||
|
"x DOUBLE NOT NULL, " +
|
||||||
|
"y DOUBLE NOT NULL, " +
|
||||||
|
"z DOUBLE NOT NULL " +
|
||||||
|
")");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create access lists.
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.execute("CREATE TABLE " + tablePrefix + "access_list (" +
|
||||||
|
"furnace_id INTEGER NOT NULL, " +
|
||||||
|
"uuid VARCHAR(36)" +
|
||||||
|
")");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create items to level up.
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.execute("CREATE TABLE " + tablePrefix + "to_level_new (" +
|
||||||
|
"furnace_id INTEGER NOT NULL, " +
|
||||||
|
"item TEXT NOT NULL," +
|
||||||
|
"amount INT NOT NULL " +
|
||||||
|
")");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create player boosts
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.execute("CREATE TABLE " + tablePrefix + "boosted_players (" +
|
||||||
|
"player VARCHAR(36) NOT NULL, " +
|
||||||
|
"multiplier INTEGER NOT NULL," +
|
||||||
|
"end_time BIGINT NOT NULL " +
|
||||||
|
")");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -41,6 +41,9 @@ public class Furnace {
|
|||||||
|
|
||||||
private final EpicFurnaces plugin = EpicFurnaces.getInstance();
|
private final EpicFurnaces plugin = EpicFurnaces.getInstance();
|
||||||
|
|
||||||
|
// Identifier for database use.
|
||||||
|
private int id;
|
||||||
|
|
||||||
private final Location location;
|
private final Location location;
|
||||||
private Level level = plugin.getLevelManager().getLowestLevel();
|
private Level level = plugin.getLevelManager().getLowestLevel();
|
||||||
private String nickname = null;
|
private String nickname = null;
|
||||||
@ -70,12 +73,14 @@ public class Furnace {
|
|||||||
if (!block.getType().name().contains("FURNACE") && !block.getType().name().contains("SMOKER")) return;
|
if (!block.getType().name().contains("FURNACE") && !block.getType().name().contains("SMOKER")) return;
|
||||||
|
|
||||||
this.uses++;
|
this.uses++;
|
||||||
|
plugin.getDataManager().updateFurnace(this);
|
||||||
|
|
||||||
CompatibleMaterial material = CompatibleMaterial.getMaterial(event.getResult());
|
CompatibleMaterial material = CompatibleMaterial.getMaterial(event.getResult());
|
||||||
int needed = -1;
|
int needed = -1;
|
||||||
|
|
||||||
if (level.getMaterials().containsKey(material)) {
|
if (level.getMaterials().containsKey(material)) {
|
||||||
addToLevel(material, 1);
|
int amount = addToLevel(material, 1);
|
||||||
|
plugin.getDataManager().updateLevelupItems(this, material, amount);
|
||||||
needed = level.getMaterials().get(material) - getToLevel(material);
|
needed = level.getMaterials().get(material) - getToLevel(material);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,6 +166,7 @@ public class Furnace {
|
|||||||
private void upgradeFinal(Player player) {
|
private void upgradeFinal(Player player) {
|
||||||
levelUp();
|
levelUp();
|
||||||
syncName();
|
syncName();
|
||||||
|
plugin.getDataManager().updateFurnace(this);
|
||||||
if (plugin.getLevelManager().getHighestLevel() != level) {
|
if (plugin.getLevelManager().getHighestLevel() != level) {
|
||||||
plugin.getLocale().getMessage("event.upgrade.success")
|
plugin.getLocale().getMessage("event.upgrade.success")
|
||||||
.processPlaceholder("level", level.getLevel()).sendPrefixedMessage(player);
|
.processPlaceholder("level", level.getLevel()).sendPrefixedMessage(player);
|
||||||
@ -352,12 +358,15 @@ public class Furnace {
|
|||||||
return Collections.unmodifiableMap(toLevel);
|
return Collections.unmodifiableMap(toLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToLevel(CompatibleMaterial material, int amount) {
|
public int addToLevel(CompatibleMaterial material, int amount) {
|
||||||
if (this.toLevel.containsKey(material)) {
|
if (this.toLevel.containsKey(material)) {
|
||||||
this.toLevel.put(material, this.toLevel.get(material) + amount);
|
int newAmount = this.toLevel.get(material) + amount;
|
||||||
return;
|
this.toLevel.put(material, newAmount);
|
||||||
|
return newAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.toLevel.put(material, amount);
|
this.toLevel.put(material, amount);
|
||||||
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRadiusOverheatLast() {
|
public int getRadiusOverheatLast() {
|
||||||
@ -375,4 +384,12 @@ public class Furnace {
|
|||||||
public void setRadiusFuelshareLast(int radiusFuelshareLast) {
|
public void setRadiusFuelshareLast(int radiusFuelshareLast) {
|
||||||
this.radiusFuelshareLast = radiusFuelshareLast;
|
this.radiusFuelshareLast = radiusFuelshareLast;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,11 @@ public class FurnaceBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FurnaceBuilder setId(int id) {
|
||||||
|
this.furnace.setId(id);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Furnace build() {
|
public Furnace build() {
|
||||||
return this.furnace;
|
return this.furnace;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.songoda.epicfurnaces.furnace;
|
|||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -16,6 +17,12 @@ public class FurnaceManager {
|
|||||||
return registeredFurnaces.put(roundLocation(furnace.getLocation()), furnace);
|
return registeredFurnaces.put(roundLocation(furnace.getLocation()), furnace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addFurnaces(Collection<Furnace> furnaces) {
|
||||||
|
for (Furnace furnace : furnaces) {
|
||||||
|
addFurnace(furnace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Furnace removeFurnace(Location location) {
|
public Furnace removeFurnace(Location location) {
|
||||||
return registeredFurnaces.remove(location);
|
return registeredFurnaces.remove(location);
|
||||||
}
|
}
|
||||||
|
@ -51,11 +51,11 @@ public class GUIOverview extends Gui {
|
|||||||
|
|
||||||
setDefaultItem(glass1);
|
setDefaultItem(glass1);
|
||||||
|
|
||||||
GuiUtils.mirrorFill(this, 0, 0, true, true, glass2);
|
mirrorFill(0, 0, true, true, glass2);
|
||||||
GuiUtils.mirrorFill(this, 0, 1, true, true, glass2);
|
mirrorFill(0, 1, true, true, glass2);
|
||||||
GuiUtils.mirrorFill(this, 0, 2, true, true, glass3);
|
mirrorFill(0, 2, true, true, glass3);
|
||||||
GuiUtils.mirrorFill(this, 1, 0, false, true, glass2);
|
mirrorFill(1, 0, false, true, glass2);
|
||||||
GuiUtils.mirrorFill(this, 1, 1, false, true, glass3);
|
mirrorFill(1, 1, false, true, glass3);
|
||||||
|
|
||||||
Level level = furnace.getLevel();
|
Level level = furnace.getLevel();
|
||||||
Level nextLevel = plugin.getLevelManager().getHighestLevel().getLevel() > level.getLevel() ? plugin.getLevelManager().getLevel(level.getLevel() + 1) : null;
|
Level nextLevel = plugin.getLevelManager().getHighestLevel().getLevel() > level.getLevel() ? plugin.getLevelManager().getLevel(level.getLevel() + 1) : null;
|
||||||
@ -149,13 +149,16 @@ public class GUIOverview extends Gui {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
plugin.getDataManager().updateFurnace(furnace);
|
||||||
furnace.setNickname(promptEvent.getMessage());
|
furnace.setNickname(promptEvent.getMessage());
|
||||||
plugin.getLocale().getMessage("event.remote.nicknamesuccess").sendPrefixedMessage(player);
|
plugin.getLocale().getMessage("event.remote.nicknamesuccess").sendPrefixedMessage(player);
|
||||||
}).setOnClose(this::constructGUI);
|
}).setOnClose(this::constructGUI);
|
||||||
|
|
||||||
}).setAction(4, ClickType.RIGHT, (event) -> {
|
}).setAction(4, ClickType.RIGHT, (event) -> {
|
||||||
if (!furnace.isOnAccessList(player))
|
if (!furnace.isOnAccessList(player)) {
|
||||||
furnace.addToAccessList(player);
|
furnace.addToAccessList(player);
|
||||||
|
plugin.getDataManager().createAccessPlayer(furnace, player.getUniqueId());
|
||||||
|
}
|
||||||
constructGUI();
|
constructGUI();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ public class BlockListeners implements Listener {
|
|||||||
: new FurnaceBuilder(location).setPlacedBy(event.getPlayer().getUniqueId()).build();
|
: new FurnaceBuilder(location).setPlacedBy(event.getPlayer().getUniqueId()).build();
|
||||||
|
|
||||||
plugin.getFurnaceManager().addFurnace(furnace);
|
plugin.getFurnaceManager().addFurnace(furnace);
|
||||||
|
plugin.getDataManager().createFurnace(furnace);
|
||||||
|
|
||||||
plugin.updateHologram(furnace);
|
plugin.updateHologram(furnace);
|
||||||
}
|
}
|
||||||
@ -92,5 +93,6 @@ public class BlockListeners implements Listener {
|
|||||||
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), item);
|
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), item);
|
||||||
}
|
}
|
||||||
plugin.getFurnaceManager().removeFurnace(block.getLocation());
|
plugin.getFurnaceManager().removeFurnace(block.getLocation());
|
||||||
|
plugin.getDataManager().deleteFurnace(furnace);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -28,6 +28,7 @@ public class EntityListeners implements Listener {
|
|||||||
Furnace furnace = furnaceManager.getFurnace(block);
|
Furnace furnace = furnaceManager.getFurnace(block);
|
||||||
if (furnace == null) continue;
|
if (furnace == null) continue;
|
||||||
furnaceManager.removeFurnace(block.getLocation());
|
furnaceManager.removeFurnace(block.getLocation());
|
||||||
|
plugin.getDataManager().deleteFurnace(furnace);
|
||||||
plugin.clearHologram(furnace);
|
plugin.clearHologram(furnace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user