Parties integration (WIP); #1043

This commit is contained in:
Daniel Saukel 2021-08-21 23:18:38 +02:00
parent 98dda3f946
commit d32b8ad681
7 changed files with 107 additions and 282 deletions

View File

@ -30,80 +30,14 @@ import org.bukkit.entity.Player;
*/
public abstract class GroupAdapter<T> {
/**
* How the implementation handles players.
*/
public enum Philosophy {
/**
* The group persists upon restarts.
* <p>
* DungeonsXL under no circumstances creates persistent external groups.
*/
PERSISTENT,
/**
* The group continues to exist as long as the server is running, but does not persist upon restarts.
*/
RUNTIME,
/**
* Players are kicked from the group when they leave the server.
*/
ONLINE
}
public class ExternalGroupData<T> {
private T eGroup;
private boolean createdByDXL;
public ExternalGroupData(T eGroup, boolean createdByDXL) {
this.eGroup = eGroup;
this.createdByDXL = createdByDXL;
}
/**
* Returns the wrapped external group object.
*
* @return the wrapped external group object
*/
public T get() {
return eGroup;
}
/**
* Returns if the external group was created by DungeonsXL.
* <p>
* Groups may be created by DungeonsXL, for example through a command, a group sign or automatically if a dungeon is entered.
* The integration implementation should give dungeon groups equivalent groups from the external group plugin.
* External groups created to mirror dungeon groups should be removed when their dungeon group is deleted, but those created intentionally should not.
*
* @return if the external group was created by DungeonsXL.
*/
public boolean isCreatedByDXL() {
return createdByDXL;
}
}
protected DungeonsAPI dxl;
private Philosophy philosophy;
protected Map<PlayerGroup, ExternalGroupData<T>> groups = new HashMap<>();
protected Map<PlayerGroup, T> groups = new HashMap<>();
/**
* @param dxl the DungeonsAPI instance
* @param philosophy the player handling philosophy
* @param dxl the DungeonsAPI instance
*/
protected GroupAdapter(DungeonsAPI dxl, Philosophy philosophy) {
protected GroupAdapter(DungeonsAPI dxl) {
this.dxl = dxl;
this.philosophy = philosophy;
}
/**
* Returns the player handling philosophy.
*
* @return the player handling philosophy
*/
public Philosophy getPhilosophy() {
return philosophy;
}
/**
@ -114,14 +48,6 @@ public abstract class GroupAdapter<T> {
*/
public abstract PlayerGroup createDungeonGroup(T eGroup);
/**
* Creates an external group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the dungeon group.
*
* @param dGroup the dungeon group
* @return an external group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the dungeon group
*/
public abstract T createExternalGroup(PlayerGroup dGroup);
/**
* Returns the dungeon group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the external group or null of none exists.
*
@ -129,8 +55,11 @@ public abstract class GroupAdapter<T> {
* @return the dungeon group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the external group
*/
public PlayerGroup getDungeonGroup(T eGroup) {
for (Entry<PlayerGroup, ExternalGroupData<T>> entry : groups.entrySet()) {
if (entry.getValue().get().equals(eGroup)) {
if (eGroup == null) {
return null;
}
for (Entry<PlayerGroup, T> entry : groups.entrySet()) {
if (entry.getValue().equals(eGroup)) {
return entry.getKey();
}
}
@ -144,8 +73,27 @@ public abstract class GroupAdapter<T> {
* @return the external group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the dungeon group
*/
public T getExternalGroup(PlayerGroup dGroup) {
ExternalGroupData<T> data = groups.get(dGroup);
return data != null ? data.get() : null;
return groups.get(dGroup);
}
/**
* Returns the dungeon group that mirrors the external group.
* <p>
* Creates a dungeon group if none exists and if the party has no more online members than maxSize.
*
* @param eGroup the dungeon group
* @param maxSize the maximum size of the group
* @return the dungeon group that mirrors the dungeon group
*/
public PlayerGroup getOrCreateDungeonGroup(T eGroup, int maxSize) {
if (eGroup == null) {
return null;
}
PlayerGroup dGroup = getDungeonGroup(eGroup);
if (dGroup == null && getGroupOnlineSize(eGroup) <= maxSize) {
dGroup = createDungeonGroup(eGroup);
}
return dGroup;
}
/**
@ -157,6 +105,9 @@ public abstract class GroupAdapter<T> {
* @return the dungeon group that mirrors the dungeon group
*/
public PlayerGroup getOrCreateDungeonGroup(T eGroup) {
if (eGroup == null) {
return null;
}
PlayerGroup dGroup = getDungeonGroup(eGroup);
if (dGroup == null) {
dGroup = createDungeonGroup(eGroup);
@ -164,22 +115,6 @@ public abstract class GroupAdapter<T> {
return dGroup;
}
/**
* Returns the external group that mirrors the dungeon group.
* <p>
* Creates an external group if none exists.
*
* @param dGroup the dungeon group
* @return the external group that mirrors the dungeon group
*/
public T getOrCreateExternalGroup(PlayerGroup dGroup) {
T eGroup = getExternalGroup(dGroup);
if (eGroup == null && getPhilosophy() != Philosophy.PERSISTENT) {
eGroup = createExternalGroup(dGroup);
}
return eGroup;
}
/**
* Returns the external group of the given group member.
*
@ -188,6 +123,14 @@ public abstract class GroupAdapter<T> {
*/
public abstract T getExternalGroup(Player member);
/**
* Returns the amount of members in the external group who are online.
*
* @param eGroup the external group
* @return the amount of members in the external group who are online
*/
public abstract int getGroupOnlineSize(T eGroup);
/**
* Checks if two groups are corresponding.
* <p>
@ -203,94 +146,43 @@ public abstract class GroupAdapter<T> {
if (dGroup == null || eGroup == null) {
return false;
}
ExternalGroupData<T> data = groups.get(dGroup);
return data != null && eGroup.equals(data.get());
T dExternal = groups.get(dGroup);
return dExternal != null && eGroup.equals(dExternal);
}
/**
* Deletes the external group corresponding with the given dungeon group.
*
* @param dGroup the dungeon group corresponding with the external one to delete
* @return if the deletion was successful
*/
public abstract boolean deleteCorrespondingGroup(PlayerGroup dGroup);
/**
* Checks if the two groups have the same members.
*
* @param dGroup the dungeon group
* @param eGroup the external group
* @return if the two groups have the same members
*/
public abstract boolean areSimilar(PlayerGroup dGroup, T eGroup);
/**
* Ensures that the player is in {@link #areCorresponding(PlayerGroup, Object) corresponding} groups.
* <p>
* If the player is in an external group but not in a corresponding dungeon group, they are added to the corresponding dungeon group.
* If no dungeon group exists, it is created automatically. Switching dungeon groups forces the player to leave their dungeon.
* <p>
* If the player is in a dungeon group but not in an external group, the player is added to the corresponding external group if it exists.
* If no corresponding external group exists, a new one is created.
* Returns if the player is a member of any external group.
*
* @param player the player
* @return if the player is a member of any external group
*/
public void syncJoin(Player player) {
T eGroup = getExternalGroup(player);
PlayerGroup dGroup = dxl.getPlayerGroup(player);
if (eGroup != null && !areCorresponding(dGroup, eGroup)) {
if (areSimilar(dGroup, eGroup)) {
// The groups are not yet marked as corresponding because one of them is still being created.
return;
}
if (dGroup != null) {
dGroup.removeMember(player, false);
return;
}
dGroup = getDungeonGroup(eGroup);
if (dGroup != null && !dGroup.getMembers().contains(player)) {
dGroup.addMember(player);
} else {
dGroup = createDungeonGroup(eGroup);
}
} else if (eGroup == null && dGroup != null) {
eGroup = getExternalGroup(dGroup);
if (eGroup == null) {
eGroup = createExternalGroup(dGroup);
}
if (!isExternalGroupMember(eGroup, player)) {
addExternalGroupMember(eGroup, player);
}
}
public boolean isExternalGroupMember(Player player) {
return getExternalGroup(player) != null;
}
/**
* Returns if the player is a member of the external group.
*
* @param eGroup the external group
* @param player player
* @param player the 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
* Clears the external / dungeon group references.
*/
public abstract boolean addExternalGroupMember(T eGroup, Player member);
public void clear() {
groups.clear();
}
/**
* Removes the member from the external group.
* Removes the external / dungeon group reference from the cache.
*
* @param eGroup the external group
* @param member the member
* @return if removing the player was successful
* @param dGroup the DXL group that belongs to an external group.
*/
public abstract boolean removeExternalGroupMember(T eGroup, Player member);
public void removeReference(PlayerGroup dGroup) {
groups.remove(dGroup);
}
}

View File

@ -192,7 +192,7 @@ public interface PlayerGroup {
* @param color the color
*/
default void setName(Color color) {
setName(color.toString() + "#" + getId());
setName(color.toString());
}
/**
@ -243,7 +243,7 @@ public interface PlayerGroup {
* @param player the player to add
*/
default void removeMember(Player player) {
addMember(player, true);
removeMember(player, true);
}
/**

View File

@ -128,7 +128,7 @@
<dependency>
<groupId>com.alessiodp.parties</groupId>
<artifactId>parties-api</artifactId>
<version>2.6.14</version>
<version>3.1.10</version>
</dependency>
</dependencies>
<repositories>
@ -148,5 +148,9 @@
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
</repository>
<repository>
<id>alessiodp-repo</id>
<url>https://repo.alessiodp.com/releases/</url>
</repository>
</repositories>
</project>

View File

@ -18,6 +18,7 @@ package de.erethon.dungeonsxl.command;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.event.DataReloadEvent;
import de.erethon.dungeonsxl.api.player.GroupAdapter;
import de.erethon.dungeonsxl.api.player.InstancePlayer;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DPermission;
@ -100,6 +101,7 @@ public class ReloadCommand extends DCommand {
plugin.initFolders();
plugin.initCaches();
plugin.checkState();
plugin.getGroupAdapters().forEach(GroupAdapter::clear);
MessageUtil.sendPluginTag(sender, plugin);
MessageUtil.sendCenteredMessage(sender, DMessage.CMD_RELOAD_SUCCESS.getMessage());

View File

@ -19,6 +19,7 @@ package de.erethon.dungeonsxl.global;
import de.erethon.caliburn.category.Category;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.event.group.GroupCreateEvent.Cause;
import de.erethon.dungeonsxl.api.player.GroupAdapter;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DGroup;
import de.erethon.dungeonsxl.util.commons.chat.MessageUtil;
@ -163,12 +164,19 @@ public class GroupSign extends JoinSign {
return;
}
group = DGroup.create(plugin, Cause.GROUP_SIGN, player, groupName, null, dungeon);
if (group == null) {
return;
for (GroupAdapter adapter : plugin.getGroupAdapters()) {
if (adapter.isExternalGroupMember(player)) {
group = (DGroup) adapter.getOrCreateDungeonGroup(adapter.getExternalGroup(player), maxElements);
}
}
if (group == null) {
group = DGroup.create(plugin, Cause.GROUP_SIGN, player, groupName, null, dungeon);
}
if (group != null) {
group.setDungeon(dungeon);
group.setGroupSign(this);
update();
}
group.setGroupSign(this);
update();
} else if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_JOIN_GROUP.getMessage())) {
group.addMember(player);

View File

@ -129,7 +129,10 @@ public class DGroup implements PlayerGroup {
@Override
public void setName(String name) {
this.name = name;
plugin.getGroupCache().remove(this);
untaggedName = name;
this.name = name + "#" + id;
plugin.getGroupCache().add(name, this);
}
public String getUntaggedName() {
@ -194,12 +197,6 @@ public class DGroup implements PlayerGroup {
MessageUtil.sendMessage(player, DMessage.PLAYER_JOIN_GROUP.getMessage());
}
players.add(player.getUniqueId());
plugin.getGroupAdapters().forEach(a -> a.syncJoin(player));
}
@Override
public void removeMember(Player player) {
removeMember(player, true);
}
@Override
@ -219,8 +216,6 @@ public class DGroup implements PlayerGroup {
return;
}
}
plugin.getGroupAdapters().forEach(a -> a.removeExternalGroupMember(a.getExternalGroup(player), player));
}
@Override
@ -552,8 +547,7 @@ public class DGroup implements PlayerGroup {
}
plugin.getGlobalProtectionCache().updateGroupSigns(this);
plugin.getGroupAdapters().forEach(a -> a.deleteCorrespondingGroup(this));
plugin.getGroupAdapters().forEach(a -> a.removeReference(this));
}
public boolean checkStartGame(Game game) {

View File

@ -17,9 +17,8 @@
package de.erethon.dungeonsxl.player.groupadapter;
import com.alessiodp.parties.api.Parties;
import com.alessiodp.parties.api.events.bukkit.party.BukkitPartiesPartyPostCreateEvent;
import com.alessiodp.parties.api.events.bukkit.party.BukkitPartiesPartyPreDeleteEvent;
import com.alessiodp.parties.api.events.bukkit.party.BukkitPartiesPartyRenameEvent;
import com.alessiodp.parties.api.events.bukkit.party.BukkitPartiesPartyPreRenameEvent;
import com.alessiodp.parties.api.events.bukkit.player.BukkitPartiesPlayerPostJoinEvent;
import com.alessiodp.parties.api.events.bukkit.player.BukkitPartiesPlayerPostLeaveEvent;
import com.alessiodp.parties.api.interfaces.PartiesAPI;
@ -28,11 +27,6 @@ import com.alessiodp.parties.api.interfaces.PartyPlayer;
import de.erethon.dungeonsxl.api.DungeonsAPI;
import de.erethon.dungeonsxl.api.player.GroupAdapter;
import de.erethon.dungeonsxl.api.player.PlayerGroup;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.util.commons.chat.MessageUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -49,38 +43,33 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
private PartiesAPI partiesAPI;
public PartiesAdapter(DungeonsAPI api) {
super(api, Philosophy.PERSISTENT);
super(api);
Bukkit.getPluginManager().registerEvents(this, api);
partiesAPI = Parties.getApi();
}
@Override
public Party createExternalGroup(PlayerGroup dGroup) {
if (!partiesAPI.createParty(dGroup.getRawName(), partiesAPI.getPartyPlayer(dGroup.getLeader().getUniqueId()))) {
return null;
}
Party eGroup = partiesAPI.getParty(dGroup.getRawName());
groups.put(dGroup, new ExternalGroupData<>(eGroup, true));
return eGroup;
}
@Override
public PlayerGroup createDungeonGroup(Party eGroup) {
PlayerGroup dGroup = dxl.createGroup(Bukkit.getPlayer(eGroup.getLeader()), eGroup.getName());
eGroup.getMembers().forEach(uuid -> dGroup.addMember(Bukkit.getPlayer(uuid), false));
groups.put(dGroup, new ExternalGroupData<>(eGroup, false));
eGroup.getOnlineMembers().forEach(p -> dGroup.addMember(Bukkit.getPlayer(p.getPlayerUUID()), false));
groups.put(dGroup, eGroup);
return dGroup;
}
@Override
public Party getExternalGroup(Player member) {
PartyPlayer pPlayer = getPartyPlayer(member);
PartyPlayer pPlayer = partiesAPI.getPartyPlayer(member.getUniqueId());
if (pPlayer == null) {
return null;
}
return partiesAPI.getParty(pPlayer.getPartyName());
}
@Override
public int getGroupOnlineSize(Party eGroup) {
return eGroup.getOnlineMembers().size();
}
@Override
public boolean isExternalGroupMember(Party eGroup, Player player) {
if (eGroup == null) {
@ -89,86 +78,22 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
return eGroup.getMembers().contains(player.getUniqueId());
}
@Override
public boolean addExternalGroupMember(Party eGroup, Player member) {
return eGroup.addMember(getPartyPlayer(member));
}
@Override
public boolean removeExternalGroupMember(Party eGroup, Player member) {
PartyPlayer pPlayer = getPartyPlayer(member);
if (pPlayer == null) {
return false;
}
if (eGroup == null) {
return false;
}
eGroup.removeMember(pPlayer);
if (eGroup.getMembers().isEmpty()) {
eGroup.delete();
}
return true;
}
@Override
public boolean deleteCorrespondingGroup(PlayerGroup dGroup) {
ExternalGroupData<Party> data = groups.get(dGroup);
if (data == null || !data.isCreatedByDXL()) {
return false;
}
data.get().delete();
groups.remove(dGroup);
return true;
}
@Override
public boolean areSimilar(PlayerGroup dGroup, Party eGroup) {
if (dGroup == null || eGroup == null) {
return false;
}
Collection<UUID> members = new ArrayList<>(dGroup.getMembers().getUniqueIds());
for (UUID member : eGroup.getMembers()) {
if (!members.contains(member)) {
return false;
}
members.remove(member);
}
return members.isEmpty();
}
@EventHandler
public void onCreation(BukkitPartiesPartyPostCreateEvent event) {
// Event is called asynchronously
new BukkitRunnable() {
@Override
public void run() {
createDungeonGroup(event.getParty());
}
}.runTask(dxl);
}
@EventHandler
public void onDeletion(BukkitPartiesPartyPreDeleteEvent event) {
PlayerGroup dGroup = getDungeonGroup(event.getParty());
if (dGroup != null) {
groups.remove(dGroup); // This avoids circular deleting of groups
groups.remove(dGroup);
dGroup.delete();
}
}
@EventHandler
public void onRename(BukkitPartiesPartyRenameEvent event) {
PlayerGroup dGroup = getDungeonGroup(event.getParty());
if (dGroup != null) {
dGroup.delete();
}
if (dxl.getGroupCache().get(event.getNewPartyName()) != null) {
MessageUtil.sendMessage(getPlayer(event.getPartyPlayer()), DMessage.ERROR_NAME_IN_USE.getMessage(event.getNewPartyName()));
event.setCancelled(true);
public void onRename(BukkitPartiesPartyPreRenameEvent event) {
PlayerGroup group = getDungeonGroup(event.getParty());
if (group == null) {
return;
}
dGroup.setName(event.getNewPartyName());
group.setName(event.getNewPartyName());
}
@EventHandler
@ -176,7 +101,11 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
new BukkitRunnable() {
@Override
public void run() {
syncJoin(getPlayer(event.getPartyPlayer()));
PlayerGroup group = getDungeonGroup(event.getParty());
if (group == null || group.isPlaying()) {
return;
}
group.addMember(getPlayer(event.getPartyPlayer()), false);
}
}.runTask(dxl);
}
@ -186,11 +115,11 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
new BukkitRunnable() {
@Override
public void run() {
Player player = getPlayer(event.getPartyPlayer());
PlayerGroup group = dxl.getPlayerGroup(player);
if (group != null) {
group.removeMember(player);
PlayerGroup group = getDungeonGroup(event.getParty());
if (group == null || group.isPlaying()) {
return;
}
group.removeMember(getPlayer(event.getPartyPlayer()), false);
}
}.runTask(dxl);
}
@ -199,8 +128,4 @@ public class PartiesAdapter extends GroupAdapter<Party> implements Listener {
return Bukkit.getPlayer(player.getPlayerUUID());
}
private PartyPlayer getPartyPlayer(Player player) {
return partiesAPI.getPartyPlayer(player.getUniqueId());
}
}