Search in sources :

Example 1 with SqlIdMapQuery

use of me.botsko.prism.database.sql.SqlIdMapQuery in project Prism-Bukkit by prism.

the class MaterialAliases method getIdsOf.

private Set<IntPair> getIdsOf(Material material) {
    Set<IntPair> ids = allIdsCache.get(material);
    if (ids != null) {
        return ids;
    }
    IdMapQuery query = new SqlIdMapQuery(Prism.getPrismDataSource());
    query.findAllIds(material.name().toLowerCase(Locale.ENGLISH), list -> allIdsCache.put(material, new HashSet<>(list)));
    return allIdsCache.get(material);
}
Also used : IdMapQuery(me.botsko.prism.database.IdMapQuery) SqlIdMapQuery(me.botsko.prism.database.sql.SqlIdMapQuery) SqlIdMapQuery(me.botsko.prism.database.sql.SqlIdMapQuery) HashSet(java.util.HashSet)

Example 2 with SqlIdMapQuery

use of me.botsko.prism.database.sql.SqlIdMapQuery in project Prism-Bukkit by prism.

the class MaterialAliases method partialBlockDataIds.

/**
 * .
 *
 * @param material         Material
 * @param partialBlockData String
 * @return Set of IntPair
 * @throws IllegalArgumentException exception
 */
public Set<IntPair> partialBlockDataIds(Material material, String partialBlockData) throws IllegalArgumentException {
    String fullBlockData = Utilities.dataString(Bukkit.createBlockData(material, partialBlockData));
    String[] parts = fullBlockData.substring(1, fullBlockData.length() - 1).toLowerCase(Locale.ENGLISH).split(",");
    StringBuilder likeString = new StringBuilder("%");
    for (String string : parts) {
        if (partialBlockData.contains(string)) {
            likeString.append(string).append('%');
        }
    }
    String stateLike = likeString.toString();
    IdMapQuery query = new SqlIdMapQuery(Prism.getPrismDataSource());
    Set<IntPair> ids = new HashSet<>();
    query.findAllIdsPartial(material.name().toLowerCase(Locale.ENGLISH), stateLike, ids::addAll);
    return ids;
}
Also used : IdMapQuery(me.botsko.prism.database.IdMapQuery) SqlIdMapQuery(me.botsko.prism.database.sql.SqlIdMapQuery) SqlIdMapQuery(me.botsko.prism.database.sql.SqlIdMapQuery) HashSet(java.util.HashSet)

Example 3 with SqlIdMapQuery

use of me.botsko.prism.database.sql.SqlIdMapQuery in project Prism-Bukkit by prism.

the class MaterialAliases method idsToMaterial.

/**
 * .
 *
 * @param blockId           int
 * @param blockSubId        logMaterialErrorsint
 * @param logMaterialErrors boolean.
 * @return MaterialState
 */
public MaterialState idsToMaterial(int blockId, int blockSubId, Boolean logMaterialErrors) {
    MaterialState cachedMaterial = fromCache(blockId, blockSubId);
    if (cachedMaterial != null) {
        return cachedMaterial;
    }
    MaterialState result = new MaterialState();
    SqlIdMapQuery query = new SqlIdMapQuery(Prism.getPrismDataSource());
    query.findMaterial(blockId, blockSubId, (material, state) -> {
        result.material = Material.matchMaterial(material.toUpperCase(Locale.ENGLISH));
        result.state = state;
        if (result.material != null) {
            storeCache(result.material, result.state, blockId, blockSubId);
        }
    }, () -> {
        if (logMaterialErrors) {
            Prism.log("matError: [" + blockId + ", " + blockSubId + "] -> ???");
        }
    });
    if (result.material == null) {
        return null;
    }
    return result;
}
Also used : SqlIdMapQuery(me.botsko.prism.database.sql.SqlIdMapQuery) MaterialState(me.botsko.prism.api.objects.MaterialState)

Example 4 with SqlIdMapQuery

use of me.botsko.prism.database.sql.SqlIdMapQuery in project Prism-Bukkit by prism.

the class MaterialAliases method initMaterials.

/**
 * Initialize the library.
 *
 * @param materials Materials ...
 */
public void initMaterials(Material... materials) {
    Bukkit.getScheduler().runTaskAsynchronously(Prism.getInstance(), () -> {
        SqlIdMapQuery query = new SqlIdMapQuery(Prism.getPrismDataSource());
        for (Material m : materials) {
            String matName = m.name().toLowerCase(Locale.ENGLISH);
            String dataString;
            try {
                dataString = Utilities.dataString(Bukkit.createBlockData(m));
            } catch (IllegalArgumentException e) {
                continue;
            }
            query.findIds(m.name().toLowerCase(Locale.ENGLISH), dataString, (i, d) -> storeCache(m, dataString, i, d), () -> {
                int id = query.mapAutoId(matName, dataString);
                storeCache(m, dataString, id, 0);
            });
        }
    });
}
Also used : SqlIdMapQuery(me.botsko.prism.database.sql.SqlIdMapQuery) Material(org.bukkit.Material)

Example 5 with SqlIdMapQuery

use of me.botsko.prism.database.sql.SqlIdMapQuery in project Prism-Bukkit by prism.

the class MaterialAliases method materialToIds.

/**
 * Create IntPair.
 *
 * @param material Material
 * @param state    State
 * @return IntPair
 */
public IntPair materialToIds(Material material, String state) {
    int blockSubId;
    // For tools, where durability doesn't mean a different item (different cached
    // value) but is still important
    int durability = 0;
    try {
        durability = Integer.parseInt(state);
    } catch (NumberFormatException ignored) {
    // ignored
    }
    if (material.getMaxDurability() > 0) {
        blockSubId = 0;
    } else {
        blockSubId = durability;
    }
    IntPair cachedIds = fromCache(material, blockSubId == 0 ? state : String.valueOf(blockSubId));
    if (cachedIds != null) {
        return cachedIds;
    }
    IntPair result = new IntPair(0, 0);
    SqlIdMapQuery query = new SqlIdMapQuery(Prism.getPrismDataSource());
    String materialName = material.name().toLowerCase(Locale.ENGLISH);
    synchronized (this) {
        query.findIds(materialName, state, (queryId, querySubId) -> {
            result.first = queryId;
            result.second = querySubId;
            storeCache(material, state, queryId, querySubId);
        }, () -> {
            int blockId = query.mapAutoId(materialName, state);
            result.first = blockId;
            storeCache(material, state, blockId, 0);
        });
    }
    if (blockSubId != durability) {
        result.second = durability;
    }
    return result;
}
Also used : SqlIdMapQuery(me.botsko.prism.database.sql.SqlIdMapQuery)

Aggregations

SqlIdMapQuery (me.botsko.prism.database.sql.SqlIdMapQuery)5 HashSet (java.util.HashSet)2 IdMapQuery (me.botsko.prism.database.IdMapQuery)2 MaterialState (me.botsko.prism.api.objects.MaterialState)1 Material (org.bukkit.Material)1