forked from LogBlock/LogBlock
Make MaterialConverter type safe
This commit is contained in:
@ -230,7 +230,7 @@ public class Consumer extends Thread {
|
|||||||
* true if the item was removed
|
* true if the item was removed
|
||||||
*/
|
*/
|
||||||
public void queueChestAccess(Actor actor, Location loc, BlockData type, ItemStack itemStack, boolean remove) {
|
public void queueChestAccess(Actor actor, Location loc, BlockData type, ItemStack itemStack, boolean remove) {
|
||||||
queueBlock(actor, loc, type, type, null, null, new ChestAccess(itemStack, remove, MaterialConverter.getOrAddMaterialId(itemStack.getType().getKey())));
|
queueBlock(actor, loc, type, type, null, null, new ChestAccess(itemStack, remove, MaterialConverter.getOrAddMaterialId(itemStack.getType())));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -327,7 +327,7 @@ public class Consumer extends Thread {
|
|||||||
if (victim == null || !isLogged(location.getWorld())) {
|
if (victim == null || !isLogged(location.getWorld())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
addQueueLast(new KillRow(location, killer == null ? null : killer, victim, weapon == null ? 0 : MaterialConverter.getOrAddMaterialId(weapon.getType().getKey().toString())));
|
addQueueLast(new KillRow(location, killer == null ? null : killer, victim, weapon == null ? 0 : MaterialConverter.getOrAddMaterialId(weapon.getType())));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -718,12 +718,10 @@ public class Consumer extends Thread {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String replacedString = typeBefore.getAsString();
|
int replacedMaterialId = MaterialConverter.getOrAddMaterialId(typeBefore);
|
||||||
int replacedMaterialId = MaterialConverter.getOrAddMaterialId(replacedString);
|
int replacedStateId = MaterialConverter.getOrAddBlockStateId(typeBefore);
|
||||||
int replacedStateId = MaterialConverter.getOrAddBlockStateId(replacedString);
|
int typeMaterialId = MaterialConverter.getOrAddMaterialId(typeAfter);
|
||||||
String typeString = typeAfter.getAsString();
|
int typeStateId = MaterialConverter.getOrAddBlockStateId(typeAfter);
|
||||||
int typeMaterialId = MaterialConverter.getOrAddMaterialId(typeString);
|
|
||||||
int typeStateId = MaterialConverter.getOrAddBlockStateId(typeString);
|
|
||||||
|
|
||||||
addQueueLast(new BlockRow(loc, actor, replacedMaterialId, replacedStateId, Utils.serializeYamlConfiguration(stateBefore), typeMaterialId, typeStateId, Utils.serializeYamlConfiguration(stateAfter), ca));
|
addQueueLast(new BlockRow(loc, actor, replacedMaterialId, replacedStateId, Utils.serializeYamlConfiguration(stateBefore), typeMaterialId, typeStateId, Utils.serializeYamlConfiguration(stateAfter), ca));
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,7 @@ public class LogBlock extends JavaPlugin {
|
|||||||
Updater updater = new Updater(this);
|
Updater updater = new Updater(this);
|
||||||
updater.checkTables();
|
updater.checkTables();
|
||||||
MaterialConverter.initializeMaterials(getConnection());
|
MaterialConverter.initializeMaterials(getConnection());
|
||||||
MaterialConverter.getOrAddMaterialId(Material.AIR.getKey()); // AIR must be the first entry
|
MaterialConverter.getOrAddMaterialId(Material.AIR); // AIR must be the first entry
|
||||||
EntityTypeConverter.initializeEntityTypes(getConnection());
|
EntityTypeConverter.initializeEntityTypes(getConnection());
|
||||||
if (updater.update()) {
|
if (updater.update()) {
|
||||||
load(this);
|
load(this);
|
||||||
|
@ -11,7 +11,6 @@ import java.util.logging.Level;
|
|||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.block.data.BlockData;
|
import org.bukkit.block.data.BlockData;
|
||||||
|
|
||||||
public class MaterialConverter {
|
public class MaterialConverter {
|
||||||
@ -31,16 +30,15 @@ public class MaterialConverter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getOrAddMaterialId(NamespacedKey nameSpaceKey) {
|
public static int getOrAddMaterialId(BlockData blockData) {
|
||||||
return getOrAddMaterialId(nameSpaceKey.toString());
|
return getOrAddMaterialId(blockData == null ? Material.AIR : blockData.getMaterial());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getOrAddMaterialId(String blockDataString) {
|
public static int getOrAddMaterialId(Material material) {
|
||||||
String materialString = blockDataString;
|
if (material == null) {
|
||||||
int dataPart = blockDataString.indexOf("[");
|
material = Material.AIR;
|
||||||
if (dataPart >= 0) {
|
|
||||||
materialString = blockDataString.substring(0, dataPart);
|
|
||||||
}
|
}
|
||||||
|
String materialString = material.getKey().toString();
|
||||||
Integer key = materialToID.get(materialString);
|
Integer key = materialToID.get(materialString);
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
while (key == null && tries < 10) {
|
while (key == null && tries < 10) {
|
||||||
@ -78,7 +76,11 @@ public class MaterialConverter {
|
|||||||
return key.intValue();
|
return key.intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getOrAddBlockStateId(String blockDataString) {
|
public static int getOrAddBlockStateId(BlockData blockData) {
|
||||||
|
if (blockData == null) {
|
||||||
|
blockData = Material.AIR.createBlockData();
|
||||||
|
}
|
||||||
|
String blockDataString = blockData.getAsString();
|
||||||
int dataPart = blockDataString.indexOf("[");
|
int dataPart = blockDataString.indexOf("[");
|
||||||
if (dataPart < 0) {
|
if (dataPart < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -756,7 +756,7 @@ public final class QueryParams implements Cloneable {
|
|||||||
throw new IllegalArgumentException("No material matching: '" + weaponName + "'");
|
throw new IllegalArgumentException("No material matching: '" + weaponName + "'");
|
||||||
}
|
}
|
||||||
types.add(mat);
|
types.add(mat);
|
||||||
typeIds.add(MaterialConverter.getOrAddMaterialId(mat.getKey()));
|
typeIds.add(MaterialConverter.getOrAddMaterialId(mat));
|
||||||
}
|
}
|
||||||
needWeapon = true;
|
needWeapon = true;
|
||||||
} else if (param.equals("block") || param.equals("type")) {
|
} else if (param.equals("block") || param.equals("type")) {
|
||||||
@ -778,7 +778,7 @@ public final class QueryParams implements Cloneable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Material mat : tag.getValues()) {
|
for (Material mat : tag.getValues()) {
|
||||||
typeIds.add(MaterialConverter.getOrAddMaterialId(mat.getKey()));
|
typeIds.add(MaterialConverter.getOrAddMaterialId(mat));
|
||||||
}
|
}
|
||||||
typeTags.add(tag);
|
typeTags.add(tag);
|
||||||
} else if (blockName.contains("*")) {
|
} else if (blockName.contains("*")) {
|
||||||
@ -807,7 +807,7 @@ public final class QueryParams implements Cloneable {
|
|||||||
}
|
}
|
||||||
for (Material mat : matched) {
|
for (Material mat : matched) {
|
||||||
types.add(mat);
|
types.add(mat);
|
||||||
typeIds.add(MaterialConverter.getOrAddMaterialId(mat.getKey()));
|
typeIds.add(MaterialConverter.getOrAddMaterialId(mat));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final Material mat = Material.matchMaterial(blockName);
|
final Material mat = Material.matchMaterial(blockName);
|
||||||
@ -815,7 +815,7 @@ public final class QueryParams implements Cloneable {
|
|||||||
throw new IllegalArgumentException("No material matching: '" + blockName + "'");
|
throw new IllegalArgumentException("No material matching: '" + blockName + "'");
|
||||||
}
|
}
|
||||||
types.add(mat);
|
types.add(mat);
|
||||||
typeIds.add(MaterialConverter.getOrAddMaterialId(mat.getKey()));
|
typeIds.add(MaterialConverter.getOrAddMaterialId(mat));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (param.equals("area")) {
|
} else if (param.equals("area")) {
|
||||||
|
@ -454,8 +454,8 @@ class Updater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String replacedBlockData = materialUpdater.getBlockData(replaced, data).getAsString();
|
BlockData replacedBlockData = materialUpdater.getBlockData(replaced, data);
|
||||||
String setBlockData = materialUpdater.getBlockData(type, data).getAsString();
|
BlockData setBlockData = materialUpdater.getBlockData(type, data);
|
||||||
|
|
||||||
int newReplacedId = MaterialConverter.getOrAddMaterialId(replacedBlockData);
|
int newReplacedId = MaterialConverter.getOrAddMaterialId(replacedBlockData);
|
||||||
int newReplacedData = MaterialConverter.getOrAddBlockStateId(replacedBlockData);
|
int newReplacedData = MaterialConverter.getOrAddBlockStateId(replacedBlockData);
|
||||||
@ -536,7 +536,7 @@ class Updater {
|
|||||||
insertChestData.setInt(1, id);
|
insertChestData.setInt(1, id);
|
||||||
insertChestData.setBytes(2, Utils.saveItemStack(stack));
|
insertChestData.setBytes(2, Utils.saveItemStack(stack));
|
||||||
insertChestData.setInt(3, amount >= 0 ? 0 : 1);
|
insertChestData.setInt(3, amount >= 0 ? 0 : 1);
|
||||||
insertChestData.setInt(4, MaterialConverter.getOrAddMaterialId(weaponMaterial.getKey()));
|
insertChestData.setInt(4, MaterialConverter.getOrAddMaterialId(weaponMaterial));
|
||||||
insertChestData.addBatch();
|
insertChestData.addBatch();
|
||||||
|
|
||||||
deleteChest.setInt(1, id);
|
deleteChest.setInt(1, id);
|
||||||
@ -592,7 +592,7 @@ class Updater {
|
|||||||
if (weaponMaterial == null) {
|
if (weaponMaterial == null) {
|
||||||
weaponMaterial = Material.AIR;
|
weaponMaterial = Material.AIR;
|
||||||
}
|
}
|
||||||
int newWeapon = MaterialConverter.getOrAddMaterialId(weaponMaterial.getKey());
|
int newWeapon = MaterialConverter.getOrAddMaterialId(weaponMaterial);
|
||||||
if (newWeapon != weapon) {
|
if (newWeapon != weapon) {
|
||||||
anyUpdate = true;
|
anyUpdate = true;
|
||||||
updateWeaponStatement.setInt(1, newWeapon);
|
updateWeaponStatement.setInt(1, newWeapon);
|
||||||
@ -860,19 +860,19 @@ class Updater {
|
|||||||
|
|
||||||
if (comparablePreviousMinecraftVersion.compareTo("1.14") < 0 && comparableCurrentMinecraftVersion.compareTo("1.14") >= 0) {
|
if (comparablePreviousMinecraftVersion.compareTo("1.14") < 0 && comparableCurrentMinecraftVersion.compareTo("1.14") >= 0) {
|
||||||
logblock.getLogger().info("[Updater] Upgrading Materials to 1.14");
|
logblock.getLogger().info("[Updater] Upgrading Materials to 1.14");
|
||||||
renameMaterial("minecraft:sign", "minecraft:oak_sign");
|
renameMaterial("minecraft:sign", Material.OAK_SIGN);
|
||||||
renameMaterial("minecraft:wall_sign", "minecraft:oak_wall_sign");
|
renameMaterial("minecraft:wall_sign", Material.OAK_WALL_SIGN);
|
||||||
renameMaterial("minecraft:stone_slab", "minecraft:smooth_stone_slab");
|
renameMaterial("minecraft:stone_slab", Material.SMOOTH_STONE_SLAB);
|
||||||
renameMaterial("minecraft:rose_red", "minecraft:red_dye");
|
renameMaterial("minecraft:rose_red", Material.RED_DYE);
|
||||||
renameMaterial("minecraft:dandelion_yellow", "minecraft:yellow_dye");
|
renameMaterial("minecraft:dandelion_yellow", Material.YELLOW_DYE);
|
||||||
renameMaterial("minecraft:cactus_green", "minecraft:green_dye");
|
renameMaterial("minecraft:cactus_green", Material.GREEN_DYE);
|
||||||
}
|
}
|
||||||
|
|
||||||
config.set("previousMinecraftVersion", currentMinecraftVersion);
|
config.set("previousMinecraftVersion", currentMinecraftVersion);
|
||||||
logblock.saveConfig();
|
logblock.saveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renameMaterial(String oldName, String newName) {
|
private void renameMaterial(String oldName, Material newName) {
|
||||||
final Connection conn = logblock.getConnection();
|
final Connection conn = logblock.getConnection();
|
||||||
try {
|
try {
|
||||||
conn.setAutoCommit(false);
|
conn.setAutoCommit(false);
|
||||||
|
Reference in New Issue
Block a user