Party?
This commit is contained in:
parent
27b010fced
commit
e2d3417072
@ -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() {
|
||||
|
@ -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
|
||||
|
@ -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()));
|
||||
|
123
src/main/java/rip/tilly/bedwars/managers/party/Party.java
Normal file
123
src/main/java/rip/tilly/bedwars/managers/party/Party.java
Normal file
@ -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<UUID> members = new HashSet<>();
|
||||
private int limit = 8;
|
||||
private boolean open;
|
||||
|
||||
private List<GameTeam> 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<UUID> getPartySplitTeam(UUID uuid) {
|
||||
List<UUID> uuids = new ArrayList<>();
|
||||
for (UUID pUuid : this.findTeam(uuid).getPlayingPlayers()) {
|
||||
if (pUuid != uuid) {
|
||||
uuids.add(pUuid);
|
||||
}
|
||||
}
|
||||
|
||||
return uuids;
|
||||
}
|
||||
|
||||
public GameTeam[] split() {
|
||||
teams.clear();
|
||||
|
||||
List<UUID> teamA = new ArrayList<>();
|
||||
List<UUID> 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<UUID> getPartyMembersExcludeMember(UUID uuid) {
|
||||
return members.stream().filter(m -> m != uuid).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<UUID> getPartyMembersExcludeLeader() {
|
||||
return members.stream().filter(m -> !this.leader.equals(m)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Stream<Player> members() {
|
||||
return this.members.stream().map(this.plugin.getServer()::getPlayer).filter(Objects::nonNull);
|
||||
}
|
||||
}
|
128
src/main/java/rip/tilly/bedwars/managers/party/PartyManager.java
Normal file
128
src/main/java/rip/tilly/bedwars/managers/party/PartyManager.java
Normal file
@ -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<UUID, Party> parties = new ConcurrentHashMap<>();
|
||||
private Map<UUID, UUID> partyLeaders = new ConcurrentHashMap<>();
|
||||
private Map<UUID, List<UUID>> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user