packs is going to yell at me

Added custom Config classes
Added Mongo / SQLite / SQL switches
Setup main config.yml
This commit is contained in:
spacerocket62 2020-10-27 11:14:34 -07:00
parent cc40d83c95
commit 5162f3fcfb
13 changed files with 379 additions and 0 deletions

38
pom.xml
View File

@ -30,6 +30,18 @@
<version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>core</artifactId>
<version>1.5.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@ -39,5 +51,31 @@
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,21 +1,55 @@
package me.totalfreedom.plex;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.plex.config.Config;
import me.totalfreedom.plex.config.YamlConfig;
import me.totalfreedom.plex.storage.MongoConnection;
import me.totalfreedom.plex.storage.SQLConnection;
import me.totalfreedom.plex.storage.StorageType;
import me.totalfreedom.plex.util.PlexLog;
import me.totalfreedom.plex.util.PlexUtils;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
@Setter
public class Plex extends JavaPlugin
{
@Setter(AccessLevel.NONE)
private static Plex plugin;
private StorageType storageType;
private SQLConnection sqlConnection;
private MongoConnection mongoConnection;
@Override
public void onLoad()
{
plugin = this;
getConfig().options().copyDefaults(true);
saveConfig();
saveResource("database.db", false);
sqlConnection = new SQLConnection();
mongoConnection = new MongoConnection();
}
@Override
public void onEnable()
{
PlexUtils.testConnections();
}
@Override
public void onDisable()
{
}
public static Plex get() {
return plugin;
}
}

View File

@ -0,0 +1,40 @@
package me.totalfreedom.plex.config;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.util.PlexLog;
import java.io.File;
import java.io.IOException;
public class Config
{
private File file;
public Config(String name, boolean copy)
{
if (copy)
{
Plex.get().saveResource(name, false);
} else {
file = new File(Plex.get().getDataFolder(), name);
if (!file.exists())
{
try {
file.createNewFile();
PlexLog.log("Generating " + name + " configuration file!");
} catch (IOException e) {
PlexLog.error(String.format("An error occured trying to create the following file: %s", name));
e.printStackTrace();
}
} else {
PlexLog.log(name + " configuration file was loaded.");
}
}
}
public File getFile() {
return file;
}
}

View File

@ -0,0 +1,44 @@
package me.totalfreedom.plex.config;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
public class YamlConfig
{
private YamlConfiguration config;
private File file;
public YamlConfig(File file)
{
this.file = file;
this.config = YamlConfiguration.loadConfiguration(file);
}
public YamlConfiguration get() {
return config;
}
public void save()
{
try {
this.config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void reload()
{
try {
this.config.load(file);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,53 @@
package me.totalfreedom.plex.player;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.IndexOptions;
import dev.morphia.annotations.Indexed;
import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.plex.storage.MongoConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Getter
@Setter
@Entity(value = "players", noClassnameStored = true)
public class PlexPlayer
{
@Id
private String id;
@Indexed(options = @IndexOptions(unique = true))
private String uuid;
@Indexed
private String name;
private List<String> ips;
private boolean muted;
private boolean frozen;
//insert Rank check
public PlexPlayer(){}
public PlexPlayer(UUID playerUUID)
{
this.uuid = playerUUID.toString();
this.id = uuid.substring(0, 8);
this.name = "";
this.ips = new ArrayList<>();
this.muted = false;
this.frozen = false;
}
}

View File

@ -0,0 +1,44 @@
package me.totalfreedom.plex.storage;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.player.PlexPlayer;
public class MongoConnection
{
// USE MORPHIA API FOR MONGO <3
private Plex plugin = Plex.get();
public Datastore getDatastore()
{
if (!plugin.getConfig().getString("data.central.storage").equalsIgnoreCase("mongodb"))
{
return null;
}
String host = plugin.getConfig().getString("data.central.hostname");
int port = plugin.getConfig().getInt("data.central.port");
String username = plugin.getConfig().getString("data.central.user");
String password = plugin.getConfig().getString("data.central.password");
String database = plugin.getConfig().getString("data.central.database");
String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
MongoClient client = new MongoClient(new MongoClientURI(connectionString));
Morphia morphia = new Morphia();
Datastore datastore = morphia.createDatastore(client, database);
datastore.getMapper().addMappedClass(PlexPlayer.class);
datastore.ensureIndexes();
plugin.setStorageType(StorageType.MONGO);
return datastore;
}
}

View File

@ -0,0 +1,42 @@
package me.totalfreedom.plex.storage;
import me.totalfreedom.plex.Plex;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLConnection
{
private Plex plugin = Plex.get();
private Connection connection;
public Connection getCon()
{
String host = plugin.getConfig().getString("data.central.hostname");
int port = plugin.getConfig().getInt("data.central.port");
String username = plugin.getConfig().getString("data.central.user");
String password = plugin.getConfig().getString("data.central.password");
String database = plugin.getConfig().getString("data.central.database");
try {
if (plugin.getConfig().getString("data.central.storage").equalsIgnoreCase("sqlite"))
{
connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), "database.db").getAbsolutePath());
Plex.get().setStorageType(StorageType.SQLITE);
}
else if (plugin.getConfig().getString("data.central.storage").equalsIgnoreCase("mysql"))
{
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
Plex.get().setStorageType(StorageType.MONGO);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
}

View File

@ -0,0 +1,7 @@
package me.totalfreedom.plex.storage;
public enum StorageType
{
MONGO, SQL, SQLITE;
}

View File

@ -0,0 +1,18 @@
package me.totalfreedom.plex.util;
import me.totalfreedom.plex.Plex;
public class PlexLog
{
public static void log(String message)
{
Plex.get().getServer().getConsoleSender().sendMessage(String.format("§e[Plex] §7%s", message));
}
public static void error(String message)
{
Plex.get().getServer().getConsoleSender().sendMessage(String.format("§c[Plex Error] §6%s", message));
}
}

View File

@ -0,0 +1,32 @@
package me.totalfreedom.plex.util;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.storage.StorageType;
import java.sql.SQLException;
public class PlexUtils
{
public static void testConnections()
{
if (Plex.get().getSqlConnection().getCon() != null)
{
if (Plex.get().getStorageType() == StorageType.SQL)
{
PlexLog.log("Successfully enabled MySQL!");
} else if (Plex.get().getStorageType() == StorageType.SQLITE)
{
PlexLog.log("Successfully enabled SQLite!");
}
try {
Plex.get().getSqlConnection().getCon().close();
} catch (SQLException throwables) {
}
} else if (Plex.get().getMongoConnection() != null)
{
PlexLog.log("Successfully enabled MongoDB!");
}
}
}

View File

@ -0,0 +1,26 @@
# -------------------------------#
# #
# P L E X #
# #
# -------------------------------#
# ------------------------------ #
# #
# You can use MySQL, MongoDB #
# or SQLite for the data #
# type #
# -------------------------------#
data:
central:
storage: sqlite
user: ""
password: ""
hostname: 127.0.0.1
port: 27017
db: "plex"
side: # This is redis, leave password blank if auth is false
auth: true
hostname: 127.0.0.1
port: 6379
password: ""

View File

View File

@ -2,3 +2,4 @@ name: ${project.name}
version: ${project.version}
author: Telesphoreo
main: me.totalfreedom.plex.Plex
api-version: 1.16