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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Implement User Data SQL Handling by Paldiu · Pull Request #29 · SimplexDevelopment/FreedomNetworkSuite · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Implement User Data SQL Handling by Paldiu · Pull Request #29 · SimplexDevelopment/FreedomNetworkSuite · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Implement User Data SQL Handling by Paldiu · Pull Request #29 · SimplexDevelopment/FreedomNetworkSuite · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Implement User Data SQL Handling by Paldiu · Pull Request #29 · SimplexDevelopment/FreedomNetworkSuite · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Implement User Data SQL Handling by Paldiu · Pull Request #29 · SimplexDevelopment/FreedomNetworkSuite · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Implement User Data SQL Handling by Paldiu · Pull Request #29 · SimplexDevelopment/FreedomNetworkSuite · GitHub
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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