create presets module

This commit is contained in:
Serghei Borovetchi authored and Jason Mitchell committed 2022-02-10 08:43:16 -08:00
1 parent 49b22e0683
commit 5f1ed67a8b
12 files changed
+1382 -49

No files matched your search

@@ -21,7 +21,6 @@ import static codechicken.lib.gui.GuiDraw.drawRect;
import java.awt.Point;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
@@ -450,6 +449,10 @@ public class BookmarkPanel extends ItemPanel
public void saveBookmarks()
{
if (bookmarkFile == null) {
return;
}
ArrayList<String> strings = new ArrayList<>();
for (int grpIdx = 0; grpIdx < namespaces.size() - 1; grpIdx++) {
@@ -487,12 +490,10 @@ public class BookmarkPanel extends ItemPanel
}
if (bookmarkFile != null) {
try(FileWriter writer = new FileWriter(bookmarkFile)) {
IOUtils.writeLines(strings, "\n", writer);
} catch (IOException e) {
NEIClientConfig.logger.error("Filed to save bookmarks list to file {}", bookmarkFile, e);
}
try (FileOutputStream output = new FileOutputStream(bookmarkFile)) {
IOUtils.writeLines(strings, "\n", output, "UTF-8");
} catch (IOException e) {
NEIClientConfig.logger.error("Filed to save bookmarks list to file {}", bookmarkFile, e);
}
}
@@ -748,6 +749,7 @@ public class BookmarkPanel extends ItemPanel
public void mouseUp(int mousex, int mousey, int button)
{
if (sortedStackIndex != -1) {
draggedStack = null;
sortedNamespaceIndex = -1;
sortedStackIndex = -1;
mouseDownSlot = -1;
@@ -6,6 +6,8 @@ import codechicken.lib.packet.PacketCustom;
import codechicken.nei.api.API;
import codechicken.nei.api.ItemInfo;
import codechicken.nei.recipe.GuiRecipeTab;
import codechicken.nei.recipe.StackInfo;
import com.google.common.collect.Lists;
import cpw.mods.fml.client.CustomModLoadingErrorDisplayException;
import cpw.mods.fml.common.FMLCommonHandler;
@@ -160,6 +162,7 @@ public class ClientHandler
loadSerialHandlers();
loadHeightHackHandlers();
ItemInfo.preInit();
StackInfo.loadGuidFilters();
}
public static void loadSerialHandlers() {
@@ -252,6 +252,7 @@ public class ItemList
try {
filtered = ItemList.forkJoinPool.submit(() -> items.parallelStream()
.filter(PresetsWidget::matches)
.filter(filter::matches)
.collect(Collectors.toCollection(ArrayList::new))).get();
} catch (InterruptedException | ExecutionException e) {
+15 -1
View File
@@ -1,5 +1,6 @@
package codechicken.nei;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.api.GuiInfo;
import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.guihook.GuiContainerManager;
@@ -14,6 +15,7 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
import static codechicken.lib.gui.GuiDraw.drawRect;
import static codechicken.nei.LayoutManager.searchField;
import java.util.ArrayList;
@@ -82,6 +84,18 @@ public class ItemPanel extends Widget
super.refresh(gui);
}
@Override
protected void drawItem(Rectangle4i rect, int idx, ItemPanelSlot focus)
{
if (PresetsWidget.inEditMode() && !PresetsWidget.isHidden(getItem(idx))) {
drawRect(rect.x, rect.y, rect.w, rect.h, 0xee555555);
}
super.drawItem(rect, idx, PresetsWidget.inEditMode()? null: focus);
}
}
protected ItemsGrid grid = new ItemPanelGrid();
@@ -236,6 +250,7 @@ public class ItemPanel extends Widget
if (mouseOverSlot == null || mouseOverSlot.slotIndex != mouseDownSlot || heldTime > 500) {
draggedStack = getDraggedStackWithQuantity(mouseDownSlot);
mouseDownSlot = -1;
}
}
@@ -482,7 +497,6 @@ public class ItemPanel extends Widget
else if (button == 1)
GuiUsageRecipe.openRecipeGui("item", item);
draggedStack = null;
mouseDownSlot = -1;
return;
}
+1 -1
View File
@@ -14,7 +14,7 @@ import static codechicken.lib.gui.GuiDraw.drawRect;
public class ItemsGrid
{
protected static final int SLOT_SIZE = 18;
public static final int SLOT_SIZE = 18;
protected int width;
protected int height;
@@ -86,6 +86,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
public static ItemPanel itemPanel;
public static BookmarkPanel bookmarkPanel;
public static SubsetWidget dropDown;
public static PresetsWidget presetsPanel;
public static TextField searchField;
public static ButtonCycled options;
@@ -177,7 +178,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
@Override
public boolean objectUnderMouse(GuiContainer gui, int mousex, int mousey) {
if (!isHidden() && isEnabled())
for (Widget widget : controlWidgets)
for (Widget widget : drawWidgets)
if (widget.contains(mousex, mousey))
return true;
@@ -238,7 +239,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
@Override
public ItemStack getStackUnderMouse(GuiContainer gui, int mousex, int mousey) {
if (!isHidden() && isEnabled()) {
for (Widget widget : controlWidgets) {
for (Widget widget : drawWidgets) {
ItemStack stack = widget.getStackMouseOver(mousex, mousey);
if (stack != null)
return stack;
@@ -275,7 +276,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
@Override
public List<String> handleTooltip(GuiContainer gui, int mousex, int mousey, List<String> currenttip) {
if (!isHidden() && isEnabled() && GuiContainerManager.shouldShowTooltip(gui)) {
for (Widget widget : controlWidgets)
for (Widget widget : drawWidgets)
currenttip = widget.handleTooltip(mousex, mousey, currenttip);
}
return currenttip;
@@ -339,6 +340,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
bookmarkPanel.init();
dropDown = new SubsetWidget();
presetsPanel = new PresetsWidget();
searchField = new SearchField("search");
options = new ButtonCycled(3)
@@ -671,7 +673,13 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
addWidget(options);
addWidget(bookmarksButton);
if (visiblity.showItemPanel) {
addWidget(itemPanel);
if (PresetsWidget.inEditMode()) {
drawWidgets.add(itemPanel);
} else {
addWidget(itemPanel);
}
itemPanel.setVisible();
addWidget(more);
@@ -682,6 +690,8 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
if (visiblity.showBookmarkPanel) {
addWidget(bookmarkPanel);
bookmarkPanel.setVisible();
addWidget(presetsPanel);
}
if (visiblity.showSearchSection) {
@@ -738,7 +748,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
if (!isEnabled())
return;
for (Widget widget : controlWidgets)
for (Widget widget : drawWidgets)
widget.update();
}
@@ -747,7 +757,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
if (isHidden() || !isEnabled())
return false;
for (Widget widget : controlWidgets)
for (Widget widget : drawWidgets)
if (widget.onMouseWheel(scrolled, mousex, mousey))
return true;
@@ -190,6 +190,17 @@ public class NEIClientConfig {
return true;
}
});
tag.getTag("inventory.worldSpecificPresets").setComment("Global or world specific presets").getBooleanValue(false);
API.addOption(new OptionToggleButton("inventory.worldSpecificPresets", true) {
@Override
public boolean onClick(int button) {
super.onClick(button);
initPresetsFile(worldPath);
return true;
}
});
tag.getTag("inventory.bookmarksEnabled").setComment("Enable/disable bookmarks").getBooleanValue(true);
API.addOption(new OptionToggleButton("inventory.bookmarksEnabled", true));
tag.getTag("inventory.saveCurrentRecipeInBookmarksEnabled").setComment("Save Current Recipe in Bookmarks").getBooleanValue(true);
@@ -303,6 +314,7 @@ public class NEIClientConfig {
}
initBookmarkFile(worldPath);
initPresetsFile(worldPath);
world = new ConfigSet(new File(specificDir, "NEI.dat"), new ConfigFile(new File(specificDir, "NEI.cfg")));
onWorldLoad(newWorld);
}
@@ -386,6 +398,16 @@ public class NEIClientConfig {
ItemPanels.bookmarkPanel.setBookmarkFile(worldPath);
}
private static void initPresetsFile(String worldPath)
{
if (!global.config.getTag("inventory.worldSpecificPresets").getBooleanValue()) {
worldPath = "global";
}
PresetsWidget.loadPresets(worldPath);
}
public static boolean isWorldSpecific(String setting) {
if(world == null) return false;
ConfigTag tag = world.config.getTag(setting, false);
File diff suppressed because it is too large. Load diff
@@ -1,19 +1,35 @@
package codechicken.nei.recipe;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.api.IStackStringifyHandler;
import codechicken.nei.recipe.stackinfo.DefaultStackStringifyHandler;
import codechicken.nei.recipe.stackinfo.GTFluidStackStringifyHandler;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.fluids.FluidStack;
import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.WeakHashMap;
public class StackInfo
{
public static final ArrayList<IStackStringifyHandler> stackStringifyHandlers = new ArrayList<>();
public static final ArrayList<IStackStringifyHandler> stackStringifyHandlers = new ArrayList<>();
private static final HashMap<String, HashMap<String, String[]>> guidfilters = new HashMap<>();
private static final WeakHashMap<ItemStack, String> guidcache = new WeakHashMap<>();
static {
stackStringifyHandlers.add(new DefaultStackStringifyHandler());
stackStringifyHandlers.add(new GTFluidStackStringifyHandler());
@@ -39,7 +55,7 @@ public class StackInfo
{
ItemStack stack = null;
for (int i = stackStringifyHandlers.size() - 1; i >= 0 && stack == null; i--) {
for (int i = stackStringifyHandlers.size() - 1; i >= 0 && nbtTag != null && stack == null; i--) {
stack = stackStringifyHandlers.get(i).convertNBTToItemStack(nbtTag);
}
@@ -73,4 +89,119 @@ public class StackInfo
return fluid;
}
public static String getItemStackGUID(ItemStack stack)
{
if (!guidcache.containsKey(stack)) {
final NBTTagCompound nbTag = itemStackToNBT(stack, false);
if (nbTag == null) {
return null;
}
nbTag.removeTag("Count");
if (nbTag.getShort("Damage") == 0) {
nbTag.removeTag("Damage");
}
if (nbTag.hasKey("tag") && nbTag.getCompoundTag("tag").hasNoTags()) {
nbTag.removeTag("tag");
}
if (nbTag.hasKey("strId") && guidfilters.containsKey(nbTag.getString("strId"))) {
final ArrayList<String> keys = new ArrayList<>();
final String strId = nbTag.getString("strId");
keys.add(strId);
guidfilters.get(strId).forEach((key, rule) -> {
Object local = nbTag;
for (int i = 0; i < rule.length; i++) {
try {
if (local instanceof NBTTagCompound) {
local = ((NBTTagCompound) local).getTag(rule[i]);
} else if (local instanceof NBTTagList) {
local = ((NBTTagList) local).tagList.get(Integer.parseInt(rule[i]));
} else {
break;
}
} catch (Throwable e) {
break;
}
}
if (local instanceof NBTBase) {
keys.add(((NBTBase)local).toString());
} else if (local != null) {
keys.add(String.valueOf(local));
}
});
synchronized (guidcache) {
guidcache.put(stack, keys.toString());
}
} else {
synchronized (guidcache) {
guidcache.put(stack, nbTag.toString());
}
}
}
return guidcache.get(stack);
}
public static void loadGuidFilters()
{
guidfilters.clear();
final File guidFlitersFile = new File(NEIClientConfig.configDir, "guidfilters.cfg");
List<String> itemStrings = new ArrayList<>();
if (guidFlitersFile.exists()) {
try (FileReader reader = new FileReader(guidFlitersFile)) {
NEIClientConfig.logger.info("Loading guid filters from file {}", guidFlitersFile);
itemStrings = IOUtils.readLines(reader);
} catch (IOException e) {
NEIClientConfig.logger.error("Failed to load bookmarks from file {}", guidFlitersFile, e);
e.printStackTrace();
}
} else {
final URL filterUrl = Thread.currentThread().getContextClassLoader().getResource("assets/nei/guidfilters.cfg");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(filterUrl.openStream()))) {
itemStrings = IOUtils.readLines(reader);
} catch (IOException e) {
NEIClientConfig.logger.info("Error parsing guid filters");
e.printStackTrace();
} catch (Exception e) {
NEIClientConfig.logger.info("Error parsing guid filters");
e.printStackTrace();
}
}
for (String guidStr: itemStrings) {
final String[] parts = guidStr.split(",");
final HashMap<String, String[]> rules = new HashMap<>();
for (int j = 1; j < parts.length; j++) {
rules.put(parts[j], parts[j].split("\\."));
}
guidfilters.put(parts[0], rules);
}
}
}
+81 -31
View File
@@ -1,11 +1,8 @@
package codechicken.nei.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagByte;
@@ -20,26 +17,21 @@ import net.minecraft.nbt.NBTTagLong;
import net.minecraft.nbt.NBTTagShort;
import net.minecraft.nbt.NBTTagString;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
public class NBTJson {
private static final Pattern numberPattern = Pattern.compile("^([-+]?[\\d]+\\.?[0-9]*)([bBsSlLfFdD]?)$");
private static final JsonParser parser = new JsonParser();
private static final Gson gson = new GsonBuilder().create();
private static final Pattern numberPattern = Pattern.compile("^([-+]?\\d+\\.?\\d*E*\\d*)([bBsSlLfFdD]?)$");
public static String toJson(NBTTagCompound tag) {
return toJson(toJsonObject(tag));
}
public static String toJson(JsonElement json) {
final String jsonString = json.toString();
return gson.toJson(parser.parse(jsonString));
return json.toString();
}
@SuppressWarnings("unchecked")
@@ -50,9 +42,11 @@ public class NBTJson {
final Map<String, NBTBase> tagMap = (Map<String, NBTBase>)nbtTagCompound.tagMap;
JsonObject root = new JsonObject();
for (Map.Entry<String, NBTBase> nbtEntry : tagMap.entrySet() ) {
for (Map.Entry<String, NBTBase> nbtEntry : tagMap.entrySet()) {
root.add(nbtEntry.getKey(), toJsonObject(nbtEntry.getValue()));
}
return root;
} else if (nbt instanceof NBTTagByte) {
// Number (byte)
@@ -80,23 +74,48 @@ public class NBTJson {
return new JsonPrimitive(((NBTTagString) nbt).func_150285_a_());
} else if (nbt instanceof NBTTagList) {
// Tag List
JsonArray arr = new JsonArray();
((NBTTagList) nbt).tagList.forEach(c -> arr.add(toJsonObject((NBTBase) c)));
return arr;
final NBTTagList list = (NBTTagList) nbt;
if (list.tagList.isEmpty()) {
return createEmptyList(list);
} else {
JsonArray arr = new JsonArray();
list.tagList.forEach(c -> arr.add(toJsonObject((NBTBase) c)));
return arr;
}
} else if (nbt instanceof NBTTagIntArray) {
// Int Array
JsonArray arr = new JsonArray();
for(int i : ((NBTTagIntArray) nbt).func_150302_c()) {
arr.add(new JsonPrimitive(i));
final NBTTagIntArray list = (NBTTagIntArray) nbt;
if (list.func_150302_c().length == 0) {
return createEmptyList(list);
} else {
JsonArray arr = new JsonArray();
for (int i : list.func_150302_c()) {
arr.add(new JsonPrimitive(i));
}
return arr;
}
return arr;
} else if (nbt instanceof NBTTagByteArray) {
// Byte Array
JsonArray arr = new JsonArray();
for(int i : ((NBTTagByteArray) nbt).func_150292_c()) {
arr.add(new JsonPrimitive(i));
final NBTTagByteArray list = (NBTTagByteArray) nbt;
if (list.func_150292_c().length == 0) {
return createEmptyList(list);
} else {
JsonArray arr = new JsonArray();
for (byte i : list.func_150292_c()) {
arr.add(new JsonPrimitive(Byte.toString(i) + 'B'));
}
return arr;
}
return arr;
} else {
throw new IllegalArgumentException("Unsupported NBT Tag: " + NBTBase.NBTTypes[nbt.getId()] + " - " + nbt);
}
@@ -108,7 +127,7 @@ public class NBTJson {
final JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonElement;
final String jsonString = jsonPrimitive.getAsString();
final Matcher m = numberPattern.matcher(jsonString);
if(m.find()) {
if (m.find()) {
// Number
final String numberString = m.group(1);
if (m.groupCount() == 2 && m.group(2).length() > 0) {
@@ -152,7 +171,13 @@ public class NBTJson {
if (nbtList.stream().allMatch(n -> n instanceof NBTTagInt)) {
return new NBTTagIntArray(nbtList.stream().mapToInt(i -> ((NBTTagInt)i).func_150287_d() ).toArray());
} else if (nbtList.stream().allMatch(n -> n instanceof NBTTagByte)) {
return new NBTTagByteArray(toByteArray(nbtList.stream().mapToInt(i -> ((NBTTagInt)i).func_150287_d() )));
final byte[] abyte = new byte[nbtList.size()];
for (int i = 0; i < nbtList.size(); i++) {
abyte[i] = ((NBTTagByte) nbtList.get(i)).func_150290_f();
}
return new NBTTagByteArray(abyte);
} else {
NBTTagList nbtTagList = new NBTTagList();
nbtList.forEach(nbtTagList::appendTag);
@@ -163,18 +188,43 @@ public class NBTJson {
} else if (jsonElement instanceof JsonObject) {
// NBTTagCompound
final JsonObject jsonObject = (JsonObject) jsonElement;
NBTTagCompound nbtTagCompound = new NBTTagCompound();
for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
nbtTagCompound.setTag(jsonEntry.getKey(), toNbt(jsonEntry.getValue()));
final NBTBase custom = restoreEmptyList(jsonObject);
if (custom == null) {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
nbtTagCompound.setTag(jsonEntry.getKey(), toNbt(jsonEntry.getValue()));
}
return nbtTagCompound;
} else {
return custom;
}
return nbtTagCompound;
}
throw new IllegalArgumentException("Unhandled element " + jsonElement);
}
public static byte[] toByteArray(IntStream stream) {
return stream.collect(ByteArrayOutputStream::new, (baos, i) -> baos.write((byte) i), (baos1, baos2) -> baos1.write(baos2.toByteArray(), 0, baos2.size()))
.toByteArray();
protected static JsonObject createEmptyList(Object obj)
{
JsonObject empty = new JsonObject();
empty.add("__custom_type", new JsonPrimitive(obj.getClass().getName()));
return empty;
}
protected static NBTBase restoreEmptyList(JsonObject obj)
{
if (obj.has("__custom_type")) {
try {
final String className = obj.get("__custom_type").getAsString();
return (NBTBase)Class.forName(className).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException th) {}
}
return null;
}
}