Skip to content

Commit

Permalink
实现了卫星地图自定义色块功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdeZhang committed Jun 17, 2024
1 parent 7328e9a commit 4bd817b
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.lunadeer</groupId>
<artifactId>Dominion</artifactId>
<version>1.31.5-beta</version>
<version>1.31.6-beta</version>
<packaging>jar</packaging>

<name>Dominion</name>
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/cn/lunadeer/dominion/BlueMapConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public static void render() {
double z = vectors.iterator().next().getY();
double y = dominion.getY1();

Color line = new Color(0, 191, 255, 0.8F);
Color fill = new Color(0, 191, 255, 0.2F);
if (dominion.getParentDomId() != -1) { // for children dominion
line = new Color(240, 230, 140, 0.8F);
fill = new Color(240, 230, 140, 0.2F);
}
int r = dominion.getColorR();
int g = dominion.getColorG();
int b = dominion.getColorB();

Color line = new Color(r, g, b, 0.8F);
Color fill = new Color(r, g, b, 0.2F);
ExtrudeMarker marker = ExtrudeMarker.builder()
.label(dominion.getName())
.position(x, y, z)
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/cn/lunadeer/dominion/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
case "select_template":
SelectTemplate.show(sender, args);
break;
case "set_map_color":
DominionOperate.setMapColor(sender, args);
break;
// ---=== CUI ===---
case "cui_rename":
RenameDominion.open(sender, args);
Expand All @@ -193,6 +196,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
case "cui_template_create":
CreateTemplate.open(sender, args);
break;
case "cui_set_map_color":
SetMapColor.open(sender, args);
break;
default:
return false;
}
Expand Down Expand Up @@ -233,7 +239,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
"template_manage",
"template_delete",
"template_create",
"template_set_flag"
"template_set_flag",
"all_dominion",
"set_map_color"
);
}
if (args.length == 2) {
Expand Down Expand Up @@ -281,6 +289,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return allTemplates(sender);
case "template_create":
return Collections.singletonList("输入模板名称");
case "set_map_color":
return Collections.singletonList("输入颜色(16进制)");
}
}
if (args.length == 3) {
Expand All @@ -300,6 +310,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
case "set_enter_msg":
case "set_leave_msg":
case "apply_template":
case "set_map_color":
return playerDominions(sender);
case "rename":
return Collections.singletonList("输入新领地名称");
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/cn/lunadeer/dominion/commands/DominionOperate.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,25 @@ public static void teleportToDominion(CommandSender sender, String[] args) {
}
}, 20L * Dominion.config.getTpDelay());
}

/**
* 设置领地卫星地图地块颜色
*
* @param sender 命令发送者
* @param args 命令参数
*/
public static void setMapColor(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
if (args.length < 2) {
Notification.error(sender, "用法: /dominion set_map_color <颜色> [领地名称]");
return;
}
BukkitPlayerOperator operator = BukkitPlayerOperator.create(player);
if (args.length == 2) {
DominionController.setMapColor(operator, args[1]);
} else {
DominionController.setMapColor(operator, args[1], args[2]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,48 @@ public static void give(AbstractOperator operator, String dom_name, String playe
operator.setResponse(new AbstractOperator.Result(AbstractOperator.Result.SUCCESS, "成功将领地 %s 及其所有子领地转让给 %s", dom_name, player_name));
}

/**
* 设置领地的卫星地图地块颜色
*
* @param operator 操作者
* @param color 16进制颜色 例如 #ff0000
* @param dom_name 领地名称
*/
public static void setMapColor(AbstractOperator operator, String color, String dom_name) {
AbstractOperator.Result FAIL = new AbstractOperator.Result(AbstractOperator.Result.FAILURE, "设置领地地图颜色失败");
DominionDTO dominion = getExistDomAndIsOwner(operator, dom_name);
if (dominion == null) {
return;
}
if (notOwner(operator, dominion)) {
operator.setResponse(FAIL.addMessage("你不是领地 %s 的拥有者", dom_name));
return;
}
color = color.toUpperCase(); // 转换为大写
if (!color.matches("^#[0-9a-fA-F]{6}$")) {
operator.setResponse(FAIL.addMessage("颜色格式不正确"));
return;
}
dominion.setColor(color);
operator.setResponse(new AbstractOperator.Result(AbstractOperator.Result.SUCCESS, "成功设置领地 %s 的卫星地图颜色为 %s", dom_name, color));
}

/**
* 设置领地的卫星地图地块颜色
*
* @param operator 操作者
* @param color 16进制颜色 例如 #ff0000
*/
public static void setMapColor(AbstractOperator operator, String color) {
AbstractOperator.Result FAIL = new AbstractOperator.Result(AbstractOperator.Result.FAILURE, "设置领地地图颜色失败");
DominionDTO dominion = getPlayerCurrentDominion(operator);
if (dominion == null) {
operator.setResponse(FAIL.addMessage("无法获取你所处的领地,请指定名称"));
return;
}
setMapColor(operator, color, dominion.getName());
}

/**
* 判断两个领地是否相交
*/
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/cn/lunadeer/dominion/cuis/SetMapColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cn.lunadeer.dominion.cuis;

import cn.lunadeer.dominion.controllers.BukkitPlayerOperator;
import cn.lunadeer.dominion.controllers.DominionController;
import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.dominion.tuis.DominionManage;
import cn.lunadeer.minecraftpluginutils.Notification;
import cn.lunadeer.minecraftpluginutils.XLogger;
import cn.lunadeer.minecraftpluginutils.scui.CuiTextInput;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import static cn.lunadeer.dominion.commands.Apis.playerOnly;

public class SetMapColor {

private static class setMapColorCB implements CuiTextInput.InputCallback {
private final Player sender;
private final String dominionName;

public setMapColorCB(Player sender, String dominionName) {
this.sender = sender;
this.dominionName = dominionName;
}

@Override
public void handleData(String input) {
XLogger.debug("editLeaveMessageCB.run: %s", input);
BukkitPlayerOperator operator = BukkitPlayerOperator.create(sender);
DominionController.setMapColor(operator, input, dominionName);
DominionManage.show(sender, new String[]{"manage", dominionName});
}
}

public static void open(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
DominionDTO dominion = DominionDTO.select(args[1]);
if (dominion == null) {
Notification.error(sender, "领地不存在");
return;
}
CuiTextInput.InputCallback setMapColorCB = new SetMapColor.setMapColorCB(player, dominion.getName());
CuiTextInput view = CuiTextInput.create(setMapColorCB).setText(dominion.getColor()).title("输入卫星地图地块颜色(16进制)");
view.setSuggestCommand("/dominion set_map_color <颜色> [领地名称]");
view.open(player);
}

}
46 changes: 37 additions & 9 deletions src/main/java/cn/lunadeer/dominion/dtos/DominionDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.bukkit.Location;
import org.bukkit.World;

import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.List;

public class DominionDTO {
private static List<DominionDTO> query(String sql, Object... args) {
Expand Down Expand Up @@ -36,12 +38,14 @@ private static List<DominionDTO> query(String sql, Object... args) {
for (Flag f : Flag.getDominionFlagsEnabled()) {
flags.put(f, rs.getBoolean(f.getFlagName()));
}
String color = rs.getString("color");

DominionDTO dominion = new DominionDTO(id, owner, name, world, x1, y1, z1, x2, y2, z2, parentDomId,
rs.getString("join_message"),
rs.getString("leave_message"),
flags,
tp_location
tp_location,
color
);
dominions.add(dominion);
}
Expand Down Expand Up @@ -148,7 +152,7 @@ private static DominionDTO update(DominionDTO dominion) {
Location loc = dominion.getTpLocation();
tp_location = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ();
}
String sql = "UPDATE dominion SET " +
StringBuilder sql = new StringBuilder("UPDATE dominion SET " +
"owner = ?," +
"name = ?," +
"world = ?," +
Expand All @@ -160,19 +164,19 @@ private static DominionDTO update(DominionDTO dominion) {
"z2 = " + dominion.getZ2() + ", " +
"parent_dom_id = " + dominion.getParentDomId() + ", " +
"join_message = ?," +
"leave_message = ?,";
"leave_message = ?," +
"color = ?,");
for (Flag f : Flag.getDominionFlagsEnabled()) {
sql += f.getFlagName() + " = " + dominion.getFlagValue(f) + ",";
sql.append(f.getFlagName()).append(" = ").append(dominion.getFlagValue(f)).append(",");
}
sql += "tp_location = ?" +
" WHERE id = " + dominion.getId() +
" RETURNING *;";
List<DominionDTO> dominions = query(sql,
sql.append("tp_location = ?" + " WHERE id = ").append(dominion.getId()).append(" RETURNING *;");
List<DominionDTO> dominions = query(sql.toString(),
dominion.getOwner().toString(),
dominion.getName(),
dominion.getWorld(),
dominion.getJoinMessage(),
dominion.getLeaveMessage(),
dominion.getColor(),
tp_location);
if (dominions.size() == 0) return null;
return dominions.get(0);
Expand All @@ -183,7 +187,8 @@ private DominionDTO(Integer id, UUID owner, String name, String world,
Integer parentDomId,
String joinMessage, String leaveMessage,
Map<Flag, Boolean> flags,
String tp_location) {
String tp_location,
String color) {
this.id = id;
this.owner = owner;
this.name = name;
Expand All @@ -199,6 +204,7 @@ private DominionDTO(Integer id, UUID owner, String name, String world,
this.leaveMessage = leaveMessage;
this.flags.putAll(flags);
this.tp_location = tp_location;
this.color = color;
}


Expand Down Expand Up @@ -238,6 +244,7 @@ public DominionDTO(UUID owner, String name, String world,
private String leaveMessage = "再见";
private final Map<Flag, Boolean> flags = new HashMap<>();
private String tp_location;
private String color;

// getters and setters
public Integer getId() {
Expand Down Expand Up @@ -421,4 +428,25 @@ public Location getLocation1() {
public Location getLocation2() {
return new Location(Dominion.instance.getServer().getWorld(world), x2, y2, z2);
}

public DominionDTO setColor(String color) {
this.color = color;
return update(this);
}

public int getColorR() {
return Integer.valueOf(color.substring(1, 3), 16);
}

public int getColorG() {
return Integer.valueOf(color.substring(3, 5), 16);
}

public int getColorB() {
return Integer.valueOf(color.substring(5, 7), 16);
}

public String getColor() {
return color;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@ public static void migrate() {
flag.getFlagName(),
"BOOLEAN NOT NULL DEFAULT " + flag.getDefaultValue());
}

// 1.31.6
Dominion.database.addColumnIfNotExists("dominion", "color", "TEXT NOT NULL DEFAULT '#00BFFF'");
}
}
15 changes: 13 additions & 2 deletions src/main/java/cn/lunadeer/dominion/tuis/DominionManage.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cn.lunadeer.dominion.tuis;

import cn.lunadeer.dominion.Dominion;
import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.minecraftpluginutils.Notification;
import cn.lunadeer.minecraftpluginutils.stui.ListView;
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -43,6 +46,11 @@ public static void show(CommandSender sender, String[] args) {
Line leave_msg = Line.create()
.append(Button.create("编辑离开提示语").setExecuteCommand("/dominion cui_edit_leave_message " + dominion.getName()).build())
.append("当玩家离开领地时显示的消息");
Line map_color = Line.create()
.append(Button.create("设置颜色").setExecuteCommand("/dominion cui_set_map_color " + dominion.getName()).build())
.append(Component.text("设置卫星地图上的地块颜色")
.append(Component.text(dominion.getColor(),
TextColor.color(dominion.getColorR(), dominion.getColorG(), dominion.getColorB()))));
ListView view = ListView.create(10, "/dominion manage " + dominion.getName());
view.title("领地 " + dominion.getName() + " 管理界面")
.navigator(Line.create()
Expand All @@ -55,7 +63,10 @@ public static void show(CommandSender sender, String[] args) {
.add(set_tp)
.add(rename)
.add(join_msg)
.add(leave_msg)
.showOn(player, 1);
.add(leave_msg);
if (Dominion.config.getBlueMap()) {
view.add(map_color);
}
view.showOn(player, 1);
}
}

0 comments on commit 4bd817b

Please sign in to comment.