326 lines
15 KiB
Java
326 lines
15 KiB
Java
package cn.citycraft.RealBackpacks;
|
|
|
|
import java.io.File;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.List;
|
|
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import cn.citycraft.RealBackpacks.config.FileConfig;
|
|
import cn.citycraft.RealBackpacks.config.PlayerConfig;
|
|
import cn.citycraft.RealBackpacks.util.MysqlFunctions;
|
|
import cn.citycraft.RealBackpacks.util.RBUtil;
|
|
import cn.citycraft.RealBackpacks.util.Serialization;
|
|
|
|
public class MainCommand implements CommandExecutor {
|
|
|
|
private final RealBackpacks plugin;
|
|
|
|
private boolean exist = false;
|
|
|
|
private String[] helps = new String[] { "§6====== 真实背包插件 By:喵♂呜 ======",
|
|
"§4* §a查看可购买列表 §7/rb list ",
|
|
"§4* §a购买背包 §7/rb buy <背包名称> ",
|
|
"§4* §a给玩家指定背包 §7/rb give <玩家名称> <背包名称>",
|
|
"§4* §a查看玩家指定背包 §7/rb view <玩家名称> <背包名称>",
|
|
"§4* §a数据转移至MySQL §7/rb filetomysql" };
|
|
|
|
public MainCommand(final RealBackpacks plugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("deprecation")
|
|
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
|
|
if (cmd.getName().equalsIgnoreCase("rb")) {
|
|
if (args.length == 0)
|
|
sender.sendMessage(helps);
|
|
if (args.length >= 1) {
|
|
final String command = args[0];
|
|
if (command.equalsIgnoreCase("reload")) {
|
|
if (plugin.isUsingPerms() && !sender.hasPermission("rb.reload")) {
|
|
sender.sendMessage(ChatColor.RED + "你没有此命令的权限!");
|
|
return false;
|
|
}
|
|
final Long first = System.currentTimeMillis();
|
|
plugin.reloadConfig();
|
|
plugin.setupLists();
|
|
plugin.getServer().resetRecipes();
|
|
plugin.setup();
|
|
sender.sendMessage(ChatColor.GRAY + "配置文件重载完毕 用时 " + ChatColor.YELLOW + (System.currentTimeMillis() - first) + "毫秒" + ChatColor.GRAY + ".");
|
|
return true;
|
|
} else if (command.equalsIgnoreCase("buy") || command.equalsIgnoreCase("purchase")) {
|
|
if (!plugin.isUsingVault()) {
|
|
sender.sendMessage(ChatColor.RED + "当前命令无法使用,因为没有安装经济插件.");
|
|
return false;
|
|
}
|
|
if (!(sender instanceof Player)) {
|
|
sender.sendMessage(ChatColor.RED + "控制台不能使用此命令.");
|
|
return false;
|
|
}
|
|
if (!(args.length == 2)) {
|
|
sender.sendMessage(ChatColor.RED + "命令错误. 正确命令:" + ChatColor.GRAY + " /rb buy <backpack>");
|
|
return false;
|
|
}
|
|
String backpack = null;
|
|
backpack = RBUtil.stringToBackpack(args[1]);
|
|
if (backpack == null) {
|
|
sender.sendMessage("背包不存在.");
|
|
return false;
|
|
}
|
|
if (plugin.isUsingPerms() && !sender.hasPermission("rb." + backpack + ".buy")) {
|
|
sender.sendMessage("没有购买的权限.");
|
|
return false;
|
|
}
|
|
if (plugin.backpackData.get(backpack).get(13) != null && !plugin.backpackData.get(backpack).get(13).equals("true")) {
|
|
sender.sendMessage("不能被购买.");
|
|
return false;
|
|
}
|
|
final double price = Double.parseDouble(plugin.backpackData.get(backpack).get(14));
|
|
if (RealBackpacks.econ.getBalance(sender.getName()) < price) {
|
|
sender.sendMessage(ChatColor.RED + "你没有足够的钱购买这个背包.");
|
|
return false;
|
|
}
|
|
final Player p = (Player) sender;
|
|
final Inventory inv = p.getInventory();
|
|
final ItemStack backpackItem = plugin.backpackItems.get(backpack);
|
|
if (inv.firstEmpty() != -1) {
|
|
RealBackpacks.econ.withdrawPlayer(p.getName(), price);
|
|
if (plugin.backpackData.get(backpack).get(18) != null && plugin.backpackData.get(backpack).get(18).equalsIgnoreCase("true")) {
|
|
if (RealBackpacks.globalGlow && plugin.backpackData.get(backpack).get(17) != null && plugin.backpackData.get(backpack).get(17).equalsIgnoreCase("true"))
|
|
inv.setItem(inv.firstEmpty(), RealBackpacks.NMS.addGlow(backpackItem));
|
|
else
|
|
inv.setItem(inv.firstEmpty(), backpackItem);
|
|
} else if (RealBackpacks.globalGlow && plugin.backpackData.get(backpack).get(17) != null && plugin.backpackData.get(backpack).get(17).equalsIgnoreCase("true"))
|
|
inv.addItem(RealBackpacks.NMS.addGlow(backpackItem));
|
|
else
|
|
inv.addItem(backpackItem);
|
|
p.updateInventory();
|
|
sender.sendMessage(ChatColor.GREEN + "你花费了 " + ChatColor.GOLD + price + ChatColor.GREEN + " 购买了背包: " + ChatColor.GOLD + backpack);
|
|
return true;
|
|
} else {
|
|
sender.sendMessage(ChatColor.RED + "你的背包是空的.");
|
|
return false;
|
|
}
|
|
} else if (command.equalsIgnoreCase("list")) {
|
|
if (plugin.isUsingPerms() && !sender.hasPermission("rb.list")) {
|
|
sender.sendMessage(ChatColor.RED + "你没有此命令的权限!");
|
|
return false;
|
|
}
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + " 名称 " + ChatColor.GOLD + "|" + ChatColor.AQUA + " 大小 " + ChatColor.GOLD + "|" + ChatColor.GREEN + " 价格 ");
|
|
sender.sendMessage(ChatColor.GOLD + "-----------------------------------");
|
|
if (plugin.isUsingPerms())
|
|
for (final String backpack : plugin.backpacks) {
|
|
final boolean hasPerm = sender.hasPermission("rb." + backpack + ".buy");
|
|
final List<String> key = plugin.backpackData.get(backpack);
|
|
if (plugin.backpackData.get(backpack).get(13).equalsIgnoreCase("true") && hasPerm)
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + backpack + ChatColor.GOLD + " | " + ChatColor.AQUA + key.get(0) + ChatColor.GOLD + " | " + ChatColor.GREEN
|
|
+ Double.parseDouble(key.get(14)));
|
|
else if (plugin.backpackData.get(backpack).get(13) != null && !plugin.backpackData.get(backpack).get(13).equalsIgnoreCase("true") && hasPerm)
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + backpack + ChatColor.GOLD + " | " + ChatColor.AQUA + key.get(0) + ChatColor.GOLD + " | " + ChatColor.RED + "不能购买");
|
|
else
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + backpack + ChatColor.GOLD + " | " + ChatColor.AQUA + key.get(0) + ChatColor.GOLD + " | " + ChatColor.RED + "没有足够的权限购买");
|
|
}
|
|
else
|
|
for (final String backpack : plugin.backpacks) {
|
|
final List<String> key = plugin.backpackData.get(backpack);
|
|
if (plugin.backpackData.get(backpack).get(13) != null && plugin.backpackData.get(backpack).get(13).equalsIgnoreCase("true"))
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + backpack + ChatColor.GOLD + " | " + ChatColor.AQUA + key.get(0) + ChatColor.GOLD + " | " + ChatColor.GREEN
|
|
+ Double.parseDouble(key.get(14)));
|
|
else
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + backpack + ChatColor.GOLD + " | " + ChatColor.AQUA + key.get(0) + ChatColor.GOLD + " | " + ChatColor.RED + "不能购买");
|
|
}
|
|
} else if (command.equalsIgnoreCase("give")) {
|
|
if (!(args.length == 3)) {
|
|
sender.sendMessage(ChatColor.RED + "错误的命令. 正确方式:" + ChatColor.GRAY + " /rb give <玩家> <背包名称>");
|
|
return false;
|
|
}
|
|
String backpack = null;
|
|
backpack = RBUtil.stringToBackpack(args[2]);
|
|
if (plugin.isUsingPerms() && !sender.hasPermission("rb." + backpack + ".give")) {
|
|
sender.sendMessage(ChatColor.RED + "没有足够的权限");
|
|
return false;
|
|
}
|
|
if (backpack == null) {
|
|
sender.sendMessage(ChatColor.RED + "背包不存在");
|
|
return false;
|
|
}
|
|
final Player other = plugin.getServer().getPlayer(args[1]);
|
|
if (other == null) {
|
|
sender.sendMessage(ChatColor.RED + "玩家不存在");
|
|
return false;
|
|
}
|
|
final Inventory inv = other.getInventory();
|
|
final ItemStack backpackItem = plugin.backpackItems.get(backpack);
|
|
if (inv.firstEmpty() != -1) {
|
|
if (plugin.backpackData.get(backpack).get(18) != null && plugin.backpackData.get(backpack).get(18).equalsIgnoreCase("true")) {
|
|
if (RealBackpacks.globalGlow && plugin.backpackData.get(backpack).get(17) != null && plugin.backpackData.get(backpack).get(17).equalsIgnoreCase("true"))
|
|
inv.setItem(inv.firstEmpty(), RealBackpacks.NMS.addGlow(backpackItem));
|
|
else
|
|
inv.setItem(inv.firstEmpty(), backpackItem);
|
|
} else if (RealBackpacks.globalGlow && plugin.backpackData.get(backpack).get(17) != null && plugin.backpackData.get(backpack).get(17).equalsIgnoreCase("true"))
|
|
inv.addItem(RealBackpacks.NMS.addGlow(backpackItem));
|
|
else
|
|
inv.addItem(backpackItem);
|
|
other.updateInventory();
|
|
sender.sendMessage(ChatColor.GREEN + "你把背包 " + ChatColor.GOLD + backpack + ChatColor.GREEN + " 发送给了 " + ChatColor.GOLD + other.getName());
|
|
return true;
|
|
} else {
|
|
sender.sendMessage(ChatColor.RED + other.getName() + "的背包已经满了");
|
|
return false;
|
|
}
|
|
} else if (command.equalsIgnoreCase("filetomysql")) {
|
|
if (plugin.isUsingPerms() && !sender.hasPermission("rb.filetomysql"))
|
|
return false;
|
|
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (!MysqlFunctions.checkIfTableExists("rb_data")) {
|
|
MysqlFunctions.createTables();
|
|
exist = false;
|
|
} else
|
|
exist = true;
|
|
try {
|
|
final Connection conn = DriverManager.getConnection(plugin.getUrl(), plugin.getUser(), plugin.getPass());
|
|
final File dir = new File(plugin.getDataFolder() + File.separator + "userdata");
|
|
int i = 0, times = 0;
|
|
final int files = dir.listFiles().length;
|
|
for (final File child : dir.listFiles()) {
|
|
final String player = child.getName().replace(".yml", "");
|
|
final FileConfig config = PlayerConfig.getInstance(plugin, player);
|
|
|
|
i++;
|
|
PreparedStatement statement = null;
|
|
PreparedStatement state = null;
|
|
for (final String backpack : config.getConfigurationSection("").getKeys(false)) {
|
|
if (exist) {
|
|
statement = conn.prepareStatement("SELECT EXISTS(SELECT 1 FROM rb_data WHERE player = ? AND backpack = ? LIMIT 1);");
|
|
statement.setString(1, player);
|
|
statement.setString(2, backpack);
|
|
final ResultSet res = statement.executeQuery();
|
|
if (res.next())
|
|
if (res.getInt(1) == 1) {
|
|
state = conn.prepareStatement("UPDATE rb_data SET player=?, backpack=?, inventory=? WHERE player=? AND backpack=?;");
|
|
state.setString(1, player);
|
|
state.setString(2, backpack);
|
|
state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
|
state.setString(4, player);
|
|
state.setString(5, backpack);
|
|
} else {
|
|
state = conn.prepareStatement("INSERT INTO rb_data (player, backpack, inventory) VALUES(?, ?, ?);");
|
|
state.setString(1, player);
|
|
state.setString(2, backpack);
|
|
state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
|
}
|
|
} else {
|
|
state = conn.prepareStatement("INSERT INTO rb_data (player, backpack, inventory) VALUES(?, ?, ?);");
|
|
state.setString(1, player);
|
|
state.setString(2, backpack);
|
|
state.setString(3, Serialization.listToString(config.getStringList(backpack + ".Inventory")));
|
|
}
|
|
if (state != null) {
|
|
state.executeUpdate();
|
|
state.close();
|
|
}
|
|
}
|
|
if (i == 50) {
|
|
i = 0;
|
|
times++;
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + times * 50 + "/" + files + " files have been transferred.");
|
|
}
|
|
}
|
|
conn.close();
|
|
sender.sendMessage(ChatColor.LIGHT_PURPLE + "数据转换完成.");
|
|
} catch (final SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
} else if (command.equalsIgnoreCase("view")) {
|
|
if (!(args.length == 3)) {
|
|
sender.sendMessage(ChatColor.RED + "命令错误. 正确命令:" + ChatColor.GRAY + " /rb view <player> <backpack>");
|
|
return false;
|
|
}
|
|
String backpack = null;
|
|
backpack = RBUtil.stringToBackpack(args[2]);
|
|
boolean fullview = false;
|
|
boolean restrictedview = false;
|
|
if (plugin.isUsingPerms() && sender.hasPermission("rb.fullview"))
|
|
fullview = true;
|
|
else if (plugin.isUsingPerms() && sender.hasPermission("rb.restrictedview"))
|
|
restrictedview = true;
|
|
if (plugin.isUsingPerms() && !fullview && !restrictedview) {
|
|
sender.sendMessage(ChatColor.RED + "没有足够的权限购买");
|
|
return false;
|
|
}
|
|
if (backpack == null) {
|
|
sender.sendMessage(ChatColor.RED + "背包不存在");
|
|
return false;
|
|
}
|
|
Inventory inv = null;
|
|
String name = args[1];
|
|
final Player p = (Player) sender;
|
|
final List<String> key = plugin.backpackData.get(backpack);
|
|
if (!plugin.isUsingMysql()) {
|
|
boolean fileExists = false;
|
|
String fullName = null;
|
|
final File dir = new File(plugin.getDataFolder() + File.separator + "userdata");
|
|
for (final File f : dir.listFiles()) {
|
|
final String fileName = f.getName();
|
|
fullName = fileName.replace(".yml", "");
|
|
if (fullName.equalsIgnoreCase(name)) {
|
|
name = fullName;
|
|
fileExists = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!fileExists) {
|
|
sender.sendMessage(ChatColor.RED + "这货从来没打开过背包,所以是空的,2333.");
|
|
return false;
|
|
}
|
|
final FileConfig config = PlayerConfig.getInstance(plugin, fullName);
|
|
if (config.getStringList(backpack + ".Inventory") == null)
|
|
inv = plugin.getServer().createInventory(p, Integer.parseInt(key.get(0)), ChatColor.translateAlternateColorCodes('&', fullName + "'s " + backpack + " data"));
|
|
else
|
|
inv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"), fullName + "'s " + backpack + " data", Integer.parseInt(key.get(0)));
|
|
} else {
|
|
try {
|
|
inv = MysqlFunctions.getBackpackInv(name, backpack);
|
|
} catch (final SQLException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
if (inv == null) {
|
|
sender.sendMessage(ChatColor.RED + "这货从来没打开过背包,所以是空的,2333.");
|
|
return false;
|
|
}
|
|
}
|
|
if (plugin.playerData.containsKey(name)) {
|
|
sender.sendMessage(ChatColor.RED + "玩家打开了背包,请等待玩家关闭.");
|
|
return false;
|
|
}
|
|
if (fullview || !plugin.isUsingPerms())
|
|
plugin.adminFullView.put(sender.getName(), backpack + ":" + name);
|
|
else
|
|
plugin.adminRestrictedView.add(sender.getName());
|
|
p.openInventory(inv);
|
|
} else {
|
|
sender.sendMessage(ChatColor.RED + "命令未找到.");
|
|
sender.sendMessage(helps);
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|