Skip to content

Commit

Permalink
Add placeholder support to vaultreward option
Browse files Browse the repository at this point in the history
  • Loading branch information
Krakenied committed Nov 15, 2024
1 parent 102ed9a commit dc83007
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) {
List<String> startCommands = config.getStringList("startcommands");
List<String> cancelCommands = config.getStringList("cancelcommands");
List<String> expiryCommands = config.getStringList("expirycommands");
double vaultReward = config.getDouble("vaultreward", 0.0D);
String vaultReward = config.getString("vaultreward", null);
boolean repeatable = config.getBoolean("options.repeatable", false);
boolean cooldown = config.getBoolean("options.cooldown.enabled", false);
boolean timeLimit = config.getBoolean("options.time-limit.enabled", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leonardobishop.quests.bukkit.hook.vault.rewards;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

final class DummyVaultReward extends VaultReward {

static final DummyVaultReward INSTANCE = new DummyVaultReward();

DummyVaultReward() {
// temporarily ignore it
//noinspection DataFlowIssue
super(null);
}

@Override
public double getRewardValue(final @NotNull Player player) {
return 0.0d;
}

@Override
public void give(final @NotNull Player player) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.leonardobishop.quests.bukkit.hook.vault.rewards;

import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

final class NumericVaultReward extends VaultReward {

private final double value;

NumericVaultReward(final @NotNull BukkitQuestsPlugin plugin, final double value) {
super(plugin);

this.value = value;
}

@Override
public double getRewardValue(final @NotNull Player player) {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.leonardobishop.quests.bukkit.hook.vault.rewards;

import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
import com.leonardobishop.quests.bukkit.hook.papi.AbstractPlaceholderAPIHook;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

final class PlaceholderVaultReward extends VaultReward {

private final String rewardString;

PlaceholderVaultReward(final @NotNull BukkitQuestsPlugin plugin, final @NotNull String rewardString) {
super(plugin);

this.rewardString = rewardString;
}

@Override
public double getRewardValue(final @NotNull Player player) {
final AbstractPlaceholderAPIHook hook = this.plugin.getPlaceholderAPIHook();

if (hook == null) {
this.plugin.getLogger().warning("Could not give '" + this.rewardString + "' vault reward to " + player.getName() + ". No PlaceholderAPI hook found!");
return 0.0d;
}

final String papiRewardString = this.plugin.getPlaceholderAPIHook().replacePlaceholders(player, this.rewardString);

final double vaultReward;
try {
vaultReward = Double.parseDouble(papiRewardString);
} catch (final NumberFormatException e) {
this.plugin.getLogger().warning("Could not give '" + this.rewardString + "' (PAPI: '" + papiRewardString + "') vault reward to " + player.getName() + ". Invalid double format!");
return 0.0d;
}

return vaultReward;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.leonardobishop.quests.bukkit.hook.vault.rewards;

import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class VaultReward {

final BukkitQuestsPlugin plugin;

VaultReward(final @NotNull BukkitQuestsPlugin plugin) {
this.plugin = plugin;
}

public abstract double getRewardValue(final @NotNull Player player);

public void give(final @NotNull Player player) {
this.plugin.getVaultHook().depositPlayer(player, this.getRewardValue(player));
}

public static @NotNull VaultReward parse(final @NotNull BukkitQuestsPlugin plugin, final @Nullable String str) {
if (str == null) {
return DummyVaultReward.INSTANCE;
}

if (str.indexOf('%') == -1) {
return parseNumeric(plugin, str);
}

return new PlaceholderVaultReward(plugin, str);
}

private static @NotNull VaultReward parseNumeric(final @NotNull BukkitQuestsPlugin plugin, final @NotNull String str) {
final double value;

try {
value = Double.parseDouble(str);
} catch (final NumberFormatException e) {
plugin.getLogger().warning("Could not parse '" + str + "' vault reward. Invalid double format!");
return DummyVaultReward.INSTANCE;
}

return new NumericVaultReward(plugin, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.leonardobishop.quests.bukkit.api.event.PlayerStopTrackQuestEvent;
import com.leonardobishop.quests.bukkit.api.event.PreStartQuestEvent;
import com.leonardobishop.quests.bukkit.config.BukkitQuestsConfig;
import com.leonardobishop.quests.bukkit.hook.vault.rewards.VaultReward;
import com.leonardobishop.quests.bukkit.menu.itemstack.QItemStack;
import com.leonardobishop.quests.bukkit.util.FormatUtils;
import com.leonardobishop.quests.bukkit.util.Messages;
Expand All @@ -29,8 +30,10 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

public class NormalQuestController implements QuestController {
Expand All @@ -39,6 +42,7 @@ public class NormalQuestController implements QuestController {
private final BukkitQuestsConfig config;

private final List<Quest> autoStartQuestCache;
private final Map<Quest, VaultReward> vaultRewardCache;

public NormalQuestController(BukkitQuestsPlugin plugin) {
this.plugin = plugin;
Expand All @@ -49,6 +53,8 @@ public NormalQuestController(BukkitQuestsPlugin plugin) {
if (quest.isAutoStartEnabled()) autoStartQuestCache.add(quest);
}
this.autoStartQuestCache = autoStartQuestCache;

this.vaultRewardCache = new WeakHashMap<>();
}

@Override
Expand Down Expand Up @@ -235,10 +241,12 @@ public boolean completeQuestForPlayer(QPlayer qPlayer, Quest quest) {
Bukkit.getPluginManager().callEvent(questFinishEvent);
// PlayerFinishQuestEvent -- end
plugin.getScheduler().doSync(() -> {
final double vaultReward = quest.getVaultReward();
if (vaultReward > 0.0D) {
this.plugin.getVaultHook().depositPlayer(player, vaultReward);
}
final VaultReward vaultReward = this.vaultRewardCache.computeIfAbsent(quest,
k -> VaultReward.parse(this.plugin, k.getVaultReward())
);

// Use cached reward to do not parse it every single time
vaultReward.give(player);

for (String s : quest.getRewards()) {
s = s.replace("{player}", player.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Quest implements Comparable<Quest> {
private List<String> startCommands;
private List<String> cancelCommands;
private List<String> expiryCommands;
private double vaultReward;
private String vaultReward;
private boolean repeatEnabled;
private boolean cooldownEnabled;
private int cooldown;
Expand Down Expand Up @@ -210,9 +210,9 @@ public List<String> getExpiryCommands() {
* Get the Vault reward for this quest.
* The Vault reward is an amount of Vault economy money to be given upon completing the quest.
*
* @return double
* @return string
*/
public double getVaultReward() {
public @Nullable String getVaultReward() {
return this.vaultReward;
}

Expand Down Expand Up @@ -353,7 +353,7 @@ public static class Builder {
private List<String> startCommands = Collections.emptyList();
private List<String> cancelCommands = Collections.emptyList();
private List<String> expiryCommands = Collections.emptyList();
private double vaultReward = 0.0D;
private String vaultReward = null;
private boolean repeatEnabled = false;
private boolean cooldownEnabled = false;
private int cooldown = 0;
Expand Down Expand Up @@ -417,7 +417,7 @@ public Builder withExpiryCommands(List<String> expiryCommands) {
return this;
}

public Builder withVaultReward(double vaultReward) {
public Builder withVaultReward(String vaultReward) {
this.vaultReward = vaultReward;
return this;
}
Expand Down

0 comments on commit dc83007

Please sign in to comment.