mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 04:57:27 +00:00
Clean up class filter registration
This commit is contained in:
1 parent
950a749be6
commit
69c87404e7
2 files changed
+94
-2
No files matched your search
@@ -13,9 +13,11 @@ import cpw.mods.fml.common.event.FMLServerStoppedEvent;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
import cpw.mods.fml.common.registry.EntityRegistry;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import foxz.command.CommandNoppes;
|
||||
import io.github.somehussar.janinoloader.api.IDynamicCompiler;
|
||||
import io.github.somehussar.janinoloader.api.IDynamicCompilerBuilder;
|
||||
import io.github.somehussar.janinoloader.api.delegates.LoadClassCondition;
|
||||
import kamkeel.npcs.addon.AddonManager;
|
||||
import kamkeel.npcs.command.CommandKamkeel;
|
||||
import kamkeel.npcs.command.profile.CommandProfile;
|
||||
@@ -98,9 +100,16 @@ import noppes.npcs.entity.old.EntityNpcNagaFemale;
|
||||
import noppes.npcs.entity.old.EntityNpcNagaMale;
|
||||
import noppes.npcs.entity.old.EntityNpcSkeleton;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import somehussar.janino.AdvancedClassFilter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mod(modid = "customnpcs", name = "CustomNPC+", version = "1.10.2")
|
||||
public class CustomNpcs {
|
||||
@@ -139,12 +148,21 @@ public class CustomNpcs {
|
||||
|
||||
public static MinecraftServer Server;
|
||||
|
||||
private static IDynamicCompiler jls;
|
||||
@SideOnly(Side.CLIENT)
|
||||
private static IDynamicCompiler clientJaninoCompiler;
|
||||
@SideOnly(Side.CLIENT)
|
||||
private final static Set<Consumer<AdvancedClassFilter>> clientClassFilterConsumer = new HashSet<>();
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static void addClassesToClientClassFilter(Consumer<AdvancedClassFilter> consumer) {
|
||||
clientClassFilterConsumer.add(consumer);
|
||||
}
|
||||
|
||||
public CustomNpcs() {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private static IDynamicCompiler jls;
|
||||
|
||||
public static IDynamicCompiler getDynamicCompiler() {
|
||||
if (jls == null) {
|
||||
jls = IDynamicCompilerBuilder.createBuilder().getCompiler();
|
||||
@@ -153,6 +171,26 @@ public class CustomNpcs {
|
||||
return jls;
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static IDynamicCompiler getClientCompiler() {
|
||||
|
||||
if (clientJaninoCompiler == null) {
|
||||
AdvancedClassFilter filter = new AdvancedClassFilter()
|
||||
.addRegexes("noppes\\.npcs\\.api\\..*")
|
||||
.banRegexes(".*ClassLoader.*",
|
||||
".*File*.");
|
||||
|
||||
for (Consumer<AdvancedClassFilter> consumer : clientClassFilterConsumer) {
|
||||
consumer.accept(filter);
|
||||
}
|
||||
|
||||
|
||||
clientJaninoCompiler = IDynamicCompilerBuilder.createBuilder().setClassFilter(filter).getCompiler();
|
||||
}
|
||||
|
||||
return clientJaninoCompiler;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void load(FMLPreInitializationEvent ev) {
|
||||
PacketHandler.Instance = new PacketHandler();
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package somehussar.janino;
|
||||
|
||||
import io.github.somehussar.janinoloader.api.delegates.LoadClassCondition;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class AdvancedClassFilter implements LoadClassCondition {
|
||||
|
||||
private final Set<String> allowedClasses = new HashSet<>();
|
||||
private final Set<Pattern> allowedWildCards = new HashSet<>();
|
||||
private final Set<String> bannedClasses = new HashSet<>();
|
||||
private final Set<Pattern> bannedWildCards = new HashSet<>();
|
||||
|
||||
public AdvancedClassFilter() {
|
||||
addClasses("java.io.Serializable", "java.util.Iterator");
|
||||
addRegexes("java\\.lang\\..");
|
||||
}
|
||||
|
||||
public AdvancedClassFilter addRegexes(String... classRegexes) {
|
||||
allowedWildCards.addAll(Arrays.stream(classRegexes).map(Pattern::compile).collect(Collectors.toList()));
|
||||
return this;
|
||||
}
|
||||
public AdvancedClassFilter banRegexes(String... classRegexes) {
|
||||
bannedWildCards.addAll(Arrays.stream(classRegexes).map(Pattern::compile).collect(Collectors.toList()));
|
||||
return this;
|
||||
}
|
||||
public AdvancedClassFilter addClasses(String... classPath) {
|
||||
allowedClasses.addAll(Arrays.asList(classPath));
|
||||
return this;
|
||||
}
|
||||
public AdvancedClassFilter banClasses(String... classPath) {
|
||||
bannedClasses.addAll(Arrays.asList(classPath));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isValid(String name) {
|
||||
if (bannedClasses.contains(name))
|
||||
return false;
|
||||
if (bannedWildCards.stream().anyMatch(pattern -> pattern.matcher(name).matches()))
|
||||
return false;
|
||||
|
||||
if (name.startsWith("java.lang")) return true;
|
||||
|
||||
if (allowedClasses.contains(name)) return true;
|
||||
|
||||
return allowedWildCards.stream().anyMatch(pattern -> pattern.matcher(name).matches());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user