pala M-market-2: expiration annonces 7j + purge flush

This commit is contained in:
Anaïs Saraiva
2026-10-19 06:22:56 +02:00
parent ee42ccca7c
commit 53c7416aae
5 changed files with 67 additions and 1 deletions
@@ -88,6 +88,9 @@ public final class PalaConfig {
// M-faction-2 : claims de chunks (quota par faction, defaut 10, UNVERIFIED).
public int factionMaxClaims = 10;
// M-market-2 (wiki §3.8) : expiration des annonces en jours (defaut 7).
public int marketExpiryDays = 7;
// M-rtp (wiki commandes) : cooldown minutes entre deux /rtp (wiki : 15).
public int rtpCooldownMinutes = 15;
// Rayon max du /rtp autour du spawn (blocs). UNVERIFIED (wiki : "aleatoire", sans borne).
@@ -238,7 +241,7 @@ public final class PalaConfig {
"furnaceSmelts", "furnaceFuels",
"serverName", "wikiUrl", "discordUrl", "shopUrl",
"afkKickMinutes", "rtpCooldownMinutes", "rtpRadius",
"factionMaxClaims");
"factionMaxClaims", "marketExpiryDays");
boolean complete = true;
try (Reader keysReader = Files.newBufferedReader(path)) {
com.google.gson.JsonObject obj =
@@ -28,6 +28,10 @@ public final class PalaCore {
tickCounter = 0;
PalaStorage.get().saveIfDirty();
PalaFactions.get().saveIfDirty();
// M-market-2 (wiki §3.8) : purge des annonces expirees au flush.
PalaMarket.setServer(server);
PalaMarket.get().purgeExpired(
(long) PalaConfig.get().marketExpiryDays * 86400000L);
PalaMarket.get().saveIfDirty();
}
});
@@ -31,6 +31,8 @@ public final class PalaMarket {
public String itemId = "";
public int count = 1;
public double price = 0.0;
// M-market-2 (wiki §3.8) : expiration 7 j (epoch ms, 0 = a initialiser au load).
public long createdAt = 0L;
}
private final Map<Integer, Listing> listings = new HashMap<>();
@@ -59,6 +61,7 @@ public final class PalaMarket {
listing.itemId = itemId;
listing.count = count;
listing.price = price;
listing.createdAt = System.currentTimeMillis();
listings.put(listing.id, listing);
dirty = true;
return listing;
@@ -86,6 +89,56 @@ public final class PalaMarket {
dirty = true;
}
// M-market-2 : purge les annonces expirees (vendues rendues si vendeur connecte,
// sinon supprimees). Appele par PalaCore au flush (set server chaque tick flush).
private static net.minecraft.server.MinecraftServer serverRef = null;
public static void setServer(net.minecraft.server.MinecraftServer server) {
serverRef = server;
}
public void purgeExpired(long expiryMs) {
long now = System.currentTimeMillis();
java.util.List<Integer> gone = new java.util.ArrayList<>();
for (Listing listing : listings.values()) {
if (now - listing.createdAt > expiryMs) {
gone.add(listing.id);
}
}
for (int id : gone) {
Listing listing = listings.remove(id);
if (listing == null) {
continue;
}
dirty = true;
if (serverRef != null) {
try {
net.minecraft.server.level.ServerPlayer seller = serverRef.getPlayerList()
.getPlayer(java.util.UUID.fromString(listing.sellerUuid));
if (seller != null) {
net.minecraft.world.item.Item item =
net.minecraft.core.registries.BuiltInRegistries.ITEM.getValue(
net.minecraft.resources.Identifier.parse(listing.itemId));
if (item != null) {
net.minecraft.world.item.ItemStack stack =
new net.minecraft.world.item.ItemStack(item, listing.count);
if (!seller.getInventory().add(stack)) {
net.minecraft.world.entity.item.ItemEntity entity =
new net.minecraft.world.entity.item.ItemEntity(seller.level(),
seller.getX(), seller.getY(), seller.getZ(), stack);
seller.level().addFreshEntity(entity);
}
seller.sendSystemMessage(net.minecraft.network.chat.Component.literal(
"§6[Market] §7Annonce #" + id + " expiree, objets rendus."));
}
}
} catch (IllegalArgumentException e) {
// UUID vendeur invalide : annonce supprimee sans rendu.
}
}
}
}
public void load() {
Path path = file();
if (!Files.exists(path)) {
@@ -100,6 +153,9 @@ public final class PalaMarket {
Listing listing = GSON.fromJson(GSON.toJson(entry.getValue()), Listing.class);
if (listing != null && listing.id > 0 && listing.itemId != null
&& !listing.itemId.isEmpty()) {
if (listing.createdAt <= 0L) {
listing.createdAt = System.currentTimeMillis();
}
listings.put(listing.id, listing);
if (listing.id >= nextId) {
nextId = listing.id + 1;