mirror of
https://gitlab.com/SamB440/rpgregions-2.git
synced 2025-12-28 11:19:24 +00:00
First SQL code, add listeners
This commit is contained in:
@@ -5,10 +5,14 @@ import net.islandearth.languagy.language.LanguagyImplementation;
|
||||
import net.islandearth.languagy.language.LanguagyPluginHook;
|
||||
import net.islandearth.languagy.language.Translator;
|
||||
import net.islandearth.rpgregions.api.RPGRegionsAPI;
|
||||
import net.islandearth.rpgregions.listener.ConnectionListener;
|
||||
import net.islandearth.rpgregions.listener.RegionListener;
|
||||
import net.islandearth.rpgregions.managers.RPGRegionsManagers;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
@@ -90,6 +94,12 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
PluginManager pm = Bukkit.getPluginManager();
|
||||
pm.registerEvents(new ConnectionListener(this), this);
|
||||
pm.registerEvents(new RegionListener(), this);
|
||||
}
|
||||
|
||||
public static RPGRegionsAPI getAPI() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package net.islandearth.rpgregions.listener;
|
||||
|
||||
import net.islandearth.rpgregions.RPGRegions;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class ConnectionListener implements Listener {
|
||||
|
||||
private final RPGRegions plugin;
|
||||
|
||||
public ConnectionListener(RPGRegions plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onLeave(PlayerQuitEvent pqe) {
|
||||
Player player = pqe.getPlayer();
|
||||
if (plugin.getManagers().getStorageManager().getCachedAccounts().containsKey(player.getUniqueId()))
|
||||
plugin.getManagers().getStorageManager().removeCachedAccount(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package net.islandearth.rpgregions.listener;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
public class RegionListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onMove(PlayerMoveEvent pme) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,20 @@
|
||||
package net.islandearth.rpgregions.managers.data;
|
||||
|
||||
import net.islandearth.rpgregions.managers.data.account.RPGRegionsAccount;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public interface StorageManager {
|
||||
|
||||
RPGRegionsAccount getAccount(Player player);
|
||||
CompletableFuture<RPGRegionsAccount> getAccount(UUID uuid);
|
||||
|
||||
ConcurrentMap<UUID, RPGRegionsAccount> getCachedAccounts();
|
||||
|
||||
void removeCachedAccount(UUID uuid);
|
||||
|
||||
default String getDatabaseUuid(UUID uuid) {
|
||||
return uuid.toString().replace("-", "");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,14 @@ import java.util.UUID;
|
||||
|
||||
public class RPGRegionsAccount {
|
||||
|
||||
private UUID uuid;
|
||||
private List<String> discoveredRegions = new ArrayList<>();
|
||||
|
||||
public RPGRegionsAccount(UUID uuid, List<String> discoveredRegions) {
|
||||
this.discoveredRegions = discoveredRegions;
|
||||
}
|
||||
|
||||
public List<String> getDiscoveredRegions() {
|
||||
return discoveredRegions;
|
||||
}
|
||||
|
||||
private List<String> discoveredRegions = new ArrayList<>();
|
||||
|
||||
public RPGRegionsAccount(UUID uuid) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,24 @@
|
||||
package net.islandearth.rpgregions.managers.data.sql;
|
||||
|
||||
import co.aikar.idb.DB;
|
||||
import co.aikar.idb.Database;
|
||||
import co.aikar.idb.DatabaseOptions;
|
||||
import co.aikar.idb.PooledDatabaseOptions;
|
||||
import co.aikar.idb.*;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import net.islandearth.rpgregions.RPGRegions;
|
||||
import net.islandearth.rpgregions.managers.data.StorageManager;
|
||||
import net.islandearth.rpgregions.managers.data.account.RPGRegionsAccount;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public class SqlStorage implements StorageManager {
|
||||
|
||||
private ConcurrentMap<UUID, RPGRegionsAccount> cachedAccounts = new ConcurrentHashMap<>();
|
||||
|
||||
public SqlStorage() {
|
||||
RPGRegions plugin = JavaPlugin.getPlugin(RPGRegions.class);
|
||||
DatabaseOptions options = DatabaseOptions.builder().mysql(plugin.getConfig().getString("sql.user"),
|
||||
@@ -20,11 +27,41 @@ public class SqlStorage implements StorageManager {
|
||||
plugin.getConfig().getString("sql.host") + ":" + plugin.getConfig().getString("sql.port")).build();
|
||||
Database db = PooledDatabaseOptions.builder().options(options).createHikariDatabase();
|
||||
DB.setGlobalDatabase(db);
|
||||
//TODO make table
|
||||
try {
|
||||
db.executeUpdate("CREATE TABLE IF NOT EXISTS Discoveries (uuid varchar(32) NOT NULL, region varchar(32) NOT NULL, time datetime NOT NULL, PRIMARY KEY(uuid))");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public RPGRegionsAccount getAccount(Player player) {
|
||||
return null;
|
||||
public CompletableFuture<RPGRegionsAccount> getAccount(UUID uuid) {
|
||||
CompletableFuture<RPGRegionsAccount> future = new CompletableFuture<>();
|
||||
if (cachedAccounts.containsKey(uuid)) {
|
||||
future.complete(cachedAccounts.get(uuid));
|
||||
} else {
|
||||
DB.getResultsAsync("SELECT region FROM Discoveries WHERE uuid = ?", getDatabaseUuid(uuid)).whenComplete((results, error) -> {
|
||||
List<String> regions = new ArrayList<>();
|
||||
for (DbRow row : results) {
|
||||
regions.add(row.getString("region"));
|
||||
}
|
||||
|
||||
RPGRegionsAccount account = new RPGRegionsAccount(uuid, regions);
|
||||
cachedAccounts.put(uuid, account);
|
||||
future.complete(account);
|
||||
});
|
||||
}
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConcurrentMap<UUID, RPGRegionsAccount> getCachedAccounts() {
|
||||
return (ConcurrentMap<UUID, RPGRegionsAccount>) ImmutableMap.copyOf(cachedAccounts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCachedAccount(UUID uuid) {
|
||||
//TODO save new regions
|
||||
cachedAccounts.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user