feat/fabric: Add Support for Impactor Economy and PlaceholderAPI (#572)

* Fabric: add economy and placeholderapi hook

* Update gradle.properties
This commit is contained in:
ahdg authored and GitHub committed 2024-02-18 14:03:01 +00:00
1 parent bf5ba2a7a3
commit 046d8e99f7
5 files changed
+208

No files matched your search

@@ -51,7 +51,9 @@ import net.william278.huskhomes.database.H2Database;
import net.william278.huskhomes.database.MySqlDatabase;
import net.william278.huskhomes.database.SqLiteDatabase;
import net.william278.huskhomes.event.FabricEventDispatcher;
import net.william278.huskhomes.hook.FabricImpactorEconomyHook;
import net.william278.huskhomes.hook.Hook;
import net.william278.huskhomes.hook.PlaceHolderAPIHook;
import net.william278.huskhomes.listener.EventListener;
import net.william278.huskhomes.listener.FabricEventListener;
import net.william278.huskhomes.manager.Manager;
@@ -191,6 +193,20 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
this.checkForUpdates();
}
@Override
public void registerHooks() {
HuskHomes.super.registerHooks();
// Register the impactor economy service if it is available
if (getSettings().getEconomy().isEnabled() && isDependencyLoaded("impactor")) {
getHooks().add(new FabricImpactorEconomyHook(this));
}
if (isDependencyLoaded("placeholder-api")) {
getHooks().add(new PlaceHolderAPIHook(this));
}
}
private void onDisable() {
if (this.eventListener != null) {
this.eventListener.handlePluginDisable();
@@ -0,0 +1,89 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.william278.huskhomes.hook;
import net.impactdev.impactor.api.economy.EconomyService;
import net.impactdev.impactor.api.economy.accounts.Account;
import net.impactdev.impactor.api.economy.currency.Currency;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;
public class FabricImpactorEconomyHook extends EconomyHook {
private EconomyService economyService;
private Currency currency;
public FabricImpactorEconomyHook(@NotNull HuskHomes plugin) {
super(plugin, "Fabric Impactor Economy");
}
@Override
public double getPlayerBalance(@NotNull OnlineUser player) {
try {
//If no account is found, a new account will be generated instead.
final Account account = economyService.account(player.getUuid()).get();
return account.balance().doubleValue();
} catch (ExecutionException | InterruptedException e) {
return BigDecimal.ZERO.doubleValue();
}
}
@Override
public void changePlayerBalance(@NotNull OnlineUser player, double amount) {
if (amount == 0d) {
return;
}
final Account account;
try {
account = economyService.account(player.getUuid()).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
final double currentBalance = account.balance().doubleValue();
final double amountToChange = Math.abs(currentBalance - Math.max(0d, currentBalance + amount));
if (amount < 0d) {
account.withdrawAsync(
BigDecimal.valueOf(amountToChange)
);
} else {
account.depositAsync(
BigDecimal.valueOf(amountToChange)
);
}
}
@Override
public String formatCurrency(double amount) {
return PlainTextComponentSerializer.plainText().serialize(currency.format(BigDecimal.valueOf(amount)));
}
@Override
public void initialize() {
this.economyService = EconomyService.instance();
if (this.economyService == null) {
throw new IllegalStateException("Impactor API is not available, No economy service has been registered!");
}
this.currency = this.economyService.currencies().primary();
}
}
@@ -0,0 +1,88 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.william278.huskhomes.hook;
import eu.pb4.placeholders.api.PlaceholderResult;
import eu.pb4.placeholders.api.Placeholders;
import net.minecraft.util.Identifier;
import net.william278.huskhomes.FabricHuskHomes;
import net.william278.huskhomes.user.FabricUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class PlaceHolderAPIHook extends Hook {
public PlaceHolderAPIHook(@NotNull FabricHuskHomes plugin) {
super(plugin, "Fabric PlaceholderAPI");
}
private Identifier createIdentifier(String placeholderName) {
return new Identifier("huskhomes", placeholderName);
}
@Override
public void initialize() {
Placeholders.register(createIdentifier("player"), (ctx, arg) -> {
if (!ctx.hasPlayer()) {
return PlaceholderResult.invalid("No player!");
}
if (arg == null) {
return PlaceholderResult.invalid("No argument for player!");
}
assert ctx.player() != null;
final OnlineUser player = FabricUser.adapt(ctx.player(), (FabricHuskHomes) plugin);
final String response = switch (arg) {
case "homes_count" -> String.valueOf(plugin.getManager().homes()
.getUserHomes()
.getOrDefault(player.getUsername(), List.of()).size());
case "max_homes" -> String.valueOf(plugin.getManager().homes().getMaxHomes(player));
case "max_public_homes" -> String.valueOf(plugin.getManager().homes().getMaxPublicHomes(player));
case "free_home_slots" -> String.valueOf(plugin.getManager().homes().getFreeHomes(player));
case "home_slots" -> String.valueOf(plugin.getSavedUser(player)
.map(SavedUser::getHomeSlots).orElse(0));
case "homes_list" -> String.join(", ", plugin.getManager().homes()
.getUserHomes()
.getOrDefault(player.getUsername(), List.of()));
case "public_homes_count" -> String.valueOf(plugin.getManager().homes()
.getPublicHomes()
.getOrDefault(player.getUsername(), List.of()).size());
case "public_homes_list" -> String.join(", ", plugin.getManager().homes()
.getPublicHomes()
.getOrDefault(player.getUsername(), List.of()));
case "ignoring_tp_requests" -> String.valueOf(plugin.getManager().requests()
.isIgnoringRequests(player));
default -> null;
};
if (response == null) {
return PlaceholderResult.invalid("invaild argument for player!");
}
return PlaceholderResult.value(response);
});
}
}