Access MaterialConverter only from main thread, avoid sync

This commit is contained in:
Brokkonaut
2018-07-21 15:11:50 +02:00
parent 0792fbd32a
commit 6d35da12e3
2 changed files with 856 additions and 848 deletions

View File

@@ -31,11 +31,11 @@ public class MaterialConverter {
} }
} }
public synchronized static int getOrAddMaterialId(NamespacedKey nameSpaceKey) { public static int getOrAddMaterialId(NamespacedKey nameSpaceKey) {
return getOrAddMaterialId(nameSpaceKey.toString()); return getOrAddMaterialId(nameSpaceKey.toString());
} }
public synchronized static int getOrAddMaterialId(String blockDataString) { public static int getOrAddMaterialId(String blockDataString) {
String materialString = blockDataString; String materialString = blockDataString;
int dataPart = blockDataString.indexOf("["); int dataPart = blockDataString.indexOf("[");
if (dataPart >= 0) { if (dataPart >= 0) {
@@ -58,7 +58,7 @@ public class MaterialConverter {
return key.intValue(); return key.intValue();
} }
public synchronized static int getOrAddBlockStateId(String blockDataString) { public static int getOrAddBlockStateId(String blockDataString) {
int dataPart = blockDataString.indexOf("["); int dataPart = blockDataString.indexOf("[");
if (dataPart < 0) { if (dataPart < 0) {
return -1; return -1;
@@ -81,7 +81,7 @@ public class MaterialConverter {
return key.intValue(); return key.intValue();
} }
public synchronized static BlockData getBlockData(int materialId, int blockStateId) { public static BlockData getBlockData(int materialId, int blockStateId) {
String material = idToMaterial[materialId]; String material = idToMaterial[materialId];
if (blockStateId >= 0) { if (blockStateId >= 0) {
material = material + idToBlockState[blockStateId]; material = material + idToBlockState[blockStateId];

View File

@@ -29,6 +29,7 @@ public final class QueryParams implements Cloneable {
public RegionContainer sel = null; public RegionContainer sel = null;
public SummarizationMode sum = SummarizationMode.NONE; public SummarizationMode sum = SummarizationMode.NONE;
public List<Material> types = new ArrayList<Material>(); public List<Material> types = new ArrayList<Material>();
public List<Integer> typeIds = new ArrayList<Integer>();
public World world = null; public World world = null;
public String match = null; public String match = null;
public boolean needCount = false, needId = false, needDate = false, needType = false, needData = false, needPlayer = false, needCoords = false, needSignText = false, needChestAccess = false, needMessage = false, needKiller = false, needVictim = false, needWeapon = false; public boolean needCount = false, needId = false, needDate = false, needType = false, needData = false, needPlayer = false, needCoords = false, needSignText = false, needChestAccess = false, needMessage = false, needKiller = false, needVictim = false, needWeapon = false;
@@ -354,13 +355,13 @@ public final class QueryParams implements Cloneable {
} else { } else {
switch (blockChangeType) { switch (blockChangeType) {
case ALL: case ALL:
if (!types.isEmpty()) { if (!typeIds.isEmpty()) {
if (excludeBlocksMode) { if (excludeBlocksMode) {
where.append("NOT "); where.append("NOT ");
} }
where.append('('); where.append('(');
for (final Material block : types) { for (final Integer block : typeIds) {
where.append("((type = ").append(MaterialConverter.getOrAddMaterialId(block.getKey())).append(" OR replaced = ").append(MaterialConverter.getOrAddMaterialId(block.getKey())); where.append("((type = ").append(block).append(" OR replaced = ").append(block);
where.append(")"); where.append(")");
where.append(") OR "); where.append(") OR ");
} }
@@ -369,13 +370,13 @@ public final class QueryParams implements Cloneable {
} }
break; break;
case BOTH: case BOTH:
if (!types.isEmpty()) { if (!typeIds.isEmpty()) {
if (excludeBlocksMode) { if (excludeBlocksMode) {
where.append("NOT "); where.append("NOT ");
} }
where.append('('); where.append('(');
for (final Material block : types) { for (final Integer block : typeIds) {
where.append("((type = ").append(MaterialConverter.getOrAddMaterialId(block.getKey())).append(" OR replaced = ").append(MaterialConverter.getOrAddMaterialId(block.getKey())); where.append("((type = ").append(block).append(" OR replaced = ").append(block);
where.append(")"); where.append(")");
where.append(") OR "); where.append(") OR ");
} }
@@ -385,13 +386,13 @@ public final class QueryParams implements Cloneable {
where.append("type != replaced AND "); where.append("type != replaced AND ");
break; break;
case CREATED: case CREATED:
if (!types.isEmpty()) { if (!typeIds.isEmpty()) {
if (excludeBlocksMode) { if (excludeBlocksMode) {
where.append("NOT "); where.append("NOT ");
} }
where.append('('); where.append('(');
for (final Material block : types) { for (final Integer block : typeIds) {
where.append("((type = ").append(MaterialConverter.getOrAddMaterialId(block.getKey())); where.append("((type = ").append(block);
where.append(")"); where.append(")");
where.append(") OR "); where.append(") OR ");
} }
@@ -401,13 +402,13 @@ public final class QueryParams implements Cloneable {
where.append("type != 0 AND type != replaced AND "); where.append("type != 0 AND type != replaced AND ");
break; break;
case DESTROYED: case DESTROYED:
if (!types.isEmpty()) { if (!typeIds.isEmpty()) {
if (excludeBlocksMode) { if (excludeBlocksMode) {
where.append("NOT "); where.append("NOT ");
} }
where.append('('); where.append('(');
for (final Material block : types) { for (final Integer block : typeIds) {
where.append("((replaced = ").append(MaterialConverter.getOrAddMaterialId(block.getKey())); where.append("((replaced = ").append(block);
where.append(")"); where.append(")");
where.append(") OR "); where.append(") OR ");
} }
@@ -417,13 +418,13 @@ public final class QueryParams implements Cloneable {
where.append("replaced != 0 AND type != replaced AND "); where.append("replaced != 0 AND type != replaced AND ");
break; break;
case CHESTACCESS: case CHESTACCESS:
if (!types.isEmpty()) { if (!typeIds.isEmpty()) {
if (excludeBlocksMode) { if (excludeBlocksMode) {
where.append("NOT "); where.append("NOT ");
} }
where.append('('); where.append('(');
for (final Material block : types) { for (final Integer block : typeIds) {
where.append("((itemtype = ").append(MaterialConverter.getOrAddMaterialId(block.getKey())); where.append("((itemtype = ").append(block);
where.append(")"); where.append(")");
where.append(") OR "); where.append(") OR ");
} }
@@ -607,6 +608,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()));
} }
needWeapon = true; needWeapon = true;
} else if (param.equals("block") || param.equals("type")) { } else if (param.equals("block") || param.equals("type")) {
@@ -620,7 +622,11 @@ public final class QueryParams implements Cloneable {
} }
final Material mat = Material.matchMaterial(blockName); final Material mat = Material.matchMaterial(blockName);
if (mat == null) {
throw new IllegalArgumentException("No material matching: '" + blockName + "'");
}
types.add(mat); types.add(mat);
typeIds.add(MaterialConverter.getOrAddMaterialId(mat.getKey()));
} }
} else if (param.equals("area")) { } else if (param.equals("area")) {
if (player == null && !prepareToolQuery && loc == null) { if (player == null && !prepareToolQuery && loc == null) {
@@ -775,6 +781,7 @@ public final class QueryParams implements Cloneable {
try { try {
final QueryParams params = (QueryParams) super.clone(); final QueryParams params = (QueryParams) super.clone();
params.players = new ArrayList<String>(players); params.players = new ArrayList<String>(players);
params.typeIds = new ArrayList<Integer>(typeIds);
params.types = new ArrayList<Material>(types); params.types = new ArrayList<Material>(types);
return params; return params;
} catch (final CloneNotSupportedException ex) { } catch (final CloneNotSupportedException ex) {
@@ -814,6 +821,7 @@ public final class QueryParams implements Cloneable {
public void merge(QueryParams p) { public void merge(QueryParams p) {
players = p.players; players = p.players;
excludePlayersMode = p.excludePlayersMode; excludePlayersMode = p.excludePlayersMode;
typeIds = p.typeIds;
types = p.types; types = p.types;
loc = p.loc; loc = p.loc;
radius = p.radius; radius = p.radius;