DungeonsXL/api/src/main/java/de/erethon/dungeonsxl/api/player/GroupAdapter.java

189 lines
6.0 KiB
Java
Raw Normal View History

2020-01-29 00:44:45 +01:00
/*
2023-09-28 19:16:23 +02:00
* Copyright (C) 2014-2023 Daniel Saukel
2020-01-29 00:44:45 +01:00
*
* This library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNULesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.api.player;
2020-03-29 17:15:22 +02:00
import de.erethon.dungeonsxl.api.DungeonsAPI;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
2020-01-29 00:44:45 +01:00
import org.bukkit.entity.Player;
/**
* Implement and register in order to track a group.
2020-03-29 17:15:22 +02:00
* <p>
* See implementation classes in de.erethon.dungeonsxl.player.groupadapter for reference.
2020-01-29 00:44:45 +01:00
*
* @param <T> the external group object
* @author Daniel Saukel
*/
public abstract class GroupAdapter<T> {
2020-03-29 17:15:22 +02:00
protected DungeonsAPI dxl;
2021-08-21 23:18:38 +02:00
protected Map<PlayerGroup, T> groups = new HashMap<>();
2020-01-29 00:44:45 +01:00
/**
2021-08-21 23:18:38 +02:00
* @param dxl the DungeonsAPI instance
2020-01-29 00:44:45 +01:00
*/
2021-08-21 23:18:38 +02:00
protected GroupAdapter(DungeonsAPI dxl) {
2020-03-29 17:15:22 +02:00
this.dxl = dxl;
2020-01-29 00:44:45 +01:00
}
/**
* Creates a dungeon group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the external group.
*
* @param eGroup the external group
* @return a dungeon group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the external group
*/
2020-03-29 17:15:22 +02:00
public abstract PlayerGroup createDungeonGroup(T eGroup);
2020-01-29 00:44:45 +01:00
/**
2020-03-29 17:15:22 +02:00
* Returns the dungeon group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the external group or null of none exists.
2020-01-29 00:44:45 +01:00
*
* @param eGroup the external group
2020-03-29 17:15:22 +02:00
* @return the dungeon group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the external group
2020-01-29 00:44:45 +01:00
*/
2020-03-29 17:15:22 +02:00
public PlayerGroup getDungeonGroup(T eGroup) {
2021-08-21 23:18:38 +02:00
if (eGroup == null) {
return null;
}
for (Entry<PlayerGroup, T> entry : groups.entrySet()) {
if (entry.getValue().equals(eGroup)) {
2020-03-29 17:15:22 +02:00
return entry.getKey();
}
}
return null;
}
2020-01-29 00:44:45 +01:00
/**
2020-03-29 17:15:22 +02:00
* Returns the external group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the dungeon group.
2020-01-29 00:44:45 +01:00
*
* @param dGroup the dungeon group
2020-03-29 17:15:22 +02:00
* @return the external group {@link #areCorresponding(PlayerGroup, Object) corresponding} with the dungeon group
2020-01-29 00:44:45 +01:00
*/
2020-03-29 17:15:22 +02:00
public T getExternalGroup(PlayerGroup dGroup) {
2021-08-21 23:18:38 +02:00
return groups.get(dGroup);
2020-03-29 17:15:22 +02:00
}
2020-01-29 00:44:45 +01:00
/**
* Returns the dungeon group that mirrors the external group.
* <p>
2021-08-21 23:18:38 +02:00
* Creates a dungeon group if none exists and if the party has no more online members than maxSize.
2020-01-29 00:44:45 +01:00
*
2021-08-21 23:18:38 +02:00
* @param eGroup the dungeon group
* @param maxSize the maximum size of the group
2020-01-29 00:44:45 +01:00
* @return the dungeon group that mirrors the dungeon group
*/
2021-08-21 23:18:38 +02:00
public PlayerGroup getOrCreateDungeonGroup(T eGroup, int maxSize) {
if (eGroup == null) {
return null;
}
2020-03-29 17:15:22 +02:00
PlayerGroup dGroup = getDungeonGroup(eGroup);
2021-08-21 23:18:38 +02:00
if (dGroup == null && getGroupOnlineSize(eGroup) <= maxSize) {
2020-03-29 17:15:22 +02:00
dGroup = createDungeonGroup(eGroup);
2020-01-29 00:44:45 +01:00
}
return dGroup;
}
/**
2021-08-21 23:18:38 +02:00
* Returns the dungeon group that mirrors the external group.
2020-01-29 00:44:45 +01:00
* <p>
2021-08-21 23:18:38 +02:00
* Creates a dungeon group if none exists.
2020-01-29 00:44:45 +01:00
*
2021-08-21 23:18:38 +02:00
* @param eGroup the dungeon group
* @return the dungeon group that mirrors the dungeon group
2020-01-29 00:44:45 +01:00
*/
2021-08-21 23:18:38 +02:00
public PlayerGroup getOrCreateDungeonGroup(T eGroup) {
if (eGroup == null) {
return null;
2020-01-29 00:44:45 +01:00
}
2021-08-21 23:18:38 +02:00
PlayerGroup dGroup = getDungeonGroup(eGroup);
if (dGroup == null) {
dGroup = createDungeonGroup(eGroup);
}
return dGroup;
2020-01-29 00:44:45 +01:00
}
2020-03-29 17:15:22 +02:00
/**
* Returns the external group of the given group member.
*
* @param member the group member
* @return the external group of the given group member
*/
public abstract T getExternalGroup(Player member);
2021-08-21 23:18:38 +02:00
/**
* 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);
2020-01-29 00:44:45 +01:00
/**
* Checks if two groups are corresponding.
* <p>
* Corresponding groups are groups that should be regarded as one from the perspective of a player.
* <p>
* Two null values are regarded as corresponding.
*
* @param dGroup the dungeon group
* @param eGroup the external group
* @return if the two groups are corresponding
*/
2020-03-29 17:15:22 +02:00
public boolean areCorresponding(PlayerGroup dGroup, T eGroup) {
if (dGroup == null || eGroup == null) {
return false;
}
2021-08-21 23:18:38 +02:00
T dExternal = groups.get(dGroup);
return dExternal != null && eGroup.equals(dExternal);
2020-03-29 17:15:22 +02:00
}
/**
2021-08-21 23:18:38 +02:00
* Returns if the player is a member of any external group.
2020-01-29 00:44:45 +01:00
*
* @param player the player
2021-08-21 23:18:38 +02:00
* @return if the player is a member of any external group
2020-01-29 00:44:45 +01:00
*/
2021-08-21 23:18:38 +02:00
public boolean isExternalGroupMember(Player player) {
return getExternalGroup(player) != null;
2020-01-29 00:44:45 +01:00
}
/**
* Returns if the player is a member of the external group.
*
* @param eGroup the external group
2021-08-21 23:18:38 +02:00
* @param player the player
* @return if the player is a member of the external group
*/
public abstract boolean isExternalGroupMember(T eGroup, Player player);
/**
2021-08-21 23:18:38 +02:00
* Clears the external / dungeon group references.
*/
2021-08-21 23:18:38 +02:00
public void clear() {
groups.clear();
}
/**
2021-08-21 23:18:38 +02:00
* Removes the external / dungeon group reference from the cache.
*
2021-08-21 23:18:38 +02:00
* @param dGroup the DXL group that belongs to an external group.
*/
2021-08-21 23:18:38 +02:00
public void removeReference(PlayerGroup dGroup) {
groups.remove(dGroup);
}
2020-01-29 00:44:45 +01:00
}