Moved to SBUtilities <3
This commit is contained in:
parent
211b331d4d
commit
4696050b86
10 changed files with 56 additions and 302 deletions
10
pom.xml
10
pom.xml
|
@ -60,6 +60,10 @@
|
|||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>iobyte-repo</id>
|
||||
<url>https://nexus.iobyte.nl/repository/maven-public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
@ -90,6 +94,12 @@
|
|||
<version>1.18.10</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nl.SBDevelopment</groupId>
|
||||
<artifactId>SBUtilities</artifactId>
|
||||
<version>1.0</version>
|
||||
<classifier>SBUtilities</classifier>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
|
|
@ -3,10 +3,10 @@ package nl.SBDeveloper.V10Lift.API.Runnables;
|
|||
import nl.SBDeveloper.V10Lift.API.Objects.*;
|
||||
import nl.SBDeveloper.V10Lift.Managers.DataManager;
|
||||
import nl.SBDeveloper.V10Lift.Utils.DirectionUtil;
|
||||
import nl.SBDeveloper.V10Lift.Utils.LocationSerializer;
|
||||
import nl.SBDeveloper.V10Lift.Utils.XMaterial;
|
||||
import nl.SBDeveloper.V10Lift.Utils.XSound;
|
||||
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
|
||||
import nl.SBDevelopment.SBUtilities.Utils.LocationSerializer;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
|
|
|
@ -8,9 +8,9 @@ import nl.SBDeveloper.V10Lift.Managers.DataManager;
|
|||
import nl.SBDeveloper.V10Lift.Managers.ForbiddenBlockManager;
|
||||
import nl.SBDeveloper.V10Lift.Utils.ConfigUtil;
|
||||
import nl.SBDeveloper.V10Lift.Utils.DirectionUtil;
|
||||
import nl.SBDeveloper.V10Lift.Utils.LocationSerializer;
|
||||
import nl.SBDeveloper.V10Lift.Utils.XMaterial;
|
||||
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
|
||||
import nl.SBDevelopment.SBUtilities.Utils.LocationSerializer;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
|
|
|
@ -6,9 +6,9 @@ import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock;
|
|||
import nl.SBDeveloper.V10Lift.API.Objects.LiftSign;
|
||||
import nl.SBDeveloper.V10Lift.Managers.DataManager;
|
||||
import nl.SBDeveloper.V10Lift.Utils.ConfigUtil;
|
||||
import nl.SBDeveloper.V10Lift.Utils.LocationSerializer;
|
||||
import nl.SBDeveloper.V10Lift.Utils.XMaterial;
|
||||
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
|
||||
import nl.SBDevelopment.SBUtilities.Utils.LocationSerializer;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
|
|
|
@ -3,23 +3,22 @@ package nl.SBDeveloper.V10Lift.Managers;
|
|||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import nl.SBDeveloper.V10Lift.API.Objects.Lift;
|
||||
import nl.SBDeveloper.V10Lift.Utils.SBSQLiteDB;
|
||||
import nl.SBDevelopment.SBUtilities.Data.SQLiteDB;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.Map;
|
||||
|
||||
public class DBManager {
|
||||
|
||||
private static SBSQLiteDB data;
|
||||
private static SQLiteDB data;
|
||||
private static Connection con;
|
||||
|
||||
public DBManager(JavaPlugin plugin, String name) {
|
||||
data = new SBSQLiteDB(plugin, name);
|
||||
public DBManager(String name) {
|
||||
data = new SQLiteDB(name);
|
||||
|
||||
try {
|
||||
con = data.getConnection();
|
||||
|
@ -95,7 +94,33 @@ public class DBManager {
|
|||
Gson gson = new Gson();
|
||||
|
||||
for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) {
|
||||
byte[] blob = gson.toJson(entry.getValue()).getBytes();
|
||||
|
||||
|
||||
//Building JSON for debug purposes.
|
||||
String json = "{" +
|
||||
"blocks: " + gson.toJson(entry.getValue().getBlocks()) +
|
||||
"counter: " + gson.toJson(entry.getValue().getCounter()) +
|
||||
"doorcloser: " + gson.toJson(entry.getValue().getDoorCloser()) +
|
||||
"dooropen:" + gson.toJson(entry.getValue().getDoorOpen()) +
|
||||
"floors: " + gson.toJson(entry.getValue().getFloors()) +
|
||||
"inputs: " + gson.toJson(entry.getValue().getInputs()) +
|
||||
"offlineinputs: " + gson.toJson(entry.getValue().getOfflineInputs()) +
|
||||
"owners: " + gson.toJson(entry.getValue().getOwners()) +
|
||||
"queue: " + gson.toJson(entry.getValue().getQueue()) +
|
||||
"ropes: " + gson.toJson(entry.getValue().getRopes()) +
|
||||
"signs: " + gson.toJson(entry.getValue().getSigns()) +
|
||||
"signtext: " + gson.toJson(entry.getValue().getSignText()) +
|
||||
"speed: " + gson.toJson(entry.getValue().getSpeed()) +
|
||||
"tomove: " + gson.toJson(entry.getValue().getToMove()) +
|
||||
"worldname: " + gson.toJson(entry.getValue().getWorldName()) +
|
||||
"y: " + gson.toJson(entry.getValue().getY()) +
|
||||
"}";
|
||||
|
||||
Bukkit.getLogger().info(entry.getKey() + " : " + json);
|
||||
|
||||
Bukkit.getLogger().info(gson.toJson(entry.getValue()));
|
||||
|
||||
/*byte[] blob = gson.toJson(entry.getValue()).getBytes();
|
||||
|
||||
Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data...");
|
||||
|
||||
|
@ -115,7 +140,7 @@ public class DBManager {
|
|||
statement2.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
package nl.SBDeveloper.V10Lift.Utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class LocationSerializer {
|
||||
|
||||
//Hieronder de methodes zonder yaw & pitch!
|
||||
@Nonnull
|
||||
public static Location deserialize(@Nonnull String string) {
|
||||
String[] split = string.split("_");
|
||||
|
||||
//world_x_y_z
|
||||
return new Location(
|
||||
Bukkit.getWorld(split[0]),
|
||||
Double.parseDouble(split[1]),
|
||||
Double.parseDouble(split[2]),
|
||||
Double.parseDouble(split[3])
|
||||
);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String serialize(@Nonnull Location loc) {
|
||||
if (loc.getWorld() == null) return null;
|
||||
return loc.getWorld().getName() + "_" + loc.getX() + "_" + loc.getY() + "_" + loc.getZ();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
package nl.SBDeveloper.V10Lift.Utils;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.zaxxer.hikari.util.DriverDataSource;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
public class SBSQLiteDB {
|
||||
|
||||
private HikariDataSource source;
|
||||
|
||||
/**
|
||||
* Initialize a new connection
|
||||
*
|
||||
* @param plugin The plugin (Main class)
|
||||
* @param name The database name
|
||||
*/
|
||||
public SBSQLiteDB(@Nonnull Plugin plugin, String name) {
|
||||
Bukkit.getLogger().info("[V10Lift] Loading databases...");
|
||||
|
||||
File dbFile = new File(plugin.getDataFolder(), name + ".db");
|
||||
|
||||
if (!dbFile.exists()) {
|
||||
try {
|
||||
Bukkit.getLogger().info("[V10Lift] Generating the " + name + ".db!");
|
||||
dbFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
Bukkit.getLogger().severe("[V10Lift] Couldn't generate the " + name + ".db!");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
DataSource dataSource = new DriverDataSource("jdbc:sqlite:" + dbFile.getAbsolutePath(), "org.sqlite.JDBC", new Properties(), null, null);
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setPoolName("SQLiteConnectionPool");
|
||||
config.setDataSource(dataSource);
|
||||
|
||||
this.source = new HikariDataSource(config);
|
||||
}
|
||||
|
||||
//CREATE TABLE -> execute()
|
||||
//SELECT -> executeQuery()
|
||||
//UPDATE -> executeUpdate()
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
return this.source.getConnection();
|
||||
}
|
||||
|
||||
public void closeSource() {
|
||||
Bukkit.getLogger().info("[V10Lift] Closing the database connection!");
|
||||
this.source.close();
|
||||
}
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
package nl.SBDeveloper.V10Lift.Utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class SBYamlFile {
|
||||
//SBYamlFile file = new SBYamlFile(this, "data");
|
||||
|
||||
private FileConfiguration fileConfiguration;
|
||||
private File file;
|
||||
private String name;
|
||||
private Plugin pl;
|
||||
|
||||
public SBYamlFile(@Nonnull Plugin pl, String name) {
|
||||
this.pl = pl;
|
||||
this.name = name;
|
||||
|
||||
if (!pl.getDataFolder().exists()) {
|
||||
pl.getDataFolder().mkdir();
|
||||
}
|
||||
|
||||
this.file = new File(pl.getDataFolder(), name + ".yml");
|
||||
if (!this.file.exists()) {
|
||||
try {
|
||||
this.file.createNewFile();
|
||||
Bukkit.getLogger().info("[V10Lift] Generating the " + name + ".yml!");
|
||||
} catch (IOException e) {
|
||||
Bukkit.getLogger().severe("[V10Lift] Couldn't generate the " + name + ".yml!");
|
||||
}
|
||||
}
|
||||
this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
|
||||
}
|
||||
|
||||
public void loadDefaults() {
|
||||
Bukkit.getLogger().info("[V10Lift] Copying default " + name + ".yml to the folder, if needed!");
|
||||
Reader defConfigStream1 = new InputStreamReader(this.pl.getResource(name + ".yml"), StandardCharsets.UTF_8);
|
||||
YamlConfiguration defConfig1 = YamlConfiguration.loadConfiguration(defConfigStream1);
|
||||
getFile().setDefaults(defConfig1);
|
||||
getFile().options().copyDefaults(true);
|
||||
saveFile();
|
||||
}
|
||||
|
||||
public FileConfiguration getFile() {
|
||||
return this.fileConfiguration;
|
||||
}
|
||||
|
||||
public void saveFile() {
|
||||
try {
|
||||
this.fileConfiguration.save(this.file);
|
||||
} catch (IOException e) {
|
||||
Bukkit.getLogger().severe("[V10Lift] Couldn't save the " + name + ".yml!");
|
||||
}
|
||||
}
|
||||
|
||||
public void reloadConfig() {
|
||||
this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
|
||||
}
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
package nl.SBDeveloper.V10Lift.Utils;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* Update class for SBDevelopment
|
||||
* @author Stijn [SBDeveloper]
|
||||
* @since 12-01-2020
|
||||
* @version 1.1
|
||||
*
|
||||
* © Stijn Bannink <stijnbannink23@gmail.com> - All rights reserved.
|
||||
*/
|
||||
public class UpdateManager {
|
||||
|
||||
private static String SPIGOT_API = "http://api.spiget.org/v2/resources/%d/versions?size=1&sort=-releaseDate";
|
||||
private static String SBDPLUGINS_API = "http://updates.sbdplugins.nl:4000/api/resources/%d";
|
||||
|
||||
private Plugin plugin;
|
||||
private String currentVersion;
|
||||
private int resourceID;
|
||||
private CheckType type;
|
||||
private BiConsumer<VersionResponse, String> versionResponse;
|
||||
|
||||
/**
|
||||
* Construct a new UpdateManager
|
||||
*s
|
||||
* @param plugin The javaplugin (Main class)
|
||||
* @param resourceID The resourceID on spigot/sbdplugins
|
||||
* @param type The check type
|
||||
*/
|
||||
public UpdateManager(@Nonnull Plugin plugin, int resourceID, CheckType type) {
|
||||
this.plugin = plugin;
|
||||
this.currentVersion = plugin.getDescription().getVersion();
|
||||
this.resourceID = resourceID;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the response given by check();
|
||||
* @param versionResponse The response
|
||||
* @return The updatemanager
|
||||
*/
|
||||
public UpdateManager handleResponse(BiConsumer<VersionResponse, String> versionResponse) {
|
||||
this.versionResponse = versionResponse;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for a new version
|
||||
*/
|
||||
public void check() {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
||||
try {
|
||||
HttpURLConnection con = null;
|
||||
if (type == CheckType.SPIGOT) {
|
||||
con = (HttpURLConnection) new URL(String.format(SPIGOT_API, this.resourceID)).openConnection();
|
||||
} else if (type == CheckType.SBDPLUGINS) {
|
||||
con = (HttpURLConnection) new URL(String.format(SBDPLUGINS_API, this.resourceID)).openConnection();
|
||||
}
|
||||
|
||||
if (con == null) return;
|
||||
|
||||
con.setRequestMethod("GET");
|
||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
|
||||
String version = null;
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder response = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
response.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
JsonParser parser = new JsonParser();
|
||||
|
||||
if (type == CheckType.SPIGOT) {
|
||||
JsonArray array = parser.parse(response.toString()).getAsJsonArray();
|
||||
|
||||
version = array.get(0).getAsJsonObject().get("name").getAsString();
|
||||
} else if (type == CheckType.SBDPLUGINS) {
|
||||
JsonObject object = parser.parse(response.toString()).getAsJsonObject();
|
||||
|
||||
version = object.get("data").getAsJsonObject().get("version").getAsString();
|
||||
}
|
||||
|
||||
if (version == null) return;
|
||||
|
||||
boolean latestVersion = version.equalsIgnoreCase(this.currentVersion);
|
||||
|
||||
String finalVersion = version;
|
||||
Bukkit.getScheduler().runTask(this.plugin, () -> this.versionResponse.accept(latestVersion ? VersionResponse.LATEST : VersionResponse.FOUND_NEW, latestVersion ? this.currentVersion : finalVersion));
|
||||
} catch (IOException | NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getScheduler().runTask(this.plugin, () -> this.versionResponse.accept(VersionResponse.UNAVAILABLE, null));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public enum CheckType {
|
||||
SPIGOT, SBDPLUGINS
|
||||
}
|
||||
|
||||
public enum VersionResponse {
|
||||
LATEST, FOUND_NEW, UNAVAILABLE
|
||||
}
|
||||
|
||||
}
|
|
@ -7,8 +7,9 @@ import nl.SBDeveloper.V10Lift.Listeners.EntityDamageListener;
|
|||
import nl.SBDeveloper.V10Lift.Listeners.PlayerInteractListener;
|
||||
import nl.SBDeveloper.V10Lift.Listeners.SignChangeListener;
|
||||
import nl.SBDeveloper.V10Lift.Managers.DBManager;
|
||||
import nl.SBDeveloper.V10Lift.Utils.SBYamlFile;
|
||||
import nl.SBDeveloper.V10Lift.Utils.UpdateManager;
|
||||
import nl.SBDevelopment.SBUtilities.Data.YamlFile;
|
||||
import nl.SBDevelopment.SBUtilities.PrivateManagers.UpdateManager;
|
||||
import nl.SBDevelopment.SBUtilities.SBUtilities;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
|
@ -18,7 +19,7 @@ import java.util.Objects;
|
|||
public class V10LiftPlugin extends JavaPlugin {
|
||||
|
||||
private static V10LiftPlugin instance;
|
||||
private static SBYamlFile config;
|
||||
private static YamlFile config;
|
||||
private static DBManager dbManager;
|
||||
private static V10LiftAPI api;
|
||||
|
||||
|
@ -26,11 +27,13 @@ public class V10LiftPlugin extends JavaPlugin {
|
|||
public void onEnable() {
|
||||
instance = this;
|
||||
|
||||
config = new SBYamlFile(this, "config");
|
||||
//Initialize the util
|
||||
new SBUtilities(this, "[V10Lift]");
|
||||
|
||||
config = new YamlFile("config");
|
||||
config.loadDefaults();
|
||||
|
||||
dbManager = new DBManager(this, "data");
|
||||
|
||||
dbManager = new DBManager("data");
|
||||
try {
|
||||
dbManager.load();
|
||||
} catch (SQLException e) {
|
||||
|
@ -72,7 +75,7 @@ public class V10LiftPlugin extends JavaPlugin {
|
|||
return instance;
|
||||
}
|
||||
|
||||
public static SBYamlFile getSConfig() {
|
||||
public static YamlFile getSConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue