diff options
| author | YannickMG <yannickmg@gmail.com> | 2022-02-03 12:55:55 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-03 18:55:55 +0100 |
| commit | a48a158ad76dfa0b8d74894bbbe8d2520ff760af (patch) | |
| tree | ce6ac0d636c9076f15febc0e1a47efa25c83cdf6 /src/main/java/gregtech/nei | |
| parent | b4ff9ad8d8a8003a2b0a63e0a2153f37f60cd02f (diff) | |
| download | GT5-Unofficial-a48a158ad76dfa0b8d74894bbbe8d2520ff760af.tar.gz GT5-Unofficial-a48a158ad76dfa0b8d74894bbbe8d2520ff760af.tar.bz2 GT5-Unofficial-a48a158ad76dfa0b8d74894bbbe8d2520ff760af.zip | |
Basic Machine NEI Power Tiers (#914)
* Add IntelliJ .shelf folder to .gitignore
* Fixing GT_GuiTooltip bug I introduced
* Deprecated superfluous String trans methods.
* Add Power class to compute and describe power usage
* trans -> GT_Utility.trans
Removed absent param from Javadoc
* Add Power to BasicMachine and use it for power calculations
* Steam BasicMachine rework
Have Bronze and Steel tier override Power with appropriate SteamPower
Make Bronze machines explicitly tier 1
Make Steel machines explicitly tier 2
Defined recipe list and bricked status for each machine
Unified checkRecipe uses machine's recipe list and Power
* Raised NEI dependency to 2.2.5
* Defining NEI transfer rect in individual UIs
Using a new interface called from GT_RectHandler
* Code cleanup & minor refactor
* Add power field to NEI Default Handler
Add method to create Power object from the recipe map
* Display power tier in NEI handler name
* User Power to draw NEI Handler description
* Enable filtering handler recipes using Power
* Repurposing aBricked parameter into aHighPressure
To fix my previous faulty method of setting mTier
* Must call getCache to get recipes properly.
* Better communicate NEI Overclock info
Diffstat (limited to 'src/main/java/gregtech/nei')
3 files changed, 366 insertions, 93 deletions
diff --git a/src/main/java/gregtech/nei/GT_NEI_DefaultHandler.java b/src/main/java/gregtech/nei/GT_NEI_DefaultHandler.java index d6f339ec7f..93fb0bd3d2 100644 --- a/src/main/java/gregtech/nei/GT_NEI_DefaultHandler.java +++ b/src/main/java/gregtech/nei/GT_NEI_DefaultHandler.java @@ -8,24 +8,36 @@ import codechicken.nei.guihook.IContainerTooltipHandler; import codechicken.nei.recipe.GuiCraftingRecipe; import codechicken.nei.recipe.GuiRecipe; import codechicken.nei.recipe.GuiUsageRecipe; +import codechicken.nei.recipe.ICraftingHandler; +import codechicken.nei.recipe.IUsageHandler; +import codechicken.nei.recipe.RecipeCatalysts; import codechicken.nei.recipe.TemplateRecipeHandler; import gregtech.GT_Mod; import gregtech.api.enums.GT_Values; import gregtech.api.enums.OrePrefixes; +import gregtech.api.gui.GT_GUIContainer; import gregtech.api.gui.GT_GUIContainer_BasicMachine; +import gregtech.api.interfaces.metatileentity.IMetaTileEntity; +import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine; import gregtech.api.objects.ItemData; import gregtech.api.util.GT_LanguageManager; import gregtech.api.util.GT_Log; import gregtech.api.util.GT_OreDictUnificator; import gregtech.api.util.GT_Recipe; import gregtech.api.util.GT_Utility; +import gregtech.common.power.EUPower; +import gregtech.common.power.Power; +import gregtech.common.power.UnspecifiedEUPower; +import gregtech.common.blocks.GT_Item_Machines; import gregtech.common.gui.GT_GUIContainer_FusionReactor; import gregtech.common.gui.GT_GUIContainer_PrimitiveBlastFurnace; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; +import org.apache.commons.lang3.Range; import org.lwjgl.opengl.GL11; import javax.annotation.Nonnull; @@ -34,16 +46,24 @@ import java.awt.*; import java.lang.ref.SoftReference; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; +import static codechicken.nei.recipe.RecipeInfo.getGuiOffset; + public class GT_NEI_DefaultHandler extends RecipeMapHandler { public static final int sOffsetX = 5; public static final int sOffsetY = 11; private static final ConcurrentMap<GT_Recipe.GT_Recipe_Map, SortedRecipeListCache> CACHE = new ConcurrentHashMap<>(); + private Power mPower; + private String mRecipeName; // Name of the handler displayed on top + private NEIHandlerAbsoluteTooltip mRecipeNameTooltip; + private static final int RECIPE_NAME_WIDTH = 140; + static { GuiContainerManager.addInputHandler(new GT_RectHandler()); GuiContainerManager.addTooltipHandler(new GT_RectHandler()); @@ -51,7 +71,7 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { public GT_NEI_DefaultHandler(GT_Recipe.GT_Recipe_Map aRecipeMap) { super(aRecipeMap); - this.transferRects.add(new TemplateRecipeHandler.RecipeTransferRect(new Rectangle(65, 13, 36, 18), getOverlayIdentifier())); + this.transferRects.add(new RecipeTransferRect(new Rectangle(65, 13, 36, 18), getOverlayIdentifier())); } @Deprecated @@ -61,8 +81,12 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { return result; } + private SortedRecipeListCache getCacheHolder() { + return CACHE.computeIfAbsent(mRecipeMap, m -> new SortedRecipeListCache()); + } + public List<CachedDefaultRecipe> getCache() { - SortedRecipeListCache cacheHolder = CACHE.computeIfAbsent(mRecipeMap, m -> new SortedRecipeListCache()); + SortedRecipeListCache cacheHolder = getCacheHolder(); List<CachedDefaultRecipe> cache; if (cacheHolder.getCachedRecipesVersion() != GT_Mod.gregtechproxy.getReloadCount() || (cache = cacheHolder.getCachedRecipes()) == null) { cache = mRecipeMap.mRecipeList.stream() // do not use parallel stream. This is already parallelized by NEI @@ -91,7 +115,12 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { @Override public void loadCraftingRecipes(String outputId, Object... results) { if (outputId.equals(getOverlayIdentifier())) { - arecipes.addAll(getCache()); + if (results.length > 0 && results[0] instanceof Power) { + mPower = (Power) results[0]; + loadTieredCraftingRecipesUpTo(mPower.getTier()); + } else { + arecipes.addAll(getCache()); + } } else { super.loadCraftingRecipes(outputId, results); } @@ -109,26 +138,41 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { tResults.add(GT_OreDictUnificator.get(tPrefix, tPrefixMaterial.mMaterial.mMaterial, 1L)); } } - FluidStack tFluid = GT_Utility.getFluidForFilledItem(aResult, true); + addFluidStacks(aResult, tResults); + for (CachedDefaultRecipe recipe : getCache()) { + if (tResults.stream().anyMatch(stack -> recipe.contains(recipe.mOutputs, stack))) + arecipes.add(recipe); + } + } + + private void addFluidStacks(ItemStack aStack, ArrayList<ItemStack> tResults) { + FluidStack tFluid = GT_Utility.getFluidForFilledItem(aStack, true); FluidStack tFluidStack; if (tFluid != null) { tFluidStack = tFluid; tResults.add(GT_Utility.getFluidDisplayStack(tFluid, false)); - } - else tFluidStack = GT_Utility.getFluidFromDisplayStack(aResult); + } else tFluidStack = GT_Utility.getFluidFromDisplayStack(aStack); if (tFluidStack != null) { tResults.addAll(GT_Utility.getContainersFromFluid(tFluidStack)); } - for (CachedDefaultRecipe recipe : getCache()) { - if (tResults.stream().anyMatch(stack -> recipe.contains(recipe.mOutputs, stack))) - arecipes.add(recipe); + } + + private void loadTieredCraftingRecipesUpTo(byte upperTier) { + arecipes.addAll(getTieredRecipes((byte) 0, upperTier)); + } + + private List<CachedDefaultRecipe> getTieredRecipes(byte lowerTier, byte upperTier) { + List<CachedDefaultRecipe> recipes = getCache(); + if ( recipes.size() > 0 ) { + Range<Integer> indexRange = getCacheHolder().getIndexRangeForTiers(lowerTier, upperTier); + recipes = recipes.subList(indexRange.getMinimum(), indexRange.getMaximum() + 1); } + return recipes; } @Override public void loadUsageRecipes(ItemStack aInput) { ItemData tPrefixMaterial = GT_OreDictUnificator.getAssociation(aInput); - ArrayList<ItemStack> tInputs = new ArrayList<>(); tInputs.add(aInput); tInputs.add(GT_OreDictUnificator.get(false, aInput)); @@ -137,16 +181,7 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { tInputs.add(GT_OreDictUnificator.get(tPrefix, tPrefixMaterial.mMaterial.mMaterial, 1L)); } } - FluidStack tFluid = GT_Utility.getFluidForFilledItem(aInput, true); - FluidStack tFluidStack; - if (tFluid != null) { - tFluidStack = tFluid; - tInputs.add(GT_Utility.getFluidDisplayStack(tFluid, false)); - } - else tFluidStack = GT_Utility.getFluidFromDisplayStack(aInput); - if (tFluidStack != null) { - tInputs.addAll(GT_Utility.getContainersFromFluid(tFluidStack)); - } + addFluidStacks(aInput, tInputs); for (CachedDefaultRecipe recipe : getCache()) { if (tInputs.stream().anyMatch(stack -> recipe.contains(recipe.mInputs, stack))) arecipes.add(recipe); @@ -154,6 +189,32 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { } @Override + public IUsageHandler getUsageAndCatalystHandler(String inputId, Object... ingredients) { + if (inputId.equals("item")) { + ItemStack candidate = (ItemStack) ingredients[0]; + GT_NEI_DefaultHandler handler = (GT_NEI_DefaultHandler) newInstance(); + if (RecipeCatalysts.containsCatalyst(handler, candidate)) { + IMetaTileEntity gtTileEntity = GT_Item_Machines.getMetaTileEntity(candidate); + if (gtTileEntity instanceof GT_MetaTileEntity_BasicMachine) { + Power power = ((GT_MetaTileEntity_BasicMachine) gtTileEntity).getPower(); + handler.loadCraftingRecipes(getOverlayIdentifier(), power); + return handler; + } + } + } + return this.getUsageHandler(inputId, ingredients); + } + + @Override + public ICraftingHandler getRecipeHandler(String outputId, Object... results) { + GT_NEI_DefaultHandler handler = (GT_NEI_DefaultHandler) super.getRecipeHandler(outputId, results); + if (results.length > 0 && results[0] instanceof Power) { + handler.mPower = (Power) results[0]; + } + return handler; + } + + @Override public String getOverlayIdentifier() { return this.mRecipeMap.mNEIName; } @@ -172,18 +233,57 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { @Override public String getRecipeName() { + if (mRecipeName == null) { + mRecipeName = computeRecipeName(); + } + return mRecipeName; + } + + private String computeRecipeName() { + String recipeName = GT_LanguageManager.getTranslation(this.mRecipeMap.mUnlocalizedName); + if (mPower != null) { + recipeName = addSuffixToRecipeName(recipeName, " (", mPower.getTierString() + ")"); + } + return recipeName; + } + + private String addSuffixToRecipeName(String recipeName, String separator, String suffix) { + FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; + int recipeNameWidth = fontRenderer.getStringWidth(recipeName); + int targetWidth = RECIPE_NAME_WIDTH - fontRenderer.getStringWidth(suffix); + if (recipeNameWidth + fontRenderer.getStringWidth(separator) > targetWidth) { + setupRecipeNameTooltip(recipeName + separator + suffix); + separator = "...("; + recipeName = shrinkRecipeName(recipeName, targetWidth - fontRenderer.getStringWidth(separator)); + } + return recipeName + separator + suffix; + } + + private String shrinkRecipeName(String recipeName, int targetWidth) { + FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; + do { + recipeName = recipeName.substring(0, recipeName.length() - 2); + } while (fontRenderer.getStringWidth(recipeName) > targetWidth); + return recipeName; + } + + private void setupRecipeNameTooltip(String tooltip) { + mRecipeNameTooltip = new NEIHandlerAbsoluteTooltip(tooltip, new Rectangle(13, -34, RECIPE_NAME_WIDTH - 1, 11)); + } + + @Override + public String getRecipeTabName() { return GT_LanguageManager.getTranslation(this.mRecipeMap.mUnlocalizedName); } @Override public String getGuiTexture() { -// return "gregtech:textures/gui/" + this.mRecipeMap.mUnlocalizedName + ".png"; return this.mRecipeMap.mNEIGUIPath; } @Override public List<String> handleItemTooltip(GuiRecipe gui, ItemStack aStack, List<String> currenttip, int aRecipeIndex) { - TemplateRecipeHandler.CachedRecipe tObject = (TemplateRecipeHandler.CachedRecipe) this.arecipes.get(aRecipeIndex); + CachedRecipe tObject = this.arecipes.get(aRecipeIndex); if ((tObject instanceof CachedDefaultRecipe)) { CachedDefaultRecipe tRecipe = (CachedDefaultRecipe) tObject; for (PositionedStack tStack : tRecipe.mOutputs) { @@ -191,7 +291,7 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { if ((!(tStack instanceof FixedPositionedStack)) || (((FixedPositionedStack) tStack).mChance <= 0) || (((FixedPositionedStack) tStack).mChance == 10000)) { break; } - currenttip.add(trans("150","Chance: ") + ((FixedPositionedStack) tStack).mChance / 100 + "." + (((FixedPositionedStack) tStack).mChance % 100 < 10 ? "0" + ((FixedPositionedStack) tStack).mChance % 100 : Integer.valueOf(((FixedPositionedStack) tStack).mChance % 100)) + "%"); + currenttip.add(GT_Utility.trans("150", "Chance: ") + ((FixedPositionedStack) tStack).mChance / 100 + "." + (((FixedPositionedStack) tStack).mChance % 100 < 10 ? "0" + ((FixedPositionedStack) tStack).mChance % 100 : Integer.valueOf(((FixedPositionedStack) tStack).mChance % 100)) + "%"); break; } } @@ -201,79 +301,149 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { (tStack.item.stackSize != 0)) { break; } - currenttip.add(trans("151","Does not get consumed in the process")); + currenttip.add(GT_Utility.trans("151", "Does not get consumed in the process")); break; } } } + + if (mRecipeNameTooltip != null) { + mRecipeNameTooltip.handleTooltip(currenttip, aRecipeIndex); + } return currenttip; } + private Power getPowerFromRecipeMap() { + // By default, assume generic EU LV power with no overclocks + Power power; + if (this.mRecipeMap.mShowVoltageAmperageInNEI) { + power = new EUPower((byte) 1, this.mRecipeMap.mAmperage); + } else { + power = new UnspecifiedEUPower((byte) 1, this.mRecipeMap.mAmperage); + } + return power; + } + @Override public void drawExtras(int aRecipeIndex) { - int tEUt = ((CachedDefaultRecipe) this.arecipes.get(aRecipeIndex)).mRecipe.mEUt; - int tDuration = ((CachedDefaultRecipe) this.arecipes.get(aRecipeIndex)).mRecipe.mDuration; - String[] recipeDesc = ((CachedDefaultRecipe) this.arecipes.get(aRecipeIndex)).mRecipe.getNeiDesc(); + GT_Recipe recipe = ((CachedDefaultRecipe) this.arecipes.get(aRecipeIndex)).mRecipe; + String[] recipeDesc = recipe.getNeiDesc(); if (recipeDesc == null) { - if (tEUt != 0) { - drawText(10, 73, trans("152","Total: ") + GT_Utility.formatNumbers((long) tDuration * tEUt) + " EU", 0xFF000000); - drawText(10, 83, trans("153","Usage: ") + GT_Utility.formatNumbers(tEUt) + " EU/t", 0xFF000000); - if (this.mRecipeMap.mShowVoltageAmperageInNEI) { - int voltage = tEUt / this.mRecipeMap.mAmperage; - byte tier = GT_Utility.getTier(voltage); - if (tier < 0 || tier >= 16) { - drawText(10, 93, trans("154","Voltage: ") + GT_Utility.formatNumbers(voltage) + " EU", 0xFFFF0000); -//add here gt logger - } else { - drawText(10, 93, trans("154","Voltage: ") + GT_Utility.formatNumbers(voltage) + " EU (" + GT_Values.VN[tier] + ")", 0xFF000000); - } - drawText(10, 103, trans("155","Amperage: ") + GT_Utility.formatNumbers(this.mRecipeMap.mAmperage), 0xFF000000); - } else { - drawText(10, 93, trans("156","Voltage: unspecified"), 0xFF000000); - drawText(10, 103, trans("157","Amperage: unspecified"), 0xFF000000); - } - } - if (tDuration > 0) { - drawText(10, 113, trans("158","Time: ") + GT_Utility.formatNumbers(0.05d * tDuration) + trans("161"," secs"), 0xFF000000); - } - int tSpecial = ((CachedDefaultRecipe) this.arecipes.get(aRecipeIndex)).mRecipe.mSpecialValue; - if (tSpecial == -100 && GT_Mod.gregtechproxy.mLowGravProcessing) { - drawText(10, 123, trans("159","Needs Low Gravity"), 0xFF000000); - } else if (tSpecial == -200 && GT_Mod.gregtechproxy.mEnableCleanroom) { - drawText(10, 123, trans("160","Needs Cleanroom"), 0xFF000000); - } else if (tSpecial == -201) { - drawText(10, 123, trans("206","Scan for Assembly Line"), 0xFF000000); - } else if (tSpecial == -300 && GT_Mod.gregtechproxy.mEnableCleanroom) { - drawText(10, 123, trans("160","Needs Cleanroom & LowGrav"), 0xFF000000); - } else if (tSpecial == -400) { - drawText(10, 123, trans("216","Deprecated Recipe"), 0xFF000000); - } else if ((GT_Utility.isStringValid(this.mRecipeMap.mNEISpecialValuePre)) || (GT_Utility.isStringValid(this.mRecipeMap.mNEISpecialValuePost))) { - drawText(10, 123, this.mRecipeMap.mNEISpecialValuePre + GT_Utility.formatNumbers(tSpecial * this.mRecipeMap.mNEISpecialValueMultiplier) + this.mRecipeMap.mNEISpecialValuePost, 0xFF000000); - } + drawDescription(recipe); } else { - int i = 0; - for (String descLine : recipeDesc) { - drawText(10, 73 + 10 * i, descLine, 0xFF000000); - i++; + drawOverrideDescription(recipeDesc); + } + } + + private void drawDescription(GT_Recipe recipe) { + if (mPower == null) { + mPower = getPowerFromRecipeMap(); + } + mPower.computePowerUsageAndDuration(recipe.mEUt, recipe.mDuration); + if (mPower.getEuPerTick() > 0) { + drawPowerUsageLines(); + } + if (mPower.getDurationTicks() > 0) { + drawLine(4, GT_Utility.trans("158", "Time: ") + mPower.getDurationString()); + } + drawOptionalLine(5, getSpecialInfo(recipe.mSpecialValue)); + } + + private void drawPowerUsageLines() { + drawLine(0, GT_Utility.trans("152", "Total: ") + mPower.getTotalPowerString()); + drawLine(1, GT_Utility.trans("153", "Usage: ") + mPower.getPowerUsageString()); + drawOptionalLine(2, mPower.getVoltageString(), GT_Utility.trans("154", "Voltage: ")); + drawOptionalLine(3, mPower.getAmperageString(), GT_Utility.trans("155", "Amperage: ")); + } + + private void drawOverrideDescription(String[] recipeDesc) { + for (int i = 0; i < recipeDesc.length; i++) { + if (recipeDesc[i] != null) { + drawLine(i, recipeDesc[i]); } } } + private String getSpecialInfo(int specialValue) { + String specialInfo = null; + if (specialValue == -100 && GT_Mod.gregtechproxy.mLowGravProcessing) { + specialInfo = GT_Utility.trans("159", "Needs Low Gravity"); + } else if (specialValue == -200 && GT_Mod.gregtechproxy.mEnableCleanroom) { + specialInfo = GT_Utility.trans("160", "Needs Cleanroom"); + } else if (specialValue == -201) { + specialInfo = GT_Utility.trans("206", "Scan for Assembly Line"); + } else if (specialValue == -300 && GT_Mod.gregtechproxy.mEnableCleanroom) { + specialInfo = GT_Utility.trans("160", "Needs Cleanroom & LowGrav"); + } else if (specialValue == -400) { + specialInfo = GT_Utility.trans("216", "Deprecated Recipe"); + } else if (hasSpecialValueFormat()) { + specialInfo = formatSpecialValue(specialValue); + } + return specialInfo; + } + + private boolean hasSpecialValueFormat() { + return (GT_Utility.isStringValid(this.mRecipeMap.mNEISpecialValuePre)) + || (GT_Utility.isStringValid(this.mRecipeMap.mNEISpecialValuePost)); + } + + private String formatSpecialValue(int SpecialValue) { + return this.mRecipeMap.mNEISpecialValuePre + GT_Utility.formatNumbers( + (long) SpecialValue * this.mRecipeMap.mNEISpecialValueMultiplier) + + this.mRecipeMap.mNEISpecialValuePost; + } + + private void drawOptionalLine(int lineNumber, String line, String prefix) { + if (line != null) { + drawLine(lineNumber, prefix + line); + } + } + + private void drawOptionalLine(int lineNumber, String line) { + if (line != null) { + drawLine(lineNumber, line); + } + } + + private void drawLine(int lineNumber, String line) { + drawText(10, 73 + lineNumber * 10, line, 0xFF000000); + } + public static class GT_RectHandler implements IContainerInputHandler, IContainerTooltipHandler { @Override public boolean mouseClicked(GuiContainer gui, int mousex, int mousey, int button) { if (canHandle(gui)) { - if (button == 0) { - return transferRect(gui, false); - } - if (button == 1) { - return transferRect(gui, true); + NEI_TransferRectHost host = (NEI_TransferRectHost) gui; + if (hostRectContainsMouse(host, getMousePos(gui, mousex, mousey))) { + if (button == 0) { + return handleTransferRectMouseClick(host, false); + } + if (button == 1) { + return handleTransferRectMouseClick(host, true); + } } } return false; } + private Point getMousePos(GuiContainer gui, int mousex, int mousey) { + Point point = new Point( + mousex - ((GT_GUIContainer) gui).getLeft() - getGuiOffset(gui)[0], + mousey - ((GT_GUIContainer) gui).getTop() - getGuiOffset(gui)[1]); + return point; + } + + private boolean hostRectContainsMouse(NEI_TransferRectHost host, Point mousePos) { + return host.getNeiTransferRect().contains(mousePos); + } + + private boolean handleTransferRectMouseClick(NEI_TransferRectHost gui, boolean usage) { + String mNEI = gui.getNeiTransferRectString(); + Object[] args = gui.getNeiTransferRectArgs(); + return usage ? GuiUsageRecipe.openRecipeGui(mNEI) : GuiCraftingRecipe.openRecipeGui(mNEI, args); + } + @Override public boolean lastKeyTyped(GuiContainer gui, char keyChar, int keyCode) { return false; @@ -288,29 +458,14 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { @Override public List<String> handleTooltip(GuiContainer gui, int mousex, int mousey, List<String> currenttip) { if ((canHandle(gui)) && (currenttip.isEmpty())) { - if (gui instanceof GT_GUIContainer_BasicMachine && new Rectangle(65, 13, 36, 18).contains(new Point(GuiDraw.getMousePosition().x - ((GT_GUIContainer_BasicMachine) gui).getLeft() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[0], GuiDraw.getMousePosition().y - ((GT_GUIContainer_BasicMachine) gui).getTop() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[1]))) { - currenttip.add("Recipes"); - } else if (gui instanceof GT_GUIContainer_FusionReactor && new Rectangle(145, 0, 24, 24).contains(new Point(GuiDraw.getMousePosition().x - ((GT_GUIContainer_FusionReactor) gui).getLeft() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[0], GuiDraw.getMousePosition().y - ((GT_GUIContainer_FusionReactor) gui).getTop() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[1]))) { - currenttip.add("Recipes"); - } else if (gui instanceof GT_GUIContainer_PrimitiveBlastFurnace && new Rectangle(51, 10, 24, 24).contains(new Point(GuiDraw.getMousePosition().x - ((GT_GUIContainer_PrimitiveBlastFurnace) gui).getLeft() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[0], GuiDraw.getMousePosition().y - ((GT_GUIContainer_PrimitiveBlastFurnace) gui).getTop() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[1]))) { - currenttip.add("Recipes"); + NEI_TransferRectHost host = (NEI_TransferRectHost) gui; + if (hostRectContainsMouse(host, getMousePos(gui, mousex, mousey))) { + currenttip.add(host.getNeiTransferRectTooltip()); } - } return currenttip; } - private boolean transferRect(GuiContainer gui, boolean usage) { - if (gui instanceof GT_GUIContainer_BasicMachine) { - return (canHandle(gui)) && (new Rectangle(65, 13, 36, 18).contains(new Point(GuiDraw.getMousePosition().x - ((GT_GUIContainer_BasicMachine) gui).getLeft() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[0], GuiDraw.getMousePosition().y - ((GT_GUIContainer_BasicMachine) gui).getTop() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[1]))) && (usage ? GuiUsageRecipe.openRecipeGui(((GT_GUIContainer_BasicMachine) gui).mNEI) : GuiCraftingRecipe.openRecipeGui(((GT_GUIContainer_BasicMachine) gui).mNEI)); - } else if (gui instanceof GT_GUIContainer_FusionReactor) { - return (canHandle(gui)) && (new Rectangle(145, 0, 24, 24).contains(new Point(GuiDraw.getMousePosition().x - ((GT_GUIContainer_FusionReactor) gui).getLeft() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[0], GuiDraw.getMousePosition().y - ((GT_GUIContainer_FusionReactor) gui).getTop() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[1]))) && (usage ? GuiUsageRecipe.openRecipeGui(((GT_GUIContainer_FusionReactor) gui).mNEI) : GuiCraftingRecipe.openRecipeGui(((GT_GUIContainer_FusionReactor) gui).mNEI)); - } else if (gui instanceof GT_GUIContainer_PrimitiveBlastFurnace) { - return (canHandle(gui)) && (new Rectangle(51, 10, 24, 24).contains(new Point(GuiDraw.getMousePosition().x - ((GT_GUIContainer_PrimitiveBlastFurnace) gui).getLeft() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[0], GuiDraw.getMousePosition().y - ((GT_GUIContainer_PrimitiveBlastFurnace) gui).getTop() - codechicken.nei.recipe.RecipeInfo.getGuiOffset(gui)[1]))) && (usage ? GuiUsageRecipe.openRecipeGui(((GT_GUIContainer_PrimitiveBlastFurnace) gui).mNEI) : GuiCraftingRecipe.openRecipeGui(((GT_GUIContainer_PrimitiveBlastFurnace) gui).mNEI)); - } - return false; - } - @Override public List<String> handleItemDisplayName(GuiContainer gui, ItemStack itemstack, List<String> currenttip) { return currenttip; @@ -412,7 +567,7 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { try { maybeIn = aRecipe.getInputPositionedStacks(); - } catch(NullPointerException npe) { + } catch (NullPointerException npe) { maybeIn = null; GT_Log.err.println("CachedDefaultRecipe - Invalid InputPositionedStacks " + aRecipe); npe.printStackTrace(GT_Log.err); @@ -425,7 +580,7 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { npe.printStackTrace(GT_Log.err); } - if ( maybeIn != null && maybeOut != null) { + if (maybeIn != null && maybeOut != null) { mInputs = maybeIn; mOutputs = maybeOut; return; @@ -884,14 +1039,17 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { } } - public String trans(String aKey, String aEnglish){ - return GT_LanguageManager.addStringLocalization("Interaction_DESCRIPTION_Index_"+aKey, aEnglish, false); + @Deprecated + public String trans(String aKey, String aEnglish) { + return GT_Utility.trans(aKey, aEnglish); } private static class SortedRecipeListCache { private int mCachedRecipesVersion = -1; @Nullable private SoftReference<List<CachedDefaultRecipe>> mCachedRecipes; + private Range<Integer>[] mTierIndexes; + private Range<Byte> mTierRange; public int getCachedRecipesVersion() { return mCachedRecipesVersion; @@ -909,5 +1067,58 @@ public class GT_NEI_DefaultHandler extends RecipeMapHandler { public void setCachedRecipes(@Nonnull List<CachedDefaultRecipe> aCachedRecipes) { this.mCachedRecipes = new SoftReference<>(aCachedRecipes); } + + public Range<Integer> getIndexRangeForTiers(byte lowerTier, byte upperTier) { + if (mTierIndexes == null) { + computeTierIndexes(); + } + return Range.between(getLowIndexForTier(lowerTier), getHighIndexForTier(upperTier)); + } + + private void computeTierIndexes() { + mTierIndexes = new Range[GT_Values.V.length]; + Iterator<CachedDefaultRecipe> iterator = mCachedRecipes.get().iterator(); + + int index = 0; + int minIndex = 0; + int maxIndex = -1; + byte previousTier = -1; + byte lowestTier = 0; + while(iterator.hasNext()) { + CachedDefaultRecipe recipe = iterator.next(); + byte recipeTier = GT_Utility.getTier(recipe.mRecipe.mEUt); + if (recipeTier != previousTier) { + if ( maxIndex != -1) { + mTierIndexes[previousTier] = Range.between(minIndex, maxIndex); + } else { + lowestTier = recipeTier; + } + minIndex = index; + previousTier = recipeTier; + } + maxIndex = index; + index++; + if (!iterator.hasNext()) { + mTierIndexes[recipeTier] = Range.between(minIndex, maxIndex); + mTierRange = Range.between(lowestTier, recipeTier); + } + } + } + + private int getLowIndexForTier(byte lowerTier) { + byte lowTier = (byte) Math.max(mTierRange.getMinimum(), lowerTier); + while (mTierIndexes[lowTier] == null) { + lowTier++; + } + return mTierIndexes[lowTier].getMinimum(); + } + + private int getHighIndexForTier(byte upperTier) { + byte highTier = (byte) Math.min(mTierRange.getMaximum(), upperTier); + while (mTierIndexes[highTier] == null) { + highTier--; + } + return mTierIndexes[highTier].getMaximum(); + } } } diff --git a/src/main/java/gregtech/nei/NEIHandlerAbsoluteTooltip.java b/src/main/java/gregtech/nei/NEIHandlerAbsoluteTooltip.java new file mode 100644 index 0000000000..fae3b68882 --- /dev/null +++ b/src/main/java/gregtech/nei/NEIHandlerAbsoluteTooltip.java @@ -0,0 +1,52 @@ +package gregtech.nei; + +import codechicken.lib.gui.GuiDraw; + +import java.awt.*; +import java.util.List; + +public class NEIHandlerAbsoluteTooltip { + private final Rectangle area; + private final String tooltip; + private Dimension displaySize; + + public NEIHandlerAbsoluteTooltip(String tooltip, Rectangle area) { + this.tooltip = tooltip; + this.area = area; + } + + public void handleTooltip(List<String> currenttip, int recipeIndex) { + displaySize = GuiDraw.displaySize(); + if (shouldAddTooltip(recipeIndex)) { + currenttip.add(tooltip); + } + } + + private boolean shouldAddTooltip(int recipeIndex) { + return isPageFirstRecipe(recipeIndex) && mouseInArea(); + } + + private boolean mouseInArea() { + Point mousePos = getRelMouse(); + return area.contains(mousePos); + } + + + private Point getRelMouse() { + int ySize = Math.min(Math.max(displaySize.height - 68, 166), 370); + int guiLeft = (displaySize.width - 176) / 2; + int guiTop = (displaySize.height - ySize) / 2 + 10; + Point mousePos = GuiDraw.getMousePosition(); + return new Point(mousePos.x - guiLeft - 5, mousePos.y - guiTop - 38); + } + + private boolean isPageFirstRecipe(int recipe) { + int actualRecipesPerPage = getActualRecipesPerPage(); + return actualRecipesPerPage < 2 || recipe % 2 == 0; + } + + private int getActualRecipesPerPage() { + int ySize = Math.min(Math.max(displaySize.height - 68, 166), 370); + return (ySize - (12*3)) / 135; + } +} diff --git a/src/main/java/gregtech/nei/NEI_TransferRectHost.java b/src/main/java/gregtech/nei/NEI_TransferRectHost.java new file mode 100644 index 0000000000..e04740cf99 --- /dev/null +++ b/src/main/java/gregtech/nei/NEI_TransferRectHost.java @@ -0,0 +1,10 @@ +package gregtech.nei; + +import java.awt.*; + +public interface NEI_TransferRectHost { + String getNeiTransferRectString(); + String getNeiTransferRectTooltip(); + Object[] getNeiTransferRectArgs(); + Rectangle getNeiTransferRect(); +} |
