From d6e9cf7209c4e53a3ebe5fce26c3060589462960 Mon Sep 17 00:00:00 2001 From: sleepymalc Date: Mon, 9 Aug 2021 01:59:10 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B[main/app]=20fix=20team=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++++ .../java/com/linyuanlin/minecraft/App.java | 35 +++++++++++-------- .../minecraft/models/PlayerData.java | 4 +-- .../com/linyuanlin/minecraft/models/Team.java | 13 +++---- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 7d7a7d9..82b4c79 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,12 @@ 2.4.0 provided + + + com.google.guava + guava + 28.0-jre + diff --git a/src/main/java/com/linyuanlin/minecraft/App.java b/src/main/java/com/linyuanlin/minecraft/App.java index ac779fa..e3a3cb0 100644 --- a/src/main/java/com/linyuanlin/minecraft/App.java +++ b/src/main/java/com/linyuanlin/minecraft/App.java @@ -135,7 +135,8 @@ public boolean onCommand(CommandSender sender, Command cmd, String cmdlable, Str case "invite": { Player p = Bukkit.getPlayer(args[1]); TextComponent a = new TextComponent("[確認組隊邀請]"); - a.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("點擊接受 " + sender.getName() + " 的組隊邀請 "))); + a.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, + new Text("點擊接受 " + sender.getName() + " 的組隊邀請 "))); a.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/team join " + args[1])); p.spigot().sendMessage(a); break; @@ -143,19 +144,23 @@ public boolean onCommand(CommandSender sender, Command cmd, String cmdlable, Str case "join": { Player p = Bukkit.getPlayer(args[1]); TextComponent msg = new TextComponent(""); - if (allPlayers.get(p1.getUniqueId()).team.isEmpty()) { + if (allPlayers.get(p1.getUniqueId()).team.isPresent()) { List playerArray = new ArrayList<>(); playerArray.add(allPlayers.get(p1.getUniqueId())); playerArray.add(allPlayers.get(p.getUniqueId())); - Team team = new Team(playerArray); + Optional team = Optional.of(new Team(playerArray)); allPlayers.get(p1.getUniqueId()).team = team; allPlayers.get(p.getUniqueId()).team = team; - team.leader = allPlayers.get(p1.getUniqueId()); - msg = new TextComponent(p.getName() + "已加入" + "(" + allPlayers.get(p1.getUniqueId()).team.size() + "/4"); - } else if (!allPlayers.get(p1.getUniqueId()).team.isFull()) { - allPlayers.get(p1.getUniqueId()).team.playerList.add(allPlayers.get(Bukkit.getPlayer(args[0]).getUniqueId())); - allPlayers.get(Bukkit.getPlayer(p.getName()).getUniqueId()).team = allPlayers.get(p1.getUniqueId()).team; - msg = new TextComponent(p.getName() + "已加入" + "(" + allPlayers.get(p1.getUniqueId()).team.size() + "/4"); + team.get().leader = allPlayers.get(p1.getUniqueId()); + msg = new TextComponent( + p.getName() + "已加入" + "(" + allPlayers.get(p1.getUniqueId()).team.get().size() + "/4"); + } else if (!allPlayers.get(p1.getUniqueId()).team.get().isFull()) { + allPlayers.get(p1.getUniqueId()).team.get().playerList + .add(allPlayers.get(Bukkit.getPlayer(args[0]).getUniqueId())); + allPlayers.get(Bukkit.getPlayer(p.getName()).getUniqueId()).team = allPlayers + .get(p1.getUniqueId()).team; + msg = new TextComponent( + p.getName() + "已加入" + "(" + allPlayers.get(p1.getUniqueId()).team.get().size() + "/4"); } else { msg = new TextComponent("隊伍已滿"); } @@ -165,16 +170,18 @@ public boolean onCommand(CommandSender sender, Command cmd, String cmdlable, Str } case "list": { StringBuilder teamMemberNameString = new StringBuilder(); - for (int i = 0; i < allPlayers.get(p1.getUniqueId()).team.size(); i++) { - teamMemberNameString.append(allPlayers.get(p1.getUniqueId()).team.playerList.get(i).player.getName()).append(" "); + for (int i = 0; i < allPlayers.get(p1.getUniqueId()).team.get().size(); i++) { + teamMemberNameString + .append(allPlayers.get(p1.getUniqueId()).team.get().playerList.get(i).player.getName()) + .append(" "); } TextComponent msg = new TextComponent("隊伍成員:" + teamMemberNameString); - for (int i = 0; i < allPlayers.get(p1.getUniqueId()).team.size(); i++) { - allPlayers.get(p1.getUniqueId()).team.playerList.get(i).player.spigot().sendMessage(msg); + for (int i = 0; i < allPlayers.get(p1.getUniqueId()).team.get().size(); i++) { + allPlayers.get(p1.getUniqueId()).team.get().playerList.get(i).player.spigot().sendMessage(msg); } } - break; + break; default: break; } diff --git a/src/main/java/com/linyuanlin/minecraft/models/PlayerData.java b/src/main/java/com/linyuanlin/minecraft/models/PlayerData.java index d37f29f..4bc77fa 100644 --- a/src/main/java/com/linyuanlin/minecraft/models/PlayerData.java +++ b/src/main/java/com/linyuanlin/minecraft/models/PlayerData.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.UUID; +import java.util.Optional; public class PlayerData { @@ -14,7 +15,7 @@ public class PlayerData { public int balance = 0; - public Team team; + public Optional team; public PlayerData(UUID uuid) throws Exception { @@ -34,5 +35,4 @@ public void saveData() { player.sendMessage(ChatColor.GRAY + "你的資料已自動保存至資料庫"); } - } diff --git a/src/main/java/com/linyuanlin/minecraft/models/Team.java b/src/main/java/com/linyuanlin/minecraft/models/Team.java index a5f1385..9ef2094 100644 --- a/src/main/java/com/linyuanlin/minecraft/models/Team.java +++ b/src/main/java/com/linyuanlin/minecraft/models/Team.java @@ -1,4 +1,5 @@ package com.linyuanlin.minecraft.models; + import net.md_5.bungee.api.ChatMessageType; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -11,20 +12,20 @@ public class Team { public List playerList; public PlayerData leader; - public Team(List playerList){ + public Team(List playerList) { this.playerList = playerList; this.leader = playerList.get(0); } - public boolean isEmpty(){ - return this.playerList.size() == 0 ? true : false; + public boolean isEmpty() { + return this.playerList.size() == 0; } - public boolean isFull(){ - return this.playerList.size() == 4 ? true : false; + public boolean isFull() { + return this.playerList.size() == 4; } - public int size(){ + public int size() { return this.playerList.size(); } }