-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add placeholder support to vaultreward option
- Loading branch information
Showing
7 changed files
with
148 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...t/src/main/java/com/leonardobishop/quests/bukkit/hook/vault/rewards/DummyVaultReward.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/main/java/com/leonardobishop/quests/bukkit/hook/vault/rewards/NumericVaultReward.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...main/java/com/leonardobishop/quests/bukkit/hook/vault/rewards/PlaceholderVaultReward.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/vault/rewards/VaultReward.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters