mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-17 11:05:07 +00:00
272 lines
8.7 KiB
Java
272 lines
8.7 KiB
Java
package codechicken.nei.recipe;
|
|
|
|
import java.awt.Rectangle;
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.stream.Collectors;
|
|
|
|
import net.minecraft.client.gui.GuiRepair;
|
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import codechicken.nei.ItemList;
|
|
import codechicken.nei.NEIClientConfig;
|
|
import codechicken.nei.NEIClientUtils;
|
|
import codechicken.nei.PositionedStack;
|
|
import codechicken.nei.RestartableTask;
|
|
import codechicken.nei.drawable.DrawableBuilder;
|
|
import codechicken.nei.drawable.DrawableResource;
|
|
|
|
public class RepairRecipeHandler extends TemplateRecipeHandler {
|
|
|
|
private final DrawableResource BG_TEXTURE = new DrawableBuilder("nei:textures/gui/repair.png", 0, 0, 168, 36)
|
|
.setTextureSize(168, 36).build();
|
|
|
|
private static final RestartableTask reloadRepairRecipesTask = new RestartableTask("NEI Repair Recipe Reloading") {
|
|
|
|
@Override
|
|
public void execute() {
|
|
new RepairRecipeHandler().buildCache();
|
|
}
|
|
};
|
|
|
|
private static class ItemStackKey {
|
|
|
|
private final ItemStack stack;
|
|
private final int hashCode;
|
|
|
|
public ItemStackKey(ItemStack stack) {
|
|
this.stack = stack;
|
|
this.hashCode = stack.isItemStackDamageable() ? stack.getItem().hashCode()
|
|
: 31 * stack.getItem().hashCode() + stack.getItemDamage();
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return this.hashCode;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) return true;
|
|
|
|
if (obj instanceof ItemStackKey objeKey) {
|
|
return NEIClientUtils.areStacksSameTypeWithNBT(stack, objeKey.stack);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
private static class DamagablePositionedStack extends PositionedStack {
|
|
|
|
public DamagablePositionedStack(Object item, int x, int y) {
|
|
super(item, x, y);
|
|
}
|
|
|
|
@Override
|
|
public boolean contains(ItemStack ingredient) {
|
|
return ingredient.getItemDamage() > 0 && NEIClientUtils.areStacksSameTypeWithNBT(this.item, ingredient);
|
|
}
|
|
|
|
}
|
|
|
|
private static List<RepairPair> cachedRecipes;
|
|
|
|
private static final int LEFT_X = 25;
|
|
private static final int RIGHT_X = 74;
|
|
private static final int OUTPUT_X = 132;
|
|
private static final int SLOT_Y = 10;
|
|
|
|
public class RepairPair extends CachedRecipe {
|
|
|
|
final List<PositionedStack> ingredients = new ArrayList<>(2);
|
|
final PositionedStack result;
|
|
|
|
RepairPair(ItemStack left, Object right, ItemStack result) {
|
|
ingredients.add(new DamagablePositionedStack(left, LEFT_X, SLOT_Y));
|
|
ingredients.add(new PositionedStack(right, RIGHT_X, SLOT_Y));
|
|
this.result = new PositionedStack(result, OUTPUT_X, SLOT_Y);
|
|
}
|
|
|
|
@Override
|
|
public List<PositionedStack> getIngredients() {
|
|
return this.ingredients;
|
|
}
|
|
|
|
@Override
|
|
public PositionedStack getResult() {
|
|
return this.result;
|
|
}
|
|
}
|
|
|
|
public static void findRepairRecipesOnceParallel() {
|
|
reloadRepairRecipesTask.restart();
|
|
}
|
|
|
|
private static final Set<Integer> knownBrokenItemTypes = ConcurrentHashMap.newKeySet();
|
|
private static volatile List<ItemStack> uniqueMaterials;
|
|
|
|
private void buildCache() {
|
|
if (cachedRecipes != null) return;
|
|
|
|
uniqueMaterials = collectUniqueMaterials();
|
|
|
|
final Map<ItemStackKey, RepairPair> recipes = new ConcurrentHashMap<>();
|
|
|
|
try {
|
|
ItemList.forkJoinPool.submit(
|
|
() -> ItemList.items.parallelStream().forEach(
|
|
itemstack1 -> recipes
|
|
.computeIfAbsent(new ItemStackKey(itemstack1), key -> getRepairRecipe(key.stack))))
|
|
.get();
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
NEIClientConfig.logger.error("Failed to build repair recipe cache", e);
|
|
}
|
|
|
|
cachedRecipes = recipes.values().stream().filter(recipe -> recipe != null).collect(Collectors.toList());
|
|
}
|
|
|
|
private static List<ItemStack> collectUniqueMaterials() {
|
|
final Map<Long, ItemStack> unique = new LinkedHashMap<>();
|
|
for (ItemStack stack : ItemList.items) {
|
|
final long key = ((long) Item.getIdFromItem(stack.getItem()) << 32) | (stack.getItemDamage() & 0xFFFFFFFFL);
|
|
unique.putIfAbsent(key, stack);
|
|
}
|
|
return new ArrayList<>(unique.values());
|
|
}
|
|
|
|
private RepairPair getRepairRecipe(ItemStack itemstack1) {
|
|
if (!itemstack1.isItemStackDamageable()) return null;
|
|
|
|
final int itemId = Item.getIdFromItem(itemstack1.getItem());
|
|
if (knownBrokenItemTypes.contains(itemId)) return null;
|
|
|
|
final List<ItemStack> permutations = new ArrayList<>();
|
|
final List<ItemStack> candidates = uniqueMaterials != null ? uniqueMaterials : collectUniqueMaterials();
|
|
|
|
for (ItemStack itemstack2 : candidates) {
|
|
try {
|
|
if (itemstack1.getItem().getIsRepairable(itemstack1, itemstack2)) {
|
|
final ItemStack right = itemstack2.copy();
|
|
right.stackSize = 4;
|
|
permutations.add(right);
|
|
}
|
|
} catch (Exception e) {
|
|
knownBrokenItemTypes.add(itemId);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (permutations.isEmpty()) return null;
|
|
|
|
final int maxDamage = itemstack1.getMaxDamage();
|
|
final ItemStack result = itemstack1.copy();
|
|
final ItemStack left = itemstack1.copy();
|
|
|
|
left.setItemDamage(maxDamage - 1);
|
|
result.setItemDamage(left.getItemDamageForDisplay() - (maxDamage / 4) * 4);
|
|
|
|
return new RepairPair(left, permutations, result);
|
|
}
|
|
|
|
@Override
|
|
public TemplateRecipeHandler newInstance() {
|
|
|
|
if (cachedRecipes == null) {
|
|
buildCache();
|
|
}
|
|
|
|
return super.newInstance();
|
|
}
|
|
|
|
@Override
|
|
public void loadCraftingRecipes(String outputId, Object... results) {
|
|
if (outputId.equals(getOverlayIdentifier()) && getClass() == RepairRecipeHandler.class) {
|
|
arecipes.addAll(cachedRecipes);
|
|
} else if (!NEIClientConfig.getBooleanSetting("showRepairOnlyForDamageableItems")) {
|
|
super.loadCraftingRecipes(outputId, results);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadCraftingRecipes(ItemStack result) {
|
|
for (RepairPair recipe : cachedRecipes) {
|
|
if (recipe.getResult().contains(result)) arecipes.add(recipe);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadUsageRecipes(String inputId, Object... ingredients) {
|
|
if (inputId.equals(getOverlayIdentifier()) && getClass() == RepairRecipeHandler.class) {
|
|
loadCraftingRecipes(getOverlayIdentifier());
|
|
} else {
|
|
super.loadUsageRecipes(inputId, ingredients);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadUsageRecipes(ItemStack ingredient) {
|
|
final boolean showRepairOnlyForDamageableItems = NEIClientConfig
|
|
.getBooleanSetting("showRepairOnlyForDamageableItems");
|
|
|
|
for (RepairPair recipe : cachedRecipes) {
|
|
final List<PositionedStack> ingredients = recipe.getIngredients();
|
|
|
|
if (ingredients.get(0).contains(ingredient)) {
|
|
arecipes.add(recipe);
|
|
} else if (!showRepairOnlyForDamageableItems && ingredients.get(1).contains(ingredient)) {
|
|
arecipes.add(new RepairPair(ingredients.get(0).item, ingredient, recipe.getResult().item));
|
|
}
|
|
}
|
|
|
|
if (arecipes.isEmpty()) {
|
|
final RepairPair recipe = getRepairRecipe(ingredient);
|
|
if (recipe != null) {
|
|
arecipes.add(recipe);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void loadTransferRects() {
|
|
transferRects.add(new RecipeTransferRect(new Rectangle(100, 11, 22, 15), getOverlayIdentifier()));
|
|
}
|
|
|
|
@Override
|
|
public String getRecipeName() {
|
|
return NEIClientUtils.translate("recipe.repair");
|
|
}
|
|
|
|
@Override
|
|
public String getGuiTexture() {
|
|
return "nei:textures/gui/repair.png";
|
|
}
|
|
|
|
@Override
|
|
public String getOverlayIdentifier() {
|
|
return "repair";
|
|
}
|
|
|
|
@Override
|
|
public Class<? extends GuiContainer> getGuiClass() {
|
|
return GuiRepair.class;
|
|
}
|
|
|
|
@Override
|
|
public void drawBackground(int recipe) {
|
|
BG_TEXTURE.draw(0, 0);
|
|
}
|
|
|
|
@Override
|
|
public void drawForeground(int recipe) {}
|
|
|
|
}
|