diff --git a/Datura/src/main/java/fns/datura/Datura.java b/Datura/src/main/java/fns/datura/Datura.java index 5089516..9dd126f 100644 --- a/Datura/src/main/java/fns/datura/Datura.java +++ b/Datura/src/main/java/fns/datura/Datura.java @@ -30,6 +30,7 @@ import fns.datura.punishment.Cager; import fns.datura.punishment.Halter; import fns.datura.punishment.Locker; import fns.datura.sql.MySQL; +import fns.datura.sql.SimpleSQLProperties; import fns.patchwork.base.Registration; import fns.patchwork.command.CommandHandler; import fns.patchwork.service.SubscriptionProvider; @@ -38,12 +39,11 @@ import org.bukkit.plugin.java.JavaPlugin; public class Datura extends JavaPlugin { - private final MySQL sql = new MySQL("localhost", 3011, "master"); - // Punishment private final Halter halter = new Halter(); private final Locker locker = new Locker(); private Cager cager; + private MySQL mySQL; // Features private final CommandSpy commandSpy = new CommandSpy(); @@ -53,6 +53,7 @@ public class Datura extends JavaPlugin public void onEnable() { cager = new Cager(this); + mySQL = new MySQL(new SimpleSQLProperties(this)); Registration.getServiceTaskRegistry() .registerService(SubscriptionProvider.syncService(this, locker)); @@ -74,7 +75,7 @@ public class Datura extends JavaPlugin public MySQL getSQL() { - return sql; + return mySQL; } public Halter getHalter() diff --git a/Datura/src/main/java/fns/datura/listener/UserDataListener.java b/Datura/src/main/java/fns/datura/listener/UserDataListener.java new file mode 100644 index 0000000..8fe5c37 --- /dev/null +++ b/Datura/src/main/java/fns/datura/listener/UserDataListener.java @@ -0,0 +1,53 @@ +/* + * This file is part of Freedom-Network-Suite - https://github.com/AtlasMediaGroup/Freedom-Network-Suite + * Copyright (C) 2023 Total Freedom Server Network and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package fns.datura.listener; + +import fns.datura.user.SimpleUserData; +import fns.patchwork.base.Registration; +import fns.patchwork.sql.SQL; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class UserDataListener implements Listener +{ + @EventHandler + public void onPlayerJoin(final PlayerJoinEvent event) + { + final Player player = event.getPlayer(); + if (player.hasPlayedBefore()) + { + final SQL sql = Registration.getSQLRegistry().getSQL(Bukkit.getServer().getName()); + if (sql != null) + { + SimpleUserData.fromSQL(sql, player.getUniqueId().toString()); + } + return; + } + + new SimpleUserData(player); + } +} diff --git a/Datura/src/main/java/fns/datura/sql/MySQL.java b/Datura/src/main/java/fns/datura/sql/MySQL.java index 569ded9..effd2a1 100644 --- a/Datura/src/main/java/fns/datura/sql/MySQL.java +++ b/Datura/src/main/java/fns/datura/sql/MySQL.java @@ -26,6 +26,7 @@ package fns.datura.sql; import fns.patchwork.base.Patchwork; import fns.patchwork.base.Shortcuts; import fns.patchwork.sql.SQL; +import fns.patchwork.sql.SQLProperties; import fns.patchwork.utils.container.Identity; import java.sql.Connection; import java.sql.DriverManager; @@ -41,9 +42,12 @@ public class MySQL implements SQL * Using StringBuilder for finality. */ private final StringBuilder url = new StringBuilder("jdbc:mysql://"); + private final SQLProperties properties; public MySQL(final String host, final int port, final String database) { + properties = null; + url.append(host) .append(':') .append(port) @@ -51,6 +55,20 @@ public class MySQL implements SQL .append(database); } + public MySQL(final SQLProperties properties) { + this.properties = properties; + + url.setLength(0); + url.append("jdbc:") + .append(properties.getDriver()) + .append("://") + .append(properties.getHost()) + .append(':') + .append(properties.getPort()) + .append('/') + .append(properties.getDatabase()); + } + /** * Adds credentials to the MySQL URL. If the URL already contains credentials, they will be overwritten. * @@ -280,5 +298,8 @@ public class MySQL implements SQL return execute(query.toString(), table, columns, values); } - + public SQLProperties getProperties() + { + return properties; + } } diff --git a/Datura/src/main/java/fns/datura/sql/SimpleSQLProperties.java b/Datura/src/main/java/fns/datura/sql/SimpleSQLProperties.java new file mode 100644 index 0000000..626419f --- /dev/null +++ b/Datura/src/main/java/fns/datura/sql/SimpleSQLProperties.java @@ -0,0 +1,122 @@ +/* + * This file is part of Freedom-Network-Suite - https://github.com/AtlasMediaGroup/Freedom-Network-Suite + * Copyright (C) 2023 Total Freedom Server Network and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package fns.datura.sql; + +import fns.patchwork.sql.SQLProperties; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.yaml.snakeyaml.constructor.SafeConstructor; + +public class SimpleSQLProperties implements SQLProperties +{ + private static final String PROPERTIES_NAME = "sql.properties"; + + private final Properties properties = new Properties(); + + public SimpleSQLProperties(final JavaPlugin plugin) + { + final File dataFile = new File(plugin.getDataFolder(), PROPERTIES_NAME); + if (!dataFile.exists()) { + plugin.saveResource(PROPERTIES_NAME, false); + try (final InputStream in = plugin.getResource(PROPERTIES_NAME)) { + properties.load(in); + return; + } catch (final IOException ex) { + Bukkit.getLogger().severe("Failed to copy sql.properties file: " + ex.getMessage()); + return; + } + } + + try (final FileInputStream fileInputStream = new FileInputStream(dataFile)) { + properties.load(fileInputStream); + } catch (final IOException ex) { + Bukkit.getServer().getLogger().severe("Failed to load sql.properties file: " + ex.getMessage()); + } + } + + @Override + public Properties getProperties() { + return this.properties; + } + + @Override + public Properties load(final @NotNull File propertiesFile) + { + try (final FileInputStream fileInputStream = new FileInputStream(propertiesFile)) { + properties.load(fileInputStream); + } catch (final IOException ex) { + Bukkit.getServer().getLogger().severe("Failed to load sql.properties file: " + ex.getMessage()); + } + + return properties; + } + + @Override + public String getDriver() + { + return properties.getProperty("driver"); + } + + @Override + public String getHost() + { + return properties.getProperty("host"); + } + + @Override + public String getPort() + { + return properties.getProperty("port"); + } + + @Override + public String getDatabase() + { + return properties.getProperty("database"); + } + + @Override + public String getUsername() + { + return properties.getProperty("username"); + } + + @Override + public String getPassword() + { + return properties.getProperty("password"); + } + + @Override + public String getServerName() + { + return properties.getProperty("serverName"); + } +} diff --git a/Datura/src/main/resources/sql.properties b/Datura/src/main/resources/sql.properties new file mode 100644 index 0000000..8b94ce1 --- /dev/null +++ b/Datura/src/main/resources/sql.properties @@ -0,0 +1,30 @@ +# +# This file is part of Freedom-Network-Suite - https://github.com/AtlasMediaGroup/Freedom-Network-Suite +# Copyright (C) 2023 Total Freedom Server Network and contributors +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# + +driver = "sqlite" +host = "localhost" +port = "3306" +database = "database.db" +username = "root" +password = "password" +serverName = "server" \ No newline at end of file diff --git a/Patchwork/src/main/java/fns/patchwork/base/Registration.java b/Patchwork/src/main/java/fns/patchwork/base/Registration.java index b619965..8ad568d 100644 --- a/Patchwork/src/main/java/fns/patchwork/base/Registration.java +++ b/Patchwork/src/main/java/fns/patchwork/base/Registration.java @@ -27,6 +27,7 @@ import fns.patchwork.data.ConfigRegistry; import fns.patchwork.data.EventRegistry; import fns.patchwork.data.GroupRegistry; import fns.patchwork.data.ModuleRegistry; +import fns.patchwork.data.SQLRegistry; import fns.patchwork.data.ServiceTaskRegistry; import fns.patchwork.data.UserRegistry; @@ -62,6 +63,10 @@ public class Registration * The {@link ConfigRegistry} */ private static final ConfigRegistry configRegistry = new ConfigRegistry(); + /** + * The {@link SQLRegistry} + */ + private static final SQLRegistry sqlRegistry = new SQLRegistry(); private Registration() { @@ -115,4 +120,12 @@ public class Registration { return configRegistry; } + + /** + * @return The {@link SQLRegistry} + */ + public static SQLRegistry getSQLRegistry() + { + return sqlRegistry; + } } \ No newline at end of file diff --git a/Patchwork/src/main/java/fns/patchwork/data/SQLRegistry.java b/Patchwork/src/main/java/fns/patchwork/data/SQLRegistry.java new file mode 100644 index 0000000..2be093d --- /dev/null +++ b/Patchwork/src/main/java/fns/patchwork/data/SQLRegistry.java @@ -0,0 +1,49 @@ +/* + * This file is part of Freedom-Network-Suite - https://github.com/AtlasMediaGroup/Freedom-Network-Suite + * Copyright (C) 2023 Total Freedom Server Network and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package fns.patchwork.data; + +import fns.patchwork.sql.SQL; +import java.util.HashMap; +import java.util.Map; +import org.jetbrains.annotations.NotNull; + +public class SQLRegistry +{ + private final Map sqlMapByModule = new HashMap<>(); + + public void registerSQL(@NotNull final String serverName, @NotNull final SQL sql) + { + sqlMapByModule.put(serverName, sql); + } + + public void unregisterSQL(@NotNull final String serverName) + { + sqlMapByModule.remove(serverName); + } + + public SQL getSQL(@NotNull final String serverName) + { + return sqlMapByModule.get(serverName); + } +} diff --git a/Patchwork/src/main/java/fns/patchwork/sql/SQL.java b/Patchwork/src/main/java/fns/patchwork/sql/SQL.java index fa82316..5c8e827 100644 --- a/Patchwork/src/main/java/fns/patchwork/sql/SQL.java +++ b/Patchwork/src/main/java/fns/patchwork/sql/SQL.java @@ -29,6 +29,8 @@ import java.util.concurrent.CompletableFuture; public interface SQL { + SQLProperties getProperties(); + CompletableFuture prepareStatement(final String query, final Object... args); CompletableFuture executeQuery(final String query, final Object... args); diff --git a/Patchwork/src/main/java/fns/patchwork/sql/SQLProperties.java b/Patchwork/src/main/java/fns/patchwork/sql/SQLProperties.java index 9c67f37..8ce87aa 100644 --- a/Patchwork/src/main/java/fns/patchwork/sql/SQLProperties.java +++ b/Patchwork/src/main/java/fns/patchwork/sql/SQLProperties.java @@ -28,7 +28,9 @@ import java.util.Properties; public interface SQLProperties { - Properties getProperties(File propertiesFile); + Properties getProperties(); + + Properties load(final File propertiesFile); default Properties getDefaultProperties() { @@ -39,6 +41,7 @@ public interface SQLProperties properties.setProperty("database", "database.db"); properties.setProperty("username", "root"); properties.setProperty("password", "password"); + properties.setProperty("serverName", "server"); return properties; } @@ -73,4 +76,6 @@ public interface SQLProperties String getUsername(); String getPassword(); + + String getServerName(); }