Fixed issues with ropes

This commit is contained in:
stijnb1234 2020-02-03 18:30:41 +01:00
parent 144f9cdaf1
commit c712ad1775
5 changed files with 120 additions and 22 deletions

View file

@ -3,12 +3,14 @@ package nl.SBDeveloper.V10Lift.API.Objects;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import java.lang.reflect.Field;
@Getter @Setter
public class LiftRope {
private final Material type;
private final BlockFace face;
private final String world;
private final int x;
private final int minY;
@ -16,8 +18,9 @@ public class LiftRope {
private final int z;
private int currently;
public LiftRope(Material type, String world, int x, int minY, int maxY, int z) {
public LiftRope(Material type, BlockFace face, String world, int x, int minY, int maxY, int z) {
this.type = type;
this.face = face;
this.world = world;
this.x = x;
this.minY = minY;

View file

@ -12,7 +12,6 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.block.data.Directional;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -72,6 +71,7 @@ public class MoveLift implements Runnable {
}
if (lift.getQueue().isEmpty() || lift.isOffline()) {
lift.setQueue(null);
stopMe();
return;
}
@ -96,6 +96,19 @@ public class MoveLift implements Runnable {
return;
}
lb = lift.getBlocks().last();
world = Bukkit.getWorld(lb.getWorld());
if (world == null) {
lift.setCounter(ft);
return;
}
loc = new Location(world, lb.getX(), lb.getY(), lb.getZ());
if (!loc.getChunk().isLoaded()) {
lift.setCounter(ft);
return;
}
double changeOfDefect = V10LiftPlugin.getSConfig().getFile().getDouble("DefectRate");
if (changeOfDefect > 0.0D) {
y = new Random().nextInt(100);
@ -132,7 +145,7 @@ public class MoveLift implements Runnable {
//MOVE ROPES
for (LiftRope rope : lift.getRopes()) {
if (rope.getCurrently() > rope.getMaxY()) {
Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!!");
Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!! 1");
V10LiftPlugin.getAPI().setDefective(liftName, true);
lift.getToMove().clear();
quiter.remove();
@ -387,20 +400,41 @@ public class MoveLift implements Runnable {
//MOVE ROPES
for (LiftRope rope : lift.getRopes()) {
if (rope.getCurrently() < rope.getMinY()) {
Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!!");
Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!! 2");
V10LiftPlugin.getAPI().setDefective(liftName, true);
lift.getToMove().clear();
quiter.remove();
rope.setCurrently(rope.getCurrently() - 1);
block = world.getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ());
block.setType(rope.getType());
if (XMaterial.isNewVersion()) {
org.bukkit.block.data.type.Ladder ladder = (org.bukkit.block.data.type.Ladder) block.getBlockData();
ladder.setFacing(rope.getFace());
} else {
BlockState state = block.getState();
org.bukkit.material.Ladder ladder = new org.bukkit.material.Ladder(rope.getType());
ladder.setFacingDirection(rope.getFace());
state.setData(ladder);
state.update(true);
}
return;
}
world = Objects.requireNonNull(Bukkit.getWorld(rope.getWorld()), "World is null at MoveLift");
rope.setCurrently(rope.getCurrently() - 1);
block = world.getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ());
block.setType(rope.getType());
if (XMaterial.isNewVersion()) {
org.bukkit.block.data.type.Ladder ladder = (org.bukkit.block.data.type.Ladder) block.getBlockData();
ladder.setFacing(rope.getFace());
} else {
BlockState state = block.getState();
org.bukkit.material.Ladder ladder = new org.bukkit.material.Ladder(rope.getType());
ladder.setFacingDirection(rope.getFace());
state.setData(ladder);
state.update(true);
}
}
} else {
lift.getToMove().clear();

View file

@ -13,6 +13,7 @@ import nl.SBDeveloper.V10Lift.Utils.XMaterial;
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
@ -121,6 +122,7 @@ public class V10LiftAPI {
}
DataManager.removeLift(liftName);
V10LiftPlugin.getDBManager().removeFromData(liftName);
return true;
}
@ -808,7 +810,13 @@ public class V10LiftAPI {
public int addRope(String lift, World world, int x, int minY, int maxY, int z) {
if (lift == null || !DataManager.containsLift(lift) || world == null) return -1;
boolean change = minY > maxY;
//minY = maxY, so reverse
if (minY > maxY) {
int tempY = minY;
minY = maxY;
maxY = tempY;
}
Block block = world.getBlockAt(x, minY, z);
if (isRope(block)) return -3;
Material mat = block.getType();
@ -819,7 +827,21 @@ public class V10LiftAPI {
if (isRope(block)) return -3;
if (block.getType() != mat) return -2;
}
DataManager.getLift(lift).getRopes().add(new LiftRope(mat, world.getName(), x, minY, maxY, z));
BlockFace face;
if (XMaterial.isNewVersion()) {
org.bukkit.block.data.type.Ladder ladder = (org.bukkit.block.data.type.Ladder) block.getBlockData();
Bukkit.getLogger().info(ladder.getFacing().toString());
face = ladder.getFacing();
} else {
BlockState state = block.getState();
org.bukkit.material.Ladder ladder = (org.bukkit.material.Ladder) state.getData();
face = ladder.getAttachedFace();
}
LiftRope rope = new LiftRope(mat, face, world.getName(), x, minY, maxY, z);
DataManager.getLift(lift).getRopes().add(rope);
return 0;
}

View file

@ -7,6 +7,7 @@ import nl.SBDeveloper.V10Lift.Utils.SBSQLiteDB;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -15,13 +16,16 @@ import java.util.*;
public class DBManager {
private static SBSQLiteDB data;
private static Connection con;
public DBManager(JavaPlugin plugin, String name) {
data = new SBSQLiteDB(plugin, name);
try {
con = data.getConnection();
String query = "CREATE TABLE IF NOT EXISTS lifts (liftName varchar(255) NOT NULL, liftData blob NOT NULL, UNIQUE (liftName))";
PreparedStatement statement = data.getConnection().prepareStatement(query);
PreparedStatement statement = con.prepareStatement(query);
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
@ -30,10 +34,18 @@ public class DBManager {
public void load() throws SQLException {
String query = "SELECT * FROM lifts";
PreparedStatement statement = data.getConnection().prepareStatement(query);
PreparedStatement statement = con.prepareStatement(query);
ResultSet liftSet = statement.executeQuery();
while (liftSet.next()) {
//Loading a lift...
/*
* @todo Fix migrating from 1.12.2- to 1.13+
* - byte to Facing for signs
* - Facing opposite for ropes
* - New materials
*/
byte[] blob = liftSet.getBytes("liftData");
String json = new String(blob);
Gson gson = new Gson();
@ -44,42 +56,67 @@ public class DBManager {
}
}
public void save() {
public void removeFromData() {
try {
Gson gson = new Gson();
String query0 = "SELECT * FROM lifts";
PreparedStatement statement0 = data.getConnection().prepareStatement(query0);
PreparedStatement statement0 = con.prepareStatement(query0);
ResultSet liftSet = statement0.executeQuery();
while (liftSet.next()) {
if (!DataManager.containsLift(liftSet.getString("liftName"))) {
Bukkit.getLogger().info("[V10Lift] Removing lift " + liftSet.getString("liftName") + " to data...");
String query = "DELETE FROM lifts WHERE liftName = ?";
PreparedStatement statement = data.getConnection().prepareStatement(query);
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, liftSet.getString("liftName"));
statement.executeUpdate();
}
}
} catch(SQLException e) {
e.printStackTrace();
}
}
public void removeFromData(String name) {
try {
if (!DataManager.containsLift(name)) {
Bukkit.getLogger().info("[V10Lift] Removing lift " + name + " to data...");
String query = "DELETE FROM lifts WHERE liftName = ?";
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, name);
statement.executeUpdate();
}
} catch(SQLException e) {
e.printStackTrace();
}
}
public void save() {
Gson gson = new Gson();
for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) {
byte[] blob = gson.toJson(entry.getValue()).getBytes();
Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data...");
try {
String query = "INSERT INTO lifts (liftName, liftData) VALUES (?, ?)";
PreparedStatement statement = data.getConnection().prepareStatement(query);
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, entry.getKey());
statement.setBytes(2, blob);
statement.executeUpdate();
} catch (SQLException ignored) {}
try {
String query2 = "UPDATE lifts SET liftData = ? WHERE liftName = ?";
PreparedStatement statement2 = data.getConnection().prepareStatement(query2);
PreparedStatement statement2 = con.prepareStatement(query2);
statement2.setBytes(1, blob);
statement2.setString(2, entry.getKey());
statement2.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch(SQLException ignored) {}
}
public void closeConnection() {

View file

@ -61,9 +61,11 @@ public class V10LiftPlugin extends JavaPlugin {
@Override
public void onDisable() {
instance = null;
V10LiftPlugin.getDBManager().removeFromData();
dbManager.save();
dbManager.closeConnection();
instance = null;
}
public static V10LiftPlugin getInstance() {