Skip to content

Commit

Permalink
Add converter for ModernLWC
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Aug 16, 2018
1 parent 438869f commit c68acf5
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/main/java/com/griefcraft/sql/PhysDB.java
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -280,6 +282,7 @@ public void load() {
doUpdate400_5();
doUpdate400_6();
doUpdate5_0_12();
doUpdateModernLWC();

Column column;

Expand Down Expand Up @@ -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);
Expand All @@ -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<String> 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<Integer, String> loadBlockMappings() {
HashMap<Integer, String> rv = new HashMap<>();
Statement statement = null;
Expand Down

0 comments on commit c68acf5

Please sign in to comment.