This commit is contained in:
vfyjxf committed 2021-08-24 13:26:20 +08:00
1 parent 82945909c1
commit 2ca301c381
6 files changed
+2 -424

No files matched your search

@@ -1,47 +0,0 @@
/*
* Copied from Just Enough Energistics(https://github.com/p455w0rd/JustEnoughEnergistics/blob/master/src/main/java/p455w0rd/jee/integration/JEI.java)
*/
package com.github.vfyjxf.nee.jei;
import appeng.container.implementations.ContainerPatternTerm;
import com.github.vfyjxf.nee.NotEnoughEnergistics;
import com.github.vfyjxf.nee.utils.WrappedTable;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.JEIPlugin;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
import mezz.jei.collect.Table;
import com.google.common.collect.Table.Cell;
import mezz.jei.config.Constants;
import mezz.jei.recipes.RecipeTransferRegistry;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
/**
* @author vfyjxf
*/
@JEIPlugin
public class NEEJEIPlugin implements IModPlugin {
public static IModRegistry registry;
@Override
public void register(IModRegistry registry) {
NEEJEIPlugin.registry = registry;
Table<Class<?>, String, IRecipeTransferHandler> newRegistry = Table.hashBasedTable();
boolean ae2found = false;
for (final Cell<Class, String, IRecipeTransferHandler> currentCell : ((RecipeTransferRegistry) registry.getRecipeTransferRegistry()).getRecipeTransferHandlers().cellSet()) {
if (currentCell.getRowKey().equals(ContainerPatternTerm.class)) {
ae2found = true;
continue;
}
newRegistry.put(currentCell.getRowKey(), currentCell.getColumnKey(), currentCell.getValue());
}
newRegistry.put(ContainerPatternTerm.class, Constants.UNIVERSAL_RECIPE_TRANSFER_UID, new NEERecipeTransferHandler());
if (ae2found) {
NotEnoughEnergistics.logger.info("AE2 RecipeTransferHandler Replaced Successfully (Registered prior)");
} else {
newRegistry = new WrappedTable<>(newRegistry);
}
ObfuscationReflectionHelper.setPrivateValue(RecipeTransferRegistry.class, (RecipeTransferRegistry) registry.getRecipeTransferRegistry(), newRegistry, "recipeTransferHandlers");
}
}
@@ -1,139 +0,0 @@
package com.github.vfyjxf.nee.jei;
import appeng.container.implementations.ContainerPatternTerm;
import com.github.vfyjxf.nee.NEEConfig;
import com.github.vfyjxf.nee.NotEnoughEnergistics;
import com.github.vfyjxf.nee.network.NEENetworkHandler;
import com.github.vfyjxf.nee.network.packet.NEERecipeTransferPacket;
import com.github.vfyjxf.nee.utils.ItemUtils;
import com.github.vfyjxf.nee.utils.StackProcessor;
import mezz.jei.api.gui.IGuiIngredient;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import static com.github.vfyjxf.nee.jei.NEEJEIPlugin.registry;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author vfyjxf
*/
public class NEERecipeTransferHandler implements IRecipeTransferHandler<ContainerPatternTerm> {
public static final String OUTPUT_KEY = "Outputs";
public NEERecipeTransferHandler() {
}
@Override
public Class<ContainerPatternTerm> getContainerClass() {
return ContainerPatternTerm.class;
}
@Nullable
@Override
public IRecipeTransferError transferRecipe(ContainerPatternTerm container, IRecipeLayout recipeLayout, EntityPlayer player, boolean maxTransfer, boolean doTransfer) {
String recipeType = recipeLayout.getRecipeCategory().getUid();
boolean isCraftingRecipe = isCraftingRecipe(recipeType);
if (container.isCraftingMode()) {
if (!isCraftingRecipe && !NEEConfig.allowAutomaticSwitchPatternTerminalMode) {
return registry.getJeiHelpers().recipeTransferHandlerHelper().createUserErrorWithTooltip(I18n.format("jei.tooltip.nee.need.processing"));
}
} else {
if (isCraftingRecipe && !NEEConfig.allowAutomaticSwitchPatternTerminalMode) {
return registry.getJeiHelpers().recipeTransferHandlerHelper().createUserErrorWithTooltip(I18n.format("jei.tooltip.nee.need.crating"));
}
}
if (doTransfer) {
final Map<Integer, ? extends IGuiIngredient<ItemStack>> ingredients = recipeLayout.getItemStacks().getGuiIngredients();
final NBTTagCompound recipeInputs = new NBTTagCompound();
final NBTTagCompound recipeOutputs = new NBTTagCompound();
int inputIndex = 0;
int outputIndex = 0;
List<StackProcessor> tInputs = new ArrayList<>();
for (Map.Entry<Integer, ? extends IGuiIngredient<ItemStack>> entry : ingredients.entrySet()) {
final IGuiIngredient<ItemStack> ingredient = entry.getValue();
if (ingredient != null) {
ItemStack currentStack = ingredient.getDisplayedIngredient() == null ? ItemStack.EMPTY : ingredient.getDisplayedIngredient().copy();
if (ingredient.isInput()) {
if (isCraftingRecipe) {
tInputs.add(new StackProcessor(ingredient, currentStack.getCount()));
} else {
boolean find = false;
if (currentStack.isEmpty()) {
continue;
}
for (StackProcessor storedIngredient : tInputs) {
ItemStack storedStack = storedIngredient.ingredient.getDisplayedIngredient();
if (storedStack != null && currentStack.isItemEqual(storedStack) && ItemStack.areItemStackTagsEqual(currentStack, storedStack)) {
if (currentStack.getCount() + storedIngredient.stackSize <= storedStack.getMaxStackSize()) {
find = true;
storedIngredient.stackSize = currentStack.getCount() + storedIngredient.stackSize;
}
}
}
if (!find) {
tInputs.add(new StackProcessor(ingredient, currentStack.getCount()));
}
}
} else {
if (outputIndex >= 3 || currentStack.isEmpty() || (container.isCraftingMode() && !NEEConfig.allowAutomaticSwitchPatternTerminalMode)) {
continue;
}
recipeOutputs.setTag(OUTPUT_KEY + outputIndex, currentStack.writeToNBT(new NBTTagCompound()));
outputIndex++;
}
}
}
for (StackProcessor currentIngredient : tInputs) {
ItemStack currentStack = currentIngredient.ingredient.getDisplayedIngredient() == null ? ItemStack.EMPTY : currentIngredient.ingredient.getDisplayedIngredient().copy();
ItemStack preferModItem = ItemUtils.isPreferModItem(currentStack) ? currentStack : ItemUtils.getPreferModItem(currentIngredient.ingredient);
if (!currentStack.isEmpty()) {
currentStack.setCount(currentIngredient.stackSize);
}
if (!currentStack.isEmpty() && preferModItem != null && !preferModItem.isEmpty()) {
currentStack = preferModItem.copy();
currentStack.setCount(currentIngredient.stackSize);
}
for (ItemStack stack : currentIngredient.ingredient.getAllIngredients()) {
if (ItemUtils.isPreferItems(stack, recipeType) && !currentStack.isEmpty()) {
currentStack = stack.copy();
currentStack.setCount(currentIngredient.stackSize);
}
}
if (!currentStack.isEmpty() && ItemUtils.isInBlackList(currentStack, recipeType)) {
continue;
}
recipeInputs.setTag("#" + inputIndex, currentStack.writeToNBT(new NBTTagCompound()));
inputIndex++;
}
NEENetworkHandler.getInstance().sendToServer(new NEERecipeTransferPacket(recipeInputs, recipeOutputs, isCraftingRecipe));
if (NEEConfig.allowPrintRecipeType) {
NotEnoughEnergistics.logger.info(recipeType);
}
}
return null;
}
private boolean isCraftingRecipe(String recipeType) {
if (!recipeType.equals(VanillaRecipeCategoryUid.INFORMATION) && !recipeType.equals(VanillaRecipeCategoryUid.FUEL)) {
return recipeType.equals(VanillaRecipeCategoryUid.CRAFTING);
}
return false;
}
}
@@ -1,135 +0,0 @@
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.helpers.IContainerCraftingPacket;
import appeng.util.helpers.ItemHandlerUtil;
import appeng.util.inv.WrapperInvItemHandler;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
import static com.github.vfyjxf.nee.jei.NEERecipeTransferHandler.OUTPUT_KEY;
/**
* @author vfyjxf
*/
public class NEERecipeTransferPacket implements IMessage, IMessageHandler<NEERecipeTransferPacket, IMessage> {
private NBTTagCompound input;
private NBTTagCompound output;
private boolean craftingMode;
public NEERecipeTransferPacket() {
}
public NEERecipeTransferPacket(@Nonnull NBTTagCompound input, NBTTagCompound output, boolean craftingMode) {
this.input = input;
this.output = output;
this.craftingMode = craftingMode;
}
public NBTTagCompound getInput() {
return input;
}
public NBTTagCompound getOutput() {
return output;
}
public boolean getCraftingMode() {
return craftingMode;
}
@Override
public void fromBytes(ByteBuf buf) {
this.input = ByteBufUtils.readTag(buf);
this.craftingMode = buf.readBoolean();
if (buf.readBoolean()) {
this.output = ByteBufUtils.readTag(buf);
}
}
@Override
public void toBytes(ByteBuf buf) {
ByteBufUtils.writeTag(buf, this.input);
buf.writeBoolean(this.craftingMode);
if (this.output != null) {
buf.writeBoolean(true);
ByteBufUtils.writeTag(buf, this.output);
} else {
buf.writeBoolean(false);
}
}
@Override
public IMessage onMessage(NEERecipeTransferPacket message, MessageContext ctx) {
EntityPlayerMP player = ctx.getServerHandler().player;
Container container = player.openContainer;
player.getServerWorld().addScheduledTask(() -> {
if (container instanceof ContainerPatternTerm) {
((ContainerPatternTerm) container).getPatternTerminal().setCraftingRecipe(message.getCraftingMode());
ItemStack[] recipeInputs = new ItemStack[9];
ItemStack[] recipeOutputs = null;
NBTTagCompound currentStack;
for (int i = 0; i < recipeInputs.length; i++) {
currentStack = message.getInput().getCompoundTag("#" + i);
recipeInputs[i] = currentStack.isEmpty() ? ItemStack.EMPTY : new ItemStack(currentStack);
}
if (!message.output.isEmpty()) {
recipeOutputs = new ItemStack[3];
for (int i = 0; i < recipeOutputs.length; i++) {
currentStack = message.getOutput().getCompoundTag(OUTPUT_KEY + i);
recipeOutputs[i] = currentStack.isEmpty() ? ItemStack.EMPTY : new ItemStack(currentStack);
}
}
final IContainerCraftingPacket cct = (IContainerCraftingPacket) container;
final IGridNode node = cct.getNetworkNode();
if (node == null) {
return;
}
final IGrid grid = node.getGrid();
final IStorageGrid inv = grid.getCache(IStorageGrid.class);
final ISecurityGrid security = grid.getCache(ISecurityGrid.class);
final IItemHandler craftMatrix = cct.getInventoryByName("crafting");
if (message.input != null) {
for (int i = 0; i < craftMatrix.getSlots(); i++) {
ItemStack currentItem = ItemStack.EMPTY;
if (recipeInputs[i] != null) {
currentItem = recipeInputs[i].copy();
}
ItemHandlerUtil.setStackInSlot(craftMatrix, i, currentItem);
}
if (recipeOutputs != null && !message.getCraftingMode()) {
final IItemHandler outputMatrix = cct.getInventoryByName("output");
for (int i = 0; i < outputMatrix.getSlots(); i++) {
ItemStack currentItem = ItemStack.EMPTY;
if (recipeOutputs[i] != null) {
currentItem = recipeOutputs[i].copy();
}
ItemHandlerUtil.setStackInSlot(outputMatrix, i, currentItem);
}
}
container.onCraftMatrixChanged(new WrapperInvItemHandler(craftMatrix));
}
}
});
return null;
}
}
@@ -1,101 +0,0 @@
/*
* Copied from Just Enough Energistics(https://github.com/p455w0rd/JustEnoughEnergistics/blob/master/src/main/java/p455w0rd/jee/util/WrappedTable.java)
*/
package com.github.vfyjxf.nee.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import com.github.vfyjxf.nee.NotEnoughEnergistics;
import com.github.vfyjxf.nee.jei.NEERecipeTransferHandler;
import com.google.common.collect.ImmutableTable;
import appeng.container.implementations.ContainerPatternTerm;
import mezz.jei.collect.Table;
public class WrappedTable<R, C, V> extends Table<R, C, V> {
private static final Field f_modifiers;
private static final Field f_table;
private static final Field f_rowMappingFunction;
private final Table<R, C, V> wrapped;
static {
try {
f_modifiers = Field.class.getDeclaredField("modifiers");
f_modifiers.setAccessible(true);
f_table = Table.class.getDeclaredField("table");
f_table.setAccessible(true);
makeWriteable(f_table);
f_rowMappingFunction = Table.class.getDeclaredField("rowMappingFunction");
f_rowMappingFunction.setAccessible(true);
makeWriteable(f_rowMappingFunction);
}
catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Unable to do reflection things.", e);
}
}
public WrappedTable(Table<R, C, V> other) {
super(new HashMap<>(), HashMap::new);//Dummy values to constructor, will be replaced.
wrapped = other;
//Set underlying table stuff incase anyone else reflects it.
try {
f_table.set(this, f_table.get(other));
f_rowMappingFunction.set(this, f_rowMappingFunction.get(other));
}
catch (IllegalAccessException e) {
throw new RuntimeException("Unable to do reflection things.", e);
}
}
@SuppressWarnings("unchecked")
public V onValueSet(R row, V value) {
if (row == ContainerPatternTerm.class && value.getClass().getCanonicalName().equals("appeng.integration.modules.jei.RecipeTransferHandler")) {
value = (V) new NEERecipeTransferHandler();
NotEnoughEnergistics.logger.info("AE2 RecipeTransfeHandler Replaced Successfully (Overwrite Denied)");
}
return value;
}
@Override
public V computeIfAbsent(R row, C col, Supplier<V> valueSupplier) {
Map<C, V> rowMap = getRow(row);
return rowMap.computeIfAbsent(col, k -> onValueSet(row, valueSupplier.get()));
}
@Override
public V put(R row, C col, V val) {
Map<C, V> rowMap = getRow(row);
return rowMap.put(col, onValueSet(row, val));
}
@Override
public V get(R row, C col) {
return wrapped.get(row, col);
}
@Override
public Map<C, V> getRow(R row) {
return wrapped.getRow(row);
}
@Override
public ImmutableTable<R, C, V> toImmutable() {
return wrapped.toImmutable();
}
private static void makeWriteable(Field field) throws IllegalAccessException {
if ((field.getModifiers() & Modifier.FINAL) != 0) {
f_modifiers.set(field, field.getModifiers() & ~Modifier.FINAL);
}
}
}