引入remove子命令

This commit is contained in:
2024-12-20 04:26:08 +08:00
parent 8cf3fcb6d5
commit 145557b024
3 changed files with 82 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ package ling.coordinateRecorder.Commands;
import ling.coordinateRecorder.CoordinateRecorder; import ling.coordinateRecorder.CoordinateRecorder;
import ling.coordinateRecorder.Listener.PlayerMap; import ling.coordinateRecorder.Listener.PlayerMap;
import ling.coordinateRecorder.data.PlayerData; import ling.coordinateRecorder.data.PlayerData;
import ling.database.tables.records.LocationnotepadPO;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@@ -37,12 +38,12 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
player.sendMessage(ChatColor.RED + "语法错误add需要 [name] 参数,作为当前地标的名称"); player.sendMessage(ChatColor.RED + "语法错误add需要 [name] 参数,作为当前地标的名称");
return; return;
} }
boolean isFixed = false; boolean isFixed = true;
String name = strings[1]; String name = strings[1];
if (strings.length == 3) { if (strings.length == 3) {
if (strings[2].equals("true")) { if (strings[2].equals("false")) {
isFixed = true; isFixed = false;
} else if (!strings[2].equals("false")) { } else if (!strings[2].equals("true")) {
player.sendMessage(ChatColor.RED + "无法解析的参数:" + strings[3]); player.sendMessage(ChatColor.RED + "无法解析的参数:" + strings[3]);
} }
} }
@@ -52,14 +53,37 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
return; return;
} }
try { try {
data.addLocation(name, isFixed); if (data.addLocation(name, isFixed))
player.sendMessage("坐标记录完毕!"); player.sendMessage("坐标记录完毕!");
} catch (SQLException e) { } catch (SQLException e) {
CoordinateRecorder.getCurrent().getLogger().severe("记录坐标出错:" + e.getMessage()); CoordinateRecorder.getCurrent().getLogger().severe("记录坐标出错:" + e.getMessage());
player.sendMessage(ChatColor.RED + "服务器内部错误:记录数据失败"); player.sendMessage(ChatColor.RED + "服务器内部错误:记录数据失败");
} }
} }
protected void remove(Player player, String[] strings) {
if (strings.length > 2) {
player.sendMessage(ChatColor.RED + "命令过长");
return;
}
if (strings.length < 2) {
player.sendMessage(ChatColor.RED + "语法错误remove需要 [name] 参数,指示要删除的地标名称");
return;
}
PlayerData data = PlayerMap.getCurrent().getPlayerData(player);
if (data == null) {
player.sendMessage(ChatColor.RED + "玩家未登录");
return;
}
try {
if (data.removeLocation(strings[1]))
player.sendMessage("操作完成");
} catch (SQLException e) {
CoordinateRecorder.getCurrent().getLogger().severe("移除坐标出错:" + e.getMessage());
player.sendMessage(ChatColor.RED + "服务器内部错误:移除数据失败");
}
}
/// 显示帮助 /// 显示帮助
protected void help(Player player) { protected void help(Player player) {
ItemStack book = new ItemStack(Material.WRITTEN_BOOK); ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
@@ -96,6 +120,9 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
if (strings[0].equals("help")) { if (strings[0].equals("help")) {
help(player); help(player);
} }
if (strings[0].equals("remove")) {
remove(player, strings);
}
return true; return true;
} }
@@ -111,14 +138,34 @@ public class ZbCommand implements CommandExecutor, TabCompleter {
return execute(player, strings); return execute(player, strings);
} }
protected List<String> onRemoveTab(Player player, String latest, String[] strings) {
var data = PlayerMap.getCurrent().getPlayerData(player);
if (data == null)
return List.of();
List<String> list = new ArrayList<>();
List<LocationnotepadPO> poList = data.getLocationList();
for (LocationnotepadPO locationnotepadPO : poList) {
list.add(locationnotepadPO.getName());
}
return filter(latest, list);
}
@Override @Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
if (!(commandSender instanceof Player player)) {
return List.of();
}
String latest = null; String latest = null;
if (strings.length != 0) { if (strings.length != 0) {
latest = strings[strings.length - 1]; latest = strings[strings.length - 1];
} }
if (strings.length == 1) if (strings.length == 1)
return filter(latest, new ArrayList<>(TAB_1)); return filter(latest, new ArrayList<>(TAB_1));
if (strings.length == 2) {
if (strings[0].equals("remove")) {
return onRemoveTab(player, latest, strings);
}
}
return List.of(); return List.of();
} }

View File

@@ -11,6 +11,7 @@ import org.jooq.Result;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@@ -47,13 +48,29 @@ public class PlayerData {
ui.closeUI(); ui.closeUI();
} }
/// 删除一个地标
public boolean removeLocation(String name) throws SQLException {
var ctx = CoordinateRecorder.getDatabase().getDSL();
int rows = ctx.update(LOCATIONNOTEPAD).set(LOCATIONNOTEPAD.ISDELETE, 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 false;
}
locationListUpdate();
return true;
}
/// 将当前位置记录到坐标记录中 /// 将当前位置记录到坐标记录中
public boolean addLocation(String name, boolean isFixed) throws SQLException { public boolean addLocation(String name, boolean isFixed) throws SQLException {
var ctx = CoordinateRecorder.getDatabase().getDSL(); var ctx = CoordinateRecorder.getDatabase().getDSL();
Record record = ctx.select().from(LOCATIONNOTEPAD).where( Record record = ctx.select().from(LOCATIONNOTEPAD).where(
LOCATIONNOTEPAD.UID.eq(player.getUniqueId().toString()) LOCATIONNOTEPAD.UID.eq(player.getUniqueId().toString())
.and(LOCATIONNOTEPAD.NAME.eq(name)) .and(LOCATIONNOTEPAD.NAME.eq(name))
.and(LOCATIONNOTEPAD.ISDELETE.eq(false)) .and(LOCATIONNOTEPAD.ISDELETE.eq(false))
).fetchOne(); ).fetchOne();
if (record != null) { if (record != null) {
player.sendMessage(ChatColor.RED + "该名称的地点已经存在"); player.sendMessage(ChatColor.RED + "该名称的地点已经存在");
@@ -89,9 +106,9 @@ public class PlayerData {
if (record == null) { if (record == null) {
//设置不存在,新建一行 //设置不存在,新建一行
CoordinateRecorder.getDatabase().getDSL().insertInto(PLAYERSETTINGS) CoordinateRecorder.getDatabase().getDSL().insertInto(PLAYERSETTINGS)
.columns(PLAYERSETTINGS.UID) .columns(PLAYERSETTINGS.UID)
.values(player.getUniqueId().toString()) .values(player.getUniqueId().toString())
.execute(); .execute();
record = loadPlayerSettingsRecord(); record = loadPlayerSettingsRecord();
assert record != null; assert record != null;
} }
@@ -101,12 +118,17 @@ public class PlayerData {
/// 重新载入玩家的固定地点数据 /// 重新载入玩家的固定地点数据
public void locationListUpdate() throws SQLException { public void locationListUpdate() throws SQLException {
Result<Record> result = CoordinateRecorder.getDatabase().getDSL().select().from(LOCATIONNOTEPAD).where( Result<Record> result = CoordinateRecorder.getDatabase().getDSL().select().from(LOCATIONNOTEPAD).where(
LOCATIONNOTEPAD.UID.eq(player.getUniqueId().toString()).and(LOCATIONNOTEPAD.ISFIXED.eq(true)) LOCATIONNOTEPAD.UID.eq(player.getUniqueId().toString()).and(LOCATIONNOTEPAD.ISFIXED.eq(true))
.and(LOCATIONNOTEPAD.ISDELETE.eq(false))).fetch(); .and(LOCATIONNOTEPAD.ISDELETE.eq(false))).orderBy(LOCATIONNOTEPAD.ID.desc())
.limit(10).fetch();
locationList = result.into(LocationnotepadPO.class); locationList = result.into(LocationnotepadPO.class);
ui.flashLocations(); ui.flashLocations();
} }
public List<LocationnotepadPO> getLocationList() {
return Collections.unmodifiableList(this.locationList);
}
public void flashTime() { public void flashTime() {
ui.flashTime(); ui.flashTime();
} }

View File

@@ -148,10 +148,6 @@ public class ScoreboardUI {
locals.add(item); locals.add(item);
Score score = objective.getScore(item); Score score = objective.getScore(item);
score.setScore(locals.size()); score.setScore(locals.size());
//最多只允许展示20个条目
if (locals.size() > 20) {
break;
}
} }
} }