Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Datura/src/main/java/fns/datura/Datura.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand All @@ -38,12 +39,11 @@

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();
Expand All @@ -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));
Expand All @@ -74,7 +75,7 @@ public void onEnable()

public MySQL getSQL()
{
return sql;
return mySQL;
}

public Halter getHalter()
Expand Down
53 changes: 53 additions & 0 deletions Datura/src/main/java/fns/datura/listener/UserDataListener.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 22 additions & 1 deletion Datura/src/main/java/fns/datura/sql/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -41,16 +42,33 @@ 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)
.append('/')
.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.
*
Expand Down Expand Up @@ -280,5 +298,8 @@ public CompletableFuture<Boolean> insertRow(final String table, final String[] c
return execute(query.toString(), table, columns, values);
}


public SQLProperties getProperties()
{
return properties;
}
}
122 changes: 122 additions & 0 deletions Datura/src/main/java/fns/datura/sql/SimpleSQLProperties.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
30 changes: 30 additions & 0 deletions Datura/src/main/resources/sql.properties
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions Patchwork/src/main/java/fns/patchwork/base/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;

Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -115,4 +120,12 @@ public static ConfigRegistry getConfigRegistry()
{
return configRegistry;
}

/**
* @return The {@link SQLRegistry}
*/
public static SQLRegistry getSQLRegistry()
{
return sqlRegistry;
}
}
49 changes: 49 additions & 0 deletions Patchwork/src/main/java/fns/patchwork/data/SQLRegistry.java
Original file line number Diff line number Diff line change
@@ -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<String, SQL> 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);
}
}
2 changes: 2 additions & 0 deletions Patchwork/src/main/java/fns/patchwork/sql/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

public interface SQL
{
SQLProperties getProperties();

CompletableFuture<PreparedStatement> prepareStatement(final String query, final Object... args);

CompletableFuture<ResultSet> executeQuery(final String query, final Object... args);
Expand Down
Loading