Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ore Dictionary Filter Update #2296

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/main/java/gregtech/GregTechMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.modules.ModuleContainerRegistryEvent;
import gregtech.api.util.oreglob.OreGlob;
import gregtech.client.utils.BloomEffectUtil;
import gregtech.common.covers.filter.oreglob.impl.OreGlobParser;
import gregtech.modules.GregTechModules;
import gregtech.modules.ModuleManager;

Expand All @@ -15,7 +13,17 @@
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.*;
import net.minecraftforge.fml.common.event.FMLConstructionEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLInterModComms;
import net.minecraftforge.fml.common.event.FMLLoadCompleteEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerAboutToStartEvent;
import net.minecraftforge.fml.common.event.FMLServerStartedEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.event.FMLServerStoppedEvent;
import net.minecraftforge.fml.common.event.FMLServerStoppingEvent;

@Mod(modid = GTValues.MODID,
name = "GregTech",
Expand Down Expand Up @@ -44,7 +52,6 @@ public GregTechMod() {
public void onConstruction(FMLConstructionEvent event) {
moduleManager = ModuleManager.getInstance();
GregTechAPI.moduleManager = moduleManager;
OreGlob.setCompiler(input -> new OreGlobParser(input).compile());
moduleManager.registerContainer(new GregTechModules());
MinecraftForge.EVENT_BUS.post(new ModuleContainerRegistryEvent());
moduleManager.setup(event.getASMHarvestedData(), Loader.instance().getConfigDir());
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/gregtech/api/gui/GuiTextures.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ public class GuiTextures {
.fullImage("textures/items/metaitems/cover.controller.png");

// Ore Filter
public static final TextureArea ORE_FILTER_BUTTON_CASE_SENSITIVE = TextureArea
.fullImage("textures/gui/widget/ore_filter/button_case_sensitive.png");
public static final TextureArea ORE_FILTER_BUTTON_MATCH_ALL = TextureArea
.fullImage("textures/gui/widget/ore_filter/button_match_all.png");
public static final TextureArea ORE_FILTER_INFO = TextureArea.fullImage("textures/gui/widget/ore_filter/info.png");
public static final TextureArea ORE_FILTER_SUCCESS = TextureArea
.fullImage("textures/gui/widget/ore_filter/success.png");
Expand Down
127 changes: 104 additions & 23 deletions src/main/java/gregtech/api/util/oreglob/OreGlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,51 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

/**
* Glob-like string matcher language designed for ore dictionary matching.
* <p>
* An OreGlob instance provides two functions: the ability to match strings,
* and the ability to translate expression structure into user-friendly text
* explanations. The text can be either a plaintext, or a text formatted by standard
* An OreGlob instance provides two functions: the ability to match strings, and the ability to translate expression
* structure into user-friendly text explanations. The text can be either a plaintext, or a text formatted by standard
* Minecraft text format.
*/
public abstract class OreGlob {

private static Function<String, OreGlobCompileResult> compiler;
private static OreGlobCompiler compiler;

/**
* Tries to compile the string expression into OreGlob instance.
*
* @param expression OreGlob expression
* @return Compilation result
* @throws IllegalStateException If compiler is not provided yet
* @deprecated use {@link #compile(String, boolean)}
*/
@NotNull
@Deprecated
serenibyss marked this conversation as resolved.
Show resolved Hide resolved
@ApiStatus.ScheduledForRemoval(inVersion = "2.9")
public static OreGlobCompileResult compile(@NotNull String expression) {
return compile(expression, true);
}

/**
* Tries to compile the string expression into OreGlob instance.
*
* @param expression OreGlob expression
* @param ignoreCase Whether the resulting OreGlob instance should do case-insensitive matches
* @return Compilation result
* @throws IllegalStateException If compiler is not provided yet
*/
@NotNull
public static OreGlobCompileResult compile(@NotNull String expression, boolean ignoreCase) {
if (compiler == null) throw new IllegalStateException("Compiler unavailable");
return compiler.apply(expression);
return compiler.compile(expression, ignoreCase);
}

@ApiStatus.Internal
public static void setCompiler(@NotNull Function<String, OreGlobCompileResult> compiler) {
public static void setCompiler(@NotNull OreGlobCompiler compiler) {
OreGlob.compiler = compiler;
}

Expand All @@ -60,30 +74,97 @@ public static void setCompiler(@NotNull Function<String, OreGlobCompileResult> c
public abstract boolean matches(@NotNull String input);

/**
* Tries to match each ore dictionary entries associated with given item.
* If any of them matches, {@code true} is returned.
* <p>
* For items not associated with any ore dictionary entries, this method returns
* {@code true} if this instance matches empty string instead.
* Tries to match each ore dictionary entries associated with given item. If any of them matches, {@code true} is
* returned.
* </p>
* <p>
* For items not associated with any ore dictionary entries, this method returns {@code true} if this instance
* matches empty string instead.
* </p>
*
* @param stack Item input
* @return Whether this instance matches the input
* @deprecated use {@link #matchesAll(ItemStack)} or {@link #matchesAny(ItemStack)}
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.9")
public final boolean matches(@NotNull ItemStack stack) {
Set<String> oreDicts = OreDictUnifier.getOreDictionaryNames(stack);
if (oreDicts.isEmpty()) {
return matches("");
} else {
for (String oreDict : oreDicts) {
if (matches(oreDict)) return true;
}
return false;
}
return matchesAny(stack);
}

/**
* <p>
* Tries to match each ore dictionary entries associated with given item. If any of them matches, {@code true} is
* returned.
* </p>
* <p>
* For items not associated with any ore dictionary entries, this method returns {@code true} if this instance
* matches empty string instead.
* </p>
*
* @param stack Item input
* @return Whether this instance matches the input
*/
public final boolean matchesAny(@NotNull ItemStack stack) {
return matchesAny(OreDictUnifier.getOreDictionaryNames(stack), true);
}

/**
* <p>
* Tries to match each ore dictionary entries associated with given item. If all of them matches, {@code true} is
* returned.
* </p>
* <p>
* For items not associated with any ore dictionary entries, this method returns {@code true} if this instance
* matches empty string instead.
* </p>
*
* @param stack Item input
* @return Whether this instance matches the input
*/
public final boolean matchesAll(@NotNull ItemStack stack) {
return matchesAll(OreDictUnifier.getOreDictionaryNames(stack), true);
}

/**
* <p>
* Tries to match each input. If any of them matches, {@code true} is returned.
* </p>
*
* @param inputs Collection of input strings
* @param specialEmptyMatch If {@code true}, this method will match an empty string ({@code ""}) if the input
* collection is empty. If {@code true}, this method will return {@code false} in such
* scenario.
* @return Whether this instance matches the input
*/
public final boolean matchesAny(@NotNull Collection<String> inputs, boolean specialEmptyMatch) {
if (specialEmptyMatch && inputs.isEmpty()) return matches("");
for (String input : inputs) if (matches(input)) return true;
return false;
}

/**
* <p>
* Tries to match each input. If all of them matches, {@code true} is returned. Note that this method does not have
* special case for empty inputs.
* </p>
*
* @param inputs Collection of input strings
* @param specialEmptyMatch If {@code true}, this method will match an empty string ({@code ""}) if the input
* collection is empty. If {@code true}, this method will return {@code true} in such
* scenario.
* @return Whether this instance matches the input
*/
public final boolean matchesAll(@NotNull Collection<String> inputs, boolean specialEmptyMatch) {
if (specialEmptyMatch && inputs.isEmpty()) return matches("");
for (String input : inputs) if (!matches(input)) return false;
return true;
}

/**
* Visualize this instance with standard Minecraft text formatting. Two spaces (' ') will
* be used as indentation.
* Visualize this instance with standard Minecraft text formatting. Two spaces ({@code ' '}) will be used as
* indentation.
*
* @return Formatted visualization
* @see OreGlob#toFormattedString(String)
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gregtech/api/util/oreglob/OreGlobCompiler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gregtech.api.util.oreglob;

import org.jetbrains.annotations.NotNull;

@FunctionalInterface
public interface OreGlobCompiler {

@NotNull
OreGlobCompileResult compile(@NotNull String expression, boolean ignoreCase);
}
Loading