新增称号卡支持
All checks were successful
Java CI-CD with Maven / build (push) Successful in 25m27s

This commit is contained in:
zhangyuheng 2024-07-26 17:16:52 +08:00
parent 02d44610c6
commit 2981ac0183
8 changed files with 145 additions and 3 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.lunadeer</groupId>
<artifactId>MiniPlayerTitle</artifactId>
<version>4.5.0</version>
<version>4.6.0</version>
<packaging>jar</packaging>
<name>MiniPlayerTitle</name>

View File

@ -1,6 +1,7 @@
package cn.lunadeer.miniplayertitle;
import cn.lunadeer.miniplayertitle.commands.PlayerManage;
import cn.lunadeer.miniplayertitle.commands.TitleCard;
import cn.lunadeer.miniplayertitle.commands.TitleManage;
import cn.lunadeer.miniplayertitle.commands.TitleShopSale;
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
@ -81,6 +82,9 @@ public class Commands implements TabExecutor {
case "grant_title": // mplt grant_title <玩家名称> <称号> <描述> [天数]
PlayerManage.grantTitle(sender, args);
break;
case "get_card": // mplt get_card <商品ID>
TitleCard.getTitleCard(sender, args);
break;
default:
return false;
}
@ -93,7 +97,7 @@ public class Commands implements TabExecutor {
return Arrays.asList("menu", "all_titles", "my_titles", "shop", "custom_info", "sale_info",
"create_sale", "set_sale", "delete_sale", "buy_sale", "use_title", "create_title",
"delete_title", "edit_title_name", "edit_title_desc", "custom_title", "add_coin", "set_coin",
"grant_title"
"grant_title", "get_card"
);
}
if (args.length == 2) {
@ -101,6 +105,7 @@ public class Commands implements TabExecutor {
case "sale_info":
case "delete_sale":
case "buy_sale":
case "get_card":
return Collections.singletonList("<商品ID>");
case "create_sale":
case "delete_title":

View File

@ -4,6 +4,7 @@ import cn.lunadeer.minecraftpluginutils.*;
import cn.lunadeer.minecraftpluginutils.VaultConnect.VaultConnect;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseType;
import cn.lunadeer.miniplayertitle.commands.TitleCard;
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
import cn.lunadeer.miniplayertitle.events.Events;
import cn.lunadeer.miniplayertitle.events.PaperChat;
@ -47,6 +48,7 @@ public final class MiniPlayerTitle extends JavaPlugin {
}
Bukkit.getPluginManager().registerEvents(new Events(), this);
Bukkit.getPluginManager().registerEvents(new TitleCard(), this);
if (Common.isPaper()) {
Bukkit.getPluginManager().registerEvents(new PaperChat(), this);
} else {

View File

@ -0,0 +1,131 @@
package cn.lunadeer.miniplayertitle.commands;
import cn.lunadeer.minecraftpluginutils.Notification;
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
import cn.lunadeer.miniplayertitle.dtos.TitleShopDTO;
import cn.lunadeer.miniplayertitle.tuis.MyTitles;
import net.kyori.adventure.text.Component;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class TitleCard implements Listener {
public static void getTitleCard(CommandSender sender, String[] args) {
try {
if (!sender.hasPermission("mplt.admin")) return;
if (!(sender instanceof Player)) {
Notification.error(sender, "该命令只能由玩家执行");
return;
}
if (args.length != 2) {
Notification.warn(sender, "用法: /mplt get_card <销售ID>");
return;
}
int saleId = Integer.parseInt(args[1]);
TitleShopDTO titleShop = TitleShopDTO.get(saleId);
if (titleShop == null) {
Notification.error(sender, "获取销售详情时出现错误");
return;
}
ItemStack card = TitleCard.create(titleShop);
Player player = (Player) sender;
player.getInventory().addItem(card);
Notification.info(player, "成功创建称号卡");
} catch (Exception e) {
Notification.error(sender, e.getMessage());
}
}
@EventHandler
public void useTitleCard(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getInventory().getItemInMainHand().getType() != Material.NAME_TAG) {
return;
}
ItemStack item = player.getInventory().getItemInMainHand();
if (item.getItemMeta() == null || item.getItemMeta().getLore() == null || item.getItemMeta().getLore().size() != 5) {
return;
}
use(player, item);
player.getInventory().removeItem(item);
}
private static ItemStack create(@NotNull TitleShopDTO saleInfo) {
ItemStack card = new ItemStack(Material.NAME_TAG);
TitleDTO title = saleInfo.getTitle();
card.editMeta(meta -> {
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', title.getTitleColoredBukkit()));
meta.setLore(
Arrays.asList(
"称号ID: " + title.getId(),
"使用后获得天数: " + (saleInfo.getDays() == -1 ? "永久" : saleInfo.getDays()),
"称号描述: " + title.getDescription(),
"",
ChatColor.GRAY + "【右键使用】"
)
);
});
return card;
}
private static void use(@NotNull Player player, @NotNull ItemStack item) {
try {
if (item.getType() != Material.NAME_TAG) {
return;
}
List<String> lore = item.getLore();
if (lore == null || lore.size() != 5) {
return;
}
int titleId = Integer.parseInt(lore.get(0).split(": ")[1]);
int day = lore.get(1).split(": ")[1].equals("永久") ? -1 : Integer.parseInt(lore.get(1).split(": ")[1]);
List<PlayerTitleDTO> playerTitles = PlayerTitleDTO.getAllOf(player.getUniqueId());
PlayerTitleDTO had = null;
for (PlayerTitleDTO playerTitle : playerTitles) {
if (Objects.equals(playerTitle.getTitle().getId(), titleId)) {
had = playerTitle;
break;
}
}
TitleDTO title = TitleDTO.get(titleId);
if (title == null) {
Notification.error(player, "称号不存在");
return;
}
if (had == null) {
had = PlayerTitleDTO.create(player.getUniqueId(), title, day == -1 ? null : LocalDateTime.now().plusDays(day));
if (had == null) {
Notification.error(player, "购买称号时出现错误,详情请查看控制台日志");
return;
}
Notification.info(player, Component.text("成功使用称号卡: ").append(had.getTitle().getTitleColored()));
} else if (!had.isExpired()) {
Notification.warn(player, "你已拥有此称号,在过期前无法使用称号卡");
} else {
had.setExpireAt(day == -1 ? null : LocalDateTime.now().plusDays(day));
Notification.info(player, Component.text("成功续续期称号: ").append(had.getTitle().getTitleColored()));
}
TitleManage.useTitle(player, new String[]{"use_title", String.valueOf(had.getId())});
} catch (Exception e) {
Notification.error(player, "使用称号卡时出现错误: %s", e.getMessage());
}
}
}

View File

@ -5,11 +5,13 @@ import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
import cn.lunadeer.miniplayertitle.dtos.TitleShopDTO;
import cn.lunadeer.miniplayertitle.tuis.AllTitles;
import cn.lunadeer.miniplayertitle.tuis.MyTitles;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import static cn.lunadeer.miniplayertitle.commands.Apis.updateName;

View File

@ -194,6 +194,7 @@ public class TitleShopSale {
Notification.info(player, Component.text("成功续续期称号: ").append(had.getTitle().getTitleColored()));
}
TitleManage.useTitle(player, new String[]{"use_title", String.valueOf(had.getId())});
int page = getArgPage(args, 3);
MyTitles.show(sender, new String[]{"my_titles", String.valueOf(page)});
}

View File

@ -110,7 +110,7 @@ public class TitleDTO {
/**
* 获取称号的颜色化字符串
* 需要使用 translateAlternateColorCodes 方法对返回字符串进行处理
* 需要使用 ChatColor.translateAlternateColorCodes 方法对返回字符串进行处理
* &#FFFFFF -> &x&f&f&f&f&f
*
* @return String

View File

@ -122,6 +122,7 @@ public class SaleInfo {
}
if (player.hasPermission("mplt.admin")) {
operate.append(Button.create("删除").setExecuteCommand("/mplt delete_sale " + args[1] + " b").build());
operate.append(Button.createGreen("导出称号卡").setExecuteCommand("/mplt get_card " + args[1]).build());
}
view.add(Line.create().append("---------------------"));
view.add(operate);