Tyr Backbone Creation

# Changes:

## Patchwork
- Renamed FreedomExecutor to ExecutorProvider and moved the class to the provider package.
- Created an SQL Registry to prevent dependencies on Datura for SQL data. SQL is returned through an Optional, in the event that there is no SQL service registered.
- Created SQLResult, a generic ORM for ResultSets to avoid working directly with SQL data.

## Tyr
- Created Identity, which houses a username and related secret key.
- Created SQLEntry which stores the information from the Identity class into an SQL table called sessionData.
- Created TOTP, a simple static class that allows easy access to TimeBasedOneTimePasswordUtils class.
- Created OAuth2 which houses identities and performs the appropriate credential validations (incomplete)
This commit is contained in:
Paul Reilly
2023-09-09 18:57:15 -05:00
parent 85cc1f7ae0
commit 33731b611f
20 changed files with 889 additions and 172 deletions

View File

@ -25,8 +25,8 @@ package fns.patchwork.base;
import fns.patchwork.display.adminchat.AdminChatDisplay;
import fns.patchwork.event.EventBus;
import fns.patchwork.service.FreedomExecutor;
import fns.patchwork.service.SubscriptionProvider;
import fns.patchwork.provider.ExecutorProvider;
import fns.patchwork.provider.SubscriptionProvider;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
@ -40,9 +40,9 @@ public class Patchwork extends JavaPlugin
*/
private EventBus eventBus;
/**
* The {@link FreedomExecutor} for this plugin.
* The {@link ExecutorProvider} for this plugin.
*/
private FreedomExecutor executor;
private ExecutorProvider executor;
/**
* The {@link AdminChatDisplay} for this plugin.
*/
@ -64,7 +64,7 @@ public class Patchwork extends JavaPlugin
public void onEnable()
{
eventBus = new EventBus(this);
executor = new FreedomExecutor(this);
executor = new ExecutorProvider(this);
acdisplay = new AdminChatDisplay(this);
@ -80,11 +80,11 @@ public class Patchwork extends JavaPlugin
}
/**
* Gets the {@link FreedomExecutor} for this plugin.
* Gets the {@link ExecutorProvider} for this plugin.
*
* @return the {@link FreedomExecutor}
* @return the {@link ExecutorProvider}
*/
public FreedomExecutor getExecutor()
public ExecutorProvider getExecutor()
{
return executor;
}

View File

@ -27,6 +27,7 @@ import fns.patchwork.registry.ConfigRegistry;
import fns.patchwork.registry.EventRegistry;
import fns.patchwork.registry.GroupRegistry;
import fns.patchwork.registry.ModuleRegistry;
import fns.patchwork.registry.SQLRegistry;
import fns.patchwork.registry.ServiceTaskRegistry;
import fns.patchwork.registry.UserRegistry;
@ -62,6 +63,10 @@ public class Registration
* The {@link ConfigRegistry}
*/
private static final ConfigRegistry configRegistry = new ConfigRegistry();
/**
* The SQL Registry
*/
private static final SQLRegistry sqlRegistry = new SQLRegistry();
private Registration()
{
@ -115,4 +120,12 @@ public class Registration
{
return configRegistry;
}
/**
* @return The {@link SQLRegistry}
*/
public static SQLRegistry getSQLRegistry()
{
return sqlRegistry;
}
}

View File

@ -23,7 +23,10 @@
package fns.patchwork.base;
import fns.patchwork.provider.ExecutorProvider;
import fns.patchwork.sql.SQL;
import fns.patchwork.user.User;
import java.util.Optional;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
@ -37,13 +40,23 @@ public final class Shortcuts
public static <T extends JavaPlugin> T provideModule(final Class<T> pluginClass)
{
return Registration.getModuleRegistry()
.getProvider(pluginClass)
.getModule();
.getProvider(pluginClass)
.getModule();
}
public static User getUser(final Player player)
{
return Registration.getUserRegistry()
.getUser(player);
.getUser(player);
}
public static ExecutorProvider getExecutors()
{
return provideModule(Patchwork.class).getExecutor();
}
public static Optional<SQL> getSQL()
{
return Registration.getSQLRegistry().getSQL();
}
}

View File

@ -21,7 +21,7 @@
* SOFTWARE.
*/
package fns.patchwork.service;
package fns.patchwork.provider;
import fns.patchwork.base.Patchwork;
import java.util.concurrent.CompletableFuture;
@ -34,7 +34,7 @@ import org.bukkit.plugin.java.JavaPlugin;
* This class is here for both convenience purposes, and also for the sake of providing easy access to executors for
* {@link CompletableFuture} invocations.
*/
public class FreedomExecutor
public class ExecutorProvider
{
/**
* An executor which runs tasks synchronously.
@ -46,9 +46,9 @@ public class FreedomExecutor
private final Executor asyncExecutor;
/**
* Creates a new {@link FreedomExecutor} instance.
* Creates a new {@link ExecutorProvider} instance.
*/
public FreedomExecutor(final Patchwork patchwork)
public ExecutorProvider(final Patchwork patchwork)
{
syncExecutor = r -> Bukkit.getScheduler()
.runTask(patchwork, r);

View File

@ -21,8 +21,12 @@
* SOFTWARE.
*/
package fns.patchwork.service;
package fns.patchwork.provider;
import fns.patchwork.service.Service;
import fns.patchwork.service.ServiceSubscription;
import fns.patchwork.service.Task;
import fns.patchwork.service.TaskSubscription;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

View File

@ -0,0 +1,44 @@
/*
* This file is part of FreedomNetworkSuite - https://github.com/SimplexDevelopment/FreedomNetworkSuite
* Copyright (C) 2023 Simplex Development 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.registry;
import fns.patchwork.sql.SQL;
import java.util.Optional;
public class SQLRegistry
{
private SQL sql;
public SQLRegistry() {
this.sql = null;
}
public Optional<SQL> getSQL() {
return (sql == null) ? Optional.empty() : Optional.of(sql);
}
public void setSQL(final SQL sql) {
this.sql = sql;
}
}

View File

@ -25,7 +25,7 @@ package fns.patchwork.registry;
import fns.patchwork.service.Service;
import fns.patchwork.service.ServiceSubscription;
import fns.patchwork.service.SubscriptionProvider;
import fns.patchwork.provider.SubscriptionProvider;
import fns.patchwork.service.Task;
import fns.patchwork.service.TaskSubscription;
import java.util.ArrayList;

View File

@ -23,6 +23,8 @@
package fns.patchwork.service;
import fns.patchwork.provider.SubscriptionProvider;
/**
* Represents a ticking service. Services may be asynchronous or synchronous, however there are some restrictions:
* <ul>

View File

@ -31,7 +31,7 @@ public interface SQL
{
CompletableFuture<PreparedStatement> prepareStatement(final String query, final Object... args);
CompletableFuture<ResultSet> executeQuery(final String query, final Object... args);
CompletableFuture<SQLResult> executeQuery(final String query, final Object... args);
CompletableFuture<Integer> executeUpdate(final String query, final Object... args);

View File

@ -0,0 +1,348 @@
/*
* This file is part of FreedomNetworkSuite - https://github.com/SimplexDevelopment/FreedomNetworkSuite
* Copyright (C) 2023 Simplex Development 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.sql;
import fns.patchwork.utils.container.Pair;
import fns.patchwork.utils.logging.FNS4J;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
public class SQLResult
{
private final Map<Integer, Map<String, Object>> resultMap = new HashMap<>();
/**
* This constructor will create a new SQLResult object from the specified ResultSet.
* This will iterate through all rows and columns of the ResultSet and store them in a Map.
* The Map will contain keys of integers representing the row number, and values of Maps
* containing the column names and their values.
*
* @param resultSet The ResultSet to create the SQLResult object from.
*/
public SQLResult(final ResultSet resultSet)
{
try
{
final ResultSetMetaData metaData = resultSet.getMetaData();
final int columnCount = metaData.getColumnCount();
int rowIndex = 0;
while (resultSet.next())
{
rowIndex++;
final Map<String, Object> rowMap = new HashMap<>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++)
{
String columnName = metaData.getColumnName(columnIndex);
Object columnValue = resultSet.getObject(columnIndex);
rowMap.put(columnName, columnValue);
}
resultMap.put(rowIndex, rowMap);
}
}
catch (SQLException ex)
{
FNS4J.getLogger("Tyr").error(ex.getMessage());
}
}
/**
* This method will return a map of all rows and their columns and values.
*
* @return A Map containing all rows and their columns and values.
*/
public Map<Integer, Map<String, Object>> getResultMap()
{
return resultMap;
}
/**
* This method will return a map of all columns and their values from the specified row.
*
* @param rowIndex The row index to get the column names from.
* @return A Map containing all column names and their values from the specified row.
*/
public Map<String, Object> getRow(final int rowIndex)
{
return resultMap.get(rowIndex);
}
/**
* This method will return the value from the specified row and column.
*
* @param rowIndex The row index to get the column name from.
* @param columnName The column name to get the value from.
* @return The value from the specified row and column.
*/
public Object getValue(final int rowIndex, final String columnName)
{
return resultMap.get(rowIndex).get(columnName);
}
/**
* This method will return the first value from the first row of the result set.
*
* @return A Pair containing the column name and the stored value.
*/
public Pair<String, Object> getFirst()
{
return new Pair<>(resultMap.get(1).entrySet().iterator().next().getKey(),
resultMap.get(1).entrySet().iterator().next().getValue());
}
/**
* This method will return the first value from the specified row of the result set.
*
* @param rowIndex The row index to get the column name from.
* @return A Pair containing the column name and the stored value.
*/
public Pair<String, Object> getFirst(final int rowIndex)
{
return new Pair<>(resultMap.get(rowIndex).entrySet().iterator().next().getKey(),
resultMap.get(rowIndex).entrySet().iterator().next().getValue());
}
/**
* This method will return the last value from the first row of the result set.
*
* @return A Pair containing the column name and the stored value.
*/
public Pair<String, Object> getLast()
{
return new Pair<>(resultMap.get(1).entrySet().iterator().next().getKey(),
resultMap.get(1).entrySet().iterator().next().getValue());
}
/**
* This method will return the last value from the specified row of the result set.
*
* @param rowIndex The row index to get the column name from.
* @return A Pair containing the column name and the stored value.
*/
public Pair<String, Object> getLast(final int rowIndex)
{
return new Pair<>(resultMap.get(rowIndex).entrySet().iterator().next().getKey(),
resultMap.get(rowIndex).entrySet().iterator().next().getValue());
}
/**
* This method will attempt to retrieve the value from the specified row and column,
* and cast it to the specified class. This will throw a {@link ClassCastException} if the
* returned value is not an instance of the provided class.
*
* @param rowIndex The row index to get the column name from.
* @param columnName The column name to get the value from.
* @param clazz The class to cast the value to.
* @param <T> The expected type.
* @return The value from the specified row and column, cast to the specified class.
*/
public <T> T autoCast(final int rowIndex, final String columnName, final Class<T> clazz)
{
final Object value = resultMap.get(rowIndex).get(columnName);
if (!clazz.isInstance(value))
throw new ClassCastException("Cannot cast " + value.getClass().getName() + " to " + clazz.getName());
return clazz.cast(resultMap.get(rowIndex).get(columnName));
}
/**
* @param rowIndex The row index to get the column names from.
* @return A Set containing all column names from the specified row of the result set.
*/
public Set<String> getColumnNames(final int rowIndex)
{
return resultMap.get(rowIndex).keySet();
}
/**
* @return A Set containing all column names from the first row of the result set.
*/
public Set<String> getColumnNames()
{
return resultMap.get(1).keySet();
}
/**
* This method will apply the specified consumer to all rows of the result set.
*
* @param columnConsumer The consumer to apply to all rows of the result set.
*/
public void accept(final Consumer<Map<String, Object>> columnConsumer)
{
this.resultMap.forEach((integer, map) -> columnConsumer.accept(map));
}
/**
* Checks to see if the result set contains the specified row number.
* Best used in a for loop, using {@link #rowCount()} as the upper bound.
*
* @param rowIndex The row index to check.
* @return True if the result set contains the specified row number, false otherwise.
*/
public boolean hasNext(final int rowIndex)
{
return this.resultMap.containsKey(rowIndex + 1);
}
/**
* Checks to see if the result set has the first row.
* If row 1 doesn't exist, it's safe to say the result set is empty.
*
* @return True if the result set has row 1, false otherwise.
*/
public boolean hasNext()
{
return this.resultMap.containsKey(1);
}
/**
* @return The number of rows in the result set.
*/
public int rowCount()
{
return this.resultMap.size();
}
/**
* @param rowIndex The row index from which to count columns.
* @return The number of columns in the specified row.
*/
public int columnCount(final int rowIndex)
{
return this.resultMap.get(rowIndex).size();
}
/**
* Retrieves a String value from the specified row and column.
*
* @param rowIndex The row index to get the column name from.
* @param columnName The column name to get the value from.
* @return The String value from the specified row and column.
* @see #autoCast(int, String, Class)
*/
public String getString(final int rowIndex, final String columnName)
{
return autoCast(rowIndex, columnName, String.class);
}
/**
* This method will attempt to retrieve a String value from the specified column within the first row of the
* result set.
*
* @param columnName The column name to get the value from.
* @return The String value from the specified column within the first row of the result set.
* @see #getString(int, String)
*/
public String getString(final String columnName)
{
return getString(1, columnName);
}
/**
* Retrieves an Integer value from the specified row and column.
*
* @param rowIndex The row index to get the column name from.
* @param columnName The column name to get the value from.
* @return The Integer value from the specified row and column.
* @see #autoCast(int, String, Class)
*/
public int getInteger(final int rowIndex, final String columnName)
{
return autoCast(rowIndex, columnName, Integer.class);
}
/**
* This method will attempt to retrieve an Integer value from the specified column within the first row of the
* result set.
*
* @param columnName The column name to get the value from.
* @return The Integer value from the specified column within the first row of the result set.
* @see #getInteger(int, String)
*/
public int getInteger(final String columnName)
{
return getInteger(1, columnName);
}
/**
* Retrieves a Long value from the specified row and column.
*
* @param rowIndex The row index to get the column name from.
* @param columnName The column name to get the value from.
* @return The Long value from the specified row and column.
* @see #autoCast(int, String, Class)
*/
public long getLong(final int rowIndex, final String columnName)
{
return autoCast(rowIndex, columnName, Long.class);
}
/**
* This method will attempt to retrieve a Long value from the specified column within the first row of the
* result set.
*
* @param columnName The column name to get the value from.
* @return The Long value from the specified column within the first row of the result set.
* @see #getLong(int, String)
*/
public long getLong(final String columnName)
{
return getLong(1, columnName);
}
/**
* Retrieves a Double value from the specified row and column.
*
* @param rowIndex The row index to get the column name from.
* @param columnName The column name to get the value from.
* @return The Double value from the specified row and column.
* @see #autoCast(int, String, Class)
*/
public boolean getBoolean(final int rowIndex, final String columnName)
{
return autoCast(rowIndex, columnName, Boolean.class);
}
/**
* This method will attempt to retrieve a Boolean value from the specified column within the first row of the
* result set.
*
* @param columnName The column name to get the value from.
* @return The Boolean value from the specified column within the first row of the result set.
* @see #getBoolean(int, String)
*/
public boolean getBoolean(final String columnName)
{
return getBoolean(1, columnName);
}
}