Files
src
build.gradle
Code
4 94
src/main/java/com/example/PerWorldPlugins.java
package com.example;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PerWorldPlugins extends JavaPlugin{
private static PerWorldPlugins instance;
private Map<String,List<String>> disabledPluginsPerWorld;
@Override public void onEnable(){
instance = this;
saveDefaultConfig();
loadConfig();
}
private void loadConfig(){
disabledPluginsPerWorld = new HashMap<>();
FileConfiguration config = getConfig();
for(String world : config.getKeys(false)){
disabledPluginsPerWorld.put(world,config.getStringList(world));
}
}
private void saveConfigData(){
FileConfiguration config = getConfig();
config.getKeys(false).forEach(key -> config.set(key, null));
disabledPluginsPerWorld.forEach((k,v) -> config.set(k,v));
saveConfig();
}
public static boolean isPluginEnabled(String pluginName,World world){
if(instance.disabledPluginsPerWorld.containsKey(world.getName())){
return !instance.disabledPluginsPerWorld.get(world.getName()).contains(pluginName);
}
return true;
}
@Override public boolean onCommand(CommandSender sender,Command command,String label,String[] args){
if(args.length != 3){
sender.sendMessage("Usage: /perworldplugins <world> <plugin> <enable|disable>");
return true;
}
String worldName = args[0];
String pluginName = args[1];
String action = args[2];
World world = Bukkit.getWorld(worldName);
if(world == null){
sender.sendMessage("World not found");
return true;
}
List<String> disabled = disabledPluginsPerWorld.computeIfAbsent(worldName,k -> new ArrayList<>());
if("disable".equals(action)){
if(!disabled.contains(pluginName)){
disabled.add(pluginName);
}
}
else if("enable".equals(action)){
disabled.remove(pluginName);
}
else{
sender.sendMessage("Action must be enable or disable");
return true;
}
saveConfigData();
sender.sendMessage("Plugin " + pluginName + " " + action + "d in world " + worldName);
return true;
}
}