引入fixed和unfixed功能
This commit is contained in:
@@ -26,7 +26,8 @@ import static ling.database.tables.LocationnotepadTB.LOCATIONNOTEPAD;
|
||||
/// zb命令处理器
|
||||
public class ZbCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
protected final static List<String> TAB_1 = Arrays.asList("add", "remove", "help", "reload", "list");
|
||||
protected final static List<String> TAB_1 = Arrays.asList("add", "remove", "help", "reload", "list", "fixed",
|
||||
"unfixed");
|
||||
|
||||
public ZbCommand() {
|
||||
|
||||
@@ -56,7 +57,8 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (data.addLocation(name, isFixed)) player.sendMessage("坐标记录完毕!");
|
||||
if (data.addLocation(name, isFixed))
|
||||
player.sendMessage("坐标记录完毕!");
|
||||
} catch (SQLException e) {
|
||||
CoordinateRecorder.getCurrent().getLogger().severe("记录坐标出错:" + e.getMessage());
|
||||
player.sendMessage(ChatColor.RED + "服务器内部错误:记录数据失败");
|
||||
@@ -165,6 +167,69 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
|
||||
player.sendMessage("已重新载入数据");
|
||||
}
|
||||
|
||||
protected void fixed(Player player, String[] strings) throws SQLException {
|
||||
if (strings.length > 2) {
|
||||
player.sendMessage(ChatColor.RED + "命令过长");
|
||||
return;
|
||||
}
|
||||
if (strings.length != 2) {
|
||||
player.sendMessage(ChatColor.RED + "请指定地标名称!");
|
||||
return;
|
||||
}
|
||||
String name = strings[1];
|
||||
PlayerData data = PlayerMap.getCurrent().getPlayerData(player);
|
||||
if (data == null) {
|
||||
player.sendMessage(ChatColor.RED + "玩家未登录");
|
||||
return;
|
||||
}
|
||||
if (!data.isAddLocations()) {
|
||||
player.sendMessage(ChatColor.RED + "已经无法再固定更多的地标了,请取消固定一些");
|
||||
return;
|
||||
}
|
||||
var ctx = CoordinateRecorder.getDatabase().getDSL();
|
||||
int rows = ctx.update(LOCATIONNOTEPAD).set(LOCATIONNOTEPAD.ISFIXED, true)
|
||||
.where(LOCATIONNOTEPAD.UID.eq(player.getUniqueId().toString())
|
||||
.and(LOCATIONNOTEPAD.NAME.eq(name))
|
||||
.and(LOCATIONNOTEPAD.ISDELETE.eq(false)))
|
||||
.execute();
|
||||
if (rows == 0) {
|
||||
player.sendMessage(ChatColor.RED + "操作失败:记录不存在");
|
||||
return;
|
||||
}
|
||||
data.locationListUpdate();
|
||||
player.sendMessage("固定 " + name + " 到侧边栏");
|
||||
}
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
protected void unfixed(Player player, String[] strings) throws SQLException {
|
||||
if (strings.length > 2) {
|
||||
player.sendMessage(ChatColor.RED + "命令过长");
|
||||
return;
|
||||
}
|
||||
if (strings.length != 2) {
|
||||
player.sendMessage(ChatColor.RED + "请指定地标名称!");
|
||||
return;
|
||||
}
|
||||
String name = strings[1];
|
||||
PlayerData data = PlayerMap.getCurrent().getPlayerData(player);
|
||||
if (data == null) {
|
||||
player.sendMessage(ChatColor.RED + "玩家未登录");
|
||||
return;
|
||||
}
|
||||
var ctx = CoordinateRecorder.getDatabase().getDSL();
|
||||
int rows = ctx.update(LOCATIONNOTEPAD).set(LOCATIONNOTEPAD.ISFIXED, false)
|
||||
.where(LOCATIONNOTEPAD.UID.eq(player.getUniqueId().toString())
|
||||
.and(LOCATIONNOTEPAD.NAME.eq(name))
|
||||
.and(LOCATIONNOTEPAD.ISDELETE.eq(false)))
|
||||
.execute();
|
||||
if (rows == 0) {
|
||||
player.sendMessage(ChatColor.RED + "操作失败:记录不存在");
|
||||
return;
|
||||
}
|
||||
data.locationListUpdate();
|
||||
player.sendMessage("从侧边栏取消固定 " + name);
|
||||
}
|
||||
|
||||
protected boolean execute(Player player, String[] strings) {
|
||||
if (strings.length < 1) {
|
||||
player.sendMessage(ChatColor.RED + "语法错误:需要 [选项]");
|
||||
@@ -187,6 +252,12 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
|
||||
case "list":
|
||||
list(player, strings);
|
||||
break;
|
||||
case "fixed":
|
||||
fixed(player, strings);
|
||||
break;
|
||||
case "unfixed":
|
||||
unfixed(player, strings);
|
||||
break;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
player.sendMessage(ChatColor.RED + "服务器内部错误,请联系管理员");
|
||||
@@ -217,23 +288,45 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
|
||||
return filter(latest, list);
|
||||
}
|
||||
|
||||
protected List<String> onAddTab(Player player, String latest, String[] strings) {
|
||||
if (strings.length == 2) {
|
||||
return List.of("<地标名称>");
|
||||
}
|
||||
if (strings.length == 3) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("false");
|
||||
list.add("true");
|
||||
return filter(latest, list);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
return List.of();
|
||||
}
|
||||
String latest = null;
|
||||
if (strings.length != 0) {
|
||||
latest = strings[strings.length - 1];
|
||||
if (strings.length == 0) {
|
||||
return List.of();
|
||||
}
|
||||
String latest = strings[strings.length - 1];
|
||||
if (strings.length == 1) return filter(latest, new ArrayList<>(TAB_1));
|
||||
if (strings.length == 2) {
|
||||
if (strings[0].equals("remove")) {
|
||||
return onRemoveTab(player, latest, strings);
|
||||
} else if (strings[0].equals("list")) {
|
||||
return List.of("<页码>");
|
||||
switch (strings[0]) {
|
||||
case "remove", "unfixed" -> {
|
||||
return onRemoveTab(player, latest, strings);
|
||||
}
|
||||
case "list" -> {
|
||||
return List.of("<页码>");
|
||||
}
|
||||
case "fixed" -> {
|
||||
return List.of("<地标名称>");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (strings[0].equals("add")) {
|
||||
return onAddTab(player, latest, strings);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
|
||||
@@ -66,6 +66,10 @@ public class PlayerData {
|
||||
|
||||
/// 将当前位置记录到坐标记录中
|
||||
public boolean addLocation(String name, boolean isFixed) throws SQLException {
|
||||
if (isFixed && !isAddLocations()) {
|
||||
player.sendMessage(ChatColor.RED + "已经无法再固定更多的地标了,使用/zb unfixed 命令取消固定一些");
|
||||
return false;
|
||||
}
|
||||
var ctx = CoordinateRecorder.getDatabase().getDSL();
|
||||
Record record = ctx.select().from(LOCATIONNOTEPAD).where(
|
||||
LOCATIONNOTEPAD.UID.eq(player.getUniqueId().toString())
|
||||
@@ -125,6 +129,11 @@ public class PlayerData {
|
||||
ui.flashLocations();
|
||||
}
|
||||
|
||||
/// 检查当前是否还有空余位置显示新的坐标点
|
||||
public boolean isAddLocations() {
|
||||
return locationList.size() < 10;
|
||||
}
|
||||
|
||||
public List<LocationnotepadPO> getLocationList() {
|
||||
return Collections.unmodifiableList(this.locationList);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user