Skip to content
Draft
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
19 changes: 19 additions & 0 deletions api/src/main/java/net/thenextlvl/hologram/action/ClickAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ public interface ClickAction<T> extends Copyable<ClickAction<T>> {
@Contract(mutates = "this")
boolean setPermission(@Nullable String permission);

/**
* Gets the currency name used for transactions of this click action.
*
* @return an {@code Optional} containing the name of the currency
* @since 1.2.0
*/
@Contract(pure = true)
Optional<String> getCurrency();

/**
* Sets the currency name used for transaction of this click action.
*
* @param currency the name of the new currency
* @return {@code true} if the currency was successfully set, {@code false} otherwise
* @since 1.2.0
*/
@Contract(mutates = "this")
boolean setCurrency(@Nullable String currency);

/**
* Gets the cost of this click action.
*
Expand Down
11 changes: 6 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ configurations.compileClasspath {
}

repositories {
mavenLocal()
mavenCentral()
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/")
maven("https://repo.thenextlvl.net/releases")
Expand All @@ -40,7 +41,7 @@ dependencies {
compileOnly("me.clip:placeholderapi:2.12.2")

compileOnly("net.thenextlvl:vault-api:1.7.1")
compileOnly("net.thenextlvl:service-io:2.6.0")
compileOnly("net.thenextlvl:service-io:3.0.0-pre8")

implementation("net.thenextlvl.version-checker:modrinth-paper:1.0.1")
implementation("net.thenextlvl:i18n:1.2.0")
Expand Down Expand Up @@ -151,19 +152,19 @@ paper {
}
serverDependencies {
register("MiniPlaceholders") {
load = PaperPluginDescription.RelativeLoadOrder.BEFORE
load = PaperPluginDescription.RelativeLoadOrder.AFTER
required = false
}
register("PlaceholderAPI") {
load = PaperPluginDescription.RelativeLoadOrder.BEFORE
load = PaperPluginDescription.RelativeLoadOrder.AFTER
required = false
}
register("ServiceIO") {
load = PaperPluginDescription.RelativeLoadOrder.BEFORE
load = PaperPluginDescription.RelativeLoadOrder.AFTER
required = false
}
register("Vault") {
load = PaperPluginDescription.RelativeLoadOrder.BEFORE
load = PaperPluginDescription.RelativeLoadOrder.AFTER
required = false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ final class SimpleClickAction<T> implements ClickAction<T> {
private volatile EnumSet<ClickType> clickTypes;
private volatile T input;

private volatile @Nullable String currency = null;
private volatile @Nullable String permission = null;
private volatile @Range(from = 0, to = 100) int chance = 100;
private volatile Duration cooldown = Duration.ZERO;
private volatile @Nullable String permission = null;
private volatile double cost = 0;

public SimpleClickAction(final HologramPlugin plugin, final ActionType<T> actionType, final EnumSet<ClickType> clickTypes, final T input) {
Expand Down Expand Up @@ -95,6 +96,18 @@ public boolean setPermission(@Nullable final String permission) {
return true;
}

@Override
public Optional<String> getCurrency() {
return Optional.ofNullable(currency);
}

@Override
public boolean setCurrency(@Nullable final String currency) {
if (Objects.equals(this.currency, currency)) return false;
this.currency = currency;
return true;
}

@Override
public double getCost() {
return cost;
Expand Down Expand Up @@ -137,7 +150,7 @@ public boolean invoke(final HologramLine line, final Player player) {
if (isOnCooldown(player)) return false;
if (!getPermission().map(player::hasPermission).orElse(true)) return false;
if (chance < 100 && ThreadLocalRandom.current().nextInt(100) > chance) return false;
if (cost > 0 && !plugin.economyProvider.withdraw(player, cost)) return false;
if (cost > 0 && !plugin.economyProvider.withdraw(player, currency, cost)) return false;
actionType.action().invoke(line, player, input);
if (cooldown.isPositive()) cooldowns.put(player.getUniqueId(), System.currentTimeMillis());
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ static LiteralArgumentBuilder<CommandSourceStack> create(
public int run(final CommandContext<CommandSourceStack> context, final Hologram hologram, final HologramLine line, final ClickAction<?> action, final String actionName, final TagResolver... placeholders) {
final var cost = tryGetArgument(context, "cost", double.class);
final var success = cost.map(action::setCost).orElse(false);
final var message = success ? "hologram.action.cost.set" : cost.isEmpty()
? "hologram.action.cost" : "nothing.changed";
final var message = success ? "hologram.action.cost.set"
: cost.isEmpty() ? "hologram.action.cost" : "nothing.changed";
final var sender = context.getSource().getSender();
final var formatted = plugin.economyProvider.format(sender, cost.orElse(action.getCost()));
final var formatted = plugin.economyProvider.format(sender, action.getCurrency().orElse(null), cost.orElse(action.getCost()));
plugin.bundle().sendMessage(sender, message,
TagResolver.resolver(placeholders),
Placeholder.unparsed("action", actionName),
Placeholder.parsed("cost", formatted));
Placeholder.component("cost", formatted));
return SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ private TagResolver actionResolvers(final Audience audience, final ClickAction<?
return TagResolver.resolver(
Formatter.number("chance", action.getChance()),
Formatter.number("cooldown", action.getCooldown().toMillis() / 1000d),
Placeholder.component("cost", plugin.economyProvider.format(audience, action.getCurrency().orElse(null), action.getCost())),
Placeholder.parsed("action_type", action.getActionType().name()),
Placeholder.parsed("cost", plugin.economyProvider.format(audience, action.getCost())),
Placeholder.parsed("permission", action.getPermission().orElse("null"))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.Locale;

@NullMarked
public interface EconomyProvider {
default String format(final Audience audience, final double amount) {
return format(audience.get(Identity.LOCALE).orElse(Locale.US), amount);
default Component format(final Audience audience, @Nullable final String currency, final double amount) {
return format(audience.get(Identity.LOCALE).orElse(Locale.US), currency, amount);
}

default String format(final Locale locale, final double amount) {
return String.format(locale, "%.2f", amount);
}
Component format(final Locale locale, @Nullable final String currency, final double amount);

boolean withdraw(Player player, double amount);
boolean withdraw(Player player, @Nullable final String currency, double amount);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package net.thenextlvl.hologram.economy;

import net.kyori.adventure.text.Component;
import net.thenextlvl.hologram.HologramPlugin;
import org.bukkit.entity.Player;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.Locale;

@NullMarked
public final class EmptyEconomyProvider implements EconomyProvider {
private final HologramPlugin plugin;

public EmptyEconomyProvider(final HologramPlugin plugin) {
this.plugin = plugin;
}


@Override
public Component format(final Locale locale, @Nullable final String currency, final double amount) {
return Component.text(String.format(locale, "%.2f", amount));
}

@Override
public boolean withdraw(final Player player, final double amount) {
public boolean withdraw(final Player player, @Nullable final String currency, final double amount) {
if (amount == 0) return true;
plugin.getComponentLogger().warn("No economy provider installed, cannot withdraw money");
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.thenextlvl.hologram.economy;

import net.thenextlvl.service.api.economy.EconomyController;
import net.kyori.adventure.text.Component;
import net.thenextlvl.service.economy.EconomyController;
import net.thenextlvl.service.economy.currency.Currency;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.Locale;
import java.util.Optional;
Expand All @@ -20,16 +23,26 @@ private Optional<EconomyController> getController() {
return Optional.ofNullable(plugin.getServer().getServicesManager().load(EconomyController.class));
}

private Optional<Currency> getCurrency(@Nullable final String name) {
return getController().map(controller -> getCurrency(controller, name));
}

private Currency getCurrency(final EconomyController controller, @Nullable final String name) {
final var currencyController = controller.getCurrencyController();
if (name == null) return currencyController.getDefaultCurrency();
return currencyController.getCurrency(name).orElseGet(currencyController::getDefaultCurrency);
}

@Override
public String format(final Locale locale, final double amount) {
return getController().map(controller -> controller.format(amount))
.orElseGet(() -> EconomyProvider.super.format(locale, amount));
public Component format(final Locale locale, @Nullable final String currency, final double amount) {
return getCurrency(currency).map(c -> c.format(amount, locale))
.orElseGet(() -> Component.text(String.format(locale, "%.2f", amount)));
}

@Override
public boolean withdraw(final Player player, final double amount) {
public boolean withdraw(final Player player, @Nullable final String currency, final double amount) {
return getController().flatMap(controller -> controller.getAccount(player).map(account -> {
return !account.getBalance().equals(account.withdraw(amount));
return account.withdraw(amount, getCurrency(controller, currency)).successful();
})).orElse(false);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.thenextlvl.hologram.economy;

import net.kyori.adventure.text.Component;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.Locale;
import java.util.Optional;
Expand All @@ -21,13 +23,13 @@ private Optional<Economy> getEconomy() {
}

@Override
public String format(final Locale locale, final double amount) {
return getEconomy().map(economy -> economy.format(amount))
.orElseGet(() -> EconomyProvider.super.format(locale, amount));
public Component format(final Locale locale, @Nullable final String currency, final double amount) {
return Component.text(getEconomy().map(economy -> economy.format(amount))
.orElseGet(() -> String.format(locale, "%.2f", amount)));
}

@Override
public boolean withdraw(final Player player, final double amount) {
public boolean withdraw(final Player player, @Nullable final String currency, final double amount) {
return getEconomy().map(economy -> {
return economy.withdrawPlayer(player, amount).transactionSuccess();
}).orElse(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import net.thenextlvl.hologram.economy.VaultEconomyProvider;
import net.thenextlvl.hologram.locale.MiniPlaceholdersFormatter;
import net.thenextlvl.hologram.locale.PlaceholderAPIFormatter;
import net.thenextlvl.hologram.service.ServiceHologramController;
import net.thenextlvl.service.hologram.HologramController;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.ServicePriority;
import org.jspecify.annotations.NullMarked;

@NullMarked
Expand All @@ -30,16 +33,23 @@ public void onPluginEnable(final PluginEnableEvent event) {
plugin.miniFormatter = new MiniPlaceholdersFormatter();
plugin.getComponentLogger().info("MiniPlaceholders detected, using for placeholders");
}

if (isPlugin(event.getPlugin(), "ServiceIO")) {
plugin.getServer().getServicesManager().register(
HologramController.class,
new ServiceHologramController(plugin),
plugin,
ServicePriority.Highest
);

plugin.economyProvider = new ServiceEconomyProvider(event.getPlugin());
plugin.getComponentLogger().info("ServiceIO detected, using for economy");
plugin.getComponentLogger().info("ServiceIO detected, using for all services");
} else if (isPlugin(event.getPlugin(), "Vault")) {
plugin.economyProvider = new VaultEconomyProvider(event.getPlugin());
plugin.getComponentLogger().info("Vault detected, using for economy");
}
}

private boolean isPlugin(final Plugin plugin, final String name) {
return plugin.getName().equals(name) || plugin.getPluginMeta().getProvidedPlugins().contains(name);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.thenextlvl.hologram.service;

import net.thenextlvl.hologram.line.BlockHologramLine;
import org.bukkit.block.data.BlockData;
import org.jspecify.annotations.NullMarked;

@NullMarked
public final class ServiceBlockHologramLine extends ServiceDisplayHologramLine<BlockHologramLine> implements net.thenextlvl.service.hologram.line.BlockHologramLine {
public ServiceBlockHologramLine(final ServiceHologram hologram, final BlockHologramLine line) {
super(hologram, line);
}

@Override
public BlockData getBlock() {
return line.getBlock();
}

@Override
public boolean setBlock(final BlockData block) {
return line.setBlock(block);
}
}
Loading
Loading