Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up PAPI Expansion #4906

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 54 additions & 163 deletions src/main/java/com/gmail/nossr50/placeholders/PapiExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.TreeMap;

public class PapiExpansion extends PlaceholderExpansion {
private final Map<String, Placeholder> placeholders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

public PapiExpansion() {
init();
}

@Override
Expand All @@ -37,8 +33,7 @@ public String getAuthor() {

@Override
public String getVersion() {
//grab version from pom.xml
return "1.0,0";
return mcMMO.p.getDescription().getVersion();
}

@Override
Expand All @@ -49,105 +44,41 @@ public String getRequiredPlugin() {
@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) {

// Non player-specific placeholders
if (params.equalsIgnoreCase("is_xp_event_active")) {
return mcMMO.p.isXPEventEnabled() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}else if (params.equalsIgnoreCase("xprate")) {
return String.valueOf(ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier());
}else if (params.equalsIgnoreCase("power_level_cap")) {
return mcMMO.p.getGeneralConfig().getPowerLevelCap()+"";
}

final McMMOPlayer user = UserManager.getPlayer(player);
if (user == null) return null;

if (params.startsWith("level_")) {
PrimarySkillType skill = PrimarySkillType.valueOf(params.substring(6).toUpperCase());
Copy link
Member

@nossr50 nossr50 Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could make use of constants to avoid these hard coded magic numbers
public static final String SKILL_LEVEL = "level_";

// later on
PrimarySkillType skill = PrimarySkillType.valueOf(params.substring(SKILL_LEVEL.length()).toUpperCase());

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like this done for those only needing substring or all?
Eg. "in_party"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for all would make sense to me

return skill == null ? null : user.getSkillLevel(skill)+"";
}else if (params.startsWith("xp_needed_")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer space after }

PrimarySkillType skill = PrimarySkillType.valueOf(params.substring(10).toUpperCase());
return skill == null ? null : user.getXpToLevel(skill)+"";
}else if (params.startsWith("xp_remaining_")) {
PrimarySkillType skill = PrimarySkillType.valueOf(params.substring(13).toUpperCase());
return skill == null ? null : (user.getXpToLevel(skill) - user.getSkillXpLevel(skill))+"";
}else if (params.startsWith("xp_")) {
PrimarySkillType skill = PrimarySkillType.valueOf(params.substring(3).toUpperCase());
return skill == null ? null : user.getSkillXpLevel(skill)+"";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer String.valueOf() but this is perhaps a small nitpick

}else if (params.startsWith("rank_")) {
try {
return ExperienceAPI.getPlayerRankSkill(player.getUniqueId(), StringUtils.getCapitalized(skill.toString()));
return ExperienceAPI.getPlayerRankSkill(player.getUniqueId(), StringUtils.getCapitalized(params.substring(5)))+"";
} catch (Exception ex) {
return null;
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;

}else if (params.startsWith("xprate_")) {
PrimarySkillType skill = PrimarySkillType.valueOf(params.substring(7).toUpperCase());
if (skill == null) return null;
double modifier = 1.0F;

if (Permissions.customXpBoost(player, skill))
modifier = ExperienceConfig.getInstance().getCustomXpPerkBoost();
else if (Permissions.quadrupleXp(player, skill))
Expand All @@ -162,69 +93,29 @@ else if (Permissions.oneAndOneHalfXp(player, skill))
modifier = 1.5;
else if (Permissions.oneAndOneTenthXp(player, skill))
modifier = 1.1;

return String.valueOf(modifier);
}else if (params.equalsIgnoreCase("power_level")) {
return user.getPowerLevel()+"";
}


//Party placeholders
final Party party = user.getParty();

if (params.equalsIgnoreCase("in_party")) {
return (party==null) ? PlaceholderAPIPlugin.booleanFalse() : PlaceholderAPIPlugin.booleanTrue();
}else if (params.equalsIgnoreCase("party_name")) {
return (party == null) ? "" : party.getName();
}else if (params.equalsIgnoreCase("is_party_leader")) {
if (party == null) return "";
return party.getLeader().getPlayerName().equals(player.getName()) ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}else if (params.equalsIgnoreCase("party_leader")) {
return (party == null) ? "" : party.getLeader().getPlayerName();
}else if (params.equalsIgnoreCase("party_size")) {
return (party == null) ? "" : party.getMembers().size()+"";
}

return null;
}

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));
};
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/com/gmail/nossr50/placeholders/Placeholder.java

This file was deleted.

Loading