forked from BRNSystems/RegexingHoppers
Add cooldowns
This commit is contained in:
parent
e493c9e326
commit
21a99b5143
@ -26,17 +26,40 @@ public class RegexingHoppers implements ModInitializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean shouldNotMove(Inventory hopper, String itemName) {
|
public static boolean shouldNotMove(Inventory hopper, String itemName) {
|
||||||
NamedScreenHandlerFactory factory = (NamedScreenHandlerFactory) hopper;
|
// Log entering the method with given parameters
|
||||||
String customName = factory.getDisplayName().getLiteralString();
|
LOGGER.debug("Entering shouldNotMove with itemName: {}", itemName);
|
||||||
if (customName != null) {
|
|
||||||
try {
|
if (hopper instanceof NamedScreenHandlerFactory) {
|
||||||
Pattern pattern = Pattern.compile(customName);
|
NamedScreenHandlerFactory factory = (NamedScreenHandlerFactory) hopper;
|
||||||
// Check if the text matches the regex
|
String customName = factory.getDisplayName().getLiteralString();
|
||||||
Matcher matcher = pattern.matcher(itemName);
|
|
||||||
return !matcher.matches();
|
// Log the custom name used for matching
|
||||||
} catch (PatternSyntaxException ignored) {
|
LOGGER.info("Custom regex pattern from hopper: {}", customName);
|
||||||
|
|
||||||
|
if (customName != null && !customName.isEmpty()) {
|
||||||
|
try {
|
||||||
|
Pattern pattern = Pattern.compile(customName);
|
||||||
|
Matcher matcher = pattern.matcher(itemName);
|
||||||
|
|
||||||
|
// 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;
|
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 net.minecraft.util.math.Direction;
|
||||||
import org.spongepowered.asm.mixin.Debug;
|
import org.spongepowered.asm.mixin.Debug;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
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.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
import systems.brn.regexinghoppers.RegexingHoppers;
|
import systems.brn.regexinghoppers.RegexingHoppers;
|
||||||
|
|
||||||
@Debug(export = true) // Enables exporting for the targets of this mixin
|
|
||||||
@Mixin(HopperBlockEntity.class)
|
@Mixin(HopperBlockEntity.class)
|
||||||
public class RegexingHopperMixin {
|
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;",
|
@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"),
|
at = @At(value = "HEAD"),
|
||||||
cancellable = true
|
cancellable = true
|
||||||
@ -25,14 +25,21 @@ public class RegexingHopperMixin {
|
|||||||
if(from != null) {
|
if(from != null) {
|
||||||
if (RegexingHoppers.shouldNotMove(from, itemName)) {
|
if (RegexingHoppers.shouldNotMove(from, itemName)) {
|
||||||
cir.setReturnValue(stack);
|
cir.setReturnValue(stack);
|
||||||
|
if(from instanceof HopperBlockEntity) {
|
||||||
|
((HopperBlockEntityInvoker) from).setTransferCooldown(8);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(_to != null) {
|
if(_to != null) {
|
||||||
if (RegexingHoppers.shouldNotMove(_to, itemName)) {
|
if (RegexingHoppers.shouldNotMove(_to, itemName)) {
|
||||||
cir.setReturnValue(stack);
|
cir.setReturnValue(stack);
|
||||||
|
if(_to instanceof HopperBlockEntity) {
|
||||||
|
((HopperBlockEntityInvoker) _to).setTransferCooldown(8);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user