From 4172833475c208e03b281587c85395492cf0f9d1 Mon Sep 17 00:00:00 2001 From: slinar Date: Mon, 8 Oct 2018 13:30:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yml | 6 +++ plugin.yml | 10 +++++ src/me/commandBlock/CheckCommand.java | 47 +++++++++++++++++++++ src/me/commandBlock/CmdCb.java | 46 +++++++++++++++++++++ src/me/commandBlock/CmdExecutor.java | 23 +++++++++++ src/me/commandBlock/CommandBlock.java | 52 ++++++++++++++++++++++++ src/me/commandBlock/CommandListener.java | 17 ++++++++ src/me/commandBlock/ConfigFile.java | 50 +++++++++++++++++++++++ 8 files changed, 251 insertions(+) create mode 100644 config.yml create mode 100644 plugin.yml create mode 100644 src/me/commandBlock/CheckCommand.java create mode 100644 src/me/commandBlock/CmdCb.java create mode 100644 src/me/commandBlock/CmdExecutor.java create mode 100644 src/me/commandBlock/CommandBlock.java create mode 100644 src/me/commandBlock/CommandListener.java create mode 100644 src/me/commandBlock/ConfigFile.java diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..55c356a --- /dev/null +++ b/config.yml @@ -0,0 +1,6 @@ +list: + - '/op' + - '/deop' + +playerWhiteList: + - 'test' diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..f508747 --- /dev/null +++ b/plugin.yml @@ -0,0 +1,10 @@ +name: CommandBlock +main: me.commandBlock.CommandBlock +version: 0.0.1 +authors: [me] +description: [] +commands: + cb: #指令名 + description: "测试指令" #指令的注解 + usage: §c用法:/cb - 重载配置文件 #指令的用法 当onCommand()方法返回false时提示这里的内容 + diff --git a/src/me/commandBlock/CheckCommand.java b/src/me/commandBlock/CheckCommand.java new file mode 100644 index 0000000..17f356a --- /dev/null +++ b/src/me/commandBlock/CheckCommand.java @@ -0,0 +1,47 @@ +package me.commandBlock; + +import java.util.List; + +import org.bukkit.Bukkit; + +public final class CheckCommand { + + public static boolean check(String commandStr, String playerName){ + //从ConfigFile中接收玩家白名单列表和禁用指令列表 + List playerList = ConfigFile.getPlayerWhiteList(); + List commandList = ConfigFile.getCommandList(); + + //如果玩家白名单列表匹配成功,则直接返回false + if (playerList.size() > 0){ + for (int i=0;i 0){ + //处理用户输入的信息 + String temp = ""; + String[] temp1 = commandStr.split(" "); + String[] temp2 = temp1[0].split(":"); + if (temp2.length == 1){ + temp = temp2[0]; + } + else if (temp2.length > 1) { + temp = "/" + temp2[temp2.length-1]; + } + + //如果玩家将要执行的指令和禁用指令列表匹配成功,则返回true + for (int i=0;i args1 = new ArrayList(){{ + add("reload"); + }}; + public static boolean exec(CommandSender sender, String[] args){ + if (sender instanceof org.bukkit.command.ConsoleCommandSender){ + if ((args.length == 1) && (args[0].equalsIgnoreCase("reload"))){ + ConfigFile.loadConfig(); + return true; + } + } + + if (sender instanceof Player){ + //Player player = (Player)sender; + + if (sender.isOp()){ + if ((args.length == 1) && (args[0].equalsIgnoreCase("reload"))){ + ConfigFile.loadConfig(); + sender.sendMessage(ConfigFile.PREFIX + "已重载配置!"); + return true; + } + }else{ + sender.sendMessage(ConfigFile.PREFIX + "你没有权限这样做!"); + return true; + } + + } + return false; + } + + public static List tab(CommandSender sender, String[] args){ + if ((args == null) || (args.length <= 1)){ + return args1; + } + return new ArrayList(); + } +} diff --git a/src/me/commandBlock/CmdExecutor.java b/src/me/commandBlock/CmdExecutor.java new file mode 100644 index 0000000..c226310 --- /dev/null +++ b/src/me/commandBlock/CmdExecutor.java @@ -0,0 +1,23 @@ +package me.commandBlock; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +public class CmdExecutor implements CommandExecutor,TabCompleter { + public boolean onCommand(CommandSender sender,Command cmd, String label,String[] args) { + if (cmd.getName().equalsIgnoreCase("cb")) return CmdCb.exec(sender, args); + return false; + } + + public List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { + if (cmd.getName().equalsIgnoreCase("cb")){ + return CmdCb.tab(sender, args); + } + return new ArrayList(); + } +} diff --git a/src/me/commandBlock/CommandBlock.java b/src/me/commandBlock/CommandBlock.java new file mode 100644 index 0000000..6803e2a --- /dev/null +++ b/src/me/commandBlock/CommandBlock.java @@ -0,0 +1,52 @@ +package me.commandBlock; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import net.milkbowl.vault.permission.Permission; + +import org.bukkit.Bukkit; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.RegisteredServiceProvider; + +public final class CommandBlock extends org.bukkit.plugin.java.JavaPlugin{ + public static Permission permission = null; + private static CommandBlock INSTANCE; + private static ScheduledExecutorService ses = Executors.newScheduledThreadPool(2); + + @Override + public void onEnable(){ + INSTANCE = this; + setupPermissions(); + ConfigFile.loadConfig(); + getServer().getPluginManager().registerEvents(new CommandListener(),this); + Bukkit.getPluginCommand("cb").setExecutor(new CmdExecutor()); + Bukkit.getPluginCommand("cb").setTabCompleter(new CmdExecutor()); + ses.scheduleAtFixedRate(ConfigFile.getTask(), 5, 5, TimeUnit.SECONDS); + getLogger().info("CommandBlock is enabled!"); + } + + @Override + public void onDisable(){ + HandlerList.unregisterAll(this); + ses.shutdownNow(); + getLogger().info("CommandBlock is disabled"); + } + + private void setupPermissions() { + try { + RegisteredServiceProvider permissionProvider = getServer().getServicesManager().getRegistration(Permission.class); + if(permissionProvider != null) { + permission = (Permission)permissionProvider.getProvider(); + getLogger().info("已找到 Vault!"); + } + } catch (NoClassDefFoundError e) { + getLogger().info("没有找到 Vault!"); + } + } + + public static CommandBlock getIns(){ + return INSTANCE; + } +} diff --git a/src/me/commandBlock/CommandListener.java b/src/me/commandBlock/CommandListener.java new file mode 100644 index 0000000..dd0e848 --- /dev/null +++ b/src/me/commandBlock/CommandListener.java @@ -0,0 +1,17 @@ +package me.commandBlock; + +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +public class CommandListener implements Listener { + + @EventHandler + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e){ + if (CheckCommand.check(e.getMessage(), e.getPlayer().getName())){ + e.setCancelled(true); + Bukkit.getLogger().info(ConfigFile.PREFIX_C + "玩家 " + e.getPlayer().getName() + " 试图执行:" + e.getMessage() + ", 已拦截操作!"); + } + } +} diff --git a/src/me/commandBlock/ConfigFile.java b/src/me/commandBlock/ConfigFile.java new file mode 100644 index 0000000..663943e --- /dev/null +++ b/src/me/commandBlock/ConfigFile.java @@ -0,0 +1,50 @@ +package me.commandBlock; + +import java.io.File; +import java.util.List; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +public class ConfigFile { + static final String PREFIX = "§c[CommandBlock] "; + static final String PREFIX_C = "[CommandBlock] "; + private static CommandBlock ins = CommandBlock.getIns(); + private static File file; + private static FileConfiguration config; + private static Runnable task = new Runnable() { + public void run() { + updateConfig(); + } + }; + + public static void loadConfig(){ + if (!ins.getDataFolder().exists()) { + ins.getDataFolder().mkdirs(); + } + file = new File(CommandBlock.getIns().getDataFolder(), "config.yml"); + if (!file.exists()) { + ins.getLogger().info("配置文件不存在,载入默认配置文件!"); + ins.saveDefaultConfig(); + } + config = YamlConfiguration.loadConfiguration(file); + ins.getLogger().info("配置文件已载入!"); + } + + public static void updateConfig(){ + config = YamlConfiguration.loadConfiguration(file); + } + + public static Runnable getTask(){ + return task; + } + + public static List getCommandList(){ + return config.getStringList("list"); + } + + public static List getPlayerWhiteList(){ + return config.getStringList("playerWhiteList"); + } + +}