mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-29 12:27:59 +01:00
Add PlayerTradeEvent and PlayerPurchaseEvent
Co-authored-by: Alexander <protonull@protonmail.com>
This commit is contained in:
parent
46c2fbfb74
commit
597ce940a4
@ -45,7 +45,35 @@
|
|||||||
@Override
|
@Override
|
||||||
public MerchantOffers getOffers() {
|
public MerchantOffers getOffers() {
|
||||||
if (this.level().isClientSide) {
|
if (this.level().isClientSide) {
|
||||||
@@ -179,7 +199,7 @@
|
@@ -121,11 +141,24 @@
|
||||||
|
@Override
|
||||||
|
public void overrideXp(int experience) {}
|
||||||
|
|
||||||
|
+ // Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
@Override
|
||||||
|
- public void notifyTrade(MerchantOffer offer) {
|
||||||
|
- offer.increaseUses();
|
||||||
|
+ public void processTrade(MerchantOffer recipe, @Nullable io.papermc.paper.event.player.PlayerPurchaseEvent event) { // The MerchantRecipe passed in here is the one set by the PlayerPurchaseEvent
|
||||||
|
+ if (event == null || event.willIncreaseTradeUses()) {
|
||||||
|
+ recipe.increaseUses();
|
||||||
|
+ }
|
||||||
|
+ if (event == null || event.isRewardingExp()) {
|
||||||
|
+ this.rewardTradeXp(recipe);
|
||||||
|
+ }
|
||||||
|
+ this.notifyTrade(recipe);
|
||||||
|
+ }
|
||||||
|
+ // Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
+
|
||||||
|
+ @Override
|
||||||
|
+ public void notifyTrade(MerchantOffer offer) {
|
||||||
|
+ // offer.increaseUses(); // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
this.ambientSoundTime = -this.getAmbientSoundInterval();
|
||||||
|
- this.rewardTradeXp(offer);
|
||||||
|
+ // this.rewardTradeXp(offer); // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
if (this.tradingPlayer instanceof ServerPlayer) {
|
||||||
|
CriteriaTriggers.TRADE.trigger((ServerPlayer) this.tradingPlayer, this, offer.getResult());
|
||||||
|
}
|
||||||
|
@@ -179,7 +212,7 @@
|
||||||
public void readAdditionalSaveData(CompoundTag nbt) {
|
public void readAdditionalSaveData(CompoundTag nbt) {
|
||||||
super.readAdditionalSaveData(nbt);
|
super.readAdditionalSaveData(nbt);
|
||||||
if (nbt.contains("Offers")) {
|
if (nbt.contains("Offers")) {
|
||||||
@ -54,7 +82,7 @@
|
|||||||
Logger logger = AbstractVillager.LOGGER;
|
Logger logger = AbstractVillager.LOGGER;
|
||||||
|
|
||||||
Objects.requireNonNull(logger);
|
Objects.requireNonNull(logger);
|
||||||
@@ -246,7 +266,20 @@
|
@@ -246,7 +279,20 @@
|
||||||
MerchantOffer merchantrecipe = ((VillagerTrades.ItemListing) arraylist.remove(this.random.nextInt(arraylist.size()))).getOffer(this, this.random);
|
MerchantOffer merchantrecipe = ((VillagerTrades.ItemListing) arraylist.remove(this.random.nextInt(arraylist.size()))).getOffer(this, this.random);
|
||||||
|
|
||||||
if (merchantrecipe != null) {
|
if (merchantrecipe != null) {
|
||||||
|
@ -214,7 +214,73 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -893,6 +995,11 @@
|
@@ -729,6 +831,14 @@
|
||||||
|
public abstract boolean stillValid(Player player);
|
||||||
|
|
||||||
|
protected boolean moveItemStackTo(ItemStack stack, int startIndex, int endIndex, boolean fromLast) {
|
||||||
|
+ // Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
+ return this.moveItemStackTo(stack, startIndex, endIndex, fromLast, false);
|
||||||
|
+ }
|
||||||
|
+ protected boolean moveItemStackTo(ItemStack stack, int startIndex, int endIndex, boolean fromLast, boolean isCheck) {
|
||||||
|
+ if (isCheck) {
|
||||||
|
+ stack = stack.copy();
|
||||||
|
+ }
|
||||||
|
+ // Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
boolean flag1 = false;
|
||||||
|
int k = startIndex;
|
||||||
|
|
||||||
|
@@ -752,6 +862,11 @@
|
||||||
|
|
||||||
|
slot = (Slot) this.slots.get(k);
|
||||||
|
itemstack1 = slot.getItem();
|
||||||
|
+ // Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent; clone if only a check
|
||||||
|
+ if (isCheck) {
|
||||||
|
+ itemstack1 = itemstack1.copy();
|
||||||
|
+ }
|
||||||
|
+ // Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
if (!itemstack1.isEmpty() && ItemStack.isSameItemSameComponents(stack, itemstack1)) {
|
||||||
|
l = itemstack1.getCount() + stack.getCount();
|
||||||
|
int i1 = slot.getMaxStackSize(itemstack1);
|
||||||
|
@@ -759,12 +874,16 @@
|
||||||
|
if (l <= i1) {
|
||||||
|
stack.setCount(0);
|
||||||
|
itemstack1.setCount(l);
|
||||||
|
+ if (!isCheck) { // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
slot.setChanged();
|
||||||
|
+ } // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
flag1 = true;
|
||||||
|
} else if (itemstack1.getCount() < i1) {
|
||||||
|
stack.shrink(i1 - itemstack1.getCount());
|
||||||
|
itemstack1.setCount(i1);
|
||||||
|
+ if (!isCheck) { // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
slot.setChanged();
|
||||||
|
+ } // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
flag1 = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -795,10 +914,21 @@
|
||||||
|
|
||||||
|
slot = (Slot) this.slots.get(k);
|
||||||
|
itemstack1 = slot.getItem();
|
||||||
|
+ // Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
+ if (isCheck) {
|
||||||
|
+ itemstack1 = itemstack1.copy();
|
||||||
|
+ }
|
||||||
|
+ // Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
if (itemstack1.isEmpty() && slot.mayPlace(stack)) {
|
||||||
|
l = slot.getMaxStackSize(stack);
|
||||||
|
+ // Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
+ if (isCheck) {
|
||||||
|
+ stack.shrink(Math.min(stack.getCount(), l));
|
||||||
|
+ } else {
|
||||||
|
+ // Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
slot.setByPlayer(stack.split(Math.min(stack.getCount(), l)));
|
||||||
|
slot.setChanged();
|
||||||
|
+ } // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
flag1 = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
@@ -893,6 +1023,11 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack getCarried() {
|
public ItemStack getCarried() {
|
||||||
@ -226,7 +292,7 @@
|
|||||||
return this.carried;
|
return this.carried;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -947,4 +1054,15 @@
|
@@ -947,4 +1082,15 @@
|
||||||
this.stateId = this.stateId + 1 & 32767;
|
this.stateId = this.stateId + 1 & 32767;
|
||||||
return this.stateId;
|
return this.stateId;
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,52 @@
|
|||||||
this.addStandardInventorySlots(playerInventory, 108, 84);
|
this.addStandardInventorySlots(playerInventory, 108, 84);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,7 +158,7 @@
|
@@ -108,12 +123,12 @@
|
||||||
|
|
||||||
|
itemstack = itemstack1.copy();
|
||||||
|
if (slot == 2) {
|
||||||
|
- if (!this.moveItemStackTo(itemstack1, 3, 39, true)) {
|
||||||
|
+ if (!this.moveItemStackTo(itemstack1, 3, 39, true, true)) { // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
return ItemStack.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
- slot1.onQuickCraft(itemstack1, itemstack);
|
||||||
|
- this.playTradeSound();
|
||||||
|
+ // slot1.onQuickCraft(itemstack1, itemstack); // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent; moved to after the non-check moveItemStackTo call
|
||||||
|
+ // this.playTradeSound();
|
||||||
|
} else if (slot != 0 && slot != 1) {
|
||||||
|
if (slot >= 3 && slot < 30) {
|
||||||
|
if (!this.moveItemStackTo(itemstack1, 30, 39, false)) {
|
||||||
|
@@ -126,6 +141,7 @@
|
||||||
|
return ItemStack.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
+ if (slot != 2) { // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent; moved down for slot 2
|
||||||
|
if (itemstack1.isEmpty()) {
|
||||||
|
slot1.setByPlayer(ItemStack.EMPTY);
|
||||||
|
} else {
|
||||||
|
@@ -137,13 +153,28 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
slot1.onTake(player, itemstack1);
|
||||||
|
+ } // Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent; handle slot 2
|
||||||
|
+ if (slot == 2) { // is merchant result slot
|
||||||
|
+ slot1.onTake(player, itemstack1);
|
||||||
|
+ if (itemstack1.isEmpty()) {
|
||||||
|
+ slot1.set(ItemStack.EMPTY);
|
||||||
|
+ return ItemStack.EMPTY;
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ this.moveItemStackTo(itemstack1, 3, 39, true, false); // This should always succeed because it's checked above
|
||||||
|
+
|
||||||
|
+ slot1.onQuickCraft(itemstack1, itemstack);
|
||||||
|
+ this.playTradeSound();
|
||||||
|
+ slot1.set(ItemStack.EMPTY); // itemstack1 should ALWAYS be empty
|
||||||
|
+ }
|
||||||
|
+ // Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemstack;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playTradeSound() {
|
private void playTradeSound() {
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
--- a/net/minecraft/world/inventory/MerchantResultSlot.java
|
||||||
|
+++ b/net/minecraft/world/inventory/MerchantResultSlot.java
|
||||||
|
@@ -47,13 +47,32 @@
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTake(Player player, ItemStack stack) {
|
||||||
|
- this.checkTakeAchievements(stack);
|
||||||
|
+ // this.checkTakeAchievements(stack); // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent; move to after event is called and not cancelled
|
||||||
|
MerchantOffer merchantOffer = this.slots.getActiveOffer();
|
||||||
|
+ // Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
+ io.papermc.paper.event.player.PlayerPurchaseEvent event = null;
|
||||||
|
+ if (merchantOffer != null && player instanceof net.minecraft.server.level.ServerPlayer serverPlayer) {
|
||||||
|
+ if (this.merchant instanceof net.minecraft.world.entity.npc.AbstractVillager abstractVillager) {
|
||||||
|
+ event = new io.papermc.paper.event.player.PlayerTradeEvent(serverPlayer.getBukkitEntity(), (org.bukkit.entity.AbstractVillager) abstractVillager.getBukkitEntity(), merchantOffer.asBukkit(), true, true);
|
||||||
|
+ } else if (this.merchant instanceof org.bukkit.craftbukkit.inventory.CraftMerchantCustom.MinecraftMerchant) {
|
||||||
|
+ event = new io.papermc.paper.event.player.PlayerPurchaseEvent(serverPlayer.getBukkitEntity(), merchantOffer.asBukkit(), false, true);
|
||||||
|
+ }
|
||||||
|
+ if (event != null) {
|
||||||
|
+ if (!event.callEvent()) {
|
||||||
|
+ stack.setCount(0);
|
||||||
|
+ event.getPlayer().updateInventory();
|
||||||
|
+ return;
|
||||||
|
+ }
|
||||||
|
+ merchantOffer = org.bukkit.craftbukkit.inventory.CraftMerchantRecipe.fromBukkit(event.getTrade()).toMinecraft();
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+ this.checkTakeAchievements(stack);
|
||||||
|
+ // Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
if (merchantOffer != null) {
|
||||||
|
ItemStack itemStack = this.slots.getItem(0);
|
||||||
|
ItemStack itemStack2 = this.slots.getItem(1);
|
||||||
|
if (merchantOffer.take(itemStack, itemStack2) || merchantOffer.take(itemStack2, itemStack)) {
|
||||||
|
- this.merchant.notifyTrade(merchantOffer);
|
||||||
|
+ this.merchant.processTrade(merchantOffer, event); // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
player.awardStat(Stats.TRADED_WITH_VILLAGER);
|
||||||
|
this.slots.setItem(0, itemStack);
|
||||||
|
this.slots.setItem(1, itemStack2);
|
@ -1,6 +1,14 @@
|
|||||||
--- a/net/minecraft/world/item/trading/Merchant.java
|
--- a/net/minecraft/world/item/trading/Merchant.java
|
||||||
+++ b/net/minecraft/world/item/trading/Merchant.java
|
+++ b/net/minecraft/world/item/trading/Merchant.java
|
||||||
@@ -54,4 +54,6 @@
|
@@ -20,6 +20,7 @@
|
||||||
|
|
||||||
|
void overrideOffers(MerchantOffers offers);
|
||||||
|
|
||||||
|
+ default void processTrade(MerchantOffer merchantRecipe, @Nullable io.papermc.paper.event.player.PlayerPurchaseEvent event) { this.notifyTrade(merchantRecipe); } // Paper
|
||||||
|
void notifyTrade(MerchantOffer offer);
|
||||||
|
|
||||||
|
void notifyTradeUpdated(ItemStack stack);
|
||||||
|
@@ -54,4 +55,6 @@
|
||||||
boolean isClientSide();
|
boolean isClientSide();
|
||||||
|
|
||||||
boolean stillValid(Player player);
|
boolean stillValid(Player player);
|
||||||
|
@ -76,10 +76,25 @@ public class CraftMerchantCustom implements CraftMerchant {
|
|||||||
return this.trades;
|
return this.trades;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Paper start - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
|
@Override
|
||||||
|
public void processTrade(MerchantOffer merchantRecipe, @javax.annotation.Nullable io.papermc.paper.event.player.PlayerPurchaseEvent event) { // The MerchantRecipe passed in here is the one set by the PlayerPurchaseEvent
|
||||||
|
/** Based on {@link net.minecraft.world.entity.npc.AbstractVillager#processTrade(MerchantOffer, io.papermc.paper.event.player.PlayerPurchaseEvent)} */
|
||||||
|
if (getTradingPlayer() instanceof net.minecraft.server.level.ServerPlayer) {
|
||||||
|
if (event == null || event.willIncreaseTradeUses()) {
|
||||||
|
merchantRecipe.increaseUses();
|
||||||
|
}
|
||||||
|
if (event == null || event.isRewardingExp()) {
|
||||||
|
this.tradingPlayer.level().addFreshEntity(new net.minecraft.world.entity.ExperienceOrb(this.tradingPlayer.level(), this.tradingPlayer.getX(), this.tradingPlayer.getY(), this.tradingPlayer.getZ(), merchantRecipe.getXp(), org.bukkit.entity.ExperienceOrb.SpawnReason.VILLAGER_TRADE, this.tradingPlayer, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.notifyTrade(merchantRecipe);
|
||||||
|
}
|
||||||
|
// Paper end - Add PlayerTradeEvent and PlayerPurchaseEvent
|
||||||
@Override
|
@Override
|
||||||
public void notifyTrade(MerchantOffer offer) {
|
public void notifyTrade(MerchantOffer offer) {
|
||||||
// increase recipe's uses
|
// increase recipe's uses
|
||||||
offer.increaseUses();
|
// offer.increaseUses(); // Paper - Add PlayerTradeEvent and PlayerPurchaseEvent; handled above in processTrade
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user