57 lines
2.0 KiB
Java
57 lines
2.0 KiB
Java
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;
|
|
}
|
|
}
|