Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 253
src/main/java/com/takshun/donutahpopup/DonutAhPopupClient.java
package com.takshun.donutahpopup;
import com.mojang.brigadier.arguments.StringArgumentType;
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.message.v1.ClientReceiveMessageEvents;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DonutAhPopupClient implements ClientModInitializer {
private static final List<SaleNotification> ACTIVE_POPUPS = new ArrayList<>();
private static final long POPUP_DURATION_MS = 5500L;
private static final Pattern BOUGHT_YOUR_PATTERN = Pattern.compile(
"(?i)(?:\\[.*?\\]\\s*)?([A-Za-z0-9_.]{2,20})\\s+(?:just\\s+)?(?:bought|purchased)\\s+your\\s+(.+?)\\s+for\\s+\\$?([0-9][0-9,.]*[kKmMbBtT]?)"
);
private static final Pattern SOLD_FOR_PATTERN = Pattern.compile(
"(?i)(?:you\\s+sold|your\\s+(?:item|listing|auction)\\s+(.+?)\\s+sold)\\s*(?:(.+?)\\s+)?for\\s+\\$?([0-9][0-9,.]*[kKmMbBtT]?)"
);
private static final Pattern PRICE_PATTERN = Pattern.compile("\\$([0-9][0-9,.]*[kKmMbBtT]?)");
@Override
public void onInitializeClient() {
ClientReceiveMessageEvents.GAME.register((message, overlay) -> {
if (!overlay) {
checkChatMessage(message.getString());
}
});
ClientReceiveMessageEvents.CHAT.register((message, signedMessage, sender, params, receptionTimestamp) ->
checkChatMessage(message.getString()));
HudRenderCallback.EVENT.register(this::renderPopupHud);
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
dispatcher.register(ClientCommandManager.literal("ahpopuptest")
.executes(context -> {
triggerSalePopup("Netherite Sword", "2.5M", "DrDonut");
context.getSource().sendFeedback(
Text.literal("[AH Popup] ").formatted(Formatting.GOLD)
.append(Text.literal("Triggered a test Auction House sale popup!").formatted(Formatting.GREEN))
);
return 1;
})
.then(ClientCommandManager.argument("price", StringArgumentType.word())
.executes(context -> {
String price = StringArgumentType.getString(context, "price");
triggerSalePopup("1x Spawner", price, "TakShun3");
return 1;
})));
});
}
private void checkChatMessage(String rawText) {
if (rawText == null || rawText.isEmpty()) return;
String clean = rawText.replaceAll("§[0-9a-fk-orA-FK-OR]", "").trim();
String lower = clean.toLowerCase(java.util.Locale.ROOT);
Matcher m1 = BOUGHT_YOUR_PATTERN.matcher(clean);
if (m1.find()) {
triggerSalePopup(m1.group(2).trim(), m1.group(3).trim(), m1.group(1).trim());
return;
}
Matcher m2 = SOLD_FOR_PATTERN.matcher(clean);
if (m2.find()) {
String item = m2.group(1) != null ? m2.group(1).trim() : (m2.group(2) != null ? m2.group(2).trim() : "Auction Item");
triggerSalePopup(item, m2.group(3).trim(), "Buyer");
return;
}
if ((lower.contains("auction") || lower.contains("[ah]") || lower.contains("bought your") || lower.contains("sold your"))
&& (lower.contains("sold") || lower.contains("bought") || lower.contains("purchased"))) {
Matcher priceMatcher = PRICE_PATTERN.matcher(clean);
if (priceMatcher.find()) {
triggerSalePopup("Auction Listing", priceMatcher.group(1).trim(), "DonutSMP AH");
}
}
}
public static void triggerSalePopup(String itemName, String rawPrice, String buyerName) {
String formattedPrice = rawPrice.startsWith("$") ? rawPrice : "$" + rawPrice;
synchronized (ACTIVE_POPUPS) {
ACTIVE_POPUPS.add(0, new SaleNotification(itemName, formattedPrice, buyerName, System.currentTimeMillis()));
if (ACTIVE_POPUPS.size() > 3) {
ACTIVE_POPUPS.remove(ACTIVE_POPUPS.size() - 1);
}
}
MinecraftClient client = MinecraftClient.getInstance();
if (client.player != null) {
client.player.playSound(SoundEvents.ENTITY_PLAYER_LEVELUP, 0.85f, 1.25f);
client.player.playSound(SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, 1.0f, 1.1f);
}
}
private void renderPopupHud(DrawContext context, RenderTickCounter tickCounter) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.player == null || client.options.hudHidden) return;
long now = System.currentTimeMillis();
TextRenderer font = client.textRenderer;
int screenWidth = context.getScaledWindowWidth();
synchronized (ACTIVE_POPUPS) {
Iterator<SaleNotification> iterator = ACTIVE_POPUPS.iterator();
int index = 0;
while (iterator.hasNext()) {
SaleNotification popup = iterator.next();
long elapsed = now - popup.startTime;
if (elapsed > POPUP_DURATION_MS) {
iterator.remove();
continue;
}
float progress = 1.0f;
if (elapsed < 350L) {
float t = elapsed / 350.0f;
progress = 1.0f - (float) Math.pow(1.0f - t, 3);
} else if (elapsed > POPUP_DURATION_MS - 450L) {
float t = (POPUP_DURATION_MS - elapsed) / 450.0f;
progress = Math.max(0.0f, t);
}
String headerText = "★ AUCTION HOUSE SOLD! ★";
String priceText = "+" + popup.price;
String detailText = popup.item + (popup.buyer.isEmpty() ? "" : " • Buyer: " + popup.buyer);
int headerW = font.getWidth(headerText);
int priceW = font.getWidth(priceText) * 2;
int detailW = font.getWidth(detailText);
int boxWidth = Math.max(190, Math.max(headerW, Math.max(priceW, detailW)) + 28);
int boxHeight = 52;
int x = (screenWidth - boxWidth) / 2;
int targetY = 14 + (index * (boxHeight + 8));
int y = (int) (-boxHeight + (targetY + boxHeight) * progress);
int bgColor = 0xE614141F;
int goldBorder = 0xFFFFAA00;
int emeraldBar = 0xFF55FF55;
context.fill(x, y, x + boxWidth, y + boxHeight, bgColor);
context.fill(x, y, x + boxWidth, y + 1, goldBorder);
context.fill(x, y + boxHeight - 1, x + boxWidth, y + boxHeight, goldBorder);
context.fill(x, y, x + 1, y + boxHeight, goldBorder);
context.fill(x + boxWidth - 1, y, x + boxWidth, y + boxHeight, goldBorder);
float timeRemainingRatio = 1.0f - ((float) elapsed / POPUP_DURATION_MS);
int barWidth = (int) ((boxWidth - 4) * timeRemainingRatio);
context.fill(x + 2, y + boxHeight - 3, x + 2 + barWidth, y + boxHeight - 1, emeraldBar);
context.drawTextWithShadow(font, headerText, x + (boxWidth - headerW) / 2, y + 6, 0xFFFFD700);
context.getMatrices().push();
float scale = 1.5f;
context.getMatrices().scale(scale, scale, 1.0f);
int scaledPriceX = (int) ((x + (boxWidth - font.getWidth(priceText) * scale) / 2.0f) / scale);
int scaledPriceY = (int) ((y + 18) / scale);
context.drawTextWithShadow(font, priceText, scaledPriceX, scaledPriceY, 0xFF55FF55);
context.getMatrices().pop();
context.drawTextWithShadow(font, detailText, x + (boxWidth - detailW) / 2, y + 36, 0xFFDDDDDD);
index++;
}
}
}
private static class SaleNotification {
final String item;
final String price;
final String buyer;
final long startTime;
SaleNotification(String item, String price, String buyer, long startTime) {
this.item = item;
this.price = price;
this.buyer = buyer;
this.startTime = startTime;
}
}
}