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:
2024-10-16 00:12:49 +02:00
parent 424567ec3a
commit fd53a7b3cb
5 changed files with 157 additions and 84 deletions

View 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;
}
}