Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PlaceholderAPI support #102

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.tnemc</groupId>
<artifactId>Reserve</artifactId>
Expand Down Expand Up @@ -165,6 +171,10 @@
<id>glaremasters repo</id>
<url>https://repo.glaremasters.me/repository/towny/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>

<distributionManagement>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/gestern/gringotts/AccountChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public InventoryHolder chest() {
*
* @return Location of the storage block of this account chest.
*/
private Location chestLocation() {
public Location chestLocation() {
Block block = Util.chestBlock(sign);

return block != null ? block.getLocation() : null;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/gestern/gringotts/Gringotts.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.gestern.gringotts.data.Migration;
import org.gestern.gringotts.dependency.DependencyProviderImpl;
import org.gestern.gringotts.dependency.GenericDependency;
import org.gestern.gringotts.dependency.placeholdersapi.PlaceholderAPIDependency;
import org.gestern.gringotts.dependency.towny.TownyDependency;
import org.gestern.gringotts.event.AccountListener;
import org.gestern.gringotts.event.PlayerVaultListener;
Expand Down Expand Up @@ -201,6 +202,27 @@ public void onLoad() {
);
}

try {
Plugin plugin = this.dependencies.hookPlugin(
"PlaceholderAPI",
"me.clip.placeholderapi.PlaceholderAPIPlugin",
"2.11.0"
);

if (plugin != null) {
if (!this.dependencies.registerDependency(new PlaceholderAPIDependency(
this,
plugin
))) {
getLogger().warning("PlaceholderAPI plugin is already assigned into the dependencies.");
}
}
} catch (IllegalArgumentException e) {
getLogger().warning(
"Looks like PlaceholderAPI plugin is not compatible with Gringotts's code."
);
}

this.registerGenericDependency(
"vault",
"Vault",
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/gestern/gringotts/GringottsAccount.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gestern.gringotts;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.ShulkerBox;
Expand Down Expand Up @@ -93,6 +94,33 @@ public long getVaultBalance() {
return getTimeout(countChestInventories());
}

/**
* Current balance this account has in chest(s) in cents
*
* @return current balance this account has in chest(s) in cents
*/
public long getVaultBalance(int index) {
return getTimeout(countChestInventory(index));
}

/**
* Current balance this account has in chest(s) in cents
*
* @return current balance this account has in chest(s) in cents
*/
public Location getVaultLocation(int index) {
return getTimeout(countChestLocation(index));
}


public List<AccountChest> getVaultChests() {
return getTimeout(getChests());
}

public int getVaultCount() {
return getVaultChests().size();
}

/**
* Current balance this account has in inventory in cents
*
Expand Down Expand Up @@ -363,6 +391,45 @@ private CompletableFuture<Long> countChestInventories() {
return callSync(callMe);
}

private CompletableFuture<Long> countChestInventory(int index) {
Callable<Long> callMe = () -> {
List<AccountChest> chests = dao.retrieveChests(this);

if (CONF.usevaultContainer && index < chests.size() && index >= 0) {
return chests.get(index).balance();
}

Optional<Player> playerOpt = playerOwner();
if (playerOpt.isPresent()) {
Player player = playerOpt.get();

if (CONF.usevaultEnderchest && USE_VAULT_ENDERCHEST.isAllowed(player) && index == -1) {
return new AccountInventory(player.getEnderChest()).balance();
}
}
return -1L;
};

return callSync(callMe);
}

private CompletableFuture<Location> countChestLocation(int index) {
Callable<Location> callMe = () -> {
List<AccountChest> chests = dao.retrieveChests(this);

if (CONF.usevaultContainer && index < chests.size() && index >= 0) {
return chests.get(index).chestLocation();
}
return null;
};

return callSync(callMe);
}

private CompletableFuture<List<AccountChest>> getChests() {
return callSync(() -> dao.retrieveChests(this));
}

private CompletableFuture<Long> countPlayerInventory() {
Callable<Long> callMe = () -> {
long balance = 0;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/gestern/gringotts/api/Account.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.gestern.gringotts.api;

import org.bukkit.Location;

/**
* Defines actions possible on an account in an economy.
*/
Expand Down Expand Up @@ -43,6 +45,27 @@ public interface Account {
*/
double vaultBalance();

/**
* Return the vault at index balance of this account.
*
* @return the vault at index balance of this account.
*/
double vaultBalance(int index);

/**
* Return the vault at index location of this account.
*
* @return the vault at index location of this account.
*/
Location vaultLocation(int index);

/**
* Return the number of vault of this account.
*
* @return the number of vault of this account.
*/
int vaultCount();

/**
* Return the inventory balance of this account.
*
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/gestern/gringotts/api/impl/GringottsEco.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gestern.gringotts.api.impl;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.gestern.gringotts.AccountInventory;
Expand Down Expand Up @@ -258,6 +259,36 @@ public double vaultBalance() {
return 0;
}

/**
* Vault balance double.
*
* @return the double
*/
@Override
public double vaultBalance(int index) {
return 0;
}

/**
* Vault location.
*
* @return the location
*/
@Override
public Location vaultLocation(int index) {
return null;
}

/**
* Vault count.
*
* @return the int
*/
@Override
public int vaultCount() {
return 0;
}

/**
* Inv balance double.
*
Expand Down Expand Up @@ -562,6 +593,36 @@ public double vaultBalance() {
return CONF.getCurrency().getDisplayValue(acc.getVaultBalance());
}

/**
* Vault balance double.
*
* @return the double
*/
@Override
public double vaultBalance(int index) {
return CONF.getCurrency().getDisplayValue(acc.getVaultBalance(index));
}

/**
* Vault location.
*
* @return the location
*/
@Override
public Location vaultLocation(int index) {
return acc.getVaultLocation(index);
}

/**
* Vault count.
*
* @return the int
*/
@Override
public int vaultCount() {
return acc.getVaultCount();
}

/**
* Inv balance double.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.gestern.gringotts.dependency.placeholdersapi;

import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.gestern.gringotts.Gringotts;
import org.gestern.gringotts.api.dependency.Dependency;
import org.gestern.gringotts.dependency.placeholdersapi.placeholders.PlaceholdersRegister;

public class PlaceholderAPIDependency implements Dependency, Listener {

private final Gringotts gringotts;
private final PlaceholderAPIPlugin plugin;
private final String id;

/**
* Instantiates a new PlaceholderAPI dependency.
*
* @param gringotts the gringotts
* @param plugin the plugin
*/
public PlaceholderAPIDependency(Gringotts gringotts,
Plugin plugin) {
if (plugin == null) {
throw new NullPointerException("'plugin' is null");
}

if (!(plugin instanceof PlaceholderAPIPlugin)) {
throw new IllegalArgumentException(
"The 'plugin' needs to be an instance of me.clip.placeholderapi.PlaceholderAPIPlugin"
);
}

this.gringotts = gringotts;
this.plugin = (PlaceholderAPIPlugin) plugin;
this.id = "placeholderapi";
}


/**
* Gets id.
*
* @return the id
*/
@Override
public String getId() {
return id;
}

/**
* Gets plugin.
*
* @return the plugin
*/
@Override
public Plugin getPlugin() {
return this.plugin;
}

@Override
public void onEnable() {
new PlaceholdersRegister(gringotts).register();
}
}
Loading