From e2d3417072219d935bc69536c1e7499a7b50af23 Mon Sep 17 00:00:00 2001 From: Luca Date: Sun, 21 Nov 2021 20:54:26 +0100 Subject: [PATCH] Party? --- src/main/java/rip/tilly/bedwars/BedWars.java | 3 + .../bedwars/listeners/InteractListener.java | 2 +- .../bedwars/managers/PlayerDataManager.java | 2 +- .../tilly/bedwars/managers/party/Party.java | 123 +++++++++++++++++ .../bedwars/managers/party/PartyManager.java | 128 ++++++++++++++++++ 5 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 src/main/java/rip/tilly/bedwars/managers/party/Party.java create mode 100644 src/main/java/rip/tilly/bedwars/managers/party/PartyManager.java diff --git a/src/main/java/rip/tilly/bedwars/BedWars.java b/src/main/java/rip/tilly/bedwars/BedWars.java index 3975ca2..4995c6e 100644 --- a/src/main/java/rip/tilly/bedwars/BedWars.java +++ b/src/main/java/rip/tilly/bedwars/BedWars.java @@ -19,6 +19,7 @@ import rip.tilly.bedwars.managers.arena.chunk.ChunkClearingManager; import rip.tilly.bedwars.managers.arena.chunk.ChunkManager; import rip.tilly.bedwars.managers.hotbar.HotbarManager; import rip.tilly.bedwars.managers.mongo.MongoManager; +import rip.tilly.bedwars.managers.party.PartyManager; import rip.tilly.bedwars.providers.scoreboard.ScoreboardProvider; import rip.tilly.bedwars.utils.CC; import rip.tilly.bedwars.utils.assemble.Assemble; @@ -47,6 +48,7 @@ public final class BedWars extends JavaPlugin { private HotbarManager hotbarManager; private CommandManager commandManager; private GameManager gameManager; + private PartyManager partyManager; @Override public void onEnable() { @@ -108,6 +110,7 @@ public final class BedWars extends JavaPlugin { this.hotbarManager = new HotbarManager(); this.commandManager = new CommandManager(); this.gameManager = new GameManager(); + this.partyManager = new PartyManager(); } private void loadListeners() { diff --git a/src/main/java/rip/tilly/bedwars/listeners/InteractListener.java b/src/main/java/rip/tilly/bedwars/listeners/InteractListener.java index 91a1930..5f9471d 100644 --- a/src/main/java/rip/tilly/bedwars/listeners/InteractListener.java +++ b/src/main/java/rip/tilly/bedwars/listeners/InteractListener.java @@ -78,7 +78,7 @@ public class InteractListener implements Listener { // open settings menu break; case CREATE_PARTY: - // create party + this.plugin.getPartyManager().createParty(player); break; case COSMETICS_MENU: // open cosmetics menu diff --git a/src/main/java/rip/tilly/bedwars/managers/PlayerDataManager.java b/src/main/java/rip/tilly/bedwars/managers/PlayerDataManager.java index 0e92a26..4917df9 100644 --- a/src/main/java/rip/tilly/bedwars/managers/PlayerDataManager.java +++ b/src/main/java/rip/tilly/bedwars/managers/PlayerDataManager.java @@ -97,7 +97,7 @@ public class PlayerDataManager { } public void giveSpawnItems(Player player) { - boolean inParty = false; // check if the player is in a party + boolean inParty = this.plugin.getPartyManager().getParty(player.getUniqueId()) != null; if (inParty) { this.plugin.getHotbarManager().getPartyItems().stream().filter(HotbarItem::isEnabled).forEach(item -> player.getInventory().setItem(item.getSlot(), item.getItemStack())); diff --git a/src/main/java/rip/tilly/bedwars/managers/party/Party.java b/src/main/java/rip/tilly/bedwars/managers/party/Party.java new file mode 100644 index 0000000..1ef8e60 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/managers/party/Party.java @@ -0,0 +1,123 @@ +package rip.tilly.bedwars.managers.party; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.Player; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.game.GameTeam; +import rip.tilly.bedwars.player.PlayerTeam; +import sun.misc.UUDecoder; + +import java.util.*; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Getter +@Setter +public class Party { + + private final BedWars plugin = BedWars.getInstance(); + + private UUID leader; + private Set members = new HashSet<>(); + private int limit = 8; + private boolean open; + + private List teams = new ArrayList<>(); + + public Party(UUID leader) { + this.leader = leader; + this.members.add(leader); + } + + public void addMember(UUID uuid) { + this.members.add(uuid); + } + + public void removeMember(UUID uuid) { + this.members.remove(uuid); + } + + public void broadcast(String message) { + this.members().forEach(member -> member.sendMessage(message)); + } + + private GameTeam findTeam(UUID uuid) { + GameTeam team = null; + for (GameTeam gameTeams : teams) { + if (gameTeams.getAllPlayers().contains(uuid)) { + team = gameTeams; + } + } + + return team; + } + + public GameTeam findOpponent(UUID uuid) { + GameTeam team = null; + for (GameTeam gameTeams : teams) { + if (!gameTeams.getAllPlayers().contains(uuid)) { + team = gameTeams; + } + } + + return team; + } + + public List getPartySplitTeam(UUID uuid) { + List uuids = new ArrayList<>(); + for (UUID pUuid : this.findTeam(uuid).getPlayingPlayers()) { + if (pUuid != uuid) { + uuids.add(pUuid); + } + } + + return uuids; + } + + public GameTeam[] split() { + teams.clear(); + + List teamA = new ArrayList<>(); + List teamB = new ArrayList<>(); + + ThreadLocalRandom random = ThreadLocalRandom.current(); + + for (UUID member : this.members) { + if (teamA.size() == teamB.size()) { + if (random.nextBoolean()) { + teamA.add(member); + } else { + teamB.add(member); + } + } else { + if (teamA.size() < teamB.size()) { + teamA.add(member); + } else { + teamB.add(member); + } + } + } + + GameTeam team1 = new GameTeam(teamA.get(0), teamA, 0, PlayerTeam.RED); + GameTeam team2 = new GameTeam(teamB.get(0), teamB, 1, PlayerTeam.LIME); + + teams.add(team1); + teams.add(team2); + + return new GameTeam[]{team1, team2}; + } + + public List getPartyMembersExcludeMember(UUID uuid) { + return members.stream().filter(m -> m != uuid).collect(Collectors.toList()); + } + + public List getPartyMembersExcludeLeader() { + return members.stream().filter(m -> !this.leader.equals(m)).collect(Collectors.toList()); + } + + public Stream members() { + return this.members.stream().map(this.plugin.getServer()::getPlayer).filter(Objects::nonNull); + } +} diff --git a/src/main/java/rip/tilly/bedwars/managers/party/PartyManager.java b/src/main/java/rip/tilly/bedwars/managers/party/PartyManager.java new file mode 100644 index 0000000..6bab269 --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/managers/party/PartyManager.java @@ -0,0 +1,128 @@ +package rip.tilly.bedwars.managers.party; + +import lombok.Getter; +import org.bukkit.entity.Player; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.player.PlayerData; +import rip.tilly.bedwars.player.PlayerState; +import rip.tilly.bedwars.utils.CC; +import rip.tilly.bedwars.utils.TtlHashMap; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +public class PartyManager { + + private final BedWars plugin = BedWars.getInstance(); + + @Getter private Map parties = new ConcurrentHashMap<>(); + private Map partyLeaders = new ConcurrentHashMap<>(); + private Map> partyInvites = new TtlHashMap<>(TimeUnit.SECONDS, 15); + + public boolean isLeader(UUID uuid) { + return this.parties.containsKey(uuid); + } + + public Party getParty(UUID player) { + if (this.parties.containsKey(player)) { + return this.parties.get(player); + } + + if (this.partyLeaders.containsKey(player)) { + UUID leader = this.partyLeaders.get(player); + return this.parties.get(leader); + } + + return null; + } + + public void removePartyInvites(UUID uuid) { + this.partyInvites.remove(uuid); + } + + public boolean hasPartyInvite(UUID player, UUID other) { + return this.partyInvites.get(player) != null && this.partyInvites.get(player).contains(other); + } + + public void createPartyInvite(UUID requester, UUID requested) { + this.partyInvites.computeIfAbsent(requested, k -> new ArrayList<>()).add(requester); + } + + public boolean isInParty(UUID player, Party party) { + Party targetParty = this.getParty(player); + return targetParty != null && targetParty.getLeader() == party.getLeader(); + } + + public Party getPartyByLeader(UUID uuid) { + if (this.partyLeaders.containsKey(uuid)) { + UUID leader = this.partyLeaders.get(uuid); + return this.parties.get(leader); + } + + return null; + } + + public void createParty(Player player) { + Party party = new Party(player.getUniqueId()); + this.parties.put(player.getUniqueId(), party); + this.plugin.getPlayerDataManager().resetPlayer(player, false); + + player.sendMessage(CC.translate("Successfully created a party!")); + } + + private void disbandParty(Party party) { + this.parties.remove(party.getLeader()); + + party.broadcast(CC.translate("&cThe party has been disbanded!")); + + party.members().forEach(member -> { + PlayerData memberData = this.plugin.getPlayerDataManager().getPlayerData(member.getUniqueId()); + if (this.partyLeaders.get(memberData.getUniqueId()) != null) { + this.partyLeaders.remove(memberData.getUniqueId()); + } + + if (memberData.getPlayerState() == PlayerState.SPAWN) { + this.plugin.getPlayerDataManager().resetPlayer(member, false); + } + }); + } + + public void joinParty(UUID leader, Player player) { + Party party = this.getParty(leader); + this.partyLeaders.put(player.getUniqueId(), leader); + party.addMember(player.getUniqueId()); + this.plugin.getPlayerDataManager().resetPlayer(player, false); + party.broadcast(CC.translate("&a" + player.getName() + " &ehas joined the party!")); + } + + public void leaveParty(Player player) { + Party party = this.getParty(player.getUniqueId()); + if (party == null) { + return; + } + + PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId()); + if (this.parties.containsKey(player.getUniqueId())) { + this.disbandParty(party); + } else { + party.broadcast(CC.translate("&a" + player.getName() + " &ehas left the party!")); + party.removeMember(player.getUniqueId()); + this.partyLeaders.remove(player.getUniqueId()); + } + + switch (playerData.getPlayerState()) { + case PLAYING: + this.plugin.getGameManager().removePlayerFromGame(player, playerData); + break; + case SPECTATING: + // this.plugin.getGameManager().removeSpectator(player); + break; + } + + this.plugin.getPlayerDataManager().resetPlayer(player, false); + } +}