feat: add match & list command to test in game regex result + fix: now the hopper will not stop at the first mismatch, it will test all items inside a container
This commit is contained in:
56
src/main/java/systems/brn/regexinghoppers/util/Tools.java
Normal file
56
src/main/java/systems/brn/regexinghoppers/util/Tools.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package systems.brn.regexinghoppers.util;
|
||||
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.minecraft.block.entity.HopperBlockEntity;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.screen.NamedScreenHandlerFactory;
|
||||
import systems.brn.regexinghoppers.mixin.HopperBlockEntityAccessor;
|
||||
|
||||
public class Tools {
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("RegexingHoppers");
|
||||
|
||||
public static boolean itemMatch(String itemName, String regex) {
|
||||
return itemName.matches(regex);
|
||||
}
|
||||
|
||||
public static String getNameOf(Item item) {
|
||||
return item.getName().getString().toLowerCase();
|
||||
}
|
||||
|
||||
public static boolean shouldMove(Inventory hopper, Item item) {
|
||||
if (!(hopper instanceof NamedScreenHandlerFactory)) {
|
||||
// Log if hopper is not an instance of NamedScreenHandlerFactory
|
||||
LOGGER.debug("Hopper is not an instance of NamedScreenHandlerFactory.");
|
||||
return true;
|
||||
}
|
||||
NamedScreenHandlerFactory factory = (NamedScreenHandlerFactory) hopper;
|
||||
String customName = factory.getDisplayName().getLiteralString();
|
||||
if (customName == null || customName.isEmpty()) {
|
||||
// Log case when custom name is null or empty
|
||||
LOGGER.debug("Custom name is null or empty, not performing regex matching.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: Test if this code still needed
|
||||
if (hopper instanceof HopperBlockEntity) {
|
||||
HopperBlockEntityAccessor hopperAccessor = (HopperBlockEntityAccessor) hopper;
|
||||
if (hopperAccessor.getTransferCooldown() > 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return Tools.itemMatch(getNameOf(item), customName);
|
||||
} catch (PatternSyntaxException e) {
|
||||
// Log exception if regex pattern is invalid
|
||||
LOGGER.debug("Invalid regex pattern: {}", customName, e);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user