feat: add readme and add 3 commands for pen test regex

This commit is contained in:
Nougato 2024-10-20 20:33:24 +02:00
parent fd53a7b3cb
commit 45de93c38c
5 changed files with 194 additions and 3 deletions

71
README.md Normal file

@ -0,0 +1,71 @@
# RegexingHoppers
RegexingHoppers is a Minecraft mod that enhances the functionality of hoppers by allowing the use of regular expressions (regex) to filter items passing through them. This mod adds a new layer of customization and control to item sorting and transportation systems in Minecraft.
## Features
- Use custom names for hoppers as regex patterns to filter items
- Three new commands for testing and managing regex patterns
- Configurable permission levels for commands
## Installation
1. Make sure you have Fabric Loader installed
2. Download the latest version of RegexingHoppers from the releases page
3. Place the downloaded .jar file in your Minecraft mods folder
4. Launch Minecraft with the Fabric profile
## Usage
### Regex Filtering
To use regex filtering on a hopper:
1. Rename the hopper using an anvil with your desired regex pattern
2. Place the renamed hopper in your item system
3. Items matching the regex pattern will be allowed through, while others will be filtered out
### Commands
RegexingHoppers adds the following commands:
1. `/regexhoppers test <regex>`: Test a regex pattern against all item names
2. `/regexhoppers list <regex>`: List all items matching the given regex pattern
3. `/regexhoppers hand`: Display the name of the item in your main hand
By default, all players can use these commands. You can adjust the permission levels in the configuration file.
## Configuration
The mod includes a configuration file (`regexhoppers.json`) located in the `config` folder. You can adjust the permission levels required for each command:
```json
{
"permissions": [
{
"name": "regexhoppers.command.test",
"level": 0
},
{
"name": "regexhoppers.command.list",
"level": 0
},
{
"name": "regexhoppers.command.hand",
"level": 0
}
]
}
```
Adjust the `level` value for each command to set the required permission level. A level of 0 allows all players to use the command.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## Support
If you encounter any issues or have any questions, please open an issue on our GitHub repository.
Happy filtering!

@ -6,6 +6,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import systems.brn.regexinghoppers.commands.RegexHoppersCommands; import systems.brn.regexinghoppers.commands.RegexHoppersCommands;
import systems.brn.regexinghoppers.config.ModConfig;
public class RegexingHoppers implements ModInitializer { public class RegexingHoppers implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("RegexingHoppers"); public static final Logger LOGGER = LoggerFactory.getLogger("RegexingHoppers");
@ -15,6 +16,7 @@ public class RegexingHoppers implements ModInitializer {
// This code runs as soon as Minecraft is in a mod-load-ready state. // This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized. // However, some things (like resources) may still be uninitialized.
// Proceed with mild caution. // Proceed with mild caution.
ModConfig.getInstance();
LOGGER.debug("RegexingHoppers initialized!"); LOGGER.debug("RegexingHoppers initialized!");
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
RegexHoppersCommands.register(dispatcher); RegexHoppersCommands.register(dispatcher);

@ -3,10 +3,13 @@ package systems.brn.regexinghoppers.commands;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import systems.brn.regexinghoppers.config.ModConfig;
import systems.brn.regexinghoppers.util.Tools; import systems.brn.regexinghoppers.util.Tools;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries; import net.minecraft.registry.Registries;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
@ -22,12 +25,17 @@ public class RegexHoppersCommands {
// TODO: Add config file for permissions system // TODO: Add config file for permissions system
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(literal("regexhoppers") dispatcher.register(literal("regexhoppers")
.then(literal("match") .then(literal("test")
.requires(source -> source.hasPermissionLevel(ModConfig.getInstance().getTestPermission().level))
.then(argument("regex", StringArgumentType.greedyString()) .then(argument("regex", StringArgumentType.greedyString())
.executes(context -> executeMatch(context.getSource(), StringArgumentType.getString(context, "regex"))))) .executes(context -> executeMatch(context.getSource(), StringArgumentType.getString(context, "regex")))))
.then(literal("list") .then(literal("list")
.requires(source -> source.hasPermissionLevel(ModConfig.getInstance().getListPermission().level))
.then(argument("regex", StringArgumentType.greedyString()) .then(argument("regex", StringArgumentType.greedyString())
.executes(context -> executeList(context.getSource(), StringArgumentType.getString(context, "regex")))))); .executes(context -> executeList(context.getSource(), StringArgumentType.getString(context, "regex")))))
.then(literal("hand")
.requires(source -> source.hasPermissionLevel(ModConfig.getInstance().getHandPermission().level))
.executes(context -> executeHand(context.getSource()))));
} }
private static int executeMatch(ServerCommandSource source, final String regexString) { private static int executeMatch(ServerCommandSource source, final String regexString) {
@ -68,10 +76,23 @@ public class RegexHoppersCommands {
final int total = count; final int total = count;
final String totalAllMatchs = allMatchs; final String totalAllMatchs = allMatchs;
source.sendFeedback(() -> Text.literal("Full match list: \n" + totalAllMatchs + regexString + " match with " + total + " items: \n"), false); source.sendFeedback(() -> Text.literal("Full match list: \n" + totalAllMatchs + regexString + " match with " + total + " items\n"), false);
return 1; return 1;
} catch (PatternSyntaxException e) { } catch (PatternSyntaxException e) {
return 0; return 0;
} }
} }
private static int executeHand(ServerCommandSource source) {
ServerPlayerEntity player = source.getPlayer();
if (player != null) {
ItemStack heldItem = player.getMainHandStack();
if(heldItem != null) {
source.sendFeedback(() -> Text.literal("Your main hand contain: '" + Tools.getNameOf(heldItem.getItem()) + "'" ), false);
return 1;
}
source.sendFeedback(() -> Text.literal("No item found"), false);
}
return 0;
}
} }

@ -0,0 +1,86 @@
package systems.brn.regexinghoppers.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;
import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
public class ModConfig {
private static final String CONFIG_FILE = "regexhoppers.json";
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static String[] permissionsName = {
"regexhoppers.command.test",
"regexhoppers.command.list",
"regexhoppers.command.hand"
};
public ArrayList<PermissionEntry> permissions;
private static ModConfig INSTANCE;
public static ModConfig getInstance() {
if (INSTANCE == null) {
INSTANCE = load();
}
return INSTANCE;
}
private static ModConfig load() {
Path configPath = FabricLoader.getInstance().getConfigDir().resolve(CONFIG_FILE);
if (configPath.toFile().exists()) {
try (Reader reader = new FileReader(configPath.toFile())) {
ModConfig mconfig = GSON.fromJson(reader, ModConfig.class);
mconfig.checkConfigFile();
return mconfig;
} catch (IOException e) {
e.printStackTrace();
}
}
ModConfig config = new ModConfig();
config.save();
return config;
}
public void save() {
// Should only run on the first time to generate the config file, or if file isn't loadable / broken
this.permissions = new ArrayList<>();
for (String permission : permissionsName) {
this.permissions.add(new PermissionEntry(permission, 0));
}
Path configPath = FabricLoader.getInstance().getConfigDir().resolve(CONFIG_FILE);
try (Writer writer = new FileWriter(configPath.toFile())) {
GSON.toJson(this, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
private void checkConfigFile() {
if (this.permissions.size() < permissionsName.length) {
this.save();
}
}
private PermissionEntry getIndex(int index) {
this.checkConfigFile();
return this.permissions.get(index);
}
public PermissionEntry getTestPermission() {
return this.getIndex(0);
}
public PermissionEntry getListPermission() {
return this.permissions.get(1);
}
public PermissionEntry getHandPermission() {
return this.permissions.get(2);
}
}

@ -0,0 +1,11 @@
package systems.brn.regexinghoppers.config;
public class PermissionEntry {
public String name;
public int level;
public PermissionEntry(String name, int level) {
this.name = name;
this.level = level;
}
}