From c68acf5e5877ef08f3c6f4eb976ba43a2500b2ad Mon Sep 17 00:00:00 2001 From: Brokkonaut Date: Fri, 17 Aug 2018 00:26:42 +0200 Subject: [PATCH] Add converter for ModernLWC --- src/main/java/com/griefcraft/sql/PhysDB.java | 48 +++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/griefcraft/sql/PhysDB.java b/src/main/java/com/griefcraft/sql/PhysDB.java index 9d705054..8bfe48b0 100644 --- a/src/main/java/com/griefcraft/sql/PhysDB.java +++ b/src/main/java/com/griefcraft/sql/PhysDB.java @@ -46,6 +46,7 @@ import org.bukkit.Material; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -59,6 +60,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.UUID; @@ -280,6 +282,7 @@ public void load() { doUpdate400_5(); doUpdate400_6(); doUpdate5_0_12(); + doUpdateModernLWC(); Column column; @@ -2218,11 +2221,12 @@ private void doUpdate5_0_12() { } blockMappings.execute(); try { + statement.executeUpdate("UPDATE " + prefix + "protections SET blockId = -1 WHERE blockId IS NULL"); ResultSet rs = statement.executeQuery("SELECT DISTINCT blockId FROM " + prefix + "protections"); PreparedStatement insertSmt = prepare("INSERT INTO " + prefix + "blocks (`id`,`name`) VALUES (?, ?)"); while (rs.next()) { int id = rs.getInt("blockId"); - if (id != EntityBlock.ENTITY_BLOCK_ID) { + if (id >= 0 && id != EntityBlock.ENTITY_BLOCK_ID) { Material mat = Material.matchMaterial(Integer.toString(id)); if (mat != null) { insertSmt.setInt(1, id); @@ -2247,6 +2251,48 @@ private void doUpdate5_0_12() { } } + /** + * Update from ModernLWC + */ + private void doUpdateModernLWC() { + Statement statement = null; + try { + statement = connection.createStatement(); + ResultSet rs = statement.executeQuery("SELECT DISTINCT blockName FROM " + prefix + "protections"); + LWC.getInstance().getPlugin().getLogger().info("Upgrading from ModernLWC"); + try { + PreparedStatement updateSmt = prepare("UPDATE " + prefix + "protections SET blockId = ? WHERE blockName = ?"); + HashSet typeMap = new HashSet<>(); + typeMap.add("Entity"); + for (EntityType e : EntityType.values()) { + typeMap.add(e.name()); + } + while (rs.next()) { + String blockName = rs.getString(1); + if (typeMap.contains(blockName)) { + updateSmt.setInt(1, EntityBlock.ENTITY_BLOCK_ID); + updateSmt.setString(2, blockName); + updateSmt.executeUpdate(); + } + } + rs.close(); + statement.executeUpdate("ALTER TABLE " + prefix + "protections DROP COLUMN blockName"); + statement.executeUpdate("UPDATE " + prefix + "protections SET blockId = -1 WHERE blockId IS NULL"); + } catch (SQLException e) { + printException(e); + } + } catch (SQLException e) { + return; // column does not exist, ignore + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { + } + } + } + } + public HashMap loadBlockMappings() { HashMap rv = new HashMap<>(); Statement statement = null;