Skip to content

Commit

Permalink
feat: parses placeholders in commands using PAPI
Browse files Browse the repository at this point in the history
Closes #70
  • Loading branch information
RoinujNosde committed Jun 16, 2022
1 parent 24039b5 commit 5027f6a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/main/java/me/roinujnosde/titansbattle/BaseGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.roinujnosde.titansbattle.events.*;
import me.roinujnosde.titansbattle.exceptions.CommandNotSupportedException;
import me.roinujnosde.titansbattle.hooks.papi.PlaceholderHook;
import me.roinujnosde.titansbattle.managers.GameManager;
import me.roinujnosde.titansbattle.managers.GroupManager;
import me.roinujnosde.titansbattle.types.Group;
Expand Down Expand Up @@ -454,17 +455,20 @@ protected void runCommandsAfterBattle(@NotNull Collection<Warrior> warriors) {

protected void runCommands(@NotNull Collection<Warrior> warriors, @Nullable Collection<String> commands) {
if (commands == null) return;
PlaceholderHook hook = plugin.getPlaceholderHook();

for (String command : commands) {
for (Warrior warrior : warriors) {
Player player = warrior.toOnlinePlayer();
if (player == null) {
continue;
}
if (!command.contains("%player%")) { // Runs the command once when %player% is not used
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), hook.parse((OfflinePlayer) null, command));
break;
}
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command.replace("%player%", player.getName()));
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), hook.parse(warrior, command,
"%player%", warrior.getName()));
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/me/roinujnosde/titansbattle/TitansBattle.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import me.roinujnosde.titansbattle.challenges.ChallengeRequest;
import me.roinujnosde.titansbattle.dao.ConfigurationDao;
import me.roinujnosde.titansbattle.games.Game;
import me.roinujnosde.titansbattle.hooks.TBExpansion;
import me.roinujnosde.titansbattle.hooks.papi.PlaceholderHook;
import me.roinujnosde.titansbattle.managers.*;
import me.roinujnosde.titansbattle.types.GameConfiguration;
import me.roinujnosde.titansbattle.types.Kit;
Expand Down Expand Up @@ -61,6 +61,7 @@ public final class TitansBattle extends JavaPlugin {
private ChallengeManager challengeManager;
private ListenerManager listenerManager;
private ConfigurationDao configurationDao;
private PlaceholderHook placeholderHook;

@Override
public void onEnable() {
Expand All @@ -86,9 +87,7 @@ public void onEnable() {
listenerManager.registerGeneralListeners();
databaseManager.loadDataToMemory();
taskManager.setupScheduler();
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
new TBExpansion(this).register();
}
placeholderHook = new PlaceholderHook(this);
new Metrics(this, 14875);
}

Expand Down Expand Up @@ -168,6 +167,10 @@ public ConfigurationDao getConfigurationDao() {
return configurationDao;
}

public PlaceholderHook getPlaceholderHook() {
return placeholderHook;
}

/**
* Returns the language for the path on the config
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.roinujnosde.titansbattle.hooks.papi;

import me.clip.placeholderapi.PlaceholderAPI;
import me.roinujnosde.titansbattle.TitansBattle;
import me.roinujnosde.titansbattle.types.Warrior;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlaceholderHook {

private final TitansBattle plugin;

public PlaceholderHook(@NotNull TitansBattle plugin) {
this.plugin = plugin;
if (isPapi()) {
new TBExpansion(plugin).register();
}
}

public String parse(@Nullable OfflinePlayer player, @NotNull String text, String... internalPlaceholders) {
for (int i = 0; i + 1 < internalPlaceholders.length; i = i + 2) {
text = text.replace(internalPlaceholders[i], internalPlaceholders[i + 1]);
}
if (isPapi()) {
return PlaceholderAPI.setPlaceholders(player, text);
}
return text;
}

public String parse(@NotNull Warrior warrior, @NotNull String text, String... internalPlaceholders) {
return parse(warrior.toPlayer(), text, internalPlaceholders);
}

public boolean isPapi() {
return plugin.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI");
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.roinujnosde.titansbattle.hooks;
package me.roinujnosde.titansbattle.hooks.papi;

import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/me/roinujnosde/titansbattle/types/Prizes.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public void give(@NotNull TitansBattle plugin, @Nullable List<Player> leaders, @
giveItemsToPlayers(plugin, members, memberItems);
}
if (leaderCommandsEnabled) {
runCommandsOnPlayers(leaders, leaderCommands, leaderCommandsSomeNumber, leaderCommandsSomeNumberDivide);
runCommandsOnPlayers(plugin, leaders, leaderCommands, leaderCommandsSomeNumber, leaderCommandsSomeNumberDivide);
}
if (memberCommandsEnabled) {
runCommandsOnPlayers(members, memberCommands, memberCommandsSomeNumber, memberCommandsSomeNumberDivide);
runCommandsOnPlayers(plugin, members, memberCommands, memberCommandsSomeNumber, memberCommandsSomeNumberDivide);
}
}

Expand All @@ -100,15 +100,16 @@ private void giveItemsToPlayers(@NotNull TitansBattle plugin,
}
}

private void runCommandsOnPlayers(List<Player> players, List<String> commands, double someNumber, boolean divide) {
private void runCommandsOnPlayers(TitansBattle plugin, List<Player> players, List<String> commands,
double someNumber, boolean divide) {
if (divide) {
someNumber = someNumber / players.size();
}
for (Player player : players) {
for (String command : commands) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
command.replaceAll("%player%", player.getName()).replaceAll("%some_number%",
Double.toString(someNumber)));
command = plugin.getPlaceholderHook().parse(player, command,
"%player%", player.getName(), "%some_number%", Double.toString(someNumber));
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}
}
}
Expand Down

0 comments on commit 5027f6a

Please sign in to comment.