Skip to content

Commit

Permalink
rework PreferenceList and IngredientBlackList
Browse files Browse the repository at this point in the history
  • Loading branch information
vfyjxf committed Nov 26, 2022
1 parent 16986ad commit d7840da
Show file tree
Hide file tree
Showing 21 changed files with 631 additions and 165 deletions.
41 changes: 41 additions & 0 deletions src/main/java/com/github/vfyjxf/nee/asm/AppengHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.vfyjxf.nee.asm;

import appeng.api.storage.data.IAEItemStack;
import appeng.client.gui.implementations.GuiMEMonitorable;
import appeng.core.sync.AppEngPacket;
import appeng.core.sync.network.INetworkInfo;
import mezz.jei.gui.recipes.RecipesGui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import p455w0rd.wct.client.gui.GuiWCT;

import java.util.List;

@SuppressWarnings("unused")
public class AppengHooks {

/**
* Before {@link appeng.core.sync.packets.PacketMEInventoryUpdate#clientPacketData(INetworkInfo, AppEngPacket, EntityPlayer)} Return
*/
public static void updateMeInventory(GuiScreen screen, final List<IAEItemStack> list) {
if (screen instanceof RecipesGui) {
GuiScreen parent = ((RecipesGui) screen).getParentScreen();
if (parent instanceof GuiMEMonitorable) {
((GuiMEMonitorable) parent).postUpdate(list);
}
}
}

/**
* Before {@link appeng.client.gui.implementations.GuiMEMonitorable#postUpdate(List)} Return
*/
public static void updateWirelessInventory(GuiScreen screen, final List<IAEItemStack> list){
if (screen instanceof RecipesGui) {
GuiScreen parent = ((RecipesGui) screen).getParentScreen();
if (parent instanceof GuiWCT) {
((GuiWCT) parent).postUpdate(list);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
/**
* A draggable widget to switch ingredient in pattern.
* //TODO:AE2FC support
* //TODO:scrolling functions
*/
public class IngredientSwitcherWidget extends Gui {

Expand Down Expand Up @@ -76,7 +75,7 @@ public IngredientSwitcherWidget(int x, int y, int width, int height, List<ItemSt
this.widgets = new ArrayList<>();
this.initWidgets(viewStacks);
this.scrollOffset = 0;
this.addButton = new AddPreferenceButton(x + width - 32, y + 5);
this.addButton = new AddPreferenceButton(this, x + width - 32, y + 5);
this.opeButton = new OpenPreferenceDataButton(x + width - 19, y + 5);
this.action = action;
}
Expand Down Expand Up @@ -192,6 +191,8 @@ public boolean handleMouseClicked(int eventButton, int mouseX, int mouseY) {
}
}
}
this.addButton.update();

return result;
}

Expand Down Expand Up @@ -279,6 +280,15 @@ public boolean isRenderingTooltip() {
return renderingTooltip;
}

public ItemWidget getSelectedWidget() {
return selectedWidget;
}

public void cleanSelection() {
this.selectedWidget.setSelected(false);
this.selectedWidget = null;
}

public interface OnResultApply {
void onApply();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.vfyjxf.nee.client.gui.widgets;

import com.github.vfyjxf.nee.client.gui.IngredientSwitcherWidget;
import com.github.vfyjxf.nee.config.PreferenceList;
import com.github.vfyjxf.nee.helper.PreferenceHelper;
import mezz.jei.gui.TooltipRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
Expand All @@ -11,10 +14,12 @@

public class AddPreferenceButton extends GuiImageButton {

private final IngredientSwitcherWidget widget;
private boolean ingredientExists;

public AddPreferenceButton(int x, int y) {
super(x, y, 9, 9, false);
public AddPreferenceButton(IngredientSwitcherWidget switcherWidget, int x, int y) {
super(x, y, 11, 11, false);
this.widget = switcherWidget;
}

@Override
Expand All @@ -28,14 +33,25 @@ public void drawImage(Minecraft mc) {
}
}


@Override
public boolean mousePressed(@Nonnull Minecraft mc, int mouseX, int mouseY) {
boolean pressable = super.mousePressed(mc, mouseX, mouseY);
if (pressable) {

if (this.hovered) {
if (widget.getSelectedWidget() != null) {
if (ingredientExists) {
PreferenceList.INSTANCE.removePreference(widget.getSelectedWidget().getIngredient());
} else {
PreferenceList.INSTANCE.addPreference(widget.getSelectedWidget().getIngredient(), null);
}
ingredientExists = !ingredientExists;
}
return true;
}
return pressable;
return false;
}

public void update() {
this.ingredientExists = widget.getSelectedWidget() != null &&
PreferenceHelper.isPreferItem(widget.getSelectedWidget().getIngredient());
}

@Override
Expand All @@ -48,8 +64,4 @@ public void drawTooltip(Minecraft mc, int mouseX, int mouseY) {
}
}

public interface OnSlotPress {
void onSlotPress();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
public abstract class GuiImageButton extends GuiButton {

private final boolean halfSize;
private OnPress onPress;

public GuiImageButton(int x, int y, int width, int height, boolean halfSize) {
this(x, y, width, height, halfSize, (button -> true));
}

public GuiImageButton(int x, int y, int width, int height, boolean halfSize, OnPress onPress) {
super(0, x, y, width, height, "");
this.halfSize = halfSize;
this.onPress = onPress;
}

@Override
Expand Down Expand Up @@ -51,15 +45,9 @@ public void drawTooltip(Minecraft mc, int mouseX, int mouseY) {

@Override
public boolean mousePressed(@Nonnull Minecraft mc, int mouseX, int mouseY) {
return onPress.onPress(this);
}

public void setOnPress(OnPress onPress) {
this.onPress = onPress;
}

public interface OnPress {
boolean onPress(GuiImageButton button);
if (this.hovered) {
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Mouse;

import javax.annotation.Nonnull;

public class MergeConfigButton extends GuiImageButton implements ITooltip {
private IngredientMergeMode mergeMode;

Expand All @@ -16,19 +18,6 @@ public MergeConfigButton(int x, int y, IngredientMergeMode value) {
this.x = x;
this.y = y;
this.mergeMode = value;
this.setOnPress(button -> {
int ordinal = Mouse.getEventButton() != 2 ? this.getMergeMode().ordinal() + 1 : this.getMergeMode().ordinal() - 1;

if (ordinal >= IngredientMergeMode.values().length) {
ordinal = 0;
}
if (ordinal < 0) {
ordinal = IngredientMergeMode.values().length - 1;
}
this.setMode(IngredientMergeMode.values()[ordinal]);
NEEConfig.setMergeMode(IngredientMergeMode.values()[ordinal]);
return true;
});
}

public void setMode(IngredientMergeMode mergeMode) {
Expand Down Expand Up @@ -59,6 +48,24 @@ public String getMessage() {
I18n.format("gui.neenergistics.button.tooltip.combination", mergeMode.getLocalName());
}

@Override
public boolean mousePressed(@Nonnull Minecraft mc, int mouseX, int mouseY) {
if (this.hovered) {
int ordinal = Mouse.getEventButton() != 2 ? this.getMergeMode().ordinal() + 1 : this.getMergeMode().ordinal() - 1;

if (ordinal >= IngredientMergeMode.values().length) {
ordinal = 0;
}
if (ordinal < 0) {
ordinal = IngredientMergeMode.values().length - 1;
}
this.setMode(IngredientMergeMode.values()[ordinal]);
NEEConfig.setMergeMode(IngredientMergeMode.values()[ordinal]);
return true;
}
return false;
}

@Override
public int xPos() {
return this.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class OpenPreferenceDataButton extends GuiImageButton {
public OpenPreferenceDataButton(int x, int y) {
super(x, y, 9, 9, false);
super(x, y, 11, 11, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.github.vfyjxf.nee.config;

import com.github.vfyjxf.nee.utils.BlackIngredient;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class IngredientBlackList {

private static final Logger LOGGER = LogManager.getLogger();

public static final IngredientBlackList INSTANCE = new IngredientBlackList();

private final List<BlackIngredient> blackList = new ArrayList<>();

private IngredientBlackList() {

}

public void addPreference(ItemStack stack, String acceptedType) {
BlackIngredient ingredient = new BlackIngredient(stack, acceptedType);
if (!blackList.contains(ingredient)) {
blackList.add(ingredient);
saveList();
}
}

public List<BlackIngredient> getBlackList() {
return blackList;
}

public void loadList() {

File file = NEEConfig.getBlacklistFile();
if (file == null || !file.exists()) {
return;
}

List<String> strings;
try (FileReader reader = new FileReader(file)) {
strings = IOUtils.readLines(reader);
} catch (IOException e) {
LOGGER.error("Failed to load blacklist from file {}", file, e);
return;
}

List<BlackIngredient> list = strings.stream()
.map(jsonString -> {
try {
return JsonToNBT.getTagFromJson(jsonString);
} catch (NBTException e) {
throw new RuntimeException(e);
}
})
.map(tagCompound -> {
ItemStack identifier = new ItemStack(tagCompound.getCompoundTag("identifier"));
String acceptedType = tagCompound.hasKey("acceptedType") ? tagCompound.getString("acceptedType") : null;
return new BlackIngredient(identifier, acceptedType);
})
.collect(Collectors.toList());

this.blackList.clear();
this.blackList.addAll(list);
}

public void saveList() {
File file = NEEConfig.getBlacklistFile();
if (file != null) {
List<String> strings = blackList.stream()
.map(blackIngredient -> blackIngredient.toTag().toString())
.collect(Collectors.toList());
try (FileWriter writer = new FileWriter(file)) {
IOUtils.writeLines(strings, "\n", writer);
} catch (IOException e) {
LOGGER.error("Failed to save blacklist to file {}", file, e);
}
}
}

}
Loading

0 comments on commit d7840da

Please sign in to comment.