Fixed confliction error and moved to Jackson for faster binding

This commit is contained in:
stijnb1234 2020-02-06 20:47:47 +01:00
parent 4696050b86
commit 79f37ee2bc
6 changed files with 51 additions and 64 deletions

View file

@ -14,6 +14,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<jackson.version>2.10.0</jackson.version>
</properties> </properties>
<build> <build>
@ -99,6 +100,12 @@
<artifactId>SBUtilities</artifactId> <artifactId>SBUtilities</artifactId>
<version>1.0</version> <version>1.0</version>
<classifier>SBUtilities</classifier> <classifier>SBUtilities</classifier>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -2,6 +2,7 @@ package nl.SBDeveloper.V10Lift.API.Objects;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -10,52 +11,50 @@ import java.util.UUID;
@Getter @Getter
public class V10Entity { public class V10Entity {
private final Entity entity; private final UUID entityUUID;
private final Location loc; private String world;
private int locX;
private int locY;
private int locZ;
private final int y; private final int y;
@Setter private short step; @Setter private short step;
public V10Entity(Entity entity, Location loc, int y) { public V10Entity(UUID entityUUID, String worldName, int x, int y, int z, int cury) {
this.entity = entity; this.entityUUID = entityUUID;
this.loc = loc; this.world = worldName;
this.y = y; this.locX = x;
this.locY = y;
this.locZ = z;
this.y = cury;
this.step = 0; this.step = 0;
} }
public void moveUp() { public void moveUp() {
if (entityUUID == null) return;
Entity entity = Bukkit.getEntity(entityUUID);
if (entity == null || entity.isDead()) return; if (entity == null || entity.isDead()) return;
loc.setY(y + step); locY = y + step;
entity.teleport(loc); entity.teleport(new Location(Bukkit.getWorld(world), locX, locY, locZ));
} }
public void moveDown() { public void moveDown() {
if (entityUUID == null) return;
Entity entity = Bukkit.getEntity(entityUUID);
if (entity == null || entity.isDead()) return; if (entity == null || entity.isDead()) return;
loc.setY(y - step); locY = y - step;
entity.teleport(loc); entity.teleport(new Location(Bukkit.getWorld(world), locX, locY, locZ));
} }
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj) return true;
if (obj == null) return false; if (obj == null) return false;
UUID uuid;
if (obj instanceof V10Entity) { if (obj instanceof V10Entity) {
Entity ent = ((V10Entity) obj).getEntity(); return ((V10Entity) obj).getEntityUUID().equals(getEntityUUID());
if (ent == null || ent.isDead()) {
return getEntity() == null || getEntity().isDead();
}
uuid = ent.getUniqueId();
} else if (obj instanceof Entity) { } else if (obj instanceof Entity) {
Entity ent = (Entity) obj; return ((Entity) obj).getUniqueId().equals(getEntityUUID());
if (ent.isDead()) {
return getEntity() == null || getEntity().isDead();
}
uuid = ent.getUniqueId();
} else { } else {
return false; return false;
} }
if (getEntity() == null || getEntity().isDead()) return false;
return uuid == getEntity().getUniqueId();
} }
public String toString() { public String toString() {

View file

@ -209,7 +209,7 @@ public class MoveLift implements Runnable {
} }
lb = lift.getBlocks().first(); 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()) { 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; if (lift.getToMove().contains(v10ent)) continue;
loc = ent.getLocation(); loc = ent.getLocation();
y = loc.getBlockY(); y = loc.getBlockY();

View file

@ -1,5 +1,6 @@
package nl.SBDeveloper.V10Lift.Commands; package nl.SBDeveloper.V10Lift.Commands;
import com.fasterxml.jackson.core.JsonProcessingException;
import nl.SBDeveloper.V10Lift.API.Objects.Floor; import nl.SBDeveloper.V10Lift.API.Objects.Floor;
import nl.SBDeveloper.V10Lift.API.Objects.Lift; import nl.SBDeveloper.V10Lift.API.Objects.Lift;
import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock;
@ -250,10 +251,10 @@ public class V10LiftCommand implements CommandExecutor {
DataManager.clearMovingTasks(); DataManager.clearMovingTasks();
V10LiftPlugin.getSConfig().reloadConfig(); V10LiftPlugin.getSConfig().reloadConfig();
V10LiftPlugin.getDBManager().save();
try { try {
V10LiftPlugin.getDBManager().save();
V10LiftPlugin.getDBManager().load(); V10LiftPlugin.getDBManager().load();
} catch (SQLException e) { } catch (SQLException | JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -1,7 +1,7 @@
package nl.SBDeveloper.V10Lift.Managers; package nl.SBDeveloper.V10Lift.Managers;
import com.google.gson.Gson; import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.reflect.TypeToken; import com.fasterxml.jackson.databind.ObjectMapper;
import nl.SBDeveloper.V10Lift.API.Objects.Lift; import nl.SBDeveloper.V10Lift.API.Objects.Lift;
import nl.SBDevelopment.SBUtilities.Data.SQLiteDB; import nl.SBDevelopment.SBUtilities.Data.SQLiteDB;
import org.bukkit.Bukkit; 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"; String query = "SELECT * FROM lifts";
PreparedStatement statement = con.prepareStatement(query); PreparedStatement statement = con.prepareStatement(query);
ResultSet liftSet = statement.executeQuery(); ResultSet liftSet = statement.executeQuery();
@ -47,8 +47,8 @@ public class DBManager {
byte[] blob = liftSet.getBytes("liftData"); byte[] blob = liftSet.getBytes("liftData");
String json = new String(blob); String json = new String(blob);
Gson gson = new Gson(); ObjectMapper mapper = new ObjectMapper();
Lift lift = gson.fromJson(json, new TypeToken<Lift>(){}.getType()); Lift lift = mapper.readValue(json, Lift.class);
DataManager.addLift(liftSet.getString("liftName"), lift); DataManager.addLift(liftSet.getString("liftName"), lift);
Bukkit.getLogger().info("[V10Lift] Loading lift " + liftSet.getString("liftName") + " from data..."); Bukkit.getLogger().info("[V10Lift] Loading lift " + liftSet.getString("liftName") + " from data...");
@ -90,37 +90,12 @@ public class DBManager {
} }
} }
public void save() { public void save() throws JsonProcessingException {
Gson gson = new Gson();
for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) { for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) {
ObjectMapper mapper = new ObjectMapper();
//Building JSON for debug purposes. byte[] blob = mapper.writeValueAsString(entry.getValue()).getBytes();
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..."); Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data...");
@ -140,7 +115,7 @@ public class DBManager {
statement2.executeUpdate(); statement2.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
}*/ }
} }
} }

View file

@ -1,5 +1,6 @@
package nl.SBDeveloper.V10Lift; package nl.SBDeveloper.V10Lift;
import com.fasterxml.jackson.core.JsonProcessingException;
import nl.SBDeveloper.V10Lift.API.V10LiftAPI; import nl.SBDeveloper.V10Lift.API.V10LiftAPI;
import nl.SBDeveloper.V10Lift.Commands.V10LiftCommand; import nl.SBDeveloper.V10Lift.Commands.V10LiftCommand;
import nl.SBDeveloper.V10Lift.Listeners.BlockBreakListener; import nl.SBDeveloper.V10Lift.Listeners.BlockBreakListener;
@ -28,7 +29,7 @@ public class V10LiftPlugin extends JavaPlugin {
instance = this; instance = this;
//Initialize the util //Initialize the util
new SBUtilities(this, "[V10Lift]"); new SBUtilities(this, "V10Lift");
config = new YamlFile("config"); config = new YamlFile("config");
config.loadDefaults(); config.loadDefaults();
@ -36,7 +37,7 @@ public class V10LiftPlugin extends JavaPlugin {
dbManager = new DBManager("data"); dbManager = new DBManager("data");
try { try {
dbManager.load(); dbManager.load();
} catch (SQLException e) { } catch (SQLException | JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -65,7 +66,11 @@ public class V10LiftPlugin extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
V10LiftPlugin.getDBManager().removeFromData(); V10LiftPlugin.getDBManager().removeFromData();
dbManager.save(); try {
dbManager.save();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
dbManager.closeConnection(); dbManager.closeConnection();
instance = null; instance = null;