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

View file

@ -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() {

View file

@ -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();

View file

@ -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();
}

View file

@ -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<Lift>(){}.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<String, Lift> 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();
}*/
}
}
}

View file

@ -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;