Skip to content

Commit

Permalink
Add external file loader
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Mar 10, 2024
1 parent 15db978 commit ef2a92d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/java/io/wdsj/asw/AdvancedSensitiveWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,20 @@ public void onEnable() {
public void doInitTasks() {
isAuthMeAvailable = Bukkit.getPluginManager().getPlugin("AuthMe") != null;
isCslAvailable = Bukkit.getPluginManager().getPlugin("CatSeedLogin") != null;
IWordAllow wA = WordAllows.chains(WordAllows.defaults(), new WordAllow());
IWordAllow wA = WordAllows.chains(WordAllows.defaults(), new WordAllow(), new ExternalWordAllow());
AtomicReference<IWordDeny> wD = new AtomicReference<>();
isInitialized = false;
sensitiveWordBs = null;
ProxySelector.setDefault(null);
getScheduler().runTaskAsynchronously(() -> {
if (settingsManager.getProperty(PluginSettings.ENABLE_DEFAULT_WORDS) && settingsManager.getProperty(PluginSettings.ENABLE_ONLINE_WORDS)) {
wD.set(WordDenys.chains(WordDenys.defaults(), new WordDeny(), new OnlineWordDeny()));
wD.set(WordDenys.chains(WordDenys.defaults(), new WordDeny(), new OnlineWordDeny(), new ExternalWordDeny()));
} else if (settingsManager.getProperty(PluginSettings.ENABLE_DEFAULT_WORDS)) {
wD.set(WordDenys.chains(new WordDeny(), WordDenys.defaults()));
wD.set(WordDenys.chains(new WordDeny(), WordDenys.defaults(), new ExternalWordDeny()));
} else if (settingsManager.getProperty(PluginSettings.ENABLE_ONLINE_WORDS)) {
wD.set(WordDenys.chains(new OnlineWordDeny(), new WordDeny()));
wD.set(WordDenys.chains(new OnlineWordDeny(), new WordDeny(), new ExternalWordDeny()));
} else {
wD.set(new WordDeny());
wD.set(WordDenys.chains(new WordDeny(), new ExternalWordDeny()));
}
// Full async reload
sensitiveWordBs = SensitiveWordBs.newInstance().ignoreCase(settingsManager.getProperty(PluginSettings.IGNORE_CASE)).ignoreWidth(settingsManager.getProperty(PluginSettings.IGNORE_WIDTH)).ignoreNumStyle(settingsManager.getProperty(PluginSettings.IGNORE_NUM_STYLE)).ignoreChineseStyle(settingsManager.getProperty(PluginSettings.IGNORE_CHINESE_STYLE)).ignoreEnglishStyle(settingsManager.getProperty(PluginSettings.IGNORE_ENGLISH_STYLE)).ignoreRepeat(settingsManager.getProperty(PluginSettings.IGNORE_REPEAT)).enableNumCheck(settingsManager.getProperty(PluginSettings.ENABLE_NUM_CHECK)).enableEmailCheck(settingsManager.getProperty(PluginSettings.ENABLE_EMAIL_CHECK)).enableUrlCheck(settingsManager.getProperty(PluginSettings.ENABLE_URL_CHECK)).enableWordCheck(settingsManager.getProperty(PluginSettings.ENABLE_WORD_CHECK)).wordResultCondition(settingsManager.getProperty(PluginSettings.FORCE_ENGLISH_FULL_MATCH) ? WordResultConditions.englishWordMatch() : WordResultConditions.alwaysTrue()).wordDeny(wD.get()).wordAllow(wA).numCheckLen(settingsManager.getProperty(PluginSettings.NUM_CHECK_LEN)).wordReplace(new WordReplace()).wordTag(WordTags.none()).charIgnore(new CharIgnore()).init();
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/io/wdsj/asw/method/ExternalWordAllow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.wdsj.asw.method;

import com.github.houbb.sensitive.word.api.IWordAllow;
import io.wdsj.asw.AdvancedSensitiveWords;
import io.wdsj.asw.impl.list.AdvancedList;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExternalWordAllow implements IWordAllow {
private final File dataFolder = new File(AdvancedSensitiveWords.getInstance().getDataFolder(), "/external/allow");

@Override
public List<String> allow() {
List<String> totalList = new AdvancedList<>();

if (Files.notExists(dataFolder.toPath())) {
try {
Files.createDirectories(dataFolder.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
try (Stream<Path> paths = Files.walk(dataFolder.toPath())) {
List<File> files = paths
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());

for (File file : files) {
List<String> lines = Files.readAllLines(file.toPath());
totalList.addAll(lines);
}
if (files.size() > 0) AdvancedSensitiveWords.getInstance().getLogger().info("Loaded " + files.size() + " external allow files.");
} catch (IOException e) {
e.printStackTrace();
return Collections.emptyList();
}
return totalList;
}
}
47 changes: 47 additions & 0 deletions src/main/java/io/wdsj/asw/method/ExternalWordDeny.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.wdsj.asw.method;

import com.github.houbb.sensitive.word.api.IWordDeny;
import io.wdsj.asw.AdvancedSensitiveWords;
import io.wdsj.asw.impl.list.AdvancedList;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExternalWordDeny implements IWordDeny {
private final File dataFolder = new File(AdvancedSensitiveWords.getInstance().getDataFolder(), "/external/deny");

@Override
public List<String> deny() {
List<String> totalList = new AdvancedList<>();

if (Files.notExists(dataFolder.toPath())) {
try {
Files.createDirectories(dataFolder.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
try (Stream<Path> paths = Files.walk(dataFolder.toPath())) {
List<File> files = paths
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());

for (File file : files) {
List<String> lines = Files.readAllLines(file.toPath());
totalList.addAll(lines);
}
if (files.size() > 0) AdvancedSensitiveWords.getInstance().getLogger().info("Loaded " + files.size() + " external deny files.");
} catch (IOException e) {
e.printStackTrace();
return Collections.emptyList();
}
return totalList;
}
}

0 comments on commit ef2a92d

Please sign in to comment.