Paper/patches/server/0757-More-vanilla-friendly-methods-to-update-trades.patch
Spottedleaf 8c5b837e05 Rework async chunk api implementation
Firstly, the old methods all routed to the CompletableFuture method.
However, the CF method could not guarantee that if the caller
was off-main that the future would be "completed" on-main. Since
the callback methods used the CF one, this meant that the callback
methods did not guarantee that the callbacks were to be called on
the main thread.

Now, all methods route to getChunkAtAsync(x, z, gen, urgent, cb)
so that the methods with the callback are guaranteed to invoke
the callback on the main thread. The CF behavior remains unchanged;
it may still appear to complete on main if invoked off-main.

Secondly, remove the scheduleOnMain invocation in the async
chunk completion. This unnecessarily delays the callback
by 1 tick.

Thirdly, add getChunksAtAsync(minX, minZ, maxX, maxZ, ...) which
will load chunks within an area. This method is provided as a helper
as keeping all chunks loaded within an area can be complicated to
implement for plugins (due to the lacking ticket API), and is
already implemented internally anyways.

Fourthly, remove the ticket addition that occured with getChunkAt
and getChunkAtAsync. The ticket addition may delay the unloading
of the chunk unnecessarily. It also fixes a very rare timing bug
where the future/callback would be completed after the chunk
unloads.
2024-11-18 23:00:59 -08:00

76 lines
3.9 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
Date: Sun, 16 Oct 2022 16:12:49 +0200
Subject: [PATCH] More vanilla friendly methods to update trades
diff --git a/src/main/java/net/minecraft/world/entity/npc/Villager.java b/src/main/java/net/minecraft/world/entity/npc/Villager.java
index f51078e4b9e6267fa43795d009f5d149b86acb5a..a573aa4d387ad3a4e1017890f2b50b83a3c27ff4 100644
--- a/src/main/java/net/minecraft/world/entity/npc/Villager.java
+++ b/src/main/java/net/minecraft/world/entity/npc/Villager.java
@@ -895,6 +895,12 @@ public class Villager extends AbstractVillager implements ReputationEventHandler
@Override
protected void updateTrades() {
+ // Paper start - More vanilla friendly methods to update trades
+ updateTrades(TRADES_PER_LEVEL);
+ }
+
+ public boolean updateTrades(int amount) {
+ // Paper end - More vanilla friendly methods to update trades
VillagerData villagerdata = this.getVillagerData();
Int2ObjectMap int2objectmap;
@@ -912,9 +918,11 @@ public class Villager extends AbstractVillager implements ReputationEventHandler
if (avillagertrades_imerchantrecipeoption != null) {
MerchantOffers merchantrecipelist = this.getOffers();
- this.addOffersFromItemListings(merchantrecipelist, avillagertrades_imerchantrecipeoption, 2);
+ this.addOffersFromItemListings(merchantrecipelist, avillagertrades_imerchantrecipeoption, amount); // Paper - More vanilla friendly methods to update trades
+ return true; // Paper - More vanilla friendly methods to update trades
}
}
+ return false; // Paper - More vanilla friendly methods to update trades
}
public void gossip(ServerLevel world, Villager villager, long time) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java
index 45c44c46edee9f46b8e197f1f54ea2779bf1184c..8e895d6f84f7d84b219f2424909dd42e5f08dec4 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java
@@ -98,6 +98,34 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
}
// Paper start
+ @Override
+ public boolean increaseLevel(int amount) {
+ Preconditions.checkArgument(amount > 0, "Level earned must be positive");
+ int supposedFinalLevel = this.getVillagerLevel() + amount;
+ Preconditions.checkArgument(net.minecraft.world.entity.npc.VillagerData.MIN_VILLAGER_LEVEL <= supposedFinalLevel && supposedFinalLevel <= net.minecraft.world.entity.npc.VillagerData.MAX_VILLAGER_LEVEL,
+ "Final level reached after the donation (%d) must be between [%d, %d]".formatted(supposedFinalLevel, net.minecraft.world.entity.npc.VillagerData.MIN_VILLAGER_LEVEL, net.minecraft.world.entity.npc.VillagerData.MAX_VILLAGER_LEVEL));
+
+ it.unimi.dsi.fastutil.ints.Int2ObjectMap<net.minecraft.world.entity.npc.VillagerTrades.ItemListing[]> trades =
+ net.minecraft.world.entity.npc.VillagerTrades.TRADES.get(this.getHandle().getVillagerData().getProfession());
+
+ if (trades == null || trades.isEmpty()) {
+ this.getHandle().setVillagerData(this.getHandle().getVillagerData().setLevel(supposedFinalLevel));
+ return false;
+ }
+
+ while (amount > 0) {
+ this.getHandle().increaseMerchantCareer();
+ amount--;
+ }
+ return true;
+ }
+
+ @Override
+ public boolean addTrades(int amount) {
+ Preconditions.checkArgument(amount > 0, "Number of trades unlocked must be positive");
+ return this.getHandle().updateTrades(amount);
+ }
+
@Override
public int getRestocksToday() {
return getHandle().numberOfRestocksToday;