Compare commits

...
17 changed files with 352 additions and 112 deletions

No files matched your search

+4 -4
View File
@@ -5,8 +5,8 @@ configurations.create('gtnhlibEmbed') {
}
dependencies {
api('com.github.GTNewHorizons:CodeChickenCore:1.4.17:dev')
devOnlyNonPublishable('com.github.GTNewHorizons:GTNHLib:0.11.37:dev')
api('com.github.GTNewHorizons:CodeChickenCore:1.4.19:dev')
devOnlyNonPublishable('com.github.GTNewHorizons:GTNHLib:0.11.48:dev')
shadowImplementation('org.apache.commons:commons-csv:1.10.0')
@@ -23,8 +23,8 @@ dependencies {
testRuntimeOnly('org.junit.platform:junit-platform-launcher')
testRuntimeOnly("org.lwjgl.lwjgl:lwjgl:2.9.3");
compileOnly("com.github.GTNewHorizons:GTNHLib:0.11.37") { transitive = false }
gtnhlibEmbed("com.github.GTNewHorizons:GTNHLib:0.11.37") { transitive = false }
compileOnly("com.github.GTNewHorizons:GTNHLib:0.11.48") { transitive = false }
gtnhlibEmbed("com.github.GTNewHorizons:GTNHLib:0.11.48") { transitive = false }
}
[tasks.jar, tasks.shadowJar].each {
+10 -10
View File
@@ -1,12 +1,12 @@
#This file is generated by updateDaemonJvm
toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/97d46b5ff4df0bf61e95652a190a5567/redirect
toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/9ef54a08e06b0754cee9fc95ed3a3134/redirect
toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/97d46b5ff4df0bf61e95652a190a5567/redirect
toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/9ef54a08e06b0754cee9fc95ed3a3134/redirect
toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/1df82b76031285ef2e6a4f59ffe9299d/redirect
toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/c7d5be6a02945ac2e045048ee54a7d32/redirect
toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/97d46b5ff4df0bf61e95652a190a5567/redirect
toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/9ef54a08e06b0754cee9fc95ed3a3134/redirect
toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/643f404f769811f5f265fd66f7af5d89/redirect
toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/38256085a73e137cb4937ea9c4807fec/redirect
toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/539ea72837c6244075f8ee037babc29e/redirect
toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/876dbbf00bf4ef6190f7da074cee903a/redirect
toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/539ea72837c6244075f8ee037babc29e/redirect
toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/876dbbf00bf4ef6190f7da074cee903a/redirect
toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/9064f22734b42ac03e85df04b632e167/redirect
toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/4d0277c49991306ac78048580fe34bcf/redirect
toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/539ea72837c6244075f8ee037babc29e/redirect
toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/876dbbf00bf4ef6190f7da074cee903a/redirect
toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/a9fa3b809e484a1e1ac4308a5dda661a/redirect
toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/aa99913fa6d767d899ffc96082da01ca/redirect
toolchainVersion=25
@@ -6,11 +6,14 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.ItemsGrid.ItemsGridSlot;
@@ -21,7 +24,6 @@ import codechicken.nei.recipe.GuiCraftingRecipe;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.ICraftingHandler;
import codechicken.nei.recipe.IRecipeHandler;
import codechicken.nei.recipe.Recipe;
import codechicken.nei.recipe.Recipe.RecipeId;
import codechicken.nei.recipe.RecipeInfo;
import codechicken.nei.recipe.StackInfo;
@@ -82,6 +84,42 @@ public class ItemCraftablesPanel
}
private static class CraftablesSnapshot {
final GuiContainer guiContainer;
final ItemStackAmount inventory;
final boolean showOnlyFavorites;
final int maxCandidates;
CraftablesSnapshot(GuiContainer guiContainer, ItemStackAmount inventory, boolean showOnlyFavorites,
int maxCandidates) {
this.guiContainer = guiContainer;
this.inventory = inventory;
this.showOnlyFavorites = showOnlyFavorites;
this.maxCandidates = maxCandidates;
}
}
private static class RecipeCandidate {
final IRecipeHandler handler;
final int recipeIndex;
final List<PositionedStack> ingredientStacks;
final ItemStack sortKeyItem;
RecipeCandidate(IRecipeHandler handler, int recipeIndex, List<PositionedStack> ingredientStacks,
ItemStack sortKeyItem) {
this.handler = handler;
this.recipeIndex = recipeIndex;
this.ingredientStacks = ingredientStacks;
this.sortKeyItem = sortKeyItem;
}
}
private static final Comparator<ItemStack> ITEM_SORT_COMPARATOR = Comparator
.comparing(FavoriteRecipes::containsManual).thenComparing(FavoriteRecipes::contains).reversed()
.thenComparing(ItemSorter.instance);
public static List<String> guiBlacklist = new ArrayList<>();
private final int MIN_DELAY = 600;
@@ -93,6 +131,16 @@ public class ItemCraftablesPanel
private long lastPlayerInventorySync = 0;
private int lastFavoritesCount = 0;
private volatile CraftablesSnapshot pendingSnapshot;
private final AtomicReference<Map<ItemStack, RecipeId>> pendingResult = new AtomicReference<>();
private final RestartableTask task = new RestartableTask("NEI Craftables Panel") {
@Override
public void execute() {
pendingResult.set(generateCraftables(ItemCraftablesPanel.this.pendingSnapshot));
}
};
public ItemCraftablesPanel() {
this.grid = new ItemsGrid<>() {
@@ -114,12 +162,7 @@ public class ItemCraftablesPanel
if (!ItemCraftablesPanel.this.availableRecipes.isEmpty()) {
final List<Map.Entry<ItemStack, RecipeId>> entries = ItemCraftablesPanel.this.availableRecipes
.entrySet().stream()
.sorted(
Map.Entry.comparingByKey(
Comparator.comparing(FavoriteRecipes::containsManual)
.thenComparing(FavoriteRecipes::contains).reversed()
.thenComparing(ItemSorter.instance)))
.entrySet().stream().sorted(Map.Entry.comparingByKey(ITEM_SORT_COMPARATOR))
.limit(maxSlotIndex).collect(Collectors.toList());
int slotIndex = 0;
int itemIndex = 0;
@@ -195,10 +238,20 @@ public class ItemCraftablesPanel
public void update() {
this.splittingLineColor = NEIClientConfig.getSetting("inventory.craftables.color").getHexValue();
applyPendingResult();
updateCraftables();
super.update();
}
private void applyPendingResult() {
final Map<ItemStack, RecipeId> result = this.pendingResult.getAndSet(null);
if (result != null && NEIClientConfig.showCraftablesPanelWidget()) {
this.availableRecipes = result;
this.grid.onGridChanged();
}
}
private void updateCraftables() {
if (!NEIClientConfig.showCraftablesPanelWidget()) {
@@ -219,8 +272,14 @@ public class ItemCraftablesPanel
this.lastPlayerInventory = inv;
this.lastGuiContainer = firstGui;
this.lastFavoritesCount = FavoriteRecipes.size();
this.availableRecipes = generateCraftables(firstGui);
this.grid.onGridChanged();
final int maxRows = NEIClientConfig.getIntSetting("inventory.craftables.useRows") + 2;
final int maxCandidates = Math.max(this.grid.columns, 1) * maxRows;
this.pendingSnapshot = new CraftablesSnapshot(
firstGui,
inv,
showOnlyFavorites,
maxCandidates);
this.task.restart();
}
} else {
@@ -244,17 +303,28 @@ public class ItemCraftablesPanel
return (firstGui instanceof GuiRecipe<?>gui) ? gui.firstGui : firstGui;
}
private Map<ItemStack, RecipeId> generateCraftables(GuiContainer firstGui) {
final List<IRecipeHandler> availableHandlers = getAvailableHandlers(firstGui);
private Map<ItemStack, RecipeId> generateCraftables(CraftablesSnapshot snapshot) {
final List<IRecipeHandler> availableHandlers = getAvailableHandlers(snapshot.guiContainer);
final Map<ItemStack, RecipeId> availableRecipes = new HashMap<>();
if (!availableHandlers.isEmpty()) {
final List<ItemStack> invStacks = ItemCraftablesPanel.this.lastPlayerInventory.values();
final Map<Item, List<ItemStack>> invByItem = indexInventoryByItem(snapshot.inventory.values());
for (IRecipeHandler handler : availableHandlers) {
availableRecipes.putAll(filterHandlerRecipes(handler, firstGui, invStacks));
if (snapshot.showOnlyFavorites) {
for (IRecipeHandler handler : availableHandlers) {
availableRecipes.putAll(filterHandlerRecipes(handler, invByItem, true));
}
} else {
final List<RecipeCandidate> candidates = Collections.synchronizedList(new ArrayList<>());
for (IRecipeHandler handler : availableHandlers) {
collectHandlerCandidates(handler, invByItem, candidates);
}
candidates.sort(Comparator.comparing(c -> c.sortKeyItem, ITEM_SORT_COMPARATOR));
candidates.subList(0, Math.min(candidates.size(), snapshot.maxCandidates)).parallelStream()
.forEach(candidate -> buildCandidate(candidate, availableRecipes));
}
}
return availableRecipes;
@@ -270,19 +340,126 @@ public class ItemCraftablesPanel
return availableHandlers;
}
private Map<ItemStack, RecipeId> filterHandlerRecipes(IRecipeHandler handler, GuiContainer firstGui,
List<ItemStack> invStacks) {
private static Map<Item, List<ItemStack>> indexInventoryByItem(List<ItemStack> invStacks) {
final Map<Item, List<ItemStack>> index = new HashMap<>();
for (ItemStack stack : invStacks) {
index.computeIfAbsent(stack.getItem(), i -> new ArrayList<>()).add(stack);
}
return index;
}
private void collectHandlerCandidates(IRecipeHandler handler, Map<Item, List<ItemStack>> invByItem,
List<RecipeCandidate> candidates) {
IntStream.range(0, handler.numRecipes()).parallel().forEach(recipeIndex -> {
final List<PositionedStack> ingredientStacks = handler.getIngredientStacks(recipeIndex);
if (existsIngredients(ingredientStacks, invByItem)) {
final ItemStack sortKeyItem = getCheapResultItem(handler, recipeIndex);
if (sortKeyItem != null) {
candidates.add(new RecipeCandidate(handler, recipeIndex, ingredientStacks, sortKeyItem));
}
}
});
}
private void buildCandidate(RecipeCandidate candidate, Map<ItemStack, RecipeId> availableRecipes) {
final ItemStack result = getCheapResult(candidate.handler, candidate.recipeIndex);
if (result != null) {
final RecipeId recipeId = RecipeId.of(candidate.handler, candidate.recipeIndex, candidate.ingredientStacks);
synchronized (availableRecipes) {
availableRecipes.put(result, recipeId);
}
}
}
private static ItemStack getCheapResultItem(IRecipeHandler handler, int recipeIndex) {
final PositionedStack resultStack = handler.getResultStack(recipeIndex);
if (resultStack != null) {
return resultStack.item;
}
final List<PositionedStack> otherStacks = handler.getOtherStacks(recipeIndex);
for (PositionedStack otherStack : otherStacks) {
if (!FluidContainerRegistry.isContainer(otherStack.items[0])
|| StackInfo.getFluid(otherStack.items[0]) != null) {
return otherStack.item;
}
}
return otherStacks.isEmpty() ? null : otherStacks.get(0).item;
}
private static ItemStack getCheapResult(IRecipeHandler handler, int recipeIndex) {
final PositionedStack resultStack = handler.getResultStack(recipeIndex);
final List<PositionedStack> results = resultStack != null ? Collections.singletonList(resultStack)
: handler.getOtherStacks(recipeIndex);
if (results.isEmpty()) {
return null;
}
ItemStack stack = null;
if (results.size() > 1) {
for (PositionedStack result : results) {
if (!FluidContainerRegistry.isContainer(result.item) || StackInfo.getFluid(result.item) != null) {
stack = result.item;
break;
}
}
}
if (stack == null) {
stack = results.get(0).item;
}
int stackSize = 0;
for (PositionedStack result : results) {
if (containsSameType(result, stack)) {
stackSize += StackInfo.getAmount(result.item);
}
}
return StackInfo.withAmount(stack, stackSize);
}
private static boolean containsSameType(PositionedStack pStack, ItemStack stack) {
final ItemStack normalized = StackInfo.withAmount(stack, 0);
for (ItemStack permutation : pStack.items) {
if (NEIServerUtils.areStacksSameTypeWithNBT(StackInfo.withAmount(permutation, 0), normalized)) {
return true;
}
}
return false;
}
private Map<ItemStack, RecipeId> filterHandlerRecipes(IRecipeHandler handler, Map<Item, List<ItemStack>> invByItem,
boolean showOnlyFavorites) {
final Map<ItemStack, RecipeId> availableRecipes = new HashMap<>();
final boolean showOnlyFavorites = NEIClientConfig.getBooleanSetting("inventory.craftables.favoritesOnly");
IntStream.range(0, handler.numRecipes()).parallel().forEach(recipeIndex -> {
if (existsIngredients(firstGui, handler.getIngredientStacks(recipeIndex), invStacks)) {
final Recipe recipe = Recipe.of(handler, recipeIndex);
final List<PositionedStack> ingredientStacks = handler.getIngredientStacks(recipeIndex);
if (!recipe.getResults().isEmpty()
&& (!showOnlyFavorites || FavoriteRecipes.getFavorite(recipe.getRecipeId()) != null)) {
synchronized (availableRecipes) {
availableRecipes.put(recipe.getResult(), recipe.getRecipeId());
if (existsIngredients(ingredientStacks, invByItem)) {
final ItemStack result = getCheapResult(handler, recipeIndex);
if (result != null) {
final RecipeId recipeId = RecipeId.of(handler, recipeIndex, ingredientStacks);
if (!showOnlyFavorites || FavoriteRecipes.getFavorite(recipeId) != null) {
synchronized (availableRecipes) {
availableRecipes.put(result, recipeId);
}
}
}
}
@@ -291,14 +468,27 @@ public class ItemCraftablesPanel
return availableRecipes;
}
private boolean existsIngredients(GuiContainer firstGui, List<PositionedStack> ingredients,
List<ItemStack> invStacks) {
private boolean existsIngredients(List<PositionedStack> ingredients, Map<Item, List<ItemStack>> invByItem) {
final Map<ItemStack, Integer> usedItems = new HashMap<>();
for (PositionedStack pStack : ingredients) {
final ItemStack used = invStacks.stream().filter(
is -> usedItems.getOrDefault(is, is.stackSize) >= pStack.item.stackSize && pStack.contains(is))
.findAny().orElse(null);
ItemStack used = null;
for (ItemStack permutation : pStack.items) {
final List<ItemStack> candidates = invByItem.get(permutation.getItem());
if (candidates == null) continue;
for (ItemStack is : candidates) {
if (usedItems.getOrDefault(is, is.stackSize) >= pStack.item.stackSize
&& NEIServerUtils.areStacksSameTypeCrafting(permutation, is)) {
used = is;
break;
}
}
if (used != null) break;
}
if (used != null) {
usedItems.put(used, usedItems.getOrDefault(used, used.stackSize) - pStack.item.stackSize);
@@ -169,7 +169,7 @@ public class NEIController implements IContainerSlotClickHandler, IContainerInpu
if (GuiInfo.hasCustomSlots(gui)) return false;
if (NEIClientUtils.shiftKey()) {
if (NEIClientUtils.shiftKey() && button != 2) { // MMB is used by other mods when an itemstack is picked.
// fix small mouse movement while clicking causing slotIndex to be -999
if (slotIndex == -999) {
final Point mouse = GuiDraw.getMousePosition();
@@ -102,6 +102,7 @@ public class NEIModContainer extends DummyModContainer {
gregTech5Loaded = Loader.isModLoaded("gregtech") && !Loader.isModLoaded("gregapi_post");
gtnhLibLoaded = Loader.isModLoaded("gtnhlib");
cubicChunksLoaded = Loader.isModLoaded("cubicchunks");
ServerHandler.preInit();
if (CommonUtils.isClient()) ClientHandler.preInit();
asmDataTable = event.getAsmData();
}
@@ -10,6 +10,7 @@ import net.minecraftforge.event.world.WorldEvent;
import codechicken.core.CommonUtils;
import codechicken.lib.packet.PacketCustom;
import codechicken.nei.item.ItemFluidDisplay;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
@@ -18,6 +19,7 @@ import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerRespawnEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import cpw.mods.fml.common.registry.GameRegistry;
public class ServerHandler {
@@ -31,6 +33,14 @@ public class ServerHandler {
NEIActions.init();
}
public static void preInit() {
registerFluidDisplayItem();
}
private static void registerFluidDisplayItem() {
GameRegistry.registerItem(new ItemFluidDisplay(), "neiFluidDisplay");
}
@SubscribeEvent
public void tickEvent(TickEvent.WorldTickEvent event) {
if (event.phase == Phase.START && !event.world.isRemote
+2 -2
View File
@@ -146,14 +146,14 @@ public class API {
* Hide an item from the item panel Damage values of OreDictionary.WILDCARD_VALUE and ItemStackMap.WILDCARD_TAG tags
* function as wildcards for their respective variables
*/
public static void hideItem(ItemStack item) {
public static synchronized void hideItem(ItemStack item) {
if (!ItemInfo.hiddenItems.contains(item)) {
ItemInfo.hiddenItems.add(item);
LayoutManager.markItemsDirty();
}
}
public static void hideItem(String rule) {
public static synchronized void hideItem(String rule) {
ItemFilter filter = ItemStackFilterParser.parse(rule);
if (filter != null) {
ItemInfo.hiddenItemsRules.filters.add(filter);
@@ -36,6 +36,7 @@ import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.RegistryNamespaced;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.common.BiomeDictionary;
import net.minecraftforge.common.IShearable;
import net.minecraftforge.oredict.OreDictionary;
@@ -60,6 +61,7 @@ import codechicken.nei.config.HandlerDumper;
import codechicken.nei.config.ItemPanelDumper;
import codechicken.nei.config.RegistryDumper;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.item.FluidDisplayRenderer;
import codechicken.nei.item.ItemFluidDisplay;
import codechicken.nei.recipe.BrewingRecipeHandler;
import codechicken.nei.recipe.RecipeItemInputHandler;
@@ -134,7 +136,7 @@ public class ItemInfo {
public static void preInit() {
addMobSpawnerItem();
addFluidDisplayItem();
setupFluidDisplayItemClient();
}
public static void load() {
@@ -320,8 +322,9 @@ public class ItemInfo {
GameDataManipulator.replaceItem(Block.getIdFromBlock(Blocks.mob_spawner), new ItemMobSpawner());
}
private static void addFluidDisplayItem() {
GameRegistry.registerItem(new ItemFluidDisplay(), "neiFluidDisplay");
private static void setupFluidDisplayItemClient() {
ItemFluidDisplay.INSTANCE.setCreativeTab(ItemFluidDisplay.tabFluids);
MinecraftForgeClient.registerItemRenderer(ItemFluidDisplay.INSTANCE, new FluidDisplayRenderer());
}
private static void addInfiniteHandlers() {
@@ -2,17 +2,20 @@ package codechicken.nei.item;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraftforge.client.IItemRenderer;
import net.minecraftforge.fluids.FluidStack;
import org.lwjgl.opengl.GL11;
import codechicken.nei.util.ReadableNumberConverter;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class FluidDisplayRenderer implements IItemRenderer {
@Override
@@ -49,7 +52,8 @@ public class FluidDisplayRenderer implements IItemRenderer {
Minecraft.getMinecraft().getTextureManager().bindTexture(TextureMap.locationBlocksTexture);
GL11.glColor4f(red, green, blue, 1F);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
OpenGlHelper
.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
final Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
@@ -64,17 +68,17 @@ public class FluidDisplayRenderer implements IItemRenderer {
}
private void renderAmountOverlay(ItemStack item, ItemFluidDisplay fluidDisplay) {
final FluidStack fluidStack = fluidDisplay.getFluid(item);
if (fluidStack == null || fluidStack.amount <= 0) {
final long amount = fluidDisplay.getAmountLong(item);
if (amount <= 0) {
return;
}
String amountString = "";
if (fluidStack.amount < 10_000) {
amountString = String.valueOf(fluidStack.amount) + "L";
if (amount < 10_000) {
amountString = String.valueOf(amount) + "L";
} else {
amountString = ReadableNumberConverter.INSTANCE.toWideReadableForm(fluidStack.amount) + "L";
amountString = ReadableNumberConverter.INSTANCE.toWideReadableForm(amount) + "L";
}
final FontRenderer fontRender = Minecraft.getMinecraft().fontRenderer;
@@ -10,7 +10,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
@@ -40,36 +39,32 @@ public class ItemFluidDisplay extends Item implements IFluidContainerItem {
};
public ItemFluidDisplay() {
setMaxStackSize(1);
setUnlocalizedName("nei.fluidDisplay");
setMaxStackSize(1);
INSTANCE = this;
setCreativeTab(tabFluids);
MinecraftForgeClient.registerItemRenderer(this, new FluidDisplayRenderer());
}
public static ItemStack createStack(FluidStack fluidStack) {
if (fluidStack == null || fluidStack.getFluid() == null) {
return fluidStack == null ? null : createStack(fluidStack.getFluid(), fluidStack.amount);
}
public static ItemStack createStack(Fluid fluid, long amount) {
if (fluid == null) {
return null;
}
int fluidId;
try {
fluidId = FluidRegistry.getFluidID(fluidStack.getFluid());
fluidId = FluidRegistry.getFluidID(fluid);
} catch (Exception e) {
NEIClientConfig.logger.error("Failed to get fluid id for: " + fluidStack.getFluid().getName(), e);
NEIClientConfig.logger.error("Failed to get fluid id for: " + fluid.getName(), e);
return null;
}
final ItemStack stack = new ItemStack(INSTANCE, 1, fluidId);
final NBTTagCompound nbtTag = new NBTTagCompound();
if (fluidStack.tag != null) {
nbtTag.setTag("FluidTag", fluidStack.tag.copy());
}
nbtTag.setInteger("neiFluidDisplayAmount", fluidStack.amount);
nbtTag.setLong("neiFluidDisplayAmount", amount);
stack.setTagCompound(nbtTag);
return stack;
}
@@ -88,23 +83,25 @@ public class ItemFluidDisplay extends Item implements IFluidContainerItem {
}
final NBTTagCompound nbTag = stack.getTagCompound();
final FluidStack fluidStack = new FluidStack(fluid, nbTag.getInteger("neiFluidDisplayAmount"));
if (nbTag.hasKey("FluidTag")) {
fluidStack.tag = nbTag.getCompoundTag("FluidTag");
}
final FluidStack fluidStack = new FluidStack(
fluid,
(int) Math.min(nbTag.getLong("neiFluidDisplayAmount"), Integer.MAX_VALUE));
return fluidStack;
}
@Override
public int getCapacity(ItemStack stack) {
public long getAmountLong(ItemStack stack) {
if (stack == null || !(stack.getItem() instanceof ItemFluidDisplay) || !stack.hasTagCompound()) {
return 0;
}
return stack.getTagCompound().getInteger("neiFluidDisplayAmount");
return stack.getTagCompound().getLong("neiFluidDisplayAmount");
}
@Override
public int getCapacity(ItemStack stack) {
return (int) Math.min(getAmountLong(stack), Integer.MAX_VALUE);
}
@Override
@@ -180,7 +177,7 @@ public class ItemFluidDisplay extends Item implements IFluidContainerItem {
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item aItem, CreativeTabs aTab, List<ItemStack> aList) {
for (int i = 0, j = FluidRegistry.getMaxID(); i < j; i++) {
for (int i = 0, j = FluidRegistry.getMaxID(); i <= j; i++) {
final Fluid fluid = FluidRegistry.getFluid(i);
if (fluid == null) {
continue;
@@ -70,7 +70,7 @@ public class GuiOverlayButton extends GuiRecipeButton {
if (format == ItemOverlayFormat.BACKGROUND) {
NEIClientUtils.gl2DRenderContext(
() -> drawRect(
() -> GuiDraw.drawRect(
this.slot.relx,
this.slot.rely,
this.slot.width,
@@ -4,7 +4,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.minecraft.item.Item;
@@ -43,7 +45,13 @@ public class Recipe {
if (!isShapedRecipe()) {
// sort to ignore ingredients position in shapeless recipes
this.ingredients.sort(Comparator.comparing(StackInfo::getItemStackGUID));
final Map<ItemStack, String> guids = new IdentityHashMap<>();
for (ItemStack ingredient : this.ingredients) {
guids.put(ingredient, StackInfo.getItemStackGUID(ingredient));
}
this.ingredients.sort(Comparator.comparing(guids::get));
}
}
@@ -52,7 +60,10 @@ public class Recipe {
}
public static RecipeId of(IRecipeHandler handler, int recipeIndex) {
final List<PositionedStack> ingredients = handler.getIngredientStacks(recipeIndex);
return of(handler, recipeIndex, handler.getIngredientStacks(recipeIndex));
}
public static RecipeId of(IRecipeHandler handler, int recipeIndex, List<PositionedStack> ingredients) {
final String handlerName = GuiRecipeTab.getHandlerInfo(handler).getHandlerName();
PositionedStack pStackResult = handler.getResultStack(recipeIndex);
@@ -2,13 +2,17 @@ package codechicken.nei.recipe;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
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;
@@ -35,14 +39,17 @@ public class RepairRecipeHandler extends TemplateRecipeHandler {
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 stack.getItem().hashCode();
return this.hashCode;
}
@Override
@@ -104,22 +111,48 @@ public class RepairRecipeHandler extends TemplateRecipeHandler {
reloadRepairRecipesTask.restart();
}
private void buildCache() {
final Map<ItemStackKey, RepairPair> recipes = new HashMap<>();
private static final Set<Integer> knownBrokenItemTypes = ConcurrentHashMap.newKeySet();
private static volatile List<ItemStack> uniqueMaterials;
for (ItemStack itemstack1 : ItemList.items) {
recipes.computeIfAbsent(new ItemStackKey(itemstack1), key -> this.getRepairRecipe(key.stack));
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 List<ItemStack> permutations = new ArrayList<>();
final int itemId = Item.getIdFromItem(itemstack1.getItem());
if (knownBrokenItemTypes.contains(itemId)) return null;
for (ItemStack itemstack2 : ItemList.items) {
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();
@@ -127,7 +160,8 @@ public class RepairRecipeHandler extends TemplateRecipeHandler {
permutations.add(right);
}
} catch (Exception e) {
// Catch exceptions from mods that don't properly implement getIsRepairable
knownBrokenItemTypes.add(itemId);
break;
}
}
@@ -59,7 +59,7 @@ public class StackInfo {
if (nbtTag != null) {
nbtTag = (NBTTagCompound) nbtTag.copy();
nbtTag.setInteger("Count", (int) Math.max(Math.min(customCount, Integer.MAX_VALUE), 0));
nbtTag.setLong("Count", Math.max(customCount, 0));
return loadFromNBT(nbtTag);
}
@@ -150,6 +150,10 @@ public class StackInfo {
}
public static synchronized FluidStack getFluid(ItemStack stack) {
if (stack == null) {
return null;
}
ItemStackKey key = new ItemStackKey(stack);
FluidStack fluid = fluidcache.get(key);
@@ -45,7 +45,7 @@ public class DefaultStackStringifyHandler implements IStackStringifyHandler {
final ItemStack stack = ItemStack.loadItemStackFromNBT(nbtTag);
if (stack != null) {
stack.stackSize = nbtTag.getInteger("Count");
stack.stackSize = (int) Math.min(nbtTag.getLong("Count"), Integer.MAX_VALUE);
}
return stack;
@@ -31,13 +31,10 @@ public class FluidDisplayStackStringifyHandler implements IStackStringifyHandler
return null;
}
final long amount = ((ItemFluidDisplay) stack.getItem()).getAmountLong(stack);
final NBTTagCompound nbTag = new NBTTagCompound();
nbTag.setString(NBT_FLUID_NAME, fluidStack.getFluid().getName());
nbTag.setInteger("Count", saveStackSize ? fluidStack.amount : 1);
if (fluidStack.tag != null) {
nbTag.setTag("FluidTag", fluidStack.tag.copy());
}
nbTag.setLong("Count", saveStackSize ? amount : 1);
return nbTag;
}
@@ -55,13 +52,7 @@ public class FluidDisplayStackStringifyHandler implements IStackStringifyHandler
return null;
}
final FluidStack fluidStack = new FluidStack(fluid, nbtTag.getInteger("Count"));
if (nbtTag.hasKey("FluidTag")) {
fluidStack.tag = nbtTag.getCompoundTag("FluidTag");
}
return ItemFluidDisplay.createStack(fluidStack);
return ItemFluidDisplay.createStack(fluid, nbtTag.getLong("Count"));
}
@Override
@@ -67,10 +67,9 @@ public class GTFluidStackStringifyHandler implements IStackStringifyHandler {
if (nbtTag.hasKey("gtFluidName")) {
final String fluidName = nbtTag.getString("gtFluidName");
final Fluid fluid = FluidRegistry.getFluid(fluidName);
final int amount = nbtTag.getInteger("Count");
if (fluid != null) {
return ItemFluidDisplay.createStack(new FluidStack(fluid, amount));
return ItemFluidDisplay.createStack(fluid, nbtTag.getLong("Count"));
}
}
@@ -98,11 +97,7 @@ public class GTFluidStackStringifyHandler implements IStackStringifyHandler {
Fluid fluid = FluidRegistry.getFluid(nbtTag.getString("Fluid").toLowerCase());
if (fluid != null) {
FluidStack fluidStack = new FluidStack(fluid, stack.stackSize);
if (nbtTag.hasKey("FluidTag")) {
fluidStack.tag = nbtTag.getCompoundTag("FluidTag");
}
return fluidStack;
return new FluidStack(fluid, stack.stackSize);
}
}
} catch (Exception e) {}