PAPI Support WIP

This commit is contained in:
nossr50 2023-06-17 20:22:49 -07:00
parent cc1b511c02
commit f22043ebb5
19 changed files with 617 additions and 1 deletions

10
pom.xml
View File

@ -241,10 +241,20 @@
<id>sonatype-oss-snapshots1</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<!-- ... -->
<!-- ... -->
</repositories>
<dependencies>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>co.aikar</groupId>
<artifactId>acf-bukkit</artifactId> <!-- Don't forget to replace this -->

View File

@ -0,0 +1,232 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.api.ExperienceAPI;
import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.text.StringUtils;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.bukkit.entity.Player;
import java.util.Map;
import java.util.TreeMap;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PapiExpansion extends PlaceholderExpansion {
private final Map<String, Placeholder> placeholders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
public PapiExpansion() {
init();
}
@Override
public String getIdentifier() {
return "mcmmo";
}
@Override
public String getAuthor() {
return "mcMMO Dev Team";
}
@Override
public String getVersion() {
//grab version from pom.xml
return "1.0,0";
}
@Override
public String getRequiredPlugin() {
return "mcMMO";
}
@Override
@Nullable
public String onPlaceholderRequest(final Player player, @NotNull final String params) {
String token;
String data = null;
int dataPosition = params.indexOf(":");
if (dataPosition != -1) {
token = params.substring(0, dataPosition);
data = params.substring(dataPosition + 1);
} else {
token = params;
}
Placeholder placeholder = placeholders.get(token);
if (placeholder != null) {
return placeholder.process(player, data);
} else {
return null;
}
}
public Integer getSkillLevel(PrimarySkillType skill, Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
return user.getSkillLevel(skill);
}
public Integer getExpNeeded(PrimarySkillType skill, Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
return user.getXpToLevel(skill);
}
public Integer getExp(PrimarySkillType skill, Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
return user.getSkillXpLevel(skill);
}
public Integer getExpRemaining(PrimarySkillType skill, Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
int current = user.getSkillXpLevel(skill);
int needed = user.getXpToLevel(skill);
return needed - current;
}
public Integer getRank(PrimarySkillType skill, Player player) {
try {
return ExperienceAPI.getPlayerRankSkill(player.getUniqueId(), StringUtils.getCapitalized(skill.toString()));
} catch (Exception ex) {
return null;
}
}
public Integer getPowerLevel(Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
return user.getPowerLevel();
}
public Integer getPowerCap(Player player) {
return mcMMO.p.getGeneralConfig().getPowerLevelCap();
}
public String getPartyName(Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
final Party party = user.getParty();
return (party == null) ? null : party.getName();
}
public String getPartyLeader(Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
final Party party = user.getParty();
return (party == null) ? null : party.getLeader().getPlayerName();
}
public Integer getPartySize(Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
final Party party = user.getParty();
return (party == null) ? null : party.getMembers().size();
}
public String getXpRate(Player player) {
return String.valueOf(ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier());
}
public String getSkillXpRate(PrimarySkillType skill, Player player) {
final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;
double modifier = 1.0F;
if (Permissions.customXpBoost(player, skill))
modifier = ExperienceConfig.getInstance().getCustomXpPerkBoost();
else if (Permissions.quadrupleXp(player, skill))
modifier = 4;
else if (Permissions.tripleXp(player, skill))
modifier = 3;
else if (Permissions.doubleAndOneHalfXp(player, skill))
modifier = 2.5;
else if (Permissions.doubleXp(player, skill))
modifier = 2;
else if (Permissions.oneAndOneHalfXp(player, skill))
modifier = 1.5;
else if (Permissions.oneAndOneTenthXp(player, skill))
modifier = 1.1;
return String.valueOf(modifier);
}
public String isExpEventActive(Player player) {
return mcMMO.p.isXPEventEnabled() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}
public void registerPlaceholder(Placeholder placeholder) {
final Placeholder registered = placeholders.get(placeholder.getName());
if (registered != null)
throw new IllegalStateException("Placeholder " + placeholder.getName() + " is already registered!");
placeholders.put(placeholder.getName(), placeholder);
}
protected void init() {
for (PrimarySkillType skill : PrimarySkillType.values()) {
// %mcmmo_level_<skillname>%
registerPlaceholder(new SkillLevelPlaceholder(this, skill));
//%mcmmo_xp_needed_<skillname>%
registerPlaceholder(new SkillExpNeededPlaceholder(this, skill));
//%mcmmo_xp_<skillname>%
registerPlaceholder(new SkillExpPlaceholder(this, skill));
//%mcmmo_xp_remaining_<skillname>%
registerPlaceholder(new SkillExpRemainingPlaceholder(this, skill));
//%mcmmo_rank_<skillname>%
registerPlaceholder(new SkillRankPlaceholder(this, skill));
//%mcmmo_xprate_<skillname>%
registerPlaceholder(new SkillXpRatePlaceholder(this, skill));
}
//%mcmmo_power_level%
registerPlaceholder(new PowerLevelPlaceholder(this));
// %mcmmo_power_level_cap%
registerPlaceholder(new PowerLevelCapPlaceholder(this));
// %mcmmo_in_party%
registerPlaceholder(new PartyIsMemberPlaceholder(this));
/// %mcmmo_party_name%
registerPlaceholder(new PartyNamePlaceholder(this));
// %mcmmo_is_party_leader%
registerPlaceholder(new PartyIsLeaderPlaceholder(this));
// %mcmmo_party_leader%
registerPlaceholder(new PartyLeaderPlaceholder(this));
// %mcmmo_party_size%
registerPlaceholder(new PartySizePlaceholder(this));
// %mcmmo_is_xp_event_active%
registerPlaceholder(new XpEventActivePlaceholder(this));
// %mcmmo_xprate%
registerPlaceholder(new XpRatePlaceholder(this));
};
}

View File

@ -0,0 +1,29 @@
package com.gmail.nossr50.placeholders;
import org.bukkit.entity.Player;
public class PartyIsLeaderPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public PartyIsLeaderPlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
/**
* {@inheritDoc}
*/
@Override
public String process(Player player, String params) {
String leader = papiExpansion.getPartyLeader(player);
return (leader.equals(player.getName())) ? "true" : "false";
}
/**
* {@inheritDoc}
*/
@Override
public String getName() {
return "is_party_leader";
}
}

View File

@ -0,0 +1,21 @@
package com.gmail.nossr50.placeholders;
import org.bukkit.entity.Player;
public class PartyIsMemberPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public PartyIsMemberPlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
return (papiExpansion.getPartyName(player) == null) ? "false" : "true";
}
@Override
public String getName() {
return "in_party";
}
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.placeholders;
import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Player;
public class PartyLeaderPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public PartyLeaderPlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
return StringUtils.stripToEmpty(papiExpansion.getPartyLeader(player));
}
@Override
public String getName() {
return "party_leader";
}
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.placeholders;
import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Player;
public class PartyNamePlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public PartyNamePlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
return StringUtils.stripToEmpty(papiExpansion.getPartyName(player));
}
@Override
public String getName() {
return "party_name";
}
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.placeholders;
import org.bukkit.entity.Player;
public class PartySizePlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public PartySizePlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
Integer partySize = papiExpansion.getPartySize(player);
return (partySize == null) ? "" : partySize.toString();
}
@Override
public String getName() {
return "party_size";
}
}

View File

@ -0,0 +1,18 @@
package com.gmail.nossr50.placeholders;
import org.bukkit.entity.Player;
public interface Placeholder {
/**
* @param player the player to process the placeholder for
* @param params the paramaters to be passed to the placeholder
* @return the value of the placeholder
*/
String process(Player player, String params);
/**
* @return the name of the placeholder
*/
String getName();
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.placeholders;
import org.bukkit.entity.Player;
public class PowerLevelCapPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public PowerLevelCapPlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
Integer cap = papiExpansion.getPowerCap(player);
return (cap == null) ? "" : cap.toString();
}
@Override
public String getName() {
return "power_level_cap";
}
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.placeholders;
import org.bukkit.entity.Player;
public class PowerLevelPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public PowerLevelPlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
Integer powerLevel = papiExpansion.getPowerLevel(player);
return (powerLevel == null) ? "" : powerLevel.toString();
}
@Override
public String getName() {
return "power_level";
}
}

View File

@ -0,0 +1,26 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class SkillExpNeededPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
private final PrimarySkillType skillType;
public SkillExpNeededPlaceholder(PapiExpansion papiExpansion, PrimarySkillType skillType) {
this.papiExpansion = papiExpansion;
this.skillType = skillType;
}
@Override
public String process(Player player, String params) {
final Integer expNeeded = papiExpansion.getExpNeeded(skillType, player);
return (expNeeded == null) ? "" : expNeeded.toString();
}
@Override
public String getName() {
return "xp_needed_" + skillType.toString().toLowerCase();
}
}

View File

@ -0,0 +1,26 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class SkillExpPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
private final PrimarySkillType skill;
public SkillExpPlaceholder(PapiExpansion papiExpansion, PrimarySkillType skill) {
this.papiExpansion = papiExpansion;
this.skill = skill;
}
@Override
public String process(Player player, String params) {
Integer exp = papiExpansion.getExp(skill, player);
return (exp == null) ? "" : exp.toString();
}
@Override
public String getName() {
return "xp_" + skill.toString().toLowerCase();
}
}

View File

@ -0,0 +1,25 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class SkillExpRemainingPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
private final PrimarySkillType skill;
public SkillExpRemainingPlaceholder(PapiExpansion papiExpansion, PrimarySkillType skill) {
this.papiExpansion = papiExpansion;
this.skill = skill;
}
@Override
public String process(Player player, String params) {
final Integer expRemaining = papiExpansion.getExpRemaining(skill, player);
return (expRemaining == null) ? "" : expRemaining.toString();
}
@Override
public String getName() {
return "xp_remaining_" + skill.toString().toLowerCase();
}
}

View File

@ -0,0 +1,25 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class SkillLevelPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
private final PrimarySkillType skillType;
public SkillLevelPlaceholder(PapiExpansion papiExpansion, PrimarySkillType skillType) {
this.papiExpansion = papiExpansion;
this.skillType = skillType;
}
@Override
public String process(Player p, String params) {
final Integer skillLevel = papiExpansion.getSkillLevel(skillType, p);
return (skillLevel == null) ? "" : skillLevel.toString();
}
@Override
public String getName() {
return "level_" + skillType.toString().toLowerCase();
}
}

View File

@ -0,0 +1,25 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class SkillRankPlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
private final PrimarySkillType skill;
public SkillRankPlaceholder(PapiExpansion papiExpansion, PrimarySkillType skill) {
this.papiExpansion = papiExpansion;
this.skill = skill;
}
@Override
public String process(Player player, String params) {
Integer rank = papiExpansion.getRank(skill, player);
return (rank == null) ? "" : rank.toString();
}
@Override
public String getName() {
return "rank_" + skill.toString().toLowerCase();
}
}

View File

@ -0,0 +1,25 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class SkillXpRatePlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
private final PrimarySkillType skillType;
public SkillXpRatePlaceholder(PapiExpansion papiExpansion, PrimarySkillType skillType) {
this.papiExpansion = papiExpansion;
this.skillType = skillType;
}
@Override
public String process(Player p, String params) {
final String skillLevel = papiExpansion.getSkillXpRate(skillType, p);
return (skillLevel == null) ? "" : skillLevel;
}
@Override
public String getName() {
return "xprate_" + skillType.toString().toLowerCase();
}
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class XpEventActivePlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public <S extends PrimarySkillType> XpEventActivePlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
return papiExpansion.isExpEventActive(player);
}
@Override
public String getName() {
return "is_xp_event_active";
}
}

View File

@ -0,0 +1,22 @@
package com.gmail.nossr50.placeholders;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import org.bukkit.entity.Player;
public class XpRatePlaceholder implements Placeholder {
private final PapiExpansion papiExpansion;
public <S extends PrimarySkillType> XpRatePlaceholder(PapiExpansion papiExpansion) {
this.papiExpansion = papiExpansion;
}
@Override
public String process(Player player, String params) {
return papiExpansion.getXpRate(player);
}
@Override
public String getName() {
return "xprate";
}
}

View File

@ -14,7 +14,7 @@ author: nossr50
authors: [GJ, NuclearW, bm01, Glitchfinder, TfT_02, t00thpick1, Riking, electronicboy, kashike]
website: https://www.mcmmo.org
main: com.gmail.nossr50.mcMMO
softdepend: [WorldGuard, CombatTag, HealthBar]
softdepend: [WorldGuard, CombatTag, HealthBar, PlaceholderAPI]
load: POSTWORLD
api-version: 1.13