fix mongo support

This commit is contained in:
Taah
2022-04-07 00:37:31 -07:00
parent b7926830f0
commit 9ddf3ceeda
21 changed files with 264 additions and 174 deletions

View File

@ -6,6 +6,20 @@ import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.config.Config;
import dev.plex.storage.StorageType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@ -16,38 +30,9 @@ import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameRule;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class PlexUtils extends PlexBase
{
@ -87,18 +72,25 @@ public class PlexUtils extends PlexBase
public static void testConnections()
{
try (Connection con = Plex.get().getSqlConnection().getCon())
if (Plex.get().getSqlConnection().getDataSource() != null)
{
if (Plex.get().getStorageType() == StorageType.MARIADB)
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PlexLog.log("Successfully enabled MySQL!");
}
else if (Plex.get().getStorageType() == StorageType.SQLITE)
if (Plex.get().getStorageType() == StorageType.MARIADB)
{
PlexLog.log("Successfully enabled MySQL!");
} else if (Plex.get().getStorageType() == StorageType.SQLITE)
{
PlexLog.log("Successfully enabled SQLite!");
}
} catch (SQLException e)
{
PlexLog.log("Successfully enabled SQLite!");
if (Plex.get().getMongoConnection().getDatastore() != null)
{
PlexLog.log("Successfully enabled MongoDB!");
}
}
}
catch (SQLException e)
} else
{
if (Plex.get().getMongoConnection().getDatastore() != null)
{
@ -209,12 +201,10 @@ public class PlexUtils extends PlexBase
if (config.getString(path) == null)
{
color = def;
}
else if (ChatColor.getByChar(config.getString(path)) == null)
} else if (ChatColor.getByChar(config.getString(path)) == null)
{
color = def;
}
else
} else
{
color = ChatColor.getByChar(config.getString(path));
}
@ -260,14 +250,13 @@ public class PlexUtils extends PlexBase
private static <T> void readGameRules(World world, String s)
{
String gameRule = s.split(";")[0];
T value = (T)s.split(";")[1];
GameRule<T> rule = (GameRule<T>)GameRule.getByName(gameRule);
T value = (T) s.split(";")[1];
GameRule<T> rule = (GameRule<T>) GameRule.getByName(gameRule);
if (rule != null && check(value).getClass().equals(rule.getType()))
{
world.setGameRule(rule, value);
PlexLog.debug("Setting game rule " + gameRule + " for world " + world.getName() + " with value " + value);
}
else
} else
{
PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value));
}
@ -307,7 +296,7 @@ public class PlexUtils extends PlexBase
try
{
URL u = new URL(url);
HttpURLConnection connection = (HttpURLConnection)u.openConnection();
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
@ -319,8 +308,7 @@ public class PlexUtils extends PlexBase
in.close();
connection.disconnect();
return new JSONParser().parse(content.toString());
}
catch (IOException | ParseException ex)
} catch (IOException | ParseException ex)
{
return null;
}
@ -329,13 +317,13 @@ public class PlexUtils extends PlexBase
public static UUID getFromName(String name)
{
JSONObject profile;
profile = (JSONObject)simpleGET("https://api.ashcon.app/mojang/v2/user/" + name);
profile = (JSONObject) simpleGET("https://api.ashcon.app/mojang/v2/user/" + name);
if (profile == null)
{
PlexLog.error("Profile from Ashcon API returned null!");
return null;
}
String uuidString = (String)profile.get("uuid");
String uuidString = (String) profile.get("uuid");
return UUID.fromString(uuidString);
}
@ -353,14 +341,12 @@ public class PlexUtils extends PlexBase
{
Class<?> clazz = Class.forName(info.getName());
classes.add(clazz);
}
catch (ClassNotFoundException ex)
} catch (ClassNotFoundException ex)
{
PlexLog.error("Unable to find class " + info.getName() + " in " + packageName);
}
});
}
catch (IOException ex)
} catch (IOException ex)
{
PlexLog.error("Something went wrong while fetching classes from " + packageName);
throw new RuntimeException(ex);
@ -377,7 +363,7 @@ public class PlexUtils extends PlexBase
{
if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()).contains(subType))
{
classes.add((Class<? extends T>)clazz);
classes.add((Class<? extends T>) clazz);
}
});
return Collections.unmodifiableSet(classes);