Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
新项目
Browse files Browse the repository at this point in the history
  • Loading branch information
slinar committed Oct 8, 2018
0 parents commit 4172833
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
list:
- '/op'
- '/deop'

playerWhiteList:
- 'test'
10 changes: 10 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: CommandBlock
main: me.commandBlock.CommandBlock
version: 0.0.1
authors: [me]
description: []
commands:
cb: #指令名
description: "测试指令" #指令的注解
usage: §c用法:/cb <reload> - 重载配置文件 #指令的用法 当onCommand()方法返回false时提示这里的内容

47 changes: 47 additions & 0 deletions src/me/commandBlock/CheckCommand.java
Original file line number Diff line number Diff line change
@@ -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<String> playerList = ConfigFile.getPlayerWhiteList();
List<String> commandList = ConfigFile.getCommandList();

//如果玩家白名单列表匹配成功,则直接返回false
if (playerList.size() > 0){
for (int i=0;i<playerList.size();i++){
if (playerList.get(i).equals(playerName)){
return false;
}
}
}

if (commandList.size() > 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<commandList.size();i++){
if (commandList.get(i).equalsIgnoreCase(temp)){
Bukkit.getLogger().info(ConfigFile.PREFIX_C + "命令列表匹配成功:" + commandList.get(i));
return true;
}
}
}

//如果全部没有匹配成功,则表示玩家不在白名单,而且玩家执行的指令也没有被禁止,返回false
return false;
}
}
46 changes: 46 additions & 0 deletions src/me/commandBlock/CmdCb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.commandBlock;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class CmdCb {
@SuppressWarnings("serial")
private static final List<String> args1 = new ArrayList<String>(){{
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<String> tab(CommandSender sender, String[] args){
if ((args == null) || (args.length <= 1)){
return args1;
}
return new ArrayList<String>();
}
}
23 changes: 23 additions & 0 deletions src/me/commandBlock/CmdExecutor.java
Original file line number Diff line number Diff line change
@@ -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<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("cb")){
return CmdCb.tab(sender, args);
}
return new ArrayList<String>();
}
}
52 changes: 52 additions & 0 deletions src/me/commandBlock/CommandBlock.java
Original file line number Diff line number Diff line change
@@ -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<Permission> 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;
}
}
17 changes: 17 additions & 0 deletions src/me/commandBlock/CommandListener.java
Original file line number Diff line number Diff line change
@@ -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() + ", 已拦截操作!");
}
}
}
50 changes: 50 additions & 0 deletions src/me/commandBlock/ConfigFile.java
Original file line number Diff line number Diff line change
@@ -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<String> getCommandList(){
return config.getStringList("list");
}

public static List<String> getPlayerWhiteList(){
return config.getStringList("playerWhiteList");
}

}

0 comments on commit 4172833

Please sign in to comment.