Skip to content

Commit

Permalink
finished #37 and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
vfyjxf committed Feb 5, 2023
1 parent 7b7618a commit 5afddda
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ dependencies {
deobfCompile "mezz.jei:jei_${mc_version}:${jei_version}"
// compile "curse.maven:modularui-624243:3895901"
// AE2 Unofficial Extended Life
// deobfCompile "curse.maven:ae2-extended-life-570458:4094223"
// deobfCompile "curse.maven:ae2-extended-life-570458:4264378"
deobfCompile "curse.maven:applied-energistics-2-223794:2747063"
deobfCompile "curse.maven:wireless-crafting-terminal-244559:2830252"
compile "curse.maven:ae2wtlib-304024:2830114"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false
# General Specifications
mc_version=1.12.2
jei_version=4.16.1.302
mod_version=2.0.1
mod_version=2.0.3
forge_version=14.23.5.2847
mod_group=com.github.vfyjxf.neenergistics
mod_id=neenergistics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class TilePatternInterface extends AENetworkInvTile implements IGridTicka
private final List<ICraftingPatternDetails> craftingList;
private List<ItemStack> waitingToSend;
private final MachineSource source;
private boolean processing = false;
private String recipeType = "";

private final boolean[] workStarted = new boolean[9];

Expand Down Expand Up @@ -317,6 +319,8 @@ public void readFromNBT(NBTTagCompound data) {
}
this.patterns.readFromNBT(data, "patterns");
this.ejectInv.readFromNBT(data, "ejectInv");
this.processing = data.getBoolean("processing");
this.recipeType = data.getString("recipeType");

}

Expand All @@ -325,6 +329,8 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
this.patterns.writeToNBT(data, "patterns");
this.ejectInv.writeToNBT(data, "ejectInv");
data.setBoolean("processing", this.processing);
data.setString("recipeType", this.recipeType);

final NBTTagList waitingToSend = new NBTTagList();
if (this.waitingToSend != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public boolean requestNext() {
} catch (Exception e) {
LOGGER.error("Fail to request ingredient: {} ,try to request next ingredient.", ingredient.getIdentifier().getDisplayName());
currentIndex++;
finished = currentIndex >= requested.size();
return requestNext();
}
finished = currentIndex >= requested.size();
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/github/vfyjxf/nee/helper/RecipeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.Optional;
import org.apache.commons.lang3.tuple.Pair;
import p455w0rd.wct.client.gui.GuiWCT;

import javax.annotation.Nonnull;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -35,6 +37,7 @@ public class RecipeAnalyzer {

private static boolean shouldCleanCache = false;

private static final List<Consumer<Pair<List<IAEItemStack>, List<IAEItemStack>>>> updateListener = new ArrayList<>();
@Nonnull
private static List<IAEItemStack> craftableCache = new ArrayList<>();
@Nonnull
Expand Down Expand Up @@ -76,7 +79,7 @@ public RecipeAnalyzer(GuiCraftingTerm craftingTerm, boolean cleanCache) {
}

/**
*For some reason, we can't explicitly reference GuiWCT。
* For some reason, we can't explicitly reference GuiWCT。
*/
public RecipeAnalyzer(GuiContainer wirelessTerm) {
this(wirelessTerm, shouldCleanCache);
Expand All @@ -94,6 +97,10 @@ public static void setCleanCache(boolean cleanCache) {
RecipeAnalyzer.shouldCleanCache = cleanCache;
}

public static void addUpdateListener(Consumer<Pair<List<IAEItemStack>, List<IAEItemStack>>> listener) {
updateListener.add(listener);
}

public List<RecipeIngredient> analyzeRecipe(IRecipeLayout recipeLayout) {
if (ingredientsCache != null) return ingredientsCache;
Stream<List<IGuiIngredient<ItemStack>>> merged = mergeIngredients(recipeLayout).stream();
Expand All @@ -114,6 +121,20 @@ public List<RecipeIngredient> analyzeRecipe(IRecipeLayout recipeLayout) {

}

@Nonnull
public static List<IAEItemStack> getAllStacks() {
return allStacksCache;
}

@Nonnull
public static List<IAEItemStack> getCraftables() {
return craftableCache;
}

public GuiContainer getTerm() {
return term;
}

public void addAvailableIngredient(@Nonnull ItemStack stack) {
if (stack.isEmpty()) return;
boolean find = availableItems.stream()
Expand Down Expand Up @@ -202,6 +223,7 @@ private void updateCache() {
} else {
allStacksCache = getStorage();
}
updateListener.forEach(listener -> listener.accept(Pair.of(craftableCache, allStacksCache)));
this.ingredientsCache = null;
}

Expand Down
66 changes: 65 additions & 1 deletion src/main/java/com/github/vfyjxf/nee/jei/CraftingInfoError.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package com.github.vfyjxf.nee.jei;

import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.container.AEBaseContainer;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketInventoryAction;
import appeng.helpers.InventoryAction;
import appeng.util.item.AEItemStack;
import com.github.vfyjxf.nee.config.NEEConfig;
import com.github.vfyjxf.nee.helper.RecipeAnalyzer;
import com.github.vfyjxf.nee.utils.IngredientStatus;
import com.google.common.base.Stopwatch;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.ITooltipCallback;
import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import mezz.jei.gui.TooltipRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TextFormatting;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.github.vfyjxf.nee.config.KeyBindings.AUTO_CRAFT_WITH_PREVIEW;
import static com.github.vfyjxf.nee.jei.CraftingTransferHandler.isIsPatternInterfaceExists;
Expand All @@ -25,9 +38,11 @@ public class CraftingInfoError implements IRecipeTransferError {
private final RecipeAnalyzer analyzer;


public CraftingInfoError(RecipeAnalyzer analyzer, boolean isCrafting) {
public CraftingInfoError(RecipeAnalyzer analyzer, IRecipeLayout recipeLayout, boolean isCrafting) {
this.analyzer = analyzer;
this.crafting = isCrafting;
recipeLayout.getItemStacks().addTooltipCallback(new CraftingInfoCallback(analyzer));
//TODO:AE2FC supports
}

/**
Expand Down Expand Up @@ -85,4 +100,53 @@ public void showError(@Nonnull Minecraft minecraft, int mouseX, int mouseY, @Non
TooltipRenderer.drawHoveringText(minecraft, tooltips, mouseX, mouseY);
}

/**
* Feature from pae2
*/
private static class CraftingInfoCallback implements ITooltipCallback<ItemStack> {

private final Stopwatch lastClicked = Stopwatch.createStarted();
private final RecipeAnalyzer analyzer;
private List<ItemStack> craftables;

private CraftingInfoCallback(RecipeAnalyzer analyzer) {
this.analyzer = analyzer;
this.craftables = RecipeAnalyzer.getCraftables()
.stream()
.map(stack -> stack.getDefinition().copy())
.collect(Collectors.toList());
RecipeAnalyzer.addUpdateListener(pair -> {
List<IAEItemStack> stacks = pair.getLeft().isEmpty() ?
pair.getRight().stream().filter(IAEStack::isCraftable).collect(Collectors.toList()) :
pair.getLeft();
craftables = stacks.stream()
.map(stack -> stack.getDefinition().copy())
.collect(Collectors.toList());
});
}

@Override
public void onTooltip(int slotIndex, boolean input, @Nonnull ItemStack ingredient, @Nonnull List<String> tooltip) {
analyzer.update();
if (!input | ingredient.isEmpty() | craftables.isEmpty()) return;
boolean anyMatch = craftables.stream().anyMatch(ingredient::isItemEqual);
if (anyMatch) {
tooltip.add(TextFormatting.BLUE + String.format("[%s]", I18n.format("jei.tooltip.nee.helper.craftable")));

if (Mouse.isButtonDown(2) && this.lastClicked.elapsed(TimeUnit.MILLISECONDS) > 200) {
this.lastClicked.reset().start();
IAEItemStack target = AEItemStack.fromItemStack(ingredient);
if (target != null && analyzer.getTerm().inventorySlots instanceof AEBaseContainer) {
AEBaseContainer container = (AEBaseContainer) analyzer.getTerm().inventorySlots;
container.setTargetStack(target);
NetworkHandler.instance().sendToServer(
new PacketInventoryAction(InventoryAction.AUTO_CRAFT, container.getInventory().size(), 0)
);
}
}
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public IRecipeTransferError transferRecipe(@Nonnull C container, @Nonnull IRecip
} else {
RecipeAnalyzer analyzer = createAnalyzer(parent);
if (analyzer == null) return null;
return new CraftingInfoError(initAnalyzer(analyzer, craftingTerm, recipeLayout, player), true);
return new CraftingInfoError(initAnalyzer(analyzer, craftingTerm, recipeLayout, player), recipeLayout, true);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public IRecipeTransferError transferRecipe(@Nonnull ContainerPatternTerm contain
}
} else {
//TODO:Wireless Pattern Term support?
return new CraftingInfoError(new RecipeAnalyzer(patternTerm), false);
return new CraftingInfoError(new RecipeAnalyzer(patternTerm), recipeLayout, false);
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ private Pair<NBTTagCompound, NBTTagCompound> packRecipe(ItemRepo repo, Container
inputsList.add(current);
}
} else {
if (outputIndex >= 3 || stack.isEmpty() || (container.isCraftingMode())) {
if (outputIndex >= 3 || stack.isEmpty() || isCraftingRecipe) {
continue;
}
outputs.setTag(OUTPUT_KEY + outputIndex, stack.writeToNBT(new NBTTagCompound()));
Expand Down Expand Up @@ -181,7 +181,7 @@ private boolean shouldMerge(String recipeType) {

private List<StackWrapper> mapToPreference(List<StackWrapper> wrappers, String recipeType) {
return wrappers.stream()
.map(wrapper -> new StackWrapper(getFromPreference(wrapper.getIngredients(), wrapper.getStack(), recipeType), wrapper.getIngredients()))
.map(wrapper -> new StackWrapper(getFromPreference(wrapper.getIngredients(), wrapper.getStack(), recipeType), wrapper.getIngredients(), wrapper.getCount()))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public static class Handler implements IMessageHandler<PacketCraftingRequest, IM
public IMessage onMessage(PacketCraftingRequest message, MessageContext ctx) {
EntityPlayerMP player = ctx.getServerHandler().player;
Container container = player.openContainer;
if(!(container instanceof AEBaseContainer)) return null;
player.getServerWorld().addScheduledTask(() -> {
AEBaseContainer baseContainer = (AEBaseContainer) container;
Object target = baseContainer.getTarget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/github/vfyjxf/nee/utils/StackWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public StackWrapper(@Nonnull ItemStack stack, List<ItemStack> ingredients) {
this.count = stack.getCount();
}

public StackWrapper(@Nonnull ItemStack stack, List<ItemStack> ingredients, int count) {
this.stack = stack;
this.ingredients = new ArrayList<>(ingredients);
this.count = count;
}

public boolean merge(ItemStack other) {
if (ItemUtils.matches(stack, other) && count + other.getCount() <= stack.getMaxStackSize()) {
count += other.getCount();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/neenergistics/lang/en_us.lang
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
jei.tooltip.nee.helper.crafting.text1=click to request missing items.
jei.tooltip.nee.helper.crafting.text2=click to request the recipe result.
jei.tooltip.nee.helper.pattern=Autocrafting available
jei.tooltip.nee.helper.craftable=Craftable

key.neenergistics.crafting.helper.preview=Crafting Help
key.neenergistics.crafting.helper.noPreview=Crafting Help(No Preview)
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/neenergistics/lang/zh_cn.lang
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
jei.tooltip.nee.helper.crafting.text1=点击来请求缺失的物品。
jei.tooltip.nee.helper.crafting.text2=点击来请求配方成品。
jei.tooltip.nee.helper.pattern=自动合成已可用
jei.tooltip.nee.helper.craftable=可合成

key.neenergistics.crafting.helper.preview=合成帮助
key.neenergistics.crafting.helper.noPreview=合成帮助(无预览)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"parent": "block/cube_all",
"parent": "block/cube",
"textures": {
"all": "neenergistics:blocks/pattern_interface"
"particle": "neenergistics:blocks/pattern_interface",
"down": "neenergistics:blocks/pattern_interface_surface",
"up": "neenergistics:blocks/pattern_interface_surface",
"north": "neenergistics:blocks/pattern_interface_side",
"east": "neenergistics:blocks/pattern_interface_side",
"south": "neenergistics:blocks/pattern_interface_side",
"west": "neenergistics:blocks/pattern_interface_side"
}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

1 comment on commit 5afddda

@mantikafasi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love

Please sign in to comment.