Files
src
AUCTION_REPORT.md
build.gradle
gradle.properties
settings.gradle
Code
7 412
src/main/java/dev/kodari/auction/ServerAuction.java
package dev.kodari.auction;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import java.util.UUID;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
public final class ServerAuction implements ModInitializer {
private static final int PAGE_SIZE = 8;
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(
CommandManager.literal("auction")
.executes(context -> list(context.getSource(), 1))
.then(CommandManager.literal("list")
.executes(context -> list(context.getSource(), 1))
.then(CommandManager.argument("page", IntegerArgumentType.integer(1))
.executes(context -> list(context.getSource(), IntegerArgumentType.getInteger(context, "page")))))
.then(CommandManager.literal("sell")
.then(CommandManager.argument("price", IntegerArgumentType.integer(1))
.executes(context -> sell(context.getSource(), IntegerArgumentType.getInteger(context, "price")))))
.then(CommandManager.literal("buy")
.then(CommandManager.argument("id", StringArgumentType.word())
.executes(context -> buy(context.getSource(), StringArgumentType.getString(context, "id")))))
.then(CommandManager.literal("cancel")
.then(CommandManager.argument("id", StringArgumentType.word())
.executes(context -> cancel(context.getSource(), StringArgumentType.getString(context, "id")))))
.then(CommandManager.literal("claim").executes(context -> claim(context.getSource())))
.then(CommandManager.literal("help").executes(context -> help(context.getSource())))
));
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("money")
.executes(context -> money(context.getSource()))
.then(CommandManager.literal("pay")
.then(CommandManager.argument("player", EntityArgumentType.player())
.then(CommandManager.argument("amount", IntegerArgumentType.integer(1))
.executes(context -> pay(context.getSource(), EntityArgumentType.getPlayer(context, "player"), IntegerArgumentType.getInteger(context, "amount"))))))
.then(CommandManager.literal("set")
.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK))
.then(CommandManager.argument("player", EntityArgumentType.player())
.then(CommandManager.argument("amount", IntegerArgumentType.integer(0))
.executes(context -> setMoney(context.getSource(), EntityArgumentType.getPlayer(context, "player"), IntegerArgumentType.getInteger(context, "amount")))))));
dispatcher.register(CommandManager.literal("buyer")
.executes(context -> buyer(context.getSource()))
.then(CommandManager.literal("sell").executes(context -> buyer(context.getSource())))
.then(CommandManager.literal("setprice")
.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK))
.then(CommandManager.argument("price", IntegerArgumentType.integer(0))
.executes(context -> setPrice(context.getSource(), IntegerArgumentType.getInteger(context, "price"))))));
});
}
private static AuctionState state(ServerCommandSource source) {
return source.getServer().getOverworld().getPersistentStateManager().getOrCreate(AuctionState.TYPE);
}
private static int help(ServerCommandSource source) {
source.sendMessage(Text.literal("Auction: /auction list [page], /auction sell <dollars>, /auction buy <id>, /auction cancel <id>, /auction claim"));
return 1;
}
private static int list(ServerCommandSource source, int page) {
var active = state(source).listings.stream().filter(entry -> !entry.sold()).toList();
int pages = Math.max(1, (active.size() + PAGE_SIZE - 1) / PAGE_SIZE);
if (page > pages) {
source.sendError(Text.literal("There are only " + pages + " pages."));
return 0;
}
source.sendMessage(Text.literal("Auction listings (page " + page + "/" + pages + ") — prices in dollars"));
int start = (page - 1) * PAGE_SIZE;
for (int i = start; i < Math.min(start + PAGE_SIZE, active.size()); i++) {
var entry = active.get(i);
source.sendMessage(Text.literal(entry.id() + " | " + entry.item().getCount() + "x " + entry.item().getName().getString()
+ " | $" + entry.price() + " | " + entry.sellerName()));
}
if (active.isEmpty()) source.sendMessage(Text.literal("No active listings."));
source.sendMessage(Text.literal("Use /auction help for commands."));
return 1;
}
private static int sell(ServerCommandSource source, int price) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayerEntity player = source.getPlayerOrThrow();
ItemStack held = player.getMainHandStack();
if (held.isEmpty()) {
source.sendError(Text.literal("Hold the stack you want to sell."));
return 0;
}
AuctionState state = state(source);
long own = state.listings.stream().filter(entry -> entry.seller().equals(player.getUuid()) && !entry.sold()).count();
if (own >= 20) {
source.sendError(Text.literal("You can have at most 20 active listings."));
return 0;
}
String id = UUID.randomUUID().toString();
state.listings.add(new AuctionState.Listing(id, player.getUuid(), player.getName().getString(), held.copy(), price, false));
held.decrement(held.getCount());
state.markDirty();
source.sendMessage(Text.literal("Listed your stack for $" + price + ". ID: " + id));
return 1;
}
private static int buy(ServerCommandSource source, String id) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayerEntity player = source.getPlayerOrThrow();
AuctionState state = state(source);
int index = find(state, id);
if (index < 0 || state.listings.get(index).sold()) {
source.sendError(Text.literal("No active listing with that ID."));
return 0;
}
var entry = state.listings.get(index);
if (entry.seller().equals(player.getUuid())) {
source.sendError(Text.literal("You cannot buy your own listing; use /auction cancel instead."));
return 0;
}
long buyerBalance = state.balance(player.getUuid());
long sellerBalance = state.balance(entry.seller());
if (buyerBalance < entry.price()) {
source.sendError(Text.literal("You need $" + entry.price() + "."));
return 0;
}
if (sellerBalance > Long.MAX_VALUE - entry.price()) {
source.sendError(Text.literal("Seller balance limit reached."));
return 0;
}
state.setBalance(player.getUuid(), buyerBalance - entry.price());
state.setBalance(entry.seller(), sellerBalance + entry.price());
state.listings.remove(index);
state.markDirty();
player.getInventory().offerOrDrop(entry.item().copy());
source.sendMessage(Text.literal("Purchase complete. Items that did not fit were dropped at your feet."));
return 1;
}
private static int cancel(ServerCommandSource source, String id) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayerEntity player = source.getPlayerOrThrow();
AuctionState state = state(source);
int index = find(state, id);
if (index < 0 || state.listings.get(index).sold() || !state.listings.get(index).seller().equals(player.getUuid())) {
source.sendError(Text.literal("No active listing belonging to you with that ID."));
return 0;
}
var entry = state.listings.remove(index);
state.markDirty();
player.getInventory().offerOrDrop(entry.item().copy());
source.sendMessage(Text.literal("Listing cancelled. Items that did not fit were dropped at your feet."));
return 1;
}
private static int claim(ServerCommandSource source) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerPlayerEntity player = source.getPlayerOrThrow();
AuctionState state = state(source);
long total = 0;
var iterator = state.listings.iterator();
while (iterator.hasNext()) {
var entry = iterator.next();
if (entry.sold() && entry.seller().equals(player.getUuid())) {
total += entry.price();
iterator.remove();
}
}
if (total == 0) {
source.sendMessage(Text.literal("You have no sale proceeds to claim."));
return 0;
}
state.markDirty();
long remaining = total;
while (remaining > 0) {
int amount = (int) Math.min(64, remaining);
player.getInventory().offerOrDrop(new ItemStack(Items.EMERALD, amount));
remaining -= amount;
}
source.sendMessage(Text.literal("Claimed " + total + " emeralds. Any that did not fit were dropped at your feet."));
return 1;
}
private static int find(AuctionState state, String id) {
for (int i = 0; i < state.listings.size(); i++) {
if (state.listings.get(i).id().equals(id)) return i;
}
return -1;
}
private static int money(ServerCommandSource source) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
source.sendMessage(Text.literal("Баланс: $" + state(source).balance(source.getPlayerOrThrow().getUuid())));
return 1;
}
private static int pay(ServerCommandSource source, ServerPlayerEntity target, int amount) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
var player = source.getPlayerOrThrow();