forked from BRNSystems/RegexingHoppers
Init
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package systems.brn.regexinghoppers;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.screen.NamedScreenHandlerFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
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
|
||||
public void onInitialize() {
|
||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
||||
// However, some things (like resources) may still be uninitialized.
|
||||
// Proceed with mild caution.
|
||||
|
||||
LOGGER.info("RegexingHoppers initialized!");
|
||||
}
|
||||
|
||||
public static boolean shouldNotMove(Inventory hopper, String itemName) {
|
||||
NamedScreenHandlerFactory factory = (NamedScreenHandlerFactory) hopper;
|
||||
String customName = factory.getDisplayName().getLiteralString();
|
||||
if (customName != null) {
|
||||
try {
|
||||
Pattern pattern = Pattern.compile(customName);
|
||||
// Check if the text matches the regex
|
||||
Matcher matcher = pattern.matcher(itemName);
|
||||
return !matcher.matches();
|
||||
} catch (PatternSyntaxException ignored) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package systems.brn.regexinghoppers.mixin;
|
||||
|
||||
import net.minecraft.block.entity.HopperBlockEntity;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
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.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
|
||||
)
|
||||
private static void processContainer(Inventory from, Inventory _to, ItemStack stack, int slot, Direction side, CallbackInfoReturnable<ItemStack> cir) {
|
||||
final String itemName = Registries.ITEM.getId(stack.getItem()).toString();
|
||||
if(from != null) {
|
||||
if (RegexingHoppers.shouldNotMove(from, itemName)) {
|
||||
cir.setReturnValue(stack);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(_to != null) {
|
||||
if (RegexingHoppers.shouldNotMove(_to, itemName)) {
|
||||
cir.setReturnValue(stack);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user