update:draw highlight for craftable item

This commit is contained in:
vfyjxf committed 2021-10-04 21:45:43 +08:00
1 parent 9495bd37ec
commit 2dcead8ebe
12 files changed
+276 -9

No files matched your search

@@ -5,6 +5,9 @@ import appeng.client.gui.implementations.GuiPatternTerm;
import appeng.client.gui.implementations.GuiPatternTermEx;
import codechicken.nei.api.API;
import codechicken.nei.api.IConfigureNEI;
import codechicken.nei.guihook.GuiContainerManager;
import com.github.vfyjxf.nee.client.NEEContainerDrawHandler;
import com.github.vfyjxf.nee.config.NEEConfig;
import com.github.vfyjxf.nee.nei.NEECraftingHandler;
import com.github.vfyjxf.nee.nei.NEECraftingHelper;
import com.github.vfyjxf.nee.processor.IRecipeProcessor;
@@ -42,6 +45,10 @@ public class NEINeeConfig implements IConfigureNEI {
API.registerGuiOverlayHandler(GuiPatternTerm.class, new NEECraftingHandler(), ident);
}
if(NEEConfig.drawHighlight){
GuiContainerManager.addDrawHandler(NEEContainerDrawHandler.instance);
}
installCraftingTermSupport();
installPatternTerminalExSupport(identifiers);
@@ -0,0 +1,188 @@
package com.github.vfyjxf.nee.client;
import appeng.client.gui.implementations.GuiCraftingTerm;
import appeng.client.gui.implementations.GuiPatternTerm;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.PositionedStack;
import codechicken.nei.guihook.IContainerDrawHandler;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.IRecipeHandler;
import com.github.vfyjxf.nee.utils.GuiUtils;
import com.github.vfyjxf.nee.utils.Ingredient;
import com.github.vfyjxf.nee.utils.IngredientTracker;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.*;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.inventory.Slot;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.client.event.GuiScreenEvent;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import java.awt.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class NEEContainerDrawHandler implements IContainerDrawHandler {
public static NEEContainerDrawHandler instance = new NEEContainerDrawHandler();
private final Field overlayButtonsField;
private final Field handlerField;
private Method recipesPerPageMethod;
public NEEContainerDrawHandler() {
this.overlayButtonsField = ReflectionHelper.findField(GuiRecipe.class, "overlayButtons");
this.handlerField = ReflectionHelper.findField(GuiRecipe.class, "handler");
try {
this.recipesPerPageMethod = GuiRecipe.class.getDeclaredMethod("getRecipesPerPage");
recipesPerPageMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
@Override
public void onPreDraw(GuiContainer gui) {
}
@Override
public void renderObjects(GuiContainer gui, int mouseX, int mouseY) {
}
@Override
public void postRenderObjects(GuiContainer gui, int mouseX, int mouseY) {
}
@Override
public void renderSlotUnderlay(GuiContainer gui, Slot slot) {
}
@Override
public void renderSlotOverlay(GuiContainer gui, Slot slot) {
if (gui instanceof GuiRecipe) {
Minecraft mc = Minecraft.getMinecraft();
final ScaledResolution scaledresolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
int i = scaledresolution.getScaledWidth();
int j = scaledresolution.getScaledHeight();
final int mouseX = Mouse.getX() * i / mc.displayWidth;
final int mouseY = j - Mouse.getY() * j / mc.displayHeight - 1;
GuiRecipe guiRecipe = (GuiRecipe) gui;
GuiButton[] overlayButtons = null;
try {
overlayButtons = (GuiButton[]) this.overlayButtonsField.get(guiRecipe);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (overlayButtons != null) {
for (GuiButton button : overlayButtons) {
if (button.visible && button.enabled && GuiUtils.isMouseOverButton(button, mouseX, mouseY)) {
final int OVERLAY_BUTTON_ID_START = 4;
IRecipeHandler handler = null;
int recipesPerPage = -1;
try {
recipesPerPage = (int) this.recipesPerPageMethod.invoke(guiRecipe);
handler = (IRecipeHandler) this.handlerField.get(guiRecipe);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
if (recipesPerPage >= 0 && handler != null) {
int recipeIndex = guiRecipe.page * recipesPerPage + button.id - OVERLAY_BUTTON_ID_START;
List<PositionedStack> ingredients = handler.getIngredientStacks(recipeIndex);
IngredientTracker tracker = createTracker(guiRecipe.firstGui, ingredients);
for (int var1 = 0; var1 < ingredients.size(); var1++) {
PositionedStack stack = ingredients.get(var1);
Ingredient ingredient = tracker.getIngredients().get(var1);
Slot stackSlot = guiRecipe.slotcontainer.getSlotWithStack(stack, guiRecipe.getRecipePosition(recipeIndex).x, guiRecipe.getRecipePosition(recipeIndex).y);
if (stackSlot.equals(slot)) {
GuiContainer firstGui = guiRecipe.firstGui;
boolean renderAutoAbleItems = firstGui instanceof GuiCraftingTerm && ingredient.isCraftable() && ingredient.requiresToCraft();
boolean renderCraftableItems = (firstGui instanceof GuiPatternTerm || GuiUtils.isPatternTermExGui(firstGui)) && ingredient.isCraftable();
boolean renderMissingItems = firstGui instanceof GuiCraftingTerm && !ingredient.isCraftable() && ingredient.requiresToCraft();
if (renderAutoAbleItems || renderCraftableItems) {
Gui.drawRect(slot.xDisplayPosition, slot.yDisplayPosition,
slot.xDisplayPosition + 16, slot.yDisplayPosition + 16,
new Color(0.0f, 0.0f, 1.0f, 0.4f).getRGB());
} else if (renderMissingItems) {
Gui.drawRect(slot.xDisplayPosition, slot.yDisplayPosition,
slot.xDisplayPosition + 16, slot.yDisplayPosition + 16,
new Color(1.0f, 0.0f, 0.0f, 0.4f).getRGB());
}
}
}
}
}
}
}
}
}
@SubscribeEvent
public void onDrawScreen(GuiScreenEvent.DrawScreenEvent.Post event) {
if (event.gui instanceof GuiRecipe) {
GuiRecipe guiRecipe = (GuiRecipe) event.gui;
GuiButton[] overlayButtons = null;
try {
overlayButtons = (GuiButton[]) ReflectionHelper.findField(GuiRecipe.class, "overlayButtons").get(guiRecipe);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (overlayButtons != null) {
for (GuiButton button : overlayButtons) {
if (button.visible && button.enabled && GuiUtils.isMouseOverButton(button, event.mouseX, event.mouseY)) {
drawCraftingHelperTooltip(guiRecipe, event.mouseX, event.mouseY);
}
}
}
}
}
private void drawCraftingHelperTooltip(GuiRecipe guiRecipe, int mouseX, int mouseY) {
List<String> tooltips = new ArrayList<>();
if (guiRecipe.firstGui instanceof GuiCraftingTerm) {
tooltips.add(String.format("%s" + EnumChatFormatting.GRAY + " + " +
EnumChatFormatting.BLUE + I18n.format("neenergistics.gui.tooltip.helper.crafting"),
EnumChatFormatting.YELLOW + Keyboard.getKeyName(NEIClientConfig.getKeyBinding("nee.preview"))));
}
if (guiRecipe.firstGui instanceof GuiPatternTerm || GuiUtils.isPatternTermExGui(guiRecipe.firstGui)) {
tooltips.add(EnumChatFormatting.BLUE + I18n.format("neenergistics.gui.tooltip.helper.pattern"));
}
try {
ReflectionHelper.findMethod(GuiScreen.class, guiRecipe,
new String[]{"drawHoveringText"},
List.class,
int.class,
int.class,
FontRenderer.class).invoke(guiRecipe, tooltips, mouseX, mouseY, Minecraft.getMinecraft().fontRenderer);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
private IngredientTracker createTracker(GuiContainer firstGui, List<PositionedStack> ingredients) {
IngredientTracker tracker = new IngredientTracker(firstGui, ingredients);
//check stacks in player's inventory and crafting grid
for (Slot slot : (List<Slot>) firstGui.inventorySlots.inventorySlots) {
boolean canGetStack = slot != null && slot.getHasStack() && slot.getStack().stackSize > 0 && slot.isItemValid(slot.getStack()) && slot.canTakeStack(Minecraft.getMinecraft().thePlayer);
if (canGetStack) {
tracker.addIngredientStack(slot.getStack());
}
}
return tracker;
}
}
@@ -21,6 +21,7 @@ public class NEEConfig {
public static boolean noShift = true;
public static boolean matchOtherItems = false;
public static boolean drawHighlight = true;
public static void loadConfig(File configFile) {
config = new Configuration(configFile);
@@ -42,6 +43,9 @@ public class NEEConfig {
matchOtherItems = config.get("client", "matchOnCraftableItems", false,
"If false, Crafting Helper will not match other items").getBoolean();
drawHighlight = config.get("client","drawHighlight",true,
"if true,it will draw highlight for missing items and item which can autocraft in nei").getBoolean();
if (config.hasChanged()) config.save();
}
@@ -76,6 +76,9 @@ public class NEECraftingHelper implements IOverlayHandler {
return tracker;
}
/**
* Copied from GTNewHorizons/Applied-Energistics-2-Unofficial
*/
private void moveItem(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex) {
try {
final List<PositionedStack> ingredients = recipe.getIngredientStacks(recipeIndex);
@@ -89,6 +92,9 @@ public class NEECraftingHelper implements IOverlayHandler {
}
}
/**
* Copied from GTNewHorizons/Applied-Energistics-2-Unofficial
*/
private boolean testSize(final NBTTagCompound recipe) throws IOException {
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final DataOutputStream outputStream = new DataOutputStream(bytes);
@@ -96,6 +102,9 @@ public class NEECraftingHelper implements IOverlayHandler {
return bytes.size() > 3 * 1024;
}
/**
* Copied from GTNewHorizons/Applied-Energistics-2-Unofficial
*/
private NBTTagCompound packIngredients(GuiContainer gui, List<PositionedStack> ingredients, boolean limited) throws IOException {
final NBTTagCompound recipe = new NBTTagCompound();
for (final PositionedStack positionedStack : ingredients) {
@@ -2,6 +2,7 @@ package com.github.vfyjxf.nee.proxy;
import com.github.vfyjxf.nee.NEECommands;
import com.github.vfyjxf.nee.client.GuiHandler;
import com.github.vfyjxf.nee.client.NEEContainerDrawHandler;
import com.github.vfyjxf.nee.config.NEEConfig;
import com.github.vfyjxf.nee.nei.NEECraftingHelper;
import cpw.mods.fml.common.FMLCommonHandler;
@@ -19,5 +20,8 @@ public class ClientProxy extends CommonProxy {
if (NEEConfig.noShift) {
MinecraftForge.EVENT_BUS.register(new NEECraftingHelper());
}
if(NEEConfig.drawHighlight){
MinecraftForge.EVENT_BUS.register(NEEContainerDrawHandler.instance);
}
}
}
@@ -10,6 +10,7 @@ import com.github.vfyjxf.nee.network.NEENetworkHandler;
import com.github.vfyjxf.nee.network.packet.PacketRecipeItemChange;
import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
@@ -89,6 +90,13 @@ public class GuiUtils {
return stacks;
}
public static boolean isMouseOverButton(GuiButton button, int mouseX, int mouseY) {
return mouseX >= button.xPosition &&
mouseY >= button.yPosition &&
mouseX < button.xPosition + button.width &&
mouseY < button.yPosition + button.height;
}
public static void handleRecipeIngredientChange(Slot currentSlot, int dWheel) {
PositionedStack currentIngredients = NEECraftingHandler.ingredients.get("input" + currentSlot.getSlotIndex());
@@ -5,11 +5,13 @@ import net.minecraft.item.ItemStack;
public class Ingredient {
private PositionedStack ingredients;
private ItemStack craftableIngredient;
private final int requireCount;
private int currentCount = 0;
private final long requireCount;
private long currentCount = 0;
public Ingredient(PositionedStack ingredients) {
this.ingredients = ingredients;
this.requireCount = ingredients.items[0].stackSize;
}
@@ -17,19 +19,31 @@ public class Ingredient {
this.craftableIngredient = craftableIngredient;
}
public PositionedStack getIngredients() {
return ingredients;
}
public ItemStack getCraftableIngredient() {
return craftableIngredient;
}
public int getMissingCount(){
public long getMissingCount(){
return requireCount - currentCount;
}
public long getRequireCount() {
return requireCount;
}
public boolean isCraftable(){
return this.craftableIngredient != null;
}
public boolean requiresToCraft(){
return this.getMissingCount() > 0;
}
public void addCurrentCount(int count){
public void addCurrentCount(long count){
currentCount += count;
}
@@ -51,6 +51,29 @@ public class IngredientTracker {
}
}
public IngredientTracker(GuiContainer gui, List<PositionedStack> requiredIngredients) {
IItemList<IAEItemStack> aeItemStacks = this.getAEStacks(gui);
for (PositionedStack positionedStack : requiredIngredients) {
this.ingredients.add(new Ingredient(positionedStack));
}
for (Ingredient ingredient : this.ingredients) {
for (IAEItemStack stack : aeItemStacks) {
if (ingredient.getIngredients().contains(stack.getItemStack())) {
if (ingredient.getCraftableIngredient() == null && stack.isCraftable()) {
ingredient.setCraftableIngredient(stack.getItemStack());
}
if (stack.getStackSize() > 0) {
ingredient.addCurrentCount(stack.getStackSize());
if (ingredient.requiresToCraft()) {
stack.setStackSize(0);
} else {
stack.setStackSize(stack.getStackSize() - ingredient.getRequireCount());
}
}
}
}
}
}
private List<IAEItemStack> getCraftableStacks(GuiContainer gui) {
List<IAEItemStack> craftableAEItemSafcks = new ArrayList<>();
@@ -64,7 +87,7 @@ public class IngredientTracker {
if (list != null) {
for (IAEItemStack stack : list) {
if (stack.isCraftable()) {
craftableAEItemSafcks.add(stack);
craftableAEItemSafcks.add(stack.copy());
}
}
}
@@ -75,20 +98,26 @@ public class IngredientTracker {
IItemList<IAEItemStack> list = AEApi.instance().storage().createItemList();
try {
ItemRepo repo = (ItemRepo) ReflectionHelper.findField(GuiMEMonitorable.class, "repo").get(gui);
list = (IItemList<IAEItemStack>) ReflectionHelper.findField(ItemRepo.class, "list").get(repo);
for (IAEItemStack stack : (IItemList<IAEItemStack>) ReflectionHelper.findField(ItemRepo.class, "list").get(repo)) {
list.add(stack.copy());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return list;
}
public List<Ingredient> getIngredients() {
return ingredients;
}
public List<ItemStack> getRequireToCraftStacks() {
List<ItemStack> requireToCraftStacks = new ArrayList<>();
for (Ingredient ingredient : this.ingredients) {
ItemStack craftableStack = ingredient.getCraftableIngredient();
if (craftableStack != null && ingredient.requiresToCraft()) {
ItemStack requireStack = craftableStack.copy();
requireStack.stackSize = ingredient.getMissingCount();
requireStack.stackSize = (int) ingredient.getMissingCount();
requireToCraftStacks.add(requireStack);
}
}