Skip to content

Commit

Permalink
Adds support for PlaceholderAPI
Browse files Browse the repository at this point in the history
Bump version
  • Loading branch information
RoinujNosde committed Aug 10, 2021
1 parent 644e92f commit 0f202f6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.epconsortium</groupId>
<artifactId>CryptoMarket</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<name>CryptoMarket</name>
<description>A plugin that brings the cryptocoins market experience to your server!</description>
<url>https://www.spigotmc.org/resources/cryptomarket.69031/</url>
Expand Down Expand Up @@ -61,14 +61,18 @@
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<properties>
Expand Down Expand Up @@ -104,6 +108,12 @@
<version>20.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
89 changes: 89 additions & 0 deletions src/main/java/net/epconsortium/cryptomarket/CMExpansion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package net.epconsortium.cryptomarket;

import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.epconsortium.cryptomarket.database.dao.Investor;
import net.epconsortium.cryptomarket.database.dao.InvestorDao;
import net.epconsortium.cryptomarket.finances.ExchangeRate;
import net.epconsortium.cryptomarket.finances.ExchangeRates;
import net.epconsortium.cryptomarket.util.Formatter;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CMExpansion extends PlaceholderExpansion {

private static final Pattern INVESTOR_BALANCE_PATTERN = Pattern.compile("balance_(?<coin>\\w+)");
private static final Pattern COIN_PRICE_PATTERN = Pattern.compile("price_(?<coin>\\w+)");
private static final Pattern INVESTOR_PATRIMONY_PATTERN = Pattern.compile("investor_patrimony");

private final CryptoMarket plugin;
private final InvestorDao investorDao;
private final ExchangeRates exchangeRates;

public CMExpansion(CryptoMarket plugin) {
this.plugin = plugin;
investorDao = plugin.getInvestorDao();
exchangeRates = plugin.getExchangeRates();
}

@Override
public @NotNull String getIdentifier() {
return "cryptomarket";
}

@Override
public @NotNull String getAuthor() {
return "RoinujNosde";
}

@Override
public @NotNull String getVersion() {
return plugin.getDescription().getVersion();
}

@Override
public boolean persist() {
return true;
}

@Override
public @NotNull List<String> getPlaceholders() {
return Arrays.asList("%cryptomarket_investor_patrimony%", "%cryptomarket_balance_<coin>%",
"%cryptomarket_price_<coin>%");
}

@Override
public String onRequest(OfflinePlayer player, @NotNull String params) {
Matcher priceMatcher = COIN_PRICE_PATTERN.matcher(params);
if (priceMatcher.matches()) {
String coin = priceMatcher.group("coin");
ExchangeRate exchangeRate = exchangeRates.getExchangeRate(LocalDate.now());
if (exchangeRate != null) {
return Formatter.formatCryptocoin(exchangeRate.getCoinValue(coin.toUpperCase(Locale.ROOT)));
}
}
Investor investor = investorDao.getInvestor(player);
if (investor == null) {
plugin.getLogger().info(String.format("%s is offline", player.getName()));
return "";
}
if (INVESTOR_PATRIMONY_PATTERN.matcher(params).matches()) {
ExchangeRate exchangeRate = exchangeRates.getExchangeRate(LocalDate.now());
if (exchangeRate != null) {
return Formatter.formatServerCurrency(investor.getConvertedPatrimony(exchangeRate));
}
}
Matcher balanceMatcher = INVESTOR_BALANCE_PATTERN.matcher(params);
if (balanceMatcher.matches()) {
String coin = balanceMatcher.group("coin");
return Formatter.formatCryptocoin(investor.getBalance(coin.toUpperCase(Locale.ROOT)).getValue());
}
return "";
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/epconsortium/cryptomarket/CryptoMarket.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.epconsortium.cryptomarket.task.UpdateExchangeRatesTask;
import net.epconsortium.cryptomarket.task.UpdateRichersListTask;
import net.epconsortium.cryptomarket.ui.InventoryController;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
Expand Down Expand Up @@ -61,6 +62,9 @@ public void onEnable() {
startTasks();
}
});
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new CMExpansion(this).register();
}
}

private void startTasks() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ main: net.epconsortium.cryptomarket.CryptoMarket
version: ${project.version}
api-version: 1.13
description: ${project.description}
softdepend:
- PlaceholderAPI
authors:
- RoinujNosde
- ZaHadden
Expand Down

0 comments on commit 0f202f6

Please sign in to comment.