Files
src
build.gradle
gradle.properties
manifest.kod
README.md
settings.gradle
Code
14 466
src/client/java/com/hipexmc/auctions/AuctionsClient.java
package com.hipexmc.auctions;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.registry.Registries;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
public final class AuctionsClient implements ClientModInitializer {
public static Settings settings;
public static Storage.History history;
public static AuctionData.Session active, completed;
public static long celebrationStart;
private static long deadline;
private static int lastCountdown = -1;
@Override public void onInitializeClient() {
settings = Storage.load("settings.json", Settings.class, new Settings());
history = Storage.load("history.json", Storage.History.class, new Storage.History());
if (history.auctions == null) history.auctions = new java.util.ArrayList<>();
if (!history.auctions.isEmpty()) completed = history.auctions.get(history.auctions.size() - 1);
ClientReceiveMessageEvents.GAME.register((message, overlay) -> {
long received = System.currentTimeMillis();
String text = message.getString();
MinecraftClient.getInstance().execute(() -> receive(text, received));
});
ClientTickEvents.END_CLIENT_TICK.register(client -> tick());
HudElementRegistry.addLast(Identifier.of("hipex_auctions", "auction"), AuctionHud::render);
ClientCommandRegistrationCallback.EVENT.register((dispatcher, access) -> {
dispatcher.register(ClientCommandManager.literal("startbidding").then(ClientCommandManager.argument("seconds", IntegerArgumentType.integer(1, 86400)).executes(c -> { start(IntegerArgumentType.getInteger(c, "seconds")); return 1; })));
dispatcher.register(ClientCommandManager.literal("stopbidding").executes(c -> { stop(); return 1; }));
dispatcher.register(ClientCommandManager.literal("leaderboard").executes(c -> { leaderboard(); return 1; }));
dispatcher.register(ClientCommandManager.literal("viewleaderboards").executes(c -> { open(true); return 1; }));
dispatcher.register(ClientCommandManager.literal("clearleaderboard").executes(c -> { MinecraftClient.getInstance().execute(() -> MinecraftClient.getInstance().setScreen(new AuctionScreen(false, true))); return 1; }));
dispatcher.register(ClientCommandManager.literal("auction").executes(c -> { open(false); return 1; }).then(ClientCommandManager.literal("help").executes(c -> { say("/startbidding <seconds> | /stopbidding | /leaderboard | /viewleaderboards | /clearleaderboard | /auction"); return 1; })));
});
}
public static void open(boolean history) { MinecraftClient.getInstance().execute(() -> MinecraftClient.getInstance().setScreen(new AuctionScreen(history, false))); }
public static void say(String text) {
var player = MinecraftClient.getInstance().player;
if (player != null) player.sendMessage(Text.literal("[Hipex Auctions] " + text), false);
}
public static void start(int seconds) {
tick();
if (active != null) { say("An auction is already running."); return; }
active = new AuctionData.Session(); active.number = history.nextNumber++; active.start = System.currentTimeMillis(); active.durationSeconds = seconds;
deadline = active.start + seconds * 1000L; lastCountdown = -1; celebrationStart = 0;
Storage.save("history.json", history);
say("Auction #" + active.number + " started for " + seconds + " seconds." + (settings.paymentRegex == null || settings.paymentRegex.isBlank() ? " Payment parser is disabled; configure it in settings." : ""));
}
public static void stop() {
if (active == null) { say("There is no active auction."); return; }
finish(Math.min(System.currentTimeMillis(), deadline));
}
private static void receive(String message, long received) {
if (active == null || received < active.start || received >= deadline) return;
PaymentParser.parse(message, settings.paymentRegex).ifPresent(payment -> {
AuctionData.Bid previous = active.bidders.stream().filter(b -> b.name.equalsIgnoreCase(payment.payer())).findFirst().orElse(null);
if (previous == null) active.bidders.add(new AuctionData.Bid(payment.payer(), payment.amount(), received));
else if (payment.amount().compareTo(previous.amount) > 0) { previous.amount = payment.amount(); previous.receivedAt = received; previous.name = payment.payer(); }
else return;
if (settings.notifications) say(payment.payer() + " bid " + AuctionData.money(payment.amount()));
});
}
private static void tick() {
if (active == null) return;
long now = System.currentTimeMillis();
if (now >= deadline) { finish(deadline); return; }
int seconds = (int) ((deadline - now + 999) / 1000);
if (seconds <= 3 && seconds != lastCountdown) {
lastCountdown = seconds;
if (settings.countdownSound) MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.ui(SoundEvents.UI_BUTTON_CLICK, 1f));
}
}
private static void finish(long end) {
active.end = end; completed = active; active = null; history.auctions.add(completed); celebrationStart = System.currentTimeMillis();
Storage.save("history.json", history);
var sorted = completed.sorted();
if (settings.notifications) say(sorted.isEmpty() ? "Auction ended: no valid bids." : sorted.getFirst().name + " won with " + AuctionData.money(sorted.getFirst().amount));
if (settings.victorySound) {
Identifier id = Identifier.tryParse(settings.victorySoundId == null ? "" : settings.victorySoundId);
if (id != null) Registries.SOUND_EVENT.getEntry(id).ifPresent(sound -> MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.ui(sound, 1f)));
}
}
public static void leaderboard() {
var session = active != null ? active : completed;
if (session == null) { say("No auction leaderboard available."); return; }
say("Auction #" + session.number + " — top five:");
var bids = session.sorted();
if (bids.isEmpty()) say("No valid bids.");
for (int i = 0; i < Math.min(5, bids.size()); i++) say((i + 1) + ". " + bids.get(i).name + " — " + AuctionData.money(bids.get(i).amount));
}
public static long remaining() { return active == null ? 0 : Math.max(0, deadline - System.currentTimeMillis()); }
}