From 68157e19de57fbdb31c834825f1c96f92310440c Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Sun, 2 Feb 2020 21:01:28 +0100 Subject: [PATCH] Added UpdateManager and started with data system using GSON --- pom.xml | 1 - .../V10Lift/API/Objects/Floor.java | 30 +++++ .../SBDeveloper/V10Lift/API/Objects/Lift.java | 32 ++++- .../V10Lift/API/Objects/LiftBlock.java | 30 +++++ .../V10Lift/API/Objects/LiftRope.java | 31 +++++ .../V10Lift/API/Objects/LiftSign.java | 33 ++++- .../V10Lift/API/Objects/V10Entity.java | 30 +++++ .../V10Lift/Managers/DBManager.java | 48 +++++++ .../SBDeveloper/V10Lift/Utils/SBSQLiteDB.java | 7 +- .../V10Lift/Utils/UpdateManager.java | 121 ++++++++++++++++++ .../nl/SBDeveloper/V10Lift/V10LiftPlugin.java | 24 +++- src/main/resources/config.yml | 10 +- 12 files changed, 382 insertions(+), 15 deletions(-) create mode 100644 src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java create mode 100644 src/main/java/nl/SBDeveloper/V10Lift/Utils/UpdateManager.java diff --git a/pom.xml b/pom.xml index 7c70711..3ba4158 100644 --- a/pom.xml +++ b/pom.xml @@ -90,7 +90,6 @@ 1.18.10 provided - diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java index d2e3541..d03aca8 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Floor.java @@ -3,6 +3,7 @@ package nl.SBDeveloper.V10Lift.API.Objects; import lombok.Getter; import lombok.Setter; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashSet; import java.util.UUID; @@ -31,4 +32,33 @@ public class Floor { } return getY() == other.getY(); } + + public String toString() { + StringBuilder result = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + + result.append(this.getClass().getName()); + result.append(" Object {"); + result.append(newLine); + + //determine fields declared in this class only (no fields of superclass) + Field[] fields = this.getClass().getDeclaredFields(); + + //print field names paired with their values + for (Field field: fields) { + result.append(" "); + try { + result.append(field.getName()); + result.append(": "); + //requires access to private field: + result.append(field.get(this)); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + result.append(newLine); + } + result.append("}"); + + return result.toString(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Lift.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Lift.java index 48680ff..4b1b02f 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Lift.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/Lift.java @@ -4,13 +4,14 @@ import lombok.Getter; import lombok.Setter; import nl.SBDeveloper.V10Lift.API.Runnables.DoorCloser; +import java.lang.reflect.Field; import java.util.*; public class Lift { @Getter @Setter private String worldName; @Getter @Setter private int y; @Getter private final HashSet owners; - @Getter @Setter private ArrayList whitelist; + //@Getter @Setter private ArrayList whitelist; @Getter private final TreeSet blocks = new TreeSet<>(); @Getter private final LinkedHashMap floors = new LinkedHashMap<>(); @Getter private final HashSet signs = new HashSet<>(); @@ -42,4 +43,33 @@ public class Lift { this.speed = speed; this.realistic = realistic; } + + public String toString() { + StringBuilder result = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + + result.append(this.getClass().getName()); + result.append(" Object {"); + result.append(newLine); + + //determine fields declared in this class only (no fields of superclass) + Field[] fields = this.getClass().getDeclaredFields(); + + //print field names paired with their values + for (Field field: fields) { + result.append(" "); + try { + result.append(field.getName()); + result.append(": "); + //requires access to private field: + result.append(field.get(this)); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + result.append(newLine); + } + result.append("}"); + + return result.toString(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java index 423e661..957af9e 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java @@ -6,6 +6,7 @@ import org.bukkit.Material; import org.bukkit.block.BlockFace; import javax.annotation.Nonnull; +import java.lang.reflect.Field; import java.util.Map; public class LiftBlock implements Comparable { @@ -107,4 +108,33 @@ public class LiftBlock implements Comparable { LiftBlock other = (LiftBlock) obj; return getWorld().equals(other.getWorld()) && getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); } + + public String toString() { + StringBuilder result = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + + result.append(this.getClass().getName()); + result.append(" Object {"); + result.append(newLine); + + //determine fields declared in this class only (no fields of superclass) + Field[] fields = this.getClass().getDeclaredFields(); + + //print field names paired with their values + for (Field field: fields) { + result.append(" "); + try { + result.append(field.getName()); + result.append(": "); + //requires access to private field: + result.append(field.get(this)); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + result.append(newLine); + } + result.append("}"); + + return result.toString(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java index 007f5a9..0b334bd 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftRope.java @@ -4,6 +4,8 @@ import lombok.Getter; import lombok.Setter; import org.bukkit.Material; +import java.lang.reflect.Field; + @Getter @Setter public class LiftRope { private final Material type; @@ -35,4 +37,33 @@ public class LiftRope { && getMaxY() == other.getMaxY() && getZ() == other.getZ(); } + + public String toString() { + StringBuilder result = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + + result.append(this.getClass().getName()); + result.append(" Object {"); + result.append(newLine); + + //determine fields declared in this class only (no fields of superclass) + Field[] fields = this.getClass().getDeclaredFields(); + + //print field names paired with their values + for (Field field: fields) { + result.append(" "); + try { + result.append(field.getName()); + result.append(": "); + //requires access to private field: + result.append(field.get(this)); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + result.append(newLine); + } + result.append("}"); + + return result.toString(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java index 78feeb4..d684271 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftSign.java @@ -2,8 +2,8 @@ package nl.SBDeveloper.V10Lift.API.Objects; import lombok.Getter; import lombok.Setter; -import org.bukkit.Rotation; -import org.bukkit.block.BlockFace; + +import java.lang.reflect.Field; @Getter @Setter public class LiftSign { @@ -34,4 +34,33 @@ public class LiftSign { LiftSign other = (LiftSign) obj; return getWorld().equals(other.getWorld()) && getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); } + + public String toString() { + StringBuilder result = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + + result.append(this.getClass().getName()); + result.append(" Object {"); + result.append(newLine); + + //determine fields declared in this class only (no fields of superclass) + Field[] fields = this.getClass().getDeclaredFields(); + + //print field names paired with their values + for (Field field: fields) { + result.append(" "); + try { + result.append(field.getName()); + result.append(": "); + //requires access to private field: + result.append(field.get(this)); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + result.append(newLine); + } + result.append("}"); + + return result.toString(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java index 31ee625..969300c 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java @@ -5,6 +5,7 @@ import lombok.Setter; import org.bukkit.Location; import org.bukkit.entity.Entity; +import java.lang.reflect.Field; import java.util.UUID; @Getter @@ -56,4 +57,33 @@ public class V10Entity { if (getEntity() == null || getEntity().isDead()) return false; return uuid == getEntity().getUniqueId(); } + + public String toString() { + StringBuilder result = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + + result.append(this.getClass().getName()); + result.append(" Object {"); + result.append(newLine); + + //determine fields declared in this class only (no fields of superclass) + Field[] fields = this.getClass().getDeclaredFields(); + + //print field names paired with their values + for (Field field: fields) { + result.append(" "); + try { + result.append(field.getName()); + result.append(": "); + //requires access to private field: + result.append(field.get(this)); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + result.append(newLine); + } + result.append("}"); + + return result.toString(); + } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java new file mode 100644 index 0000000..aff505b --- /dev/null +++ b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java @@ -0,0 +1,48 @@ +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 org.bukkit.plugin.java.JavaPlugin; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; + +public class DBManager { + + private static SBSQLiteDB data; + + public DBManager(JavaPlugin plugin, String name) { + data = new SBSQLiteDB(plugin, name); + + data.execute("CREATE TABLE IF NOT EXISTS lifts (liftName varchar(255) NOT NULL, liftData blob NOT NULL, UNIQUE (liftName))"); + } + + public void load() throws SQLException { + ResultSet liftSet = data.execute("SELECT * FROM lifts", new HashMap<>()); + while (liftSet.next()) { + //Loading a lift... + byte[] blob = liftSet.getBytes("liftData"); + String json = new String(blob); + Gson gson = new Gson(); + Lift lift = gson.fromJson(json, new TypeToken(){}.getType()); + DataManager.addLift(liftSet.getString("liftName"), lift); + } + } + + public void save() { + HashMap inserts = new HashMap<>(); + Gson gson = new Gson(); + for (Map.Entry entry : DataManager.getLifts().entrySet()) { + inserts.put(entry.getKey(), gson.toJson(entry.getValue()).getBytes()); + } + //TODO Insert + } + + public void closeConnection() { + data.closeSource(); + } + +} diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java b/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java index 1aa26cb..5eb4ce9 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Utils/SBSQLiteDB.java @@ -96,7 +96,7 @@ public class SBSQLiteDB { try { con = this.source.getConnection(); statement = con.prepareStatement(query); - if (objects != null) { + if (objects != null && !objects.isEmpty()) { for (Map.Entry entry : objects.entrySet()) { statement.setObject(entry.getKey(), entry.getValue()); } @@ -122,11 +122,10 @@ public class SBSQLiteDB { * * @param query The query you want to execute * @param objects The objects you want to insert, in the right order - * @param requests The objects you want to select from the database * * @return HashMap where the first object is the rowname and the second object is the value */ - public ResultSet execute(String query, HashMap objects, ArrayList requests) { + public ResultSet execute(String query, HashMap objects) { Connection con = null; PreparedStatement statement = null; ResultSet set = null; @@ -134,7 +133,7 @@ public class SBSQLiteDB { try { con = this.source.getConnection(); statement = con.prepareStatement(query); - if (objects != null) { + if (objects != null && !objects.isEmpty()) { for (Map.Entry entry : objects.entrySet()) { statement.setObject(entry.getKey(), entry.getValue()); } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Utils/UpdateManager.java b/src/main/java/nl/SBDeveloper/V10Lift/Utils/UpdateManager.java new file mode 100644 index 0000000..0ba160e --- /dev/null +++ b/src/main/java/nl/SBDeveloper/V10Lift/Utils/UpdateManager.java @@ -0,0 +1,121 @@ +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 - 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; + + /** + * 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) { + 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 + } + +} \ No newline at end of file diff --git a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index 4994b8f..80a394d 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -6,17 +6,17 @@ import nl.SBDeveloper.V10Lift.Listeners.BlockBreakListener; import nl.SBDeveloper.V10Lift.Listeners.EntityDamageListener; import nl.SBDeveloper.V10Lift.Listeners.PlayerInteractListener; import nl.SBDeveloper.V10Lift.Listeners.SignChangeListener; -import nl.SBDeveloper.V10Lift.Utils.SBSQLiteDB; +import nl.SBDeveloper.V10Lift.Managers.DBManager; import nl.SBDeveloper.V10Lift.Utils.SBYamlFile; +import nl.SBDeveloper.V10Lift.Utils.UpdateManager; import org.bukkit.Bukkit; -import org.bukkit.block.Sign; import org.bukkit.plugin.java.JavaPlugin; public class V10LiftPlugin extends JavaPlugin { private static V10LiftPlugin instance; private static SBYamlFile config; - private static SBSQLiteDB data; + private static DBManager dbManager; private static V10LiftAPI api; @Override @@ -25,7 +25,8 @@ public class V10LiftPlugin extends JavaPlugin { config = new SBYamlFile(this, "config"); config.loadDefaults(); - data = new SBSQLiteDB(this, "data"); + + dbManager = new DBManager(this, "data"); api = new V10LiftAPI(); @@ -36,12 +37,23 @@ public class V10LiftPlugin extends JavaPlugin { Bukkit.getPluginManager().registerEvents(new SignChangeListener(), this); Bukkit.getPluginManager().registerEvents(new EntityDamageListener(), this); + new UpdateManager(this, 72317, UpdateManager.CheckType.SPIGOT).handleResponse((versionResponse, version) -> { + if (versionResponse == UpdateManager.VersionResponse.FOUND_NEW) { + Bukkit.getLogger().warning("[V10Lift] There is a new version available! Current: " + this.getDescription().getVersion() + " New: " + version); + } else if (versionResponse == UpdateManager.VersionResponse.LATEST) { + Bukkit.getLogger().info("[V10Lift] You are running the latest version [" + this.getDescription().getVersion() + "]!"); + } else if (versionResponse == UpdateManager.VersionResponse.UNAVAILABLE) { + Bukkit.getLogger().severe("[V10Lift] Unable to perform an update check."); + } + }).check(); + getLogger().info("[V10Lift] Plugin loaded successfully!"); } @Override public void onDisable() { instance = null; + dbManager.closeConnection(); } public static V10LiftPlugin getInstance() { @@ -52,8 +64,8 @@ public class V10LiftPlugin extends JavaPlugin { return config; } - public static SBSQLiteDB getData() { - return data; + public static DBManager getDBManager() { + return dbManager; } public static V10LiftAPI getAPI() { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4ba0571..967d5f5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1 +1,9 @@ -test: "jup" \ No newline at end of file +SignText: "[v10lift]" +DefectRate: 0.0 +RepairItem: REDSTONE +RepairAmount: 5 +MasterRepairItem: DIAMOND +MasterRepairAmount: 10 +DefaultSpeed: 16 +DefaultRealistic: true +DoorCloseTime: 100 \ No newline at end of file