1.3r1
This commit is contained in:
parent
10c011ede1
commit
c45cec156d
31 changed files with 422 additions and 41 deletions
|
|
@ -10,10 +10,12 @@ import org.gsdistance.grimmsServer.GrimmsServer;
|
|||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ItemIndexer {
|
||||
private static final Map<Material, Double> calculatedMarketValues = new HashMap<>();
|
||||
private static final List<Material> skippedMarketValues = new ArrayList<>();
|
||||
private static final Set<Material> skippedMarketValues = new HashSet<>();
|
||||
private static final Map<Material, List<Recipe>> recipeCache = new HashMap<>();
|
||||
public static boolean loggingEnabled = true;
|
||||
|
||||
public static void indexWholeRegistry(){
|
||||
|
|
@ -22,6 +24,11 @@ public class ItemIndexer {
|
|||
// Clear previous values
|
||||
calculatedMarketValues.clear();
|
||||
skippedMarketValues.clear();
|
||||
recipeCache.clear();
|
||||
|
||||
// Pre-cache all recipes to avoid repeated Bukkit calls
|
||||
cacheAllRecipes();
|
||||
|
||||
// Check the cache first
|
||||
Map<Material, Double> tmp;
|
||||
try {
|
||||
|
|
@ -37,41 +44,52 @@ public class ItemIndexer {
|
|||
calculatedMarketValues.putAll(tmp);
|
||||
cacheExists = true;
|
||||
}
|
||||
// Iterate through all materials and calculate market values
|
||||
for (Material material : Material.values()) {
|
||||
if (material.isItem() && material != Material.AIR && !MarketBaseValues.marketBaseValues.containsKey(material) && !calculatedMarketValues.containsKey(material)) {
|
||||
double marketValue = calculateMarketValue(material);
|
||||
if (marketValue > 0) {
|
||||
calculatedMarketValues.put(material, Math.floor(marketValue));
|
||||
if (loggingEnabled) {
|
||||
GrimmsServer.logger.info("Calculated market value for " + material + ": " + Math.floor(marketValue));
|
||||
}
|
||||
}
|
||||
else{
|
||||
skippedMarketValues.add(material);
|
||||
|
||||
// Process only items that need calculation
|
||||
Set<Material> materialsToProcess = Arrays.stream(Material.values())
|
||||
.filter(material -> material.isItem() && material != Material.AIR)
|
||||
.filter(material -> !MarketBaseValues.marketBaseValues.containsKey(material))
|
||||
.filter(material -> !calculatedMarketValues.containsKey(material))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// First pass calculation
|
||||
Set<Material> firstPassSkipped = new HashSet<>();
|
||||
for (Material material : materialsToProcess) {
|
||||
double marketValue = calculateMarketValue(material);
|
||||
if (marketValue > 0) {
|
||||
calculatedMarketValues.put(material, Math.floor(marketValue));
|
||||
if (loggingEnabled) {
|
||||
GrimmsServer.logger.info("Calculated market value for " + material + ": " + Math.floor(marketValue));
|
||||
}
|
||||
} else {
|
||||
firstPassSkipped.add(material);
|
||||
}
|
||||
}
|
||||
// Retry calculating market values for skipped items
|
||||
if(!cacheExists){
|
||||
|
||||
// Retry logic for skipped items (only if no cache existed)
|
||||
if (!cacheExists && !firstPassSkipped.isEmpty()) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (loggingEnabled) {
|
||||
GrimmsServer.logger.info("Recalculating skipped market values, attempt " + (i + 1));
|
||||
}
|
||||
for (Material material : skippedMarketValues) {
|
||||
if (material.isItem() && material != Material.AIR && !MarketBaseValues.marketBaseValues.containsKey(material)) {
|
||||
double marketValue = calculateMarketValue(material);
|
||||
if (marketValue > 0) {
|
||||
calculatedMarketValues.put(material, marketValue);
|
||||
skippedMarketValues.remove(material);
|
||||
if (loggingEnabled) {
|
||||
GrimmsServer.logger.info("Calculated market value for " + material + ": " + marketValue);
|
||||
}
|
||||
Set<Material> stillSkipped = new HashSet<>();
|
||||
for (Material material : firstPassSkipped) {
|
||||
double marketValue = calculateMarketValue(material);
|
||||
if (marketValue > 0) {
|
||||
calculatedMarketValues.put(material, marketValue);
|
||||
if (loggingEnabled) {
|
||||
GrimmsServer.logger.info("Calculated market value for " + material + ": " + marketValue);
|
||||
}
|
||||
} else {
|
||||
stillSkipped.add(material);
|
||||
}
|
||||
}
|
||||
firstPassSkipped = stillSkipped;
|
||||
if (firstPassSkipped.isEmpty()) break;
|
||||
}
|
||||
skippedMarketValues.addAll(firstPassSkipped);
|
||||
}
|
||||
|
||||
GrimmsServer.logger.info("Indexed " + calculatedMarketValues.size() + " items in (" + stopwatch.stop() + ").");
|
||||
GrimmsServer.logger.info("Skipped " + skippedMarketValues.size() + " items that could not be calculated.");
|
||||
|
||||
|
|
@ -82,10 +100,23 @@ public class ItemIndexer {
|
|||
MarketBaseValues.marketBaseValues.putAll(calculatedMarketValues);
|
||||
}
|
||||
|
||||
private static void cacheAllRecipes() {
|
||||
for (Material material : Material.values()) {
|
||||
if (material.isItem() && material != Material.AIR) {
|
||||
List<Recipe> recipes = Bukkit.getServer().getRecipesFor(new ItemStack(material));
|
||||
if (recipes != null && !recipes.isEmpty()) {
|
||||
recipeCache.put(material, recipes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final ThreadLocal<Set<Material>> processingMaterials = ThreadLocal.withInitial(HashSet::new);
|
||||
|
||||
public static Double calculateMarketValue(@Nullable Material item) {
|
||||
// Track materials being processed to prevent infinite recursion
|
||||
Set<Material> processingMaterials = new HashSet<>();
|
||||
return calculateMarketValueInternal(item, processingMaterials);
|
||||
Set<Material> processingSet = processingMaterials.get();
|
||||
processingSet.clear();
|
||||
return calculateMarketValueInternal(item, processingSet);
|
||||
}
|
||||
|
||||
private static Double calculateMarketValueInternal(@Nullable Material item, Set<Material> processingMaterials) {
|
||||
|
|
@ -106,9 +137,14 @@ public class ItemIndexer {
|
|||
}
|
||||
|
||||
processingMaterials.add(item);
|
||||
double marketValue = 0.0;
|
||||
double marketValue = Double.MAX_VALUE;
|
||||
|
||||
for (Recipe recipe : Bukkit.getServer().getRecipesFor(new ItemStack(item))) {
|
||||
List<Recipe> recipes = recipeCache.get(item);
|
||||
if (recipes == null || recipes.isEmpty()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
for (Recipe recipe : recipes) {
|
||||
if (recipe == null || recipe.getResult().getType() == Material.AIR) {
|
||||
continue; // Skip invalid recipes
|
||||
}
|
||||
|
|
@ -136,21 +172,21 @@ public class ItemIndexer {
|
|||
ItemStack result = furnaceRecipe.getResult();
|
||||
if (result.getType() == item) {
|
||||
double ingredientValue = calculateMarketValueInternal(furnaceRecipe.getInput().getType(), processingMaterials);
|
||||
recipeValue = ingredientValue * result.getAmount();
|
||||
recipeValue = ingredientValue / result.getAmount();
|
||||
}
|
||||
}
|
||||
case org.bukkit.inventory.StonecuttingRecipe stonecuttingRecipe -> {
|
||||
ItemStack result = stonecuttingRecipe.getResult();
|
||||
if (result.getType() == item) {
|
||||
double ingredientValue = calculateMarketValueInternal(stonecuttingRecipe.getInput().getType(), processingMaterials);
|
||||
recipeValue = ingredientValue * result.getAmount();
|
||||
recipeValue = ingredientValue / result.getAmount();
|
||||
}
|
||||
}
|
||||
case org.bukkit.inventory.BlastingRecipe blastingRecipe -> {
|
||||
ItemStack result = blastingRecipe.getResult();
|
||||
if (result.getType() == item) {
|
||||
double ingredientValue = calculateMarketValueInternal(blastingRecipe.getInput().getType(), processingMaterials);
|
||||
recipeValue = ingredientValue * result.getAmount();
|
||||
recipeValue = ingredientValue / result.getAmount();
|
||||
}
|
||||
}
|
||||
case org.bukkit.inventory.SmithingRecipe smithingRecipe -> {
|
||||
|
|
@ -163,20 +199,21 @@ public class ItemIndexer {
|
|||
ItemStack addition = smithingRecipe.getAddition().getItemStack();
|
||||
double baseValue = calculateMarketValueInternal(base.getType(), processingMaterials);
|
||||
double additionValue = calculateMarketValueInternal(addition.getType(), processingMaterials);
|
||||
recipeValue = (baseValue + additionValue) * result.getAmount();
|
||||
recipeValue = (baseValue + additionValue) / result.getAmount();
|
||||
}
|
||||
}
|
||||
default -> {
|
||||
return 0.0; // Unsupported recipe type
|
||||
}
|
||||
}
|
||||
recipeValue = recipeValue / recipe.getResult().getAmount(); // Normalize by result amount
|
||||
if (recipeValue > marketValue) {
|
||||
// No normalization needed for shaped/shapeless recipes as they already account for ingredient amounts
|
||||
// For single-input recipes, we already divided by result amount above
|
||||
if (recipeValue > 0 && recipeValue < marketValue) {
|
||||
marketValue = recipeValue;
|
||||
}
|
||||
}
|
||||
|
||||
processingMaterials.remove(item);
|
||||
return marketValue;
|
||||
return marketValue == Double.MAX_VALUE ? 0.0 : marketValue;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue