Skip to content

Commit

Permalink
add more config and config gui
Browse files Browse the repository at this point in the history
  • Loading branch information
vfyjxf committed Apr 23, 2022
1 parent 6e2f4f9 commit 3bd0fbc
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 59 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.daemon=false

# General Specifications
mc_version=1.12.2
mod_version=0.2.2
mod_version=0.2.3
forge_version=14.23.5.2847
jei_version=4.16.1.302
mod_group=com.github.vfyjxf.jeiutilities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
@Mod(modid = JEIUtilities.MODID,
name = JEIUtilities.NAME,
version = JEIUtilities.VERSION,
dependencies = JEIUtilities.DEPENDENCIES,
guiFactory = JEIUtilities.GUI_FACTORY,
clientSideOnly = true
)
public class JEIUtilities {
public static final String MODID = "jeiutilities";
public static final String NAME = "JEI Utilities";
public static final String VERSION = "@VERSION@";
public static final String DEPENDENCIES = "required-after:jei";
public static final String GUI_FACTORY = "com.github.vfyjxf.jeiutilities.config.JeiUtilitiesConfigGuiFactory";

public static final Logger logger = LogManager.getLogger(JEIUtilities.NAME);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

public class JeiUtilitiesConfig {

private static final String CATEGORY_GENERAL = "general";
private static final String CATEGORY_HISTORY = "history";
private static final String CATEGORY_BOOKMARK = "bookmark";

private static Configuration config;
private static File modConfigFile;
private static File bookmarkRecipeInfoFile;

private static boolean enableHistory = true;
private static boolean matchesNBTs = true;
private static int backgroundColour = 0xee555555;

private static boolean recordRecipes = true;

private static RecordMode recordMode = RecordMode.ENABLE;

private static int backgroundColour = 0xee555555;


public static void preInit(FMLPreInitializationEvent event) {
File configDir = new File(event.getModConfigurationDirectory(), JEIUtilities.MODID);
Expand All @@ -42,15 +42,21 @@ private static void loadConfig() {

config.load();

enableHistory = config.getBoolean("enableHistory", CATEGORY_GENERAL, enableHistory, "Enable browsing history function");
recordRecipes = config.getBoolean("recordRecipes", CATEGORY_GENERAL, recordRecipes, "Record current recipe when add ingredient to bookmark in recipe screen");
recordMode = RecordMode.valueOf(config.getString(
"recordMode", CATEGORY_GENERAL, recordMode.name(),
"Current mode of recording recipes." + "\n"
+ "Enable: The opposite of RESTRICTED mode" + "\n"
+ "Disable: Don't record any recipes" + "\n"
+ "RESTRICTED: Marking a bookmark while holding down the shift key will record the recipe, and viewing the recipe while holding down the shift key will display the marked recipe"));
backgroundColour = config.getInt("backgroundColour", CATEGORY_GENERAL, backgroundColour, Integer.MIN_VALUE, Integer.MAX_VALUE, "Color of the history area display");
{
enableHistory = config.getBoolean("enableHistory", CATEGORY_HISTORY, enableHistory, "Enable browsing history function");
matchesNBTs = config.getBoolean("matchesNBTs", CATEGORY_HISTORY, matchesNBTs, "Add item with different nbt to the browsing history");
backgroundColour = config.getInt("backgroundColour", CATEGORY_HISTORY, backgroundColour, Integer.MIN_VALUE, Integer.MAX_VALUE, "Color of the history area display");
}
{
recordRecipes = config.getBoolean("recordRecipes", CATEGORY_BOOKMARK, recordRecipes, "Record current recipe when add ingredient to bookmark in recipe screen");
recordMode = RecordMode.valueOf(config.getString(
"recordMode", CATEGORY_BOOKMARK, recordMode.name(),
"Current mode of recording recipes." + "\n"
+ "Enable: The opposite of RESTRICTED mode" + "\n"
+ "Disable: Don't record any recipes" + "\n"
+ "RESTRICTED: You need to hold down Shift to view the marked recipe."));
}


if (config.hasChanged()) {
config.save();
Expand All @@ -73,6 +79,10 @@ public static boolean getEnableHistory() {
return enableHistory;
}

public static boolean isMatchesNBTs() {
return matchesNBTs;
}

public static boolean getRecordRecipes() {
return recordRecipes;
}
Expand All @@ -87,7 +97,7 @@ public static int getBackgroundColour() {

public static void setRecordMode(RecordMode mode) {
JeiUtilitiesConfig.recordMode = mode;
config.get(CATEGORY_GENERAL, "recordMode", recordMode.name(), "Current mode of recording recipes").set(mode.name());
config.get(CATEGORY_BOOKMARK, "recordMode", recordMode.name(), "Current mode of recording recipes").set(mode.name());
config.save();
}

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

import com.github.vfyjxf.jeiutilities.JEIUtilities;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.fml.client.IModGuiFactory;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.minecraftforge.fml.client.config.IConfigElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class JeiUtilitiesConfigGuiFactory implements IModGuiFactory {
@Override
public void initialize(Minecraft minecraftInstance) {

}

@Override
public boolean hasConfigGui() {
return true;
}

@Override
public GuiScreen createConfigGui(GuiScreen parentScreen) {
return new GuiConfig(parentScreen,
getConfigElements(),
JEIUtilities.MODID,
false,
false,
JEIUtilities.NAME
);
}

@Override
public Set<RuntimeOptionCategoryElement> runtimeGuiCategories() {
return null;
}

private static List<IConfigElement> getConfigElements() {
List<IConfigElement> list = new ArrayList<>();
for (String name : JeiUtilitiesConfig.getConfig().getCategoryNames()) {
list.add(new ConfigElement(JeiUtilitiesConfig.getConfig().getCategory(name)));
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.Locale;

/**
* DISABLE : The ability to completely disable recipe logging
* ENABLE : The opposite of RESTRICTED mode
* RESTRICTED : Marking a bookmark while holding down the shift key will record the recipe, and viewing the recipe while holding down the shift key will display the marked recipe
* DISABLE : The ability to completely disable recipe logging.
* ENABLE : The opposite of RESTRICTED mode.
* RESTRICTED : You need to hold down Shift to view the marked recipe.
*/
public enum RecordMode {
DISABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.vfyjxf.jeiutilities.jei.bookmark.RecipeBookmarkList;
import com.github.vfyjxf.jeiutilities.jei.bookmark.RecipeBookmarkList.RecipeInfo;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.bookmarks.BookmarkList;
import mezz.jei.config.KeyBindings;
import mezz.jei.gui.Focus;
Expand All @@ -27,6 +28,7 @@
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import javax.annotation.Nonnull;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -60,9 +62,8 @@ public static BookmarkInputHandler getInstance() {
public void onBookmarkRemove(GuiScreenEvent.KeyboardInputEvent event) {
if (ingredientUnderMouse == null) {
int eventKey = Keyboard.getEventKey();
boolean withShift = JeiUtilitiesConfig.getRecordMode() == RecordMode.RESTRICTED;

if (isBookmark(eventKey, withShift)) {
if (KeyBindings.bookmark.isActiveAndMatches(eventKey)) {
IClickedIngredient<?> clicked = getIngredientUnderMouseForKey();
if (clicked != null) {
ingredientUnderMouse = clicked.getValue();
Expand All @@ -73,31 +74,28 @@ public void onBookmarkRemove(GuiScreenEvent.KeyboardInputEvent event) {

@SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true)
public void onBookmarkListAddOrRemove(GuiScreenEvent.KeyboardInputEvent event) {
boolean withShift = JeiUtilitiesConfig.getRecordMode() == RecordMode.RESTRICTED;
if (event.isCanceled() || withShift) {
if (event.isCanceled()) {

if (JeiUtilitiesConfig.getRecordMode() == RecordMode.DISABLE) {
return;
}

final int eventKey = Keyboard.getEventKey();

if (isBookmark(eventKey, withShift)) {
if (KeyBindings.bookmark.isActiveAndMatches(eventKey)) {

Object ingredient = getOutputUnderMouse();
//If the bookmark contains this ingredient, the current recipe is recorded
if (ingredient != null) {
if (withShift) {
bookmarkList.add(ingredient);
}
if (isBookmarkContains(ingredient)) {
IngredientLookupState state = getState();
if (state != null && state.getFocus() != null) {
boolean isInputMode = state.getFocus().getMode() == IFocus.Mode.INPUT;
String recipeCategoryUid = state.getRecipeCategories().get(state.getRecipeCategoryIndex()).getUid();
RecipeInfo<?, ?> recipeInfo = new RecipeInfo<>(
RecipeBookmarkList.normalize(state.getFocus().getValue()),
RecipeBookmarkList.normalize(ingredient),
state.getRecipeCategoryIndex(),
recipeCategoryUid,
state.getRecipeIndex(),
isInputMode
);
Expand Down Expand Up @@ -139,7 +137,7 @@ public void onMouseClicked(GuiScreenEvent.MouseInputEvent event) {
JeiUtilitiesPlugin.getGrid().addHistoryIngredient(recipeInfo.getResult());
IngredientLookupState state = getState();
if (state != null) {
state.setRecipeCategoryIndex(recipeInfo.getRecipeCategoryIndex());
state.setRecipeCategoryIndex(getRecipeCategoryIndex(state, recipeInfo.getRecipeCategoryUid()));
state.setRecipeIndex(recipeInfo.getRecipeIndex());
updateRecipes();
recipesGui.onStateChange();
Expand Down Expand Up @@ -180,7 +178,7 @@ public void onKeyPressed(GuiScreenEvent.KeyboardInputEvent event) {
JeiUtilitiesPlugin.getGrid().addHistoryIngredient(recipeInfo.getResult());
IngredientLookupState state = getState();
if (state != null) {
state.setRecipeCategoryIndex(recipeInfo.getRecipeCategoryIndex());
state.setRecipeCategoryIndex(getRecipeCategoryIndex(state, recipeInfo.getRecipeCategoryUid()));
state.setRecipeIndex(recipeInfo.getRecipeIndex());
updateRecipes();
recipesGui.onStateChange();
Expand Down Expand Up @@ -245,19 +243,20 @@ private void updateRecipes() {
}
}

private boolean isShowRecipe(int keycode, boolean withShift) {
if (withShift) {
return keycode != 0 && KeyBindings.showRecipe.getKeyCode() == keycode && KeyBindings.showRecipe.getKeyConflictContext().isActive() && GuiContainer.isShiftKeyDown();
} else {
return KeyBindings.showRecipe.isActiveAndMatches(keycode);
private int getRecipeCategoryIndex(@Nonnull IngredientLookupState state, @Nonnull String recipeCategoryUid) {
for (IRecipeCategory<?> recipeCategory : state.getRecipeCategories()) {
if (recipeCategory.getUid().equals(recipeCategoryUid)) {
return state.getRecipeCategories().indexOf(recipeCategory);
}
}
return 0;
}

private boolean isBookmark(int keycode, boolean withShift) {
private boolean isShowRecipe(int keycode, boolean withShift) {
if (withShift) {
return keycode != 0 && KeyBindings.bookmark.getKeyCode() == keycode && KeyBindings.bookmark.getKeyConflictContext().isActive() && GuiContainer.isShiftKeyDown();
return keycode != 0 && KeyBindings.showRecipe.getKeyCode() == keycode && KeyBindings.showRecipe.getKeyConflictContext().isActive() && GuiContainer.isShiftKeyDown();
} else {
return KeyBindings.bookmark.isActiveAndMatches(keycode);
return KeyBindings.showRecipe.isActiveAndMatches(keycode);
}
}

Expand All @@ -273,10 +272,6 @@ public static void onInputHandlerSet() {
updateRecipesMethod = ObfuscationReflectionHelper.findMethod(RecipeGuiLogic.class, "updateRecipes", void.class);
}

public static void setBookmarkOverlay(){

}

public RecipeBookmarkList getRecipeBookmarkList() {
return recipeBookmarkList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public void setRecordMode(RecordMode mode) {
public void getTooltips(@Nonnull List<String> tooltip) {
tooltip.add(Translator.translateToLocal("jeiutilities.tooltip.recording"));
tooltip.add(TextFormatting.GRAY + Translator.translateToLocalFormatted("jeiutilities.tooltip.recording.mode", this.currentMode.getLocalizedName()));
tooltip.add(TextFormatting.GRAY + Translator.translateToLocal("jeiutilities.tooltip.recording.mode.description"));
if (currentMode == RecordMode.RESTRICTED){
tooltip.add(TextFormatting.GRAY + Translator.translateToLocal("jeiutilities.tooltip.recording.description.restricted"));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void addHistoryIngredient(Object value) {
ForgeModIdHelper.getInstance(),
ORDER_TRACKER.getOrderIndex(normalized, ingredientRegistry.getIngredientHelper(normalized))
);
historyIngredientElements.removeIf(element -> areIngredientEqual(element.getIngredient(), normalized));
historyIngredientElements.removeIf(element -> areIngredientEqual(element.getIngredient(), normalized, JeiUtilitiesConfig.isMatchesNBTs()));
historyIngredientElements.add(0, ingredient);
if (historyIngredientElements.size() > this.historySize) {
historyIngredientElements.remove(historyIngredientElements.size() - 1);
Expand Down Expand Up @@ -257,21 +257,25 @@ private <T> T normalize(T ingredient) {
return copy;
}

private boolean areIngredientEqual(@Nonnull Object ingredient1, @Nonnull Object ingredient2) {
private boolean areIngredientEqual(@Nonnull Object ingredient1, @Nonnull Object ingredient2, boolean matchesNbt) {

if (ingredient1 == ingredient2) {
return true;
}

if (ingredient1.getClass() == ingredient2.getClass()) {
IIngredientHelper<Object> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient1);
return ingredientHelper.getUniqueId(ingredient1).equals(ingredientHelper.getUniqueId(ingredient2));
if (matchesNbt) {
return ingredientHelper.getUniqueId(ingredient1).equals(ingredientHelper.getUniqueId(ingredient2));
}
return ingredientHelper.getWildcardId(ingredient1).equals(ingredientHelper.getWildcardId(ingredient2));
}

return false;
}

//TODO:implements it

/**
* An ingredient such as energy ingredient in ender:io and Multiblocked.
* It should not be added to the browsing history because it is meaningless in itself
Expand Down
Loading

0 comments on commit 3bd0fbc

Please sign in to comment.