forked from LogBlock/LogBlock
Do not add unnecessary materials or entity types to the database
This commit is contained in:
@@ -16,6 +16,10 @@ public class EntityTypeConverter {
|
||||
private static HashMap<EntityType, Integer> entityTypeToId = new HashMap<>();
|
||||
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) {
|
||||
Integer key = entityTypeToId.get(entityType);
|
||||
int tries = 0;
|
||||
|
@@ -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) {
|
||||
return getOrAddMaterialId(blockData == null ? Material.AIR : blockData.getMaterial());
|
||||
}
|
||||
@@ -76,9 +88,22 @@ public class MaterialConverter {
|
||||
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) {
|
||||
if (blockData == null) {
|
||||
blockData = Material.AIR.createBlockData();
|
||||
return -1;
|
||||
}
|
||||
String blockDataString = blockData.getAsString();
|
||||
int dataPart = blockDataString.indexOf("[");
|
||||
|
@@ -1017,16 +1017,26 @@ public final class QueryParams implements Cloneable {
|
||||
HashSet<Integer> typeIds = new HashSet<>();
|
||||
for (Material type : types) {
|
||||
if (type != null) {
|
||||
typeIds.add(MaterialConverter.getOrAddMaterialId(type));
|
||||
Integer id = MaterialConverter.getExistingMaterialId(type);
|
||||
if (id != null) {
|
||||
typeIds.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Tag<Material> tag : typeTags) {
|
||||
if (tag != null) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1034,9 +1044,13 @@ public final class QueryParams implements Cloneable {
|
||||
HashSet<Integer> typeIds = new HashSet<>();
|
||||
for (EntityType type : entityTypes) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user