Skip to content

Commit

Permalink
added vault economy support for unlocking research
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Oct 20, 2023
1 parent 7a8780b commit 97390e0
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
<!-- TODO: Remove this dependency -->
<dependency>
<groupId>commons-lang</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ public boolean canUnlock(@Nonnull Player p) {
}

boolean creativeResearch = p.getGameMode() == GameMode.CREATIVE && Slimefun.getRegistry().isFreeCreativeResearchingEnabled();
return creativeResearch || p.getLevel() >= cost;
boolean economyResearch = Slimefun.getIntegrations().isVaultInstalled() && Slimefun.getIntegrations().getVaultIntegration().hasBalanceForResearch(p, cost);
boolean expResearch = !Slimefun.getRegistry().isVaultEconomyEnabled() && p.getLevel() >= cost;
return creativeResearch || economyResearch || expResearch;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public final class SlimefunRegistry {
private boolean freeCreativeResearches;
private boolean researchFireworks;
private boolean disableLearningAnimation;
private boolean enableVaultEconomy;
private double economyPriceMultiplier;
private boolean logDuplicateBlockEntries;
private boolean talismanActionBarMessages;

Expand Down Expand Up @@ -111,6 +113,8 @@ public void load(@Nonnull Slimefun plugin, @Nonnull Config cfg) {
freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode");
researchFireworks = cfg.getBoolean("researches.enable-fireworks");
disableLearningAnimation = cfg.getBoolean("researches.disable-learning-animation");
enableVaultEconomy = cfg.getBoolean("researches.enable-vault-economy");
economyPriceMultiplier = cfg.getDouble("researches.economy-price-multiplier");
logDuplicateBlockEntries = cfg.getBoolean("options.log-duplicate-block-entries");
talismanActionBarMessages = cfg.getBoolean("talismans.use-actionbar");
}
Expand Down Expand Up @@ -224,6 +228,24 @@ public boolean isLearningAnimationDisabled() {
return disableLearningAnimation;
}

/**
* Returns whether Vault Economy is enabled for researching
*
* @return Whether Vault Economy is enabled for researching
*/
public boolean isVaultEconomyEnabled() {
return enableVaultEconomy;
}

/**
* Returns the multiplier for prices if Vault Economy is enabled
*
* @return the multiplier to apply to the cost in levels of each research
*/
public double getEconomyPriceMultiplier() {
return economyPriceMultiplier;
}

/**
* This method returns a {@link List} of every enabled {@link MultiBlock}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ default void unlockItem(Player p, SlimefunItem sfitem, Consumer<Player> callback
if (p.getGameMode() == GameMode.CREATIVE && Slimefun.getRegistry().isFreeCreativeResearchingEnabled()) {
research.unlock(p, true, callback);
} else {
p.setLevel(p.getLevel() - research.getCost());
if (Slimefun.getIntegrations().isVaultInstalled() && Slimefun.getRegistry().isVaultEconomyEnabled()) {
Slimefun.getIntegrations().getVaultIntegration().withdrawForResearch(p, research.getCost());
} else {
p.setLevel(p.getLevel() - research.getCost());
}

boolean skipLearningAnimation = Slimefun.getRegistry().isLearningAnimationDisabled() || !SlimefunGuideSettings.hasLearningAnimationEnabled(p);
research.unlock(p, skipLearningAnimation, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ private void displaySlimefunItem(ChestMenu menu, ItemGroup itemGroup, Player p,
menu.addItem(index, new CustomItemStack(ChestMenuUtils.getNoPermissionItem(), sfitem.getItemName(), message.toArray(new String[0])));
menu.addMenuClickHandler(index, ChestMenuUtils.getEmptyClickHandler());
} else if (isSurvivalMode() && research != null && !profile.hasUnlocked(research)) {
menu.addItem(index, new CustomItemStack(ChestMenuUtils.getNotResearchedItem(), ChatColor.WHITE + ItemUtils.getItemName(sfitem.getItem()), "&4&l" + Slimefun.getLocalization().getMessage(p, "guide.locked"), "", "&a> Click to unlock", "", "&7Cost: &b" + research.getCost() + " Level(s)"));
String cost = Slimefun.getIntegrations().isVaultInstalled() && Slimefun.getRegistry().isVaultEconomyEnabled() ? Slimefun.getIntegrations().getVaultIntegration().getResearchPrice(research.getCost()) : research.getCost() + " Level(s)" ;
menu.addItem(index, new CustomItemStack(ChestMenuUtils.getNotResearchedItem(), ChatColor.WHITE + ItemUtils.getItemName(sfitem.getItem()), "&4&l" + Slimefun.getLocalization().getMessage(p, "guide.locked"), "", "&a> Click to unlock", "", "&7Cost: &b" + cost));
menu.addMenuClickHandler(index, (pl, slot, item, action) -> {
research.unlockFromGuide(this, p, profile, sfitem, itemGroup, page);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class IntegrationsManager {
private boolean isClearLagInstalled = false;
private boolean isItemsAdderInstalled = false;
private boolean isOrebfuscatorInstalled = false;
private boolean isVaultInstalled = false;

VaultIntegration vaultIntegration;

/**
* This initializes the {@link IntegrationsManager}
Expand Down Expand Up @@ -130,6 +133,12 @@ private void onServerLoad() {

// ItemsAdder Integration (custom blocks)
load("ItemsAdder", integration -> isItemsAdderInstalled = true);

// Vault integration (research with server currency)
load("Vault", integration -> {
vaultIntegration = new VaultIntegration();
isVaultInstalled = true;
});
}

/**
Expand Down Expand Up @@ -210,6 +219,10 @@ private void load(@Nonnull String pluginName, @Nonnull Consumer<Plugin> consumer
return protectionManager;
}

public @Nonnull VaultIntegration getVaultIntegration() {
return vaultIntegration;
}

/**
* This checks if one of our third party integrations faked an {@link Event}.
* Faked {@link Event Events} should be ignored in our logic.
Expand Down Expand Up @@ -310,4 +323,8 @@ public boolean isItemsAdderInstalled() {
public boolean isOrebfuscatorInstalled() {
return isOrebfuscatorInstalled;
}

public boolean isVaultInstalled() {
return isVaultInstalled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.thebusybiscuit.slimefun4.integrations;

import javax.annotation.Nonnull;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import net.milkbowl.vault.economy.Economy;

/**
* This handles all integrations with Vault's economy.
* Used to unlock research.
*
* @author Mis
*
*/
public class VaultIntegration {
private RegisteredServiceProvider<Economy> economy;

public VaultIntegration() {
this.economy = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
}

public double getBalance(@Nonnull Player player) {
return this.economy.getProvider().getBalance(player);
}

public boolean hasBalance(@Nonnull Player player, double amount) {
return this.getBalance(player) >= amount;
}

public void deposit(@Nonnull Player player, double amount) {
this.economy.getProvider().depositPlayer(player, amount);
}

public void withdraw(@Nonnull Player player, double amount) {
this.economy.getProvider().withdrawPlayer(player, amount);
}

/**
* Checks if the player has enough money to buy this research,
* depending on the xp levels required and the price multiplier.
*
* @return whether the player can afford the research or not.
*/
public boolean hasBalanceForResearch(@Nonnull Player player, int xpCost) {
return this.hasBalance(player, xpCost * Slimefun.getRegistry().getEconomyPriceMultiplier());
}

/**
* Withdraws money from the player by the given amount times the price multiplier.
*/
public void withdrawForResearch(@Nonnull Player player, int xpCost) {
this.withdraw(player, xpCost * Slimefun.getRegistry().getEconomyPriceMultiplier());
}

/**
* Returns the formatted price, according to the xp level cost and price multiplier.
*
* @return the formatted price.
*/
public String getResearchPrice(int xpCost) {
return this.economy.getProvider().format(xpCost * Slimefun.getRegistry().getEconomyPriceMultiplier());
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ researches:
free-in-creative-mode: true
enable-fireworks: true
disable-learning-animation: false
enable-vault-economy: false
economy-price-multiplier: 1000.0

URID:
info-delay: 3000
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ softdepend:
- mcMMO
- ItemsAdder
- Orebfuscator
- Vault

# Our commands
commands:
Expand Down

0 comments on commit 97390e0

Please sign in to comment.