mirror of
https://github.com/TotalFreedomMC/TF-Marriage.git
synced 2024-12-31 20:42:18 +00:00
Initial commit on data system
This commit is contained in:
parent
0c4ccfb93e
commit
aa21fa1f8e
10 changed files with 384 additions and 9 deletions
|
@ -2,6 +2,8 @@ package com.lenis0012.bukkit.marriage2;
|
|||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
public interface MData {
|
||||
|
@ -13,8 +15,9 @@ public interface MData {
|
|||
/**
|
||||
* Get the home of the married people.
|
||||
*
|
||||
* @return Marriage home.
|
||||
* @return Marriage home, NULL if not set.
|
||||
*/
|
||||
@Nullable
|
||||
Location getHome();
|
||||
|
||||
/**
|
||||
|
@ -22,7 +25,7 @@ public interface MData {
|
|||
*
|
||||
* @return Marriage home.
|
||||
*/
|
||||
Location setHome();
|
||||
void setHome(Location home);
|
||||
|
||||
/**
|
||||
* Check if the married players have a home set.
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.bukkit.command.CommandSender;
|
|||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.Marriage;
|
||||
import com.lenis0012.bukkit.marriage2.lang.Message;
|
||||
import com.lenis0012.bukkit.marriage2.config.Message;
|
||||
|
||||
public abstract class Command {
|
||||
protected final Marriage marriage;
|
||||
|
|
|
@ -5,7 +5,7 @@ import org.bukkit.entity.Player;
|
|||
|
||||
import com.lenis0012.bukkit.marriage2.MPlayer;
|
||||
import com.lenis0012.bukkit.marriage2.Marriage;
|
||||
import com.lenis0012.bukkit.marriage2.lang.Message;
|
||||
import com.lenis0012.bukkit.marriage2.config.Message;
|
||||
|
||||
public class CommandMarry extends Command {
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.lenis0012.bukkit.marriage2.lang;
|
||||
package com.lenis0012.bukkit.marriage2.config;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.Marriage;
|
||||
import com.lenis0012.bukkit.marriage2.misc.BConfig;
|
|
@ -0,0 +1,59 @@
|
|||
package com.lenis0012.bukkit.marriage2.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.util.com.google.common.collect.Lists;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.internal.MarriagePlugin;
|
||||
|
||||
public class Settings<T> {
|
||||
private static final List<Settings<?>> cache = Lists.newArrayList();
|
||||
|
||||
/**
|
||||
* Values
|
||||
*/
|
||||
public static final Settings<Integer> REQUEST_EXPRY = new Settings<Integer>("requestExpiry", 60);
|
||||
public static final Settings<Integer> COOLDOWN_MARRY = new Settings<Integer>("cooldown.marry", 120);
|
||||
public static final Settings<Integer> COOLDOWN_GIFT = new Settings<Integer>("cooldown.gift", 0);
|
||||
public static final Settings<Integer> COOLDOWN_DIVORCE = new Settings<Integer>("cooldown.divorce", 0);
|
||||
|
||||
private final String key;
|
||||
private final T def;
|
||||
private T value;
|
||||
|
||||
private Settings(String key, T def) {
|
||||
cache.add(this);
|
||||
this.key = key;
|
||||
this.def = def;
|
||||
}
|
||||
|
||||
public T value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void reload(FileConfiguration config) {
|
||||
if(config.contains(key)) {
|
||||
this.value = (T) config.get(key);
|
||||
} else {
|
||||
config.set(key, def);
|
||||
this.value = def;
|
||||
}
|
||||
}
|
||||
|
||||
public static final void reloadAll() {
|
||||
MarriagePlugin plugin = MarriagePlugin.getCore().getPlugin();
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
for(Settings<?> setting : cache) {
|
||||
setting.reload(config);
|
||||
}
|
||||
|
||||
plugin.saveConfig();
|
||||
}
|
||||
|
||||
public static final List<Settings<?>> values() {
|
||||
return cache;
|
||||
}
|
||||
}
|
|
@ -4,7 +4,8 @@ import java.util.UUID;
|
|||
|
||||
import com.lenis0012.bukkit.marriage2.MPlayer;
|
||||
import com.lenis0012.bukkit.marriage2.commands.CommandMarry;
|
||||
import com.lenis0012.bukkit.marriage2.lang.Message;
|
||||
import com.lenis0012.bukkit.marriage2.config.Message;
|
||||
import com.lenis0012.bukkit.marriage2.config.Settings;
|
||||
|
||||
public class MarriageCore extends MarriageBase {
|
||||
|
||||
|
@ -12,15 +13,21 @@ public class MarriageCore extends MarriageBase {
|
|||
super(plugin);
|
||||
}
|
||||
|
||||
@Register(name = "commands", type = Register.Type.ENABLE)
|
||||
public void registerCommands() {
|
||||
register(CommandMarry.class);
|
||||
@Register(name = "config", type = Register.Type.ENABLE, priority = 0)
|
||||
public void loadConfig() {
|
||||
Settings.reloadAll();
|
||||
}
|
||||
|
||||
@Register(name = "messages", type = Register.Type.ENABLE, priority = 1)
|
||||
public void loadMessages() {
|
||||
Message.reloadAll(this);
|
||||
}
|
||||
|
||||
@Register(name = "commands", type = Register.Type.ENABLE)
|
||||
public void registerCommands() {
|
||||
register(CommandMarry.class);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MPlayer getMPlayer(UUID uuid) {
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
package com.lenis0012.bukkit.marriage2.internal.data;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
|
||||
|
||||
public class DataManager {
|
||||
private final MarriageCore core;
|
||||
private final String url;
|
||||
private final String prefix;
|
||||
|
||||
public DataManager(MarriageCore core, FileConfiguration config) {
|
||||
this.core = core;
|
||||
String driver = "org.sqlite.JDBC";
|
||||
if(config.getBoolean("MySQL.enabled")) {
|
||||
String user = config.getString("MySQL.user", "root");
|
||||
String pswd = config.getString("MySQL.password", "");
|
||||
String host = config.getString("MySQL.host", "localhost:3306");
|
||||
String database = config.getString("MySQL.database", "myserver");
|
||||
this.prefix = config.getString("MySQL.prefix", "marriage_");
|
||||
this.url = String.format("jdbc:mysql://%s/%s?user=%s&password=%s", host, database, user, pswd);
|
||||
} else {
|
||||
this.url = String.format("jdbc:sqlite:%s", new File(core.getPlugin().getDataFolder(), "database.db"));
|
||||
this.prefix = "";
|
||||
}
|
||||
|
||||
try {
|
||||
Class.forName(driver);
|
||||
} catch(Exception e) {
|
||||
core.getLogger().log(Level.SEVERE, "Failed to initiate database driver", e);
|
||||
}
|
||||
|
||||
Connection connection = newConnection();
|
||||
try {
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(String.format("CREATE TABLE IF NOT EXISTS %splayers ("
|
||||
+ "id NOT NULL UNIQUE AUTO_INCREMENT,"
|
||||
+ "unique_user_id VARCHAR(128) NOT NULL UNIQUE,"
|
||||
+ "gender VARCHAR(32),"
|
||||
+ "lastlogin BIGINT,"
|
||||
+ "PRIMARY KEY (id);", prefix));
|
||||
statement.executeUpdate(String.format("CREATE TABLE IF NOT EXISTS %sdata ("
|
||||
+ "id NOT NULL UNIQUE AUTO_INCREMENT,"
|
||||
+ "player1 VARCHAR(128) NOT NULL,"
|
||||
+ "player2 VARCHAR(128) NOT NULL,"
|
||||
+ "home_world VARCHAR(128) NOT NULL,"
|
||||
+ "home_x DOUBLE,"
|
||||
+ "home_y DOUBLE,"
|
||||
+ "home_z DOUBLE,"
|
||||
+ "home_yaw FLOAT,"
|
||||
+ "home_pitch FLOAT,"
|
||||
+ "pvp_enabled BIT,"
|
||||
+ "PRIMARY KEY (id);", prefix));
|
||||
} catch (SQLException e) {
|
||||
core.getLogger().log(Level.WARNING, "Failed to load player data", e);
|
||||
} finally {
|
||||
if(connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MarriagePlayer loadPlayer(UUID uuid) {
|
||||
MarriagePlayer player = null;
|
||||
Connection connection = newConnection();
|
||||
try {
|
||||
PreparedStatement ps = connection.prepareStatement(String.format(
|
||||
"SELECT * FROM %splayers WHERE unique_user_id=?;", prefix));
|
||||
ps.setString(1, uuid.toString());
|
||||
player = new MarriagePlayer(uuid, ps.executeQuery());
|
||||
loadMarriages(connection, player, false);
|
||||
} catch (SQLException e) {
|
||||
core.getLogger().log(Level.WARNING, "Failed to load player data", e);
|
||||
} finally {
|
||||
if(connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
private void loadMarriages(Connection connection, MarriagePlayer player, boolean alt) throws SQLException {
|
||||
PreparedStatement ps = connection.prepareStatement(String.format(
|
||||
"SELECT * FROM %sdata WHERE %s=?;", alt ? "player2" : "player1", prefix));
|
||||
ResultSet result = ps.executeQuery();
|
||||
while(result.next()) {
|
||||
player.addMarriage(new MarriageData(result));
|
||||
}
|
||||
|
||||
if(!alt) {
|
||||
loadMarriages(connection, player, true);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection newConnection() {
|
||||
try {
|
||||
return DriverManager.getConnection(url);
|
||||
} catch (SQLException e) {
|
||||
core.getLogger().log(Level.WARNING, "Failed to connect to database", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package com.lenis0012.bukkit.marriage2.internal.data;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.MData;
|
||||
|
||||
public class MarriageData implements MData {
|
||||
private final UUID player1;
|
||||
private final UUID player2;
|
||||
private Location home;
|
||||
private boolean pvpEnabled;
|
||||
|
||||
public MarriageData(UUID player1, UUID player2) {
|
||||
this.player1 = player1;
|
||||
this.player2 = player2;
|
||||
}
|
||||
|
||||
public MarriageData(ResultSet data) throws SQLException {
|
||||
this.player1 = UUID.fromString(data.getString("player1"));
|
||||
this.player2 = UUID.fromString(data.getString("player2"));
|
||||
String world = data.getString("home_world");
|
||||
if(!"NONE".equals(world)) {
|
||||
double x = data.getDouble("home_x");
|
||||
double y = data.getDouble("home_y");
|
||||
double z = data.getDouble("home_z");
|
||||
float yaw = data.getFloat("home_yaw");
|
||||
float pitch = data.getFloat("home_pitch");
|
||||
this.home = new Location(Bukkit.getWorld(UUID.fromString(world)), x, y, z, yaw, pitch);
|
||||
}
|
||||
|
||||
this.pvpEnabled = data.getBoolean("pvp_enabled");
|
||||
}
|
||||
|
||||
void save(PreparedStatement ps) throws SQLException {
|
||||
ps.setString(1, player1.toString());
|
||||
ps.setString(2, player2.toString());
|
||||
if(home != null) {
|
||||
ps.setString(3, home.getWorld().getUID().toString());
|
||||
ps.setDouble(4, home.getX());
|
||||
ps.setDouble(5, home.getY());
|
||||
ps.setDouble(6, home.getZ());
|
||||
ps.setFloat(7, home.getYaw());
|
||||
ps.setFloat(8, home.getPitch());
|
||||
} else {
|
||||
ps.setString(3, "NONE");
|
||||
}
|
||||
|
||||
ps.setBoolean(9, pvpEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getPlayer1Id() {
|
||||
return player1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getPllayer2Id() {
|
||||
return player2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getHome() {
|
||||
return home;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHome(Location home) {
|
||||
this.home = home;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHomeSet() {
|
||||
return home != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPVPEnabled() {
|
||||
return pvpEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPVPEnabled(boolean pvpEnabled) {
|
||||
this.pvpEnabled = pvpEnabled;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.lenis0012.bukkit.marriage2.internal.data;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.minecraft.util.com.google.common.collect.Lists;
|
||||
|
||||
import com.lenis0012.bukkit.marriage2.Gender;
|
||||
import com.lenis0012.bukkit.marriage2.MData;
|
||||
import com.lenis0012.bukkit.marriage2.MPlayer;
|
||||
|
||||
public class MarriagePlayer implements MPlayer {
|
||||
private final List<UUID> requests = Lists.newArrayList();
|
||||
private final UUID uuid;
|
||||
private MData marriage;
|
||||
private Gender gender;
|
||||
private boolean inChat;
|
||||
|
||||
public MarriagePlayer(UUID uuid, ResultSet data) throws SQLException {
|
||||
this.uuid = uuid;
|
||||
if(data.next()) {
|
||||
this.gender = Gender.valueOf(data.getString("gender"));
|
||||
}
|
||||
}
|
||||
|
||||
void addMarriage(MarriageData data) {
|
||||
this.marriage = data;
|
||||
}
|
||||
|
||||
void save(PreparedStatement ps) throws SQLException {
|
||||
ps.setString(1, uuid.toString());
|
||||
ps.setString(2, gender.toString());
|
||||
ps.setLong(3, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestMarriage(UUID from) {
|
||||
requests.add(from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMarriageRequested(UUID from) {
|
||||
return requests.contains(from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MData getMarriage() {
|
||||
return marriage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMarried() {
|
||||
return marriage != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInChat() {
|
||||
return inChat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInChat(boolean inChat) {
|
||||
this.inChat = inChat;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.lenis0012.bukkit.marriage2.misc;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
@ -18,6 +19,15 @@ public class BConfig extends YamlConfiguration {
|
|||
public BConfig(MarriageBase core, File file) {
|
||||
this.core = core;
|
||||
this.file = file;
|
||||
file.getParentFile().mkdirs();
|
||||
if(!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
reload();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue