Handle creation, deletion, joining and leaving at runtime

This commit is contained in:
Daniel Saukel 2020-04-01 15:31:19 +02:00
parent 9727137675
commit b87f7ff7d2
3 changed files with 60 additions and 13 deletions

View File

@ -235,7 +235,7 @@ public abstract class GroupAdapter<T> {
*
* @param player the player
*/
public void syncPlayer(Player player) {
public void syncJoin(Player player) {
T eGroup = getExternalGroup(player);
PlayerGroup dGroup = dxl.getPlayerGroup(player);
@ -248,11 +248,49 @@ public abstract class GroupAdapter<T> {
dGroup.removePlayer(player, false);
return;
}
dGroup = getDungeonGroup(eGroup);
if (dGroup != null && !dGroup.getMembers().contains(player)) {
dGroup.addPlayer(player);
} else {
dGroup = createDungeonGroup(eGroup);
}
} else if (eGroup == null && dGroup != null) {
createExternalGroup(dGroup);
eGroup = getExternalGroup(dGroup);
if (eGroup == null) {
eGroup = createExternalGroup(dGroup);
}
if (!isExternalGroupMember(eGroup, player)) {
addExternalGroupMember(eGroup, player);
}
}
}
/**
* Returns if the player is a member of the external group.
*
* @param eGroup the external group
* @param player player
* @return if the player is a member of the external group
*/
public abstract boolean isExternalGroupMember(T eGroup, Player player);
/**
* Adds the member to the external group.
*
* @param eGroup the external group
* @param member the member
* @return if adding the member was successful
*/
public abstract boolean addExternalGroupMember(T eGroup, Player member);
/**
* Removes the member from the external group.
*
* @param eGroup the external group
* @param member the member
* @return if removing the player was successful
*/
public abstract boolean removeExternalGroupMember(T eGroup, Player member);
}

View File

@ -107,8 +107,6 @@ public class DGroup implements PlayerGroup {
floorCount = 0;
id = counter++;
plugin.getGroupAdapters().forEach(a -> a.syncPlayer(player));
}
public DGroup(DungeonsXL plugin, Player player, Dungeon dungeon) {
@ -145,8 +143,6 @@ public class DGroup implements PlayerGroup {
floorCount = 0;
id = counter++;
plugin.getGroupAdapters().forEach(a -> a.syncPlayer(captain));
}
// Getters and setters
@ -221,7 +217,7 @@ public class DGroup implements PlayerGroup {
players.add(player.getUniqueId());
}
plugin.getGroupAdapters().forEach(a -> a.syncPlayer(player));
plugin.getGroupAdapters().forEach(a -> a.syncJoin(player));
}
@Override
@ -248,7 +244,7 @@ public class DGroup implements PlayerGroup {
}
}
plugin.getGroupAdapters().forEach(a -> a.syncPlayer(player));
plugin.getGroupAdapters().forEach(a -> a.removeExternalGroupMember(a.getExternalGroup(player), player));
}
@Override

View File

@ -81,16 +81,25 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
return partiesAPI.getParty(pPlayer.getPartyName());
}
@Override
public boolean isExternalGroupMember(Party eGroup, Player player) {
if (eGroup == null) {
return false;
}
return eGroup.getMembers().contains(player.getUniqueId());
}
@Override
public boolean addExternalGroupMember(Party eGroup, Player member) {
return eGroup.addMember(getPartyPlayer(member));
}
public boolean removeExternalGroupMember(Player member) {
@Override
public boolean removeExternalGroupMember(Party eGroup, Player member) {
PartyPlayer pPlayer = getPartyPlayer(member);
if (pPlayer == null) {
return false;
}
Party eGroup = partiesAPI.getParty(pPlayer.getPartyName());
if (eGroup == null) {
return false;
}
@ -167,7 +176,7 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
new BukkitRunnable() {
@Override
public void run() {
syncPlayer(getPlayer(event.getPartyPlayer()));
syncJoin(getPlayer(event.getPartyPlayer()));
}
}.runTask(dxl);
}
@ -177,7 +186,11 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
new BukkitRunnable() {
@Override
public void run() {
syncPlayer(getPlayer(event.getPartyPlayer()));
Player player = getPlayer(event.getPartyPlayer());
PlayerGroup group = dxl.getPlayerGroup(player);
if (group != null) {
group.removePlayer(player);
}
}
}.runTask(dxl);
}