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 * @param player the player
*/ */
public void syncPlayer(Player player) { public void syncJoin(Player player) {
T eGroup = getExternalGroup(player); T eGroup = getExternalGroup(player);
PlayerGroup dGroup = dxl.getPlayerGroup(player); PlayerGroup dGroup = dxl.getPlayerGroup(player);
@ -248,11 +248,49 @@ public abstract class GroupAdapter<T> {
dGroup.removePlayer(player, false); dGroup.removePlayer(player, false);
return; return;
} }
dGroup = createDungeonGroup(eGroup); dGroup = getDungeonGroup(eGroup);
if (dGroup != null && !dGroup.getMembers().contains(player)) {
dGroup.addPlayer(player);
} else {
dGroup = createDungeonGroup(eGroup);
}
} else if (eGroup == null && dGroup != null) { } 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; floorCount = 0;
id = counter++; id = counter++;
plugin.getGroupAdapters().forEach(a -> a.syncPlayer(player));
} }
public DGroup(DungeonsXL plugin, Player player, Dungeon dungeon) { public DGroup(DungeonsXL plugin, Player player, Dungeon dungeon) {
@ -145,8 +143,6 @@ public class DGroup implements PlayerGroup {
floorCount = 0; floorCount = 0;
id = counter++; id = counter++;
plugin.getGroupAdapters().forEach(a -> a.syncPlayer(captain));
} }
// Getters and setters // Getters and setters
@ -221,7 +217,7 @@ public class DGroup implements PlayerGroup {
players.add(player.getUniqueId()); players.add(player.getUniqueId());
} }
plugin.getGroupAdapters().forEach(a -> a.syncPlayer(player)); plugin.getGroupAdapters().forEach(a -> a.syncJoin(player));
} }
@Override @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 @Override

View File

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