Add cooldowns

This commit is contained in:
Bruno Rybársky 2024-05-05 11:55:57 +02:00
parent e493c9e326
commit 21a99b5143
3 changed files with 52 additions and 11 deletions

@ -26,17 +26,40 @@ public class RegexingHoppers implements ModInitializer {
}
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) {
NamedScreenHandlerFactory factory = (NamedScreenHandlerFactory) hopper;
String customName = factory.getDisplayName().getLiteralString();
if (customName != null) {
// Log the custom name used for matching
LOGGER.info("Custom regex pattern from hopper: {}", customName);
if (customName != null && !customName.isEmpty()) {
try {
Pattern pattern = Pattern.compile(customName);
// Check if the text matches the regex
Matcher matcher = pattern.matcher(itemName);
return !matcher.matches();
} catch (PatternSyntaxException ignored) {
// Log the result of the regex matching
boolean matches = matcher.matches();
LOGGER.info("Regex matching result: {}", matches);
return !matches;
} catch (PatternSyntaxException e) {
// Log exception if regex pattern is invalid
LOGGER.info("Invalid regex pattern: {}", customName, e);
}
} else {
// Log case when custom name is null or empty
LOGGER.info("Custom name is null or empty, not performing regex matching.");
}
} else {
// Log if hopper is not an instance of NamedScreenHandlerFactory
LOGGER.info("Hopper is not an instance of NamedScreenHandlerFactory.");
}
// Default return value in case no conditions are met
return false;
}
}

@ -0,0 +1,11 @@
package systems.brn.regexinghoppers.mixin;
import net.minecraft.block.entity.HopperBlockEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(HopperBlockEntity.class)
interface HopperBlockEntityInvoker {
@Invoker("setTransferCooldown")
void setTransferCooldown(int transferCooldown);
}

@ -7,15 +7,15 @@ import net.minecraft.registry.Registries;
import net.minecraft.util.math.Direction;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import systems.brn.regexinghoppers.RegexingHoppers;
@Debug(export = true) // Enables exporting for the targets of this mixin
@Mixin(HopperBlockEntity.class)
public class RegexingHopperMixin {
@Inject(method = "transfer(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/inventory/Inventory;Lnet/minecraft/item/ItemStack;ILnet/minecraft/util/math/Direction;)Lnet/minecraft/item/ItemStack;",
at = @At(value = "HEAD"),
cancellable = true
@ -25,14 +25,21 @@ public class RegexingHopperMixin {
if(from != null) {
if (RegexingHoppers.shouldNotMove(from, itemName)) {
cir.setReturnValue(stack);
if(from instanceof HopperBlockEntity) {
((HopperBlockEntityInvoker) from).setTransferCooldown(8);
}
return;
}
}
if(_to != null) {
if (RegexingHoppers.shouldNotMove(_to, itemName)) {
cir.setReturnValue(stack);
if(_to instanceof HopperBlockEntity) {
((HopperBlockEntityInvoker) _to).setTransferCooldown(8);
}
return;
}
}
}
}