Skip to content

Commit

Permalink
Adicionado placeholder top_power (#13)
Browse files Browse the repository at this point in the history
* Melhoria da formatação do /mctop

* Melhoria do uso de instanceof

* Adicionado top_power_level placeholder

* Bump dependency versions

* Update maven.yml

* Cache Top 10 Power Level
  • Loading branch information
SrBedrock authored Mar 14, 2024
1 parent 6c0eef4 commit 682a1cc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 48 deletions.
16 changes: 5 additions & 11 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,25 @@ jobs:

# 1. Check out the current working tree
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4.1.1

# 2. Setup Java 17 JDK (Adopt)
- name: Java 17 setup
uses: actions/setup-java@v3
uses: actions/setup-java@v4.2.0
with:
distribution: 'adopt'
java-package: jdk
java-version: '17'

# 3. Setup local Maven package cache to speed up building
- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
check-latest: true
cache: 'maven'

# 4. Build via Maven
- name: Build via Maven
run: mvn verify -B --file pom.xml -DdisableXmlReport=true

# 5. Upload via Github Actions
- name: Upload
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4.3.1
with:
name: mcMMO
path: target/mcMMO-*.jar
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<version>3.2.5</version>

<configuration>
<junitArtifactName>org.junit.jupiter:junit-jupiter</junitArtifactName>
Expand All @@ -84,7 +84,7 @@
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<version>3.2.5</version>

<configuration>
<junitArtifactName>org.junit.jupiter:junit-jupiter</junitArtifactName>
Expand All @@ -95,12 +95,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M6</version>
<version>3.0.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<version>3.12.1</version>
<configuration>
<release>17</release>
<compilerArgs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ private void display(int page, PrimarySkillType skill, CommandSender sender, Com
return;
}

if (sender instanceof Player) {
if (sender instanceof Player player) {
if (!CommandUtils.hasPlayerDataKey(sender)) {
return;
}

McMMOPlayer mcMMOPlayer = UserManager.getPlayer(sender.getName());
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
long cooldownMillis = Math.max(mcMMO.p.getGeneralConfig().getDatabasePlayerCooldown(), 1750);

if (mcMMOPlayer.getDatabaseATS() + cooldownMillis > System.currentTimeMillis()) {
Expand All @@ -96,11 +96,11 @@ private void display(int page, PrimarySkillType skill, CommandSender sender, Com
return;
}

if (((Player) sender).hasMetadata(MetadataConstants.METADATA_KEY_DATABASE_COMMAND)) {
if (player.hasMetadata(MetadataConstants.METADATA_KEY_DATABASE_COMMAND)) {
sender.sendMessage(LocaleLoader.getString("Commands.Database.Processing"));
return;
} else {
((Player) sender).setMetadata(MetadataConstants.METADATA_KEY_DATABASE_COMMAND, new FixedMetadataValue(mcMMO.p, null));
player.setMetadata(MetadataConstants.METADATA_KEY_DATABASE_COMMAND, new FixedMetadataValue(mcMMO.p, null));
}

mcMMOPlayer.actualizeDatabaseATS();
Expand Down
48 changes: 40 additions & 8 deletions src/main/java/com/gmail/nossr50/placeholders/PapiExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.gmail.nossr50.api.ExperienceAPI;
import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.datatypes.database.PlayerStat;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
Expand All @@ -15,6 +16,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class PapiExpansion extends PlaceholderExpansion {
public static final String SKILL_LEVEL = "level_";
Expand All @@ -34,6 +36,10 @@ public class PapiExpansion extends PlaceholderExpansion {
public static final String IS_EXP_EVENT_ACTIVE = "is_xp_event_active";
public static final String MCABILITY = "mcability";
public static final String MCNOTIFY = "mcnotify";
public static final String TOP_POWER_LEVEL = "top_power_";
public static final String NAME = "name";
public static final String LEVEL = "level";
public List<PlayerStat> topPowerLevel = getTopPowerLevel();

public PapiExpansion() {
}
Expand Down Expand Up @@ -71,9 +77,38 @@ public String onPlaceholderRequest(final Player player, @NotNull final String pa
return String.valueOf(mcMMO.p.getGeneralConfig().getPowerLevelCap());
}

if (params.startsWith(TOP_POWER_LEVEL)) {
String sub = params.substring(TOP_POWER_LEVEL.length());
String[] split = sub.split("_");
if (split.length != 2) {
return "top param not valid. Use top_power_name_0 or top_power_level_0.";
}
String type = split[0];
String rank = split[1];
if (type.equalsIgnoreCase(NAME)) {
if (Integer.parseInt(rank) >= topPowerLevel.size()) {
return "No player found at that rank.";
}
return topPowerLevel.get(Integer.parseInt(rank)).name;
} else if (type.equalsIgnoreCase(LEVEL)) {
if (Integer.parseInt(rank) >= topPowerLevel.size()) {
return "No player found at that rank.";
}
return String.valueOf(topPowerLevel.get(Integer.parseInt(rank)).statVal);
}
}

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

//Player configuration
if (params.equalsIgnoreCase(MCABILITY)) {
return user.getAbilityUse() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}
if (params.equalsIgnoreCase(MCNOTIFY)) {
return user.useChatNotifications() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}

if (params.startsWith(SKILL_LEVEL)) {
PrimarySkillType skill = PrimarySkillType.valueOf(params.substring(SKILL_LEVEL.length()).toUpperCase());
return skill == null ? null : String.valueOf(user.getSkillLevel(skill));
Expand Down Expand Up @@ -115,14 +150,6 @@ else if (Permissions.oneAndOneTenthXp(player, skill))
return String.valueOf(user.getPowerLevel());
}

//Player configuration
if (params.equalsIgnoreCase(MCABILITY)) {
return user.getAbilityUse() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}
if (params.equalsIgnoreCase(MCNOTIFY)) {
return user.useChatNotifications() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
}

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

Expand All @@ -142,4 +169,9 @@ else if (Permissions.oneAndOneTenthXp(player, skill))
return null;
}

@NotNull
private List<PlayerStat> getTopPowerLevel() {
return mcMMO.getDatabaseManager().readLeaderboard(null, 1, 10);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class MctopCommandDisplayTask extends CancellableRunnable {
private final CommandSender sender;
private final PrimarySkillType skill;
private final int page;
private final boolean useBoard, useChat;
private final boolean useBoard;
private final boolean useChat;

MctopCommandDisplayTask(List<PlayerStat> userStats, int page, PrimarySkillType skill, CommandSender sender, boolean useBoard, boolean useChat) {
this.userStats = userStats;
Expand All @@ -42,28 +43,25 @@ public void run() {
displayChat();
}

if (sender instanceof Player) {
((Player) sender).removeMetadata(MetadataConstants.METADATA_KEY_DATABASE_COMMAND, mcMMO.p);
}
if(sender instanceof Player)
if (sender instanceof Player player) {
player.removeMetadata(MetadataConstants.METADATA_KEY_DATABASE_COMMAND, mcMMO.p);
sender.sendMessage(LocaleLoader.getString("Commands.mctop.Tip"));

}
}

private void displayChat() {

if (skill == null) {
if(sender instanceof Player) {
if (sender instanceof Player) {
sender.sendMessage(LocaleLoader.getString("Commands.PowerLevel.Leaderboard"));
}
else {
} else {
sender.sendMessage(ChatColor.stripColor(LocaleLoader.getString("Commands.PowerLevel.Leaderboard")));
}
}
else {
if(sender instanceof Player) {
} else {
if (sender instanceof Player) {
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Leaderboard", mcMMO.p.getSkillTools().getLocalizedSkillName(skill)));
}
else {
} else {
sender.sendMessage(ChatColor.stripColor(LocaleLoader.getString("Commands.Skill.Leaderboard", mcMMO.p.getSkillTools().getLocalizedSkillName(skill))));
}
}
Expand All @@ -74,22 +72,20 @@ private void displayChat() {
// Format:
// 01. Playername - skill value
// 12. Playername - skill value
if(sender instanceof Player) {
sender.sendMessage(String.format("%2d. %s%s - %s%s", place, ChatColor.GREEN, stat.name, ChatColor.WHITE, stat.statVal));
}
else {
sender.sendMessage(String.format("%2d. %s - %s", place, stat.name, stat.statVal));
if (sender instanceof Player) {
sender.sendMessage(String.format("#%02d - %s%s - %s%s", place, ChatColor.GREEN, stat.name, ChatColor.WHITE, stat.statVal));
} else {
sender.sendMessage(String.format("#%02d - %s - %s", place, stat.name, stat.statVal));
}

place++;
}
}

private void displayBoard() {
if (skill == null) {
ScoreboardManager.showTopPowerScoreboard((Player) sender, page, userStats);
}
else {
} else {
ScoreboardManager.showTopScoreboard((Player) sender, skill, page, userStats);
}
}
Expand Down

0 comments on commit 682a1cc

Please sign in to comment.