From 79f37ee2bc292d37c15e0a16fa1ca3f52c9a1bc6 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Thu, 6 Feb 2020 20:47:47 +0100 Subject: [PATCH] Fixed confliction error and moved to Jackson for faster binding --- pom.xml | 7 +++ .../V10Lift/API/Objects/V10Entity.java | 47 +++++++++---------- .../V10Lift/API/Runnables/MoveLift.java | 2 +- .../V10Lift/Commands/V10LiftCommand.java | 5 +- .../V10Lift/Managers/DBManager.java | 43 ++++------------- .../nl/SBDeveloper/V10Lift/V10LiftPlugin.java | 11 +++-- 6 files changed, 51 insertions(+), 64 deletions(-) diff --git a/pom.xml b/pom.xml index 480c833..11e8f08 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ UTF-8 1.8 1.8 + 2.10.0 @@ -99,6 +100,12 @@ SBUtilities 1.0 SBUtilities + compile + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} 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 969300c..042ec96 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/V10Entity.java @@ -2,6 +2,7 @@ package nl.SBDeveloper.V10Lift.API.Objects; import lombok.Getter; import lombok.Setter; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Entity; @@ -10,52 +11,50 @@ import java.util.UUID; @Getter public class V10Entity { - private final Entity entity; - private final Location loc; + private final UUID entityUUID; + private String world; + private int locX; + private int locY; + private int locZ; private final int y; @Setter private short step; - public V10Entity(Entity entity, Location loc, int y) { - this.entity = entity; - this.loc = loc; - this.y = y; + public V10Entity(UUID entityUUID, String worldName, int x, int y, int z, int cury) { + this.entityUUID = entityUUID; + this.world = worldName; + this.locX = x; + this.locY = y; + this.locZ = z; + this.y = cury; this.step = 0; } public void moveUp() { + if (entityUUID == null) return; + Entity entity = Bukkit.getEntity(entityUUID); if (entity == null || entity.isDead()) return; - loc.setY(y + step); - entity.teleport(loc); + locY = y + step; + entity.teleport(new Location(Bukkit.getWorld(world), locX, locY, locZ)); } public void moveDown() { + if (entityUUID == null) return; + Entity entity = Bukkit.getEntity(entityUUID); if (entity == null || entity.isDead()) return; - loc.setY(y - step); - entity.teleport(loc); + locY = y - step; + entity.teleport(new Location(Bukkit.getWorld(world), locX, locY, locZ)); } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; - UUID uuid; if (obj instanceof V10Entity) { - Entity ent = ((V10Entity) obj).getEntity(); - if (ent == null || ent.isDead()) { - return getEntity() == null || getEntity().isDead(); - } - uuid = ent.getUniqueId(); + return ((V10Entity) obj).getEntityUUID().equals(getEntityUUID()); } else if (obj instanceof Entity) { - Entity ent = (Entity) obj; - if (ent.isDead()) { - return getEntity() == null || getEntity().isDead(); - } - uuid = ent.getUniqueId(); + return ((Entity) obj).getUniqueId().equals(getEntityUUID()); } else { return false; } - - if (getEntity() == null || getEntity().isDead()) return false; - return uuid == getEntity().getUniqueId(); } public String toString() { diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java index 5a51c4b..62d0f23 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java @@ -209,7 +209,7 @@ public class MoveLift implements Runnable { } lb = lift.getBlocks().first(); for (Entity ent : Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift").getBlockAt(lib.getX(), lib.getY(), lib.getZ()).getChunk().getEntities()) { - v10ent = new V10Entity(ent, null, 0); + v10ent = new V10Entity(ent.getUniqueId(), null, 0, 0, 0, 0); if (lift.getToMove().contains(v10ent)) continue; loc = ent.getLocation(); y = loc.getBlockY(); diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java b/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java index 7334b2b..c6b1578 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java @@ -1,5 +1,6 @@ package nl.SBDeveloper.V10Lift.Commands; +import com.fasterxml.jackson.core.JsonProcessingException; import nl.SBDeveloper.V10Lift.API.Objects.Floor; import nl.SBDeveloper.V10Lift.API.Objects.Lift; import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; @@ -250,10 +251,10 @@ public class V10LiftCommand implements CommandExecutor { DataManager.clearMovingTasks(); V10LiftPlugin.getSConfig().reloadConfig(); - V10LiftPlugin.getDBManager().save(); try { + V10LiftPlugin.getDBManager().save(); V10LiftPlugin.getDBManager().load(); - } catch (SQLException e) { + } catch (SQLException | JsonProcessingException e) { e.printStackTrace(); } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java index 2f38240..2e16aa5 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DBManager.java @@ -1,7 +1,7 @@ package nl.SBDeveloper.V10Lift.Managers; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import nl.SBDeveloper.V10Lift.API.Objects.Lift; import nl.SBDevelopment.SBUtilities.Data.SQLiteDB; import org.bukkit.Bukkit; @@ -31,7 +31,7 @@ public class DBManager { } } - public void load() throws SQLException { + public void load() throws SQLException, JsonProcessingException { String query = "SELECT * FROM lifts"; PreparedStatement statement = con.prepareStatement(query); ResultSet liftSet = statement.executeQuery(); @@ -47,8 +47,8 @@ public class DBManager { byte[] blob = liftSet.getBytes("liftData"); String json = new String(blob); - Gson gson = new Gson(); - Lift lift = gson.fromJson(json, new TypeToken(){}.getType()); + ObjectMapper mapper = new ObjectMapper(); + Lift lift = mapper.readValue(json, Lift.class); DataManager.addLift(liftSet.getString("liftName"), lift); Bukkit.getLogger().info("[V10Lift] Loading lift " + liftSet.getString("liftName") + " from data..."); @@ -90,37 +90,12 @@ public class DBManager { } } - public void save() { - Gson gson = new Gson(); - + public void save() throws JsonProcessingException { for (Map.Entry entry : DataManager.getLifts().entrySet()) { + ObjectMapper mapper = new ObjectMapper(); - //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(); + byte[] blob = mapper.writeValueAsString(entry.getValue()).getBytes(); Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data..."); @@ -140,7 +115,7 @@ public class DBManager { statement2.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); - }*/ + } } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index 298c7fb..f1dcf0c 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -1,5 +1,6 @@ package nl.SBDeveloper.V10Lift; +import com.fasterxml.jackson.core.JsonProcessingException; import nl.SBDeveloper.V10Lift.API.V10LiftAPI; import nl.SBDeveloper.V10Lift.Commands.V10LiftCommand; import nl.SBDeveloper.V10Lift.Listeners.BlockBreakListener; @@ -28,7 +29,7 @@ public class V10LiftPlugin extends JavaPlugin { instance = this; //Initialize the util - new SBUtilities(this, "[V10Lift]"); + new SBUtilities(this, "V10Lift"); config = new YamlFile("config"); config.loadDefaults(); @@ -36,7 +37,7 @@ public class V10LiftPlugin extends JavaPlugin { dbManager = new DBManager("data"); try { dbManager.load(); - } catch (SQLException e) { + } catch (SQLException | JsonProcessingException e) { e.printStackTrace(); } @@ -65,7 +66,11 @@ public class V10LiftPlugin extends JavaPlugin { @Override public void onDisable() { V10LiftPlugin.getDBManager().removeFromData(); - dbManager.save(); + try { + dbManager.save(); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } dbManager.closeConnection(); instance = null;