Do not add unnecessary materials or entity types to the database

This commit is contained in:
Brokkonaut
2020-08-09 23:25:30 +02:00
parent 6a398a67ab
commit 0d7a8016a1
3 changed files with 47 additions and 4 deletions

View File

@@ -16,6 +16,10 @@ public class EntityTypeConverter {
private static HashMap<EntityType, Integer> entityTypeToId = new HashMap<>(); private static HashMap<EntityType, Integer> entityTypeToId = new HashMap<>();
private static int nextEntityTypeId; private static int nextEntityTypeId;
public synchronized static int getExistingEntityTypeId(EntityType entityType) {
return entityType == null ? null : entityTypeToId.get(entityType);
}
public synchronized static int getOrAddEntityTypeId(EntityType entityType) { public synchronized static int getOrAddEntityTypeId(EntityType entityType) {
Integer key = entityTypeToId.get(entityType); Integer key = entityTypeToId.get(entityType);
int tries = 0; int tries = 0;

View File

@@ -30,6 +30,18 @@ public class MaterialConverter {
} }
} }
public synchronized static Integer getExistingMaterialId(BlockData blockData) {
return blockData == null ? null : getExistingMaterialId(blockData.getMaterial());
}
public synchronized static Integer getExistingMaterialId(Material material) {
if (material == null) {
return null;
}
String materialString = material.getKey().toString();
return materialToID.get(materialString);
}
public synchronized static int getOrAddMaterialId(BlockData blockData) { public synchronized static int getOrAddMaterialId(BlockData blockData) {
return getOrAddMaterialId(blockData == null ? Material.AIR : blockData.getMaterial()); return getOrAddMaterialId(blockData == null ? Material.AIR : blockData.getMaterial());
} }
@@ -76,9 +88,22 @@ public class MaterialConverter {
return key.intValue(); return key.intValue();
} }
public synchronized static Integer getExistingBlockStateId(BlockData blockData) {
if (blockData == null) {
return -1;
}
String blockDataString = blockData.getAsString();
int dataPart = blockDataString.indexOf("[");
if (dataPart < 0) {
return -1;
}
String materialString = blockDataString.substring(dataPart);
return blockStateToID.get(materialString);
}
public synchronized static int getOrAddBlockStateId(BlockData blockData) { public synchronized static int getOrAddBlockStateId(BlockData blockData) {
if (blockData == null) { if (blockData == null) {
blockData = Material.AIR.createBlockData(); return -1;
} }
String blockDataString = blockData.getAsString(); String blockDataString = blockData.getAsString();
int dataPart = blockDataString.indexOf("["); int dataPart = blockDataString.indexOf("[");

View File

@@ -1017,16 +1017,26 @@ public final class QueryParams implements Cloneable {
HashSet<Integer> typeIds = new HashSet<>(); HashSet<Integer> typeIds = new HashSet<>();
for (Material type : types) { for (Material type : types) {
if (type != null) { if (type != null) {
typeIds.add(MaterialConverter.getOrAddMaterialId(type)); Integer id = MaterialConverter.getExistingMaterialId(type);
if (id != null) {
typeIds.add(id);
}
} }
} }
for (Tag<Material> tag : typeTags) { for (Tag<Material> tag : typeTags) {
if (tag != null) { if (tag != null) {
for (Material type : tag.getValues()) { for (Material type : tag.getValues()) {
typeIds.add(MaterialConverter.getOrAddMaterialId(type)); Integer id = MaterialConverter.getExistingMaterialId(type);
if (id != null) {
typeIds.add(id);
}
} }
} }
} }
// add invalid id, so the type list is not ignored
if ((!types.isEmpty() || !typeTags.isEmpty()) && typeIds.isEmpty()) {
typeIds.add(-1);
}
return typeIds; return typeIds;
} }
@@ -1034,9 +1044,13 @@ public final class QueryParams implements Cloneable {
HashSet<Integer> typeIds = new HashSet<>(); HashSet<Integer> typeIds = new HashSet<>();
for (EntityType type : entityTypes) { for (EntityType type : entityTypes) {
if (type != null) { if (type != null) {
typeIds.add(EntityTypeConverter.getOrAddEntityTypeId(type)); typeIds.add(EntityTypeConverter.getExistingEntityTypeId(type));
} }
} }
// add invalid id, so the type list is not ignored
if (!entityTypes.isEmpty() && typeIds.isEmpty()) {
typeIds.add(-1);
}
return typeIds; return typeIds;
} }