Added UpdateManager and started with data system using GSON
This commit is contained in:
parent
b51f777836
commit
68157e19de
12 changed files with 382 additions and 15 deletions
1
pom.xml
1
pom.xml
|
@ -90,7 +90,6 @@
|
|||
<version>1.18.10</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<UUID> owners;
|
||||
@Getter @Setter private ArrayList<String> whitelist;
|
||||
//@Getter @Setter private ArrayList<String> whitelist;
|
||||
@Getter private final TreeSet<LiftBlock> blocks = new TreeSet<>();
|
||||
@Getter private final LinkedHashMap<String, Floor> floors = new LinkedHashMap<>();
|
||||
@Getter private final HashSet<LiftSign> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<LiftBlock> {
|
||||
|
@ -107,4 +108,33 @@ public class LiftBlock implements Comparable<LiftBlock> {
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
48
src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java
Normal file
48
src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java
Normal file
|
@ -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<Lift>(){}.getType());
|
||||
DataManager.addLift(liftSet.getString("liftName"), lift);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
HashMap<String, byte[]> inserts = new HashMap<>();
|
||||
Gson gson = new Gson();
|
||||
for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) {
|
||||
inserts.put(entry.getKey(), gson.toJson(entry.getValue()).getBytes());
|
||||
}
|
||||
//TODO Insert
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
data.closeSource();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer, Object> 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<Object, Object> where the first object is the rowname and the second object is the value
|
||||
*/
|
||||
public ResultSet execute(String query, HashMap<Integer, Object> objects, ArrayList<Object> requests) {
|
||||
public ResultSet execute(String query, HashMap<Integer, Object> 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<Integer, Object> entry : objects.entrySet()) {
|
||||
statement.setObject(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
|
121
src/main/java/nl/SBDeveloper/V10Lift/Utils/UpdateManager.java
Normal file
121
src/main/java/nl/SBDeveloper/V10Lift/Utils/UpdateManager.java
Normal file
|
@ -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 <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
|
||||
}
|
||||
|
||||
}
|
|
@ -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() {
|
||||
|
|
|
@ -1 +1,9 @@
|
|||
test: "jup"
|
||||
SignText: "[v10lift]"
|
||||
DefectRate: 0.0
|
||||
RepairItem: REDSTONE
|
||||
RepairAmount: 5
|
||||
MasterRepairItem: DIAMOND
|
||||
MasterRepairAmount: 10
|
||||
DefaultSpeed: 16
|
||||
DefaultRealistic: true
|
||||
DoorCloseTime: 100
|
Loading…
Reference in a new issue