package net.mattias.cobblecensors; import net.neoforged.fml.common.Mod; import…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
package net.mattias.cobblecensors;
import net.neoforged.fml.common.Mod; import net.neoforged.fml.loading.FMLPaths; import org.slf4j.Logger;
import com.mojang.logging.LogUtils;
import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModContainer; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.config.ModConfig; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.server.ServerStartingEvent;
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path;
// The value here should match an entry in the META-INF/neoforge.mods.toml file @Mod(CobbleCensors.MOD_ID) public class CobbleCensors { // Define mod id in a common place for everything to reference public static final String MOD_ID = "cobblecensors"; // Directly reference a slf4j logger private static final Logger LOGGER = LogUtils.getLogger();
public CobbleCensors(IEventBus modEventBus, ModContainer modContainer) {
Path configDir = FMLPaths.CONFIGDIR.get();
Path modConfigDir = configDir.resolve("cobblecensors");
try {
if (!Files.exists(modConfigDir)) {
Files.createDirectories(modConfigDir);
}
Path bannedWordsFile = modConfigDir.resolve("banned_words.txt");
if (!Files.exists(bannedWordsFile)) {
Files.createFile(bannedWordsFile);
}
BannedWords.loadBannedWords(bannedWordsFile);
} catch (IOException e) {
LOGGER.error("Failed to load banned words", e);
}
new NameCensorHandler();
modEventBus.addListener(this::commonSetup);
NeoForge.EVENT_BUS.register(this);
modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
}
private void commonSetup(final FMLCommonSetupEvent event){
}
@SubscribeEvent
public void onServerStarting(ServerStartingEvent event) {
}
@EventBusSubscriber(modid = MOD_ID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public static class ClientModEvents {
@SubscribeEvent
public static void onClientSetup(FMLClientSetupEvent event) {
}
}
}
package net.mattias.cobblecensors;
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors;
public class BannedWords { private static List bannedWords = new ArrayList<>();
public static void loadBannedWords(Path filePath) {
try {
bannedWords = Files.lines(filePath)
.map(String::trim)
.filter(line -> !line.isEmpty())
.collect(Collectors.toList());
} catch (IOException e) {
bannedWords = new ArrayList<>();
}
}
public static boolean containsBannedWord(String text) {
if (text == null) return false;
String lowerText = text.toLowerCase();
return bannedWords.stream().anyMatch(word -> lowerText.contains(word.toLowerCase()));
}
}
package net.mattias.cobblecensors;
import com.cobblemon.mod.common.api.Priority; import com.cobblemon.mod.common.api.events.CobblemonEvents; import com.cobblemon.mod.common.api.events.pokemon.PokemonNicknamedEvent; import kotlin.Unit; import kotlin.jvm.functions.Function1; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import org.slf4j.Logger; import com.mojang.logging.LogUtils;
public class NameCensorHandler { public static final String MOD_ID = "cobblecensors"; private static final Logger LOGGER = LogUtils.getLogger();
public NameCensorHandler() {
CobblemonEvents.POKEMON_NICKNAMED.subscribe(Priority.NORMAL, new Function1<PokemonNicknamedEvent, Unit>() {
@Override
public Unit invoke(PokemonNicknamedEvent event) {
onSetNickname(event);
return Unit.INSTANCE;
}
});
}
private void onSetNickname(PokemonNicknamedEvent event) {
MutableComponent nickname = event.getNickname();
String newName = nickname != null ? nickname.getString() : "";
String species = event.getPokemon().getSpecies().getName();
LOGGER.info("Pokemon: '{}', Attempted nickname: '{}', length: {}", species, newName, newName.length());
if (newName.matches("^#+$")) {
LOGGER.info("Nickname is already censored: '{}', skipping", newName);
return;
}
if (BannedWords.containsBannedWord(newName)) {
String censored = new String(new char[newName.length()]).replace('\0', '#');
LOGGER.info("Censoring to: '{}', length: {}", censored, censored.length());
event.setNickname(Component.literal(censored));
}
}
}
Can we make it so it looks for the list in resources/assets/cobblecensors/banned_words and then I have the txt file named banned_words in there
Certainly! To load the banned words list from a resource file inside your mod's JAR (e.g., resources/assets/cobblecensors/banned_words.txt), you need to read the file as a resource stream rather than from the filesystem.
banned_words.txt from resources/assets/cobblecensors/banned_words.txt inside your mod JAR.Make sure your file is located at:
src/main/resources/assets/cobblecensors/banned_words.txt
BannedWords class to load from resource stream:package net.mattias.cobblecensors;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class BannedWords {
private static List<String> bannedWords = new ArrayList<>();
public static void loadBannedWordsFromResource() {
try (InputStream stream = BannedWords.class.getClassLoader()
.getResourceAsStream("assets/cobblecensors/banned_words.txt")) {
if (stream == null) {
bannedWords = new ArrayList<>();
return;
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
bannedWords = reader.lines()
.map(String::trim)
.filter(line -> !line.isEmpty())
.collect(Collectors.toList());
}
} catch (IOException e) {
bannedWords = new ArrayList<>();
}
}
public static boolean containsBannedWord(String text) {
if (text == null) return false;
String lowerText = text.toLowerCase();
return bannedWords.stream().anyMatch(word -> lowerText.contains(word.toLowerCase()));
}
}
public CobbleCensors(IEventBus modEventBus, ModContainer modContainer) {
// No need to create config directory or file for banned words anymore
BannedWords.loadBannedWordsFromResource();
new NameCensorHandler();
modEventBus.addListener(this::commonSetup);
NeoForge.EVENT_BUS.register(this);
modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
}
getResourceAsStream("assets/cobblecensors/banned_words.txt") loads the file from inside your mod JAR.If you want the file to be editable by users, you can copy the resource to the config folder if it doesn't exist, then load from the config folder:
public static void copyResourceToConfigIfMissing(Path configPath) throws IOException {
if (!Files.exists(configPath)) {
try (InputStream stream = BannedWords.class.getClassLoader()
.getResourceAsStream("assets/cobblecensors/banned_words.txt")) {
if (stream != null) {
Files.copy(stream, configPath);
}
}
}
}
Then in your mod constructor:
Path configDir = FMLPaths.CONFIGDIR.get();
Path modConfigDir = configDir.resolve("cobblecensors");
Files.createDirectories(modConfigDir);
Path bannedWordsFile = modConfigDir.resolve("banned_words.txt");
copyResourceToConfigIfMissing(bannedWordsFile);
BannedWords.loadBannedWords(bannedWordsFile);
Let me know if you want me to help with the editable config file approach!