First Commit

This commit is contained in:
vfyjxf committed 2021-08-06 16:07:55 +08:00
1 parent 927b160fec
commit 103d6cbae3
24 files changed
+932 -2

No files matched your search

@@ -0,0 +1,4 @@
package com.github.vfyjxf.nee;
public class Config {
}
@@ -0,0 +1,41 @@
package com.github.vfyjxf.nee;
import appeng.client.gui.implementations.GuiPatternTerm;
import codechicken.nei.api.API;
import codechicken.nei.api.IConfigureNEI;
import com.github.vfyjxf.nee.nei.NEECraftingHandler;
import com.github.vfyjxf.nee.processor.IRecipeProcessor;
import com.github.vfyjxf.nee.processor.RecipeProcessor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class NEINeeConfig implements IConfigureNEI {
@Override
public void loadConfig() {
RecipeProcessor.init();
List<String> identifiers = new ArrayList<>(Arrays.asList("crafting", "crafting2x2","brewing","smelting","fuel"));
for(IRecipeProcessor processor : RecipeProcessor.recipeProcessors){
identifiers.addAll(processor.getAllOverlayIdentifier());
}
for(String ident : identifiers){
API.registerGuiOverlay(GuiPatternTerm.class,ident);
API.registerGuiOverlayHandler(GuiPatternTerm.class, new NEECraftingHandler(), ident);
}
}
@Override
public String getName() {
return NotEnoughEnergistics.NAME;
}
@Override
public String getVersion() {
return NotEnoughEnergistics.VERSION;
}
}
@@ -0,0 +1,22 @@
package com.github.vfyjxf.nee;
import com.github.vfyjxf.nee.network.NEENetworkHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Mod(modid = NotEnoughEnergistics.MODID, version = NotEnoughEnergistics.VERSION, name = NotEnoughEnergistics.NAME, dependencies = NotEnoughEnergistics.DEPENDENCIES)
public class NotEnoughEnergistics {
public static final String MODID = "neenergistics";
public static final String NAME = "NotEnoughEnergistics";
public static final String VERSION = "@VERSION@";
public static final String DEPENDENCIES = "required-after:NotEnoughItems;required-after:appliedenergistics2";
public static final Logger logger = LogManager.getLogger("NotEnoughEnergistics");
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent e) {
NEENetworkHandler.init();
}
}
@@ -0,0 +1,106 @@
package com.github.vfyjxf.nee.nei;
import appeng.client.gui.implementations.GuiPatternTerm;
import appeng.client.gui.implementations.GuiPatternTermEx;
import codechicken.nei.api.IOverlayHandler;
import codechicken.nei.recipe.TemplateRecipeHandler;
import com.github.vfyjxf.nee.network.NEENetworkHandler;
import appeng.util.Platform;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.IRecipeHandler;
import com.github.vfyjxf.nee.network.packet.PacketNEIPatternRecipe;
import com.github.vfyjxf.nee.processor.IRecipeProcessor;
import com.github.vfyjxf.nee.processor.RecipeProcessor;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import java.util.List;
/**
* @author vfyjxf
*/
public class NEECraftingHandler implements IOverlayHandler {
public static final String OUTPUT_KEY = "Outputs";
@Override
public void overlayRecipe(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex, boolean shift) {
if (firstGui instanceof GuiPatternTerm || firstGui instanceof GuiPatternTermEx) {
NEENetworkHandler.getInstance().sendToServer(packRecipe(recipe, recipeIndex));
}
}
private PacketNEIPatternRecipe packRecipe(IRecipeHandler recipe, int recipeIndex) {
if (isCraftingTableRecipe(recipe)) {
return packCraftingTableRecipe(recipe, recipeIndex);
} else {
return packProcessRecipe(recipe, recipeIndex);
}
}
private PacketNEIPatternRecipe packProcessRecipe(IRecipeHandler recipe, int recipeIndex) {
final NBTTagCompound recipeInputs = new NBTTagCompound();
NBTTagCompound recipeOutputs = new NBTTagCompound();
String identifier = ((TemplateRecipeHandler) recipe).getOverlayIdentifier();
int inputIndex = 0;
int outputIndex = 0;
//get all recipe inputs and other stacks,use first item
for (IRecipeProcessor processor : RecipeProcessor.recipeProcessors) {
List<PositionedStack> inputs = processor.getRecipeInput(recipe, recipeIndex, identifier);
List<PositionedStack> outputs = processor.getRecipeOutput(recipe, recipeIndex, identifier);
if (inputs != null && outputs != null) {
for (PositionedStack positionedStack : inputs) {
ItemStack currentStack = positionedStack.items[0];
for (ItemStack stack : positionedStack.items) {
if (Platform.isRecipePrioritized(stack)) {
currentStack = stack.copy();
break;
}
}
recipeInputs.setTag("#" + inputIndex, currentStack.writeToNBT(new NBTTagCompound()));
inputIndex++;
}
for (PositionedStack positionedStack : outputs) {
if (outputIndex >= 3 || positionedStack.item == null) {
continue;
}
recipeOutputs.setTag(OUTPUT_KEY + outputIndex, positionedStack.item.writeToNBT(new NBTTagCompound()));
outputIndex++;
}
}
}
return new PacketNEIPatternRecipe(recipeInputs, recipeOutputs);
}
private PacketNEIPatternRecipe packCraftingTableRecipe(IRecipeHandler recipe, int recipeIndex) {
final NBTTagCompound recipeInputs = new NBTTagCompound();
final List<PositionedStack> ingredients = recipe.getIngredientStacks(recipeIndex);
for (final PositionedStack positionedStack : ingredients) {
final int col = (positionedStack.relx - 25) / 18;
final int row = (positionedStack.rely - 6) / 18;
int slotIndex = col + row * 3;
if (positionedStack.items != null && positionedStack.items.length > 0) {
final ItemStack[] currentStackList = positionedStack.items;
ItemStack stack = positionedStack.items[0];
for (ItemStack currentStack : currentStackList) {
if (Platform.isRecipePrioritized(currentStack)) {
stack = currentStack.copy();
}
}
recipeInputs.setTag("#" + slotIndex, stack.writeToNBT(new NBTTagCompound()));
}
}
return new PacketNEIPatternRecipe(recipeInputs, null);
}
private boolean isCraftingTableRecipe(IRecipeHandler recipe) {
TemplateRecipeHandler templateRecipeHandler = (TemplateRecipeHandler) recipe;
String overlayIdentifier = templateRecipeHandler.getOverlayIdentifier();
return "crafting".equals(overlayIdentifier) || "crafting2x2".equals(overlayIdentifier);
}
}
@@ -0,0 +1,20 @@
package com.github.vfyjxf.nee.network;
import com.github.vfyjxf.nee.NotEnoughEnergistics;
import com.github.vfyjxf.nee.network.packet.PacketNEIPatternRecipe;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
public class NEENetworkHandler {
private static final SimpleNetworkWrapper INSTANCE = new SimpleNetworkWrapper(NotEnoughEnergistics.MODID);
public static SimpleNetworkWrapper getInstance(){
return INSTANCE;
}
public static void init(){
INSTANCE.registerMessage(PacketNEIPatternRecipe.class, PacketNEIPatternRecipe.class,0, Side.SERVER);
}
}
@@ -0,0 +1,166 @@
package com.github.vfyjxf.nee.network.packet;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridNode;
import appeng.api.networking.security.ISecurityGrid;
import appeng.api.networking.storage.IStorageGrid;
import appeng.container.implementations.ContainerPatternTerm;
import appeng.container.implementations.ContainerPatternTermEx;
import appeng.helpers.IContainerCraftingPacket;
import com.github.vfyjxf.nee.nei.NEECraftingHandler;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import static com.github.vfyjxf.nee.nei.NEECraftingHandler.OUTPUT_KEY;
import javax.annotation.Nonnull;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class PacketNEIPatternRecipe implements IMessage, IMessageHandler<PacketNEIPatternRecipe, IMessage> {
NBTTagCompound input;
NBTTagCompound output;
private final ItemStack EMPTY = (ItemStack) null;
public PacketNEIPatternRecipe() {
}
public PacketNEIPatternRecipe(@Nonnull NBTTagCompound input, NBTTagCompound output) {
this.input = input;
this.output = output;
}
@Override
public void fromBytes(ByteBuf buf) {
this.input = ByteBufUtils.readTag(buf);
if (buf.readBoolean()) {
this.output = ByteBufUtils.readTag(buf);
}
}
@Override
public void toBytes(ByteBuf buf) {
ByteBufUtils.writeTag(buf, this.input);
if (this.output != null) {
buf.writeBoolean(true);
ByteBufUtils.writeTag(buf, this.output);
} else {
buf.writeBoolean(false);
}
}
@Override
public IMessage onMessage(PacketNEIPatternRecipe message, MessageContext ctx) {
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
Container container = player.openContainer;
if (container instanceof ContainerPatternTerm && message.output == null) {
((ContainerPatternTerm) container).getPatternTerminal().setCraftingRecipe(true);
message.craftingTableRecipeHandler((ContainerPatternTerm) container, message);
} else if (container instanceof ContainerPatternTerm) {
((ContainerPatternTerm) container).getPatternTerminal().setCraftingRecipe(false);
message.processRecipeHandler((ContainerPatternTerm) container, message);
} else if (container instanceof ContainerPatternTermEx){
}
return null;
}
private void craftingTableRecipeHandler(ContainerPatternTerm container, PacketNEIPatternRecipe message) {
ItemStack[] recipeInput = new ItemStack[9];
NBTTagCompound currentStack;
for (int i = 0; i < recipeInput.length; i++) {
currentStack = (NBTTagCompound) message.input.getTag("#" + i);
recipeInput[i] = currentStack == null ? null : ItemStack.loadItemStackFromNBT(currentStack);
}
final IContainerCraftingPacket cct = container;
final IGridNode node = cct.getNetworkNode();
if (node != null) {
final IGrid grid = node.getGrid();
if (grid == null) {
return;
}
final IStorageGrid inv = grid.getCache(IStorageGrid.class);
final ISecurityGrid security = grid.getCache(ISecurityGrid.class);
final IInventory craftMatrix = cct.getInventoryByName("crafting");
if (inv != null && message.input != null && security != null) {
for (int i = 0; i < craftMatrix.getSizeInventory(); i++) {
ItemStack currentItem = null;
if (recipeInput[i] != null) {
currentItem = recipeInput[i].copy();
}
craftMatrix.setInventorySlotContents(i, currentItem);
}
container.onCraftMatrixChanged(craftMatrix);
}
}
}
private void processRecipeHandler(ContainerPatternTerm container, PacketNEIPatternRecipe message) {
ItemStack[] recipeInput = new ItemStack[9];
ItemStack[] recipeOutput = new ItemStack[3];
for (int i = 0; i < recipeInput.length; i++) {
NBTTagCompound currentStack = (NBTTagCompound) message.input.getTag("#" + i);
recipeInput[i] = currentStack == null ? null : ItemStack.loadItemStackFromNBT(currentStack);
}
for (int i = 0; i < recipeOutput.length; i++) {
NBTTagCompound currentStack = (NBTTagCompound) message.output.getTag(OUTPUT_KEY + i);
recipeOutput[i] = currentStack == null ? null : ItemStack.loadItemStackFromNBT(currentStack);
}
final IContainerCraftingPacket cct = container;
final IGridNode node = cct.getNetworkNode();
if (node != null) {
final IGrid grid = node.getGrid();
if (grid == null) {
return;
}
final IStorageGrid inv = grid.getCache(IStorageGrid.class);
final ISecurityGrid security = grid.getCache(ISecurityGrid.class);
final IInventory craftMatrix = cct.getInventoryByName("crafting");
final IInventory outputMatrix = cct.getInventoryByName("output");
if (inv != null && message.input != null && security != null) {
for (int i = 0; i < craftMatrix.getSizeInventory(); i++) {
ItemStack currentItem = null;
if (recipeInput[i] != null) {
currentItem = recipeInput[i].copy();
}
craftMatrix.setInventorySlotContents(i, currentItem);
}
for (int i = 0; i < outputMatrix.getSizeInventory(); i++) {
ItemStack currentItem = null;
if (recipeOutput[i] != null) {
currentItem = recipeOutput[i].copy();
}
outputMatrix.setInventorySlotContents(i, currentItem);
}
container.onCraftMatrixChanged(craftMatrix);
}
}
}
}
@@ -0,0 +1,37 @@
package com.github.vfyjxf.nee.processor;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.IRecipeHandler;
import java.util.*;
public class AvaritiaRecipeProcessor implements IRecipeProcessor{
@Override
public Set<String> getAllOverlayIdentifier() {
return new HashSet<>(Arrays.asList(
"extreme","extreme_compression"
));
}
@Override
public List<PositionedStack> getRecipeInput(IRecipeHandler recipe, int recipeIndex, String identifier) {
for(String ident : getAllOverlayIdentifier()){
if(ident.equals(identifier)){
return new ArrayList<>(recipe.getIngredientStacks(recipeIndex));
}
}
return null;
}
@Override
public List<PositionedStack> getRecipeOutput(IRecipeHandler recipe, int recipeIndex, String identifier) {
for(String ident : getAllOverlayIdentifier()){
if(ident.equals(identifier)){
List<PositionedStack> recipeOutput = new ArrayList<>();
recipeOutput.add(recipe.getResultStack(recipeIndex));
return recipeOutput;
}
}
return null;
}
}
@@ -0,0 +1,71 @@
package com.github.vfyjxf.nee.processor;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.IRecipeHandler;
import gregtech.api.enums.ItemList;
import gregtech.api.util.GT_Recipe;
import gregtech.api.util.GT_Utility;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author vfyjxf
*/
public class GregTechRecipeProcessor implements IRecipeProcessor {
private static final Class<?> gtDefaultClz, gtAssLineClz;
static {
Class<?> gtDH = null;
Class<?> gtAL = null;
try {
gtDH = Class.forName("gregtech.nei.GT_NEI_DefaultHandler");
gtAL = Class.forName("gregtech.nei.GT_NEI_AssLineHandler");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
gtDefaultClz = gtDH;
gtAssLineClz = gtAL;
}
@Override
public Set<String> getAllOverlayIdentifier() {
Set<String> identifiers = new HashSet<>();
for (GT_Recipe.GT_Recipe_Map tMap : GT_Recipe.GT_Recipe_Map.sMappings) {
if (tMap.mNEIAllowed) {
identifiers.add(tMap.mNEIName);
}
}
identifiers.add("gt.recipe.fakeAssemblylineProcess");
return identifiers;
}
@Override
public List<PositionedStack> getRecipeInput(IRecipeHandler recipe, int recipeIndex, String identifier) {
if (gtDefaultClz.isInstance(recipe) || gtAssLineClz.isInstance(recipe)) {
List<PositionedStack> recipeInputs = new ArrayList<>(recipe.getIngredientStacks(recipeIndex));
recipeInputs.removeIf(positionedStack -> GT_Utility.getFluidFromDisplayStack(positionedStack.items[0]) != null);
return recipeInputs;
}
return null;
}
@Override
public List<PositionedStack> getRecipeOutput(IRecipeHandler recipe, int recipeIndex, String identifier) {
if (gtDefaultClz.isInstance(recipe) || gtAssLineClz.isInstance(recipe)) {
List<PositionedStack> recipeOutputs = new ArrayList<>(recipe.getOtherStacks(recipeIndex));
recipeOutputs.removeIf(positionedStack -> GT_Utility.getFluidFromDisplayStack(positionedStack.items[0]) != null);
return recipeOutputs;
}
return null;
}
}
@@ -0,0 +1,44 @@
package com.github.vfyjxf.nee.processor;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.IRecipeHandler;
import java.util.*;
/**
* @author vfyjxf
*/
public class ICRecipeProcessor implements IRecipeProcessor {
@Override
public Set<String> getAllOverlayIdentifier() {
return new HashSet<>(Arrays.asList(
"blastfurnace", "BlockCutter", "centrifuge", "compressor", "extractor", "fluidcanner", "macerator",
"metalformer", "oreWashing", "solidcanner"
));
}
@Override
public List<PositionedStack> getRecipeInput(IRecipeHandler recipe, int recipeIndex, String identifier) {
for (String ident : getAllOverlayIdentifier()) {
if (identifier.equals(ident)) {
return new ArrayList<>(recipe.getIngredientStacks(recipeIndex));
}
}
return null;
}
@Override
public List<PositionedStack> getRecipeOutput(IRecipeHandler recipe, int recipeIndex, String identifier) {
for (String ident : getAllOverlayIdentifier()) {
if (identifier.equals(ident)) {
List<PositionedStack> recipeOutputs = new ArrayList<>();
recipeOutputs.add(recipe.getResultStack(recipeIndex));
recipeOutputs.addAll(recipe.getOtherStacks(recipeIndex));
return recipeOutputs;
}
}
return null;
}
}
@@ -0,0 +1,25 @@
package com.github.vfyjxf.nee.processor;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.IRecipeHandler;
import com.github.vfyjxf.nee.network.packet.PacketNEIPatternRecipe;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/**
* @author vfyjxf
*/
public interface IRecipeProcessor {
default Set<String> getAllOverlayIdentifier(){
return Collections.emptySet();
}
List<PositionedStack> getRecipeInput(IRecipeHandler recipe, int recipeIndex, String identifier);
List<PositionedStack> getRecipeOutput(IRecipeHandler recipe, int recipeIndex, String identifier);
}
@@ -0,0 +1,31 @@
package com.github.vfyjxf.nee.processor;
import com.github.vfyjxf.nee.NotEnoughEnergistics;
import cpw.mods.fml.common.Loader;
import java.util.ArrayList;
import java.util.List;
public class RecipeProcessor {
public static List<IRecipeProcessor> recipeProcessors = new ArrayList<>();
public static void init(){
NotEnoughEnergistics.logger.info("-----Not Enough Energistics Init Start-----");
NotEnoughEnergistics.logger.info("Install Vanilla Support");
recipeProcessors.add(new VanillaRecipeProcessor());
if(Loader.isModLoaded("gregtech")){
NotEnoughEnergistics.logger.info("Found GregTech,install GregTech Support");
recipeProcessors.add(new GregTechRecipeProcessor());
}
if(Loader.isModLoaded("IC2")){
NotEnoughEnergistics.logger.info("Found IC2,install IC2 Support");
recipeProcessors.add(new ICRecipeProcessor());
}
if(Loader.isModLoaded("Avaritia")){
NotEnoughEnergistics.logger.info("Found Avaritia, install Avaritia Support");
recipeProcessors.add(new AvaritiaRecipeProcessor());
}
NotEnoughEnergistics.logger.info("-----Not Enough Energistics Init Finished-----");
}
}
@@ -0,0 +1,38 @@
package com.github.vfyjxf.nee.processor;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.IRecipeHandler;
import java.util.*;
/**
* @author vfyjxf
*/
public class VanillaRecipeProcessor implements IRecipeProcessor {
@Override
public Set<String> getAllOverlayIdentifier() {
return new HashSet<>(Arrays.asList("brewing", "smelting", "fuel"));
}
@Override
public List<PositionedStack> getRecipeInput(IRecipeHandler recipe, int recipeIndex, String identifier) {
List<PositionedStack> recipeInputs = new ArrayList<>();
if ("brewing".equals(identifier) || "smelting".equals(identifier) || "fuel".equals(identifier)) {
recipeInputs.addAll(recipe.getIngredientStacks(recipeIndex));
recipeInputs.addAll(recipe.getOtherStacks(recipeIndex));
}
return recipeInputs;
}
@Override
public List<PositionedStack> getRecipeOutput(IRecipeHandler recipe, int recipeIndex, String identifier) {
if ("brewing".equals(identifier) || "smelting".equals(identifier) || "fuel".equals(identifier)) {
List<PositionedStack> recipeOutput = new ArrayList<>();
recipeOutput.add(recipe.getResultStack(recipeIndex));
return recipeOutput;
}
return null;
}
}