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

@@ -1,20 +1,13 @@
package systems.brn.regexinghoppers;
import net.fabricmc.api.ModInitializer;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import systems.brn.regexinghoppers.mixin.HopperBlockEntityAccessor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import systems.brn.regexinghoppers.commands.RegexHoppersCommands;
public class RegexingHoppers implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("RegexingHoppers");
@Override
@@ -23,54 +16,8 @@ public class RegexingHoppers implements ModInitializer {
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
LOGGER.debug("RegexingHoppers initialized!");
}
public static boolean shouldNotMove(Inventory hopper, String itemName) {
// Log entering the method with given parameters
LOGGER.debug("Entering shouldNotMove with itemName: {}", itemName);
if (hopper instanceof NamedScreenHandlerFactory factory) {
String customName = factory.getDisplayName().getLiteralString();
// Log the custom name used for matching
LOGGER.debug("Custom regex pattern from hopper: {}", customName);
if (customName != null && !customName.isEmpty()) {
if (hopper instanceof HopperBlockEntity) {
HopperBlockEntityAccessor hopperAccessor = (HopperBlockEntityAccessor) hopper;
if (hopperAccessor.getTransferCooldown() > 1) {
return true;
}
}
try {
Pattern pattern = Pattern.compile(customName);
Matcher matcher = pattern.matcher(itemName);
// Log the result of the regex matching
boolean matches = matcher.matches();
LOGGER.debug("Regex matching result: {}", matches);
if (!matches) {
if (hopper instanceof HopperBlockEntity) {
HopperBlockEntityAccessor hopperAccessor = (HopperBlockEntityAccessor) hopper;
hopperAccessor.setTransferCooldown(8);
}
}
return !matches;
} catch (PatternSyntaxException e) {
// Log exception if regex pattern is invalid
LOGGER.debug("Invalid regex pattern: {}", customName, e);
}
} else {
// Log case when custom name is null or empty
LOGGER.debug("Custom name is null or empty, not performing regex matching.");
}
} else {
// Log if hopper is not an instance of NamedScreenHandlerFactory
LOGGER.debug("Hopper is not an instance of NamedScreenHandlerFactory.");
}
// Default return value in case no conditions are met
return false;
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
RegexHoppersCommands.register(dispatcher);
});
}
}