Add placeholder for player balance

This commit is contained in:
Bruno Rybársky 2024-09-17 11:19:09 +02:00
parent 537a428d2c
commit c22ffdc9a2
4 changed files with 40 additions and 3 deletions

@ -17,7 +17,10 @@ repositories {
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven { url 'https://maven.nucleoid.xyz' }
maven {
url 'https://maven.nucleoid.xyz'
name "Nucleoid"
}
maven {
url 'https://kneelawk.com/maven/'
@ -47,6 +50,7 @@ dependencies {
modImplementation include("eu.pb4:polymer-virtual-entity:${project.polymer_version}")
include(modImplementation("xyz.nucleoid:server-translations-api:${project.server_translations_api_version}"))
modImplementation include("eu.pb4:sgui:${project.servergui_version}")
modImplementation include("eu.pb4:placeholder-api:${project.placeholder_api}")
}

@ -6,7 +6,7 @@ minecraft_version=1.21.1
yarn_mappings=1.21.1+build.3
loader_version=0.16.2
# Mod Properties
mod_version=1.4.9
mod_version=1.5.0
maven_group=systems.brn
archives_base_name=servershop
# Dependencies
@ -17,4 +17,5 @@ fabric_version=0.102.1+1.21.1
# Dependencies
polymer_version=0.9.9+1.21
server_translations_api_version=2.3.1+1.21-pre2
servergui_version=1.6.0+1.21
servergui_version=1.6.0+1.21
placeholder_api=2.4.1+1.21

@ -1,11 +1,16 @@
package systems.brn.servershop;
import eu.pb4.placeholders.api.PlaceholderHandler;
import eu.pb4.placeholders.api.PlaceholderResult;
import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.util.Identifier;
import systems.brn.servershop.commands.*;
import systems.brn.servershop.lib.BalancePlaceholder;
import systems.brn.servershop.lib.EventHandler;
import systems.brn.servershop.lib.storages.AuctionStorage;
import systems.brn.servershop.lib.storages.BalanceStorage;
@ -21,11 +26,15 @@ public class ServerShop implements ModInitializer {
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(EventHandler::onServerStarted);
ServerLifecycleEvents.SERVER_STOPPED.register(EventHandler::onServerStopped);
ServerPlayConnectionEvents.JOIN.register(EventHandler::onPlayerJoin);
CommandRegistrationCallback.EVENT.register(CommandRegistry::commandRegister);
BalancePlaceholder balancePlaceholder = new BalancePlaceholder();
Placeholders.register(Identifier.of(MOD_ID, "balance"), balancePlaceholder);
PolymerResourcePackUtils.addModAssets(MOD_ID);
PolymerResourcePackUtils.markAsRequired();

@ -0,0 +1,23 @@
package systems.brn.servershop.lib;
import eu.pb4.placeholders.api.PlaceholderContext;
import eu.pb4.placeholders.api.PlaceholderHandler;
import eu.pb4.placeholders.api.PlaceholderResult;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import systems.brn.servershop.lib.storages.PriceStorage;
import static systems.brn.servershop.ServerShop.balanceStorage;
public class BalancePlaceholder implements PlaceholderHandler {
@Override
public PlaceholderResult onPlaceholderRequest(PlaceholderContext context, @Nullable String argument) {
ServerPlayerEntity player = context.player();
long balance = 0;
if (balanceStorage != null && player != null) {
balance = balanceStorage.getBalance(player);
}
return PlaceholderResult.value(Text.translatable("gui.servershop.balance.editor.value", balance));
}
}