mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
Add TradeWithVillagerEvent (#9244)
This commit is contained in:
1 parent
5ccde7422c
commit
7569de7004
4 files changed
+98
No files matched your search
@@ -1,5 +1,13 @@
|
||||
--- a/net/minecraft/world/entity/npc/AbstractVillager.java
|
||||
+++ b/net/minecraft/world/entity/npc/AbstractVillager.java
|
||||
@@ -116,6 +_,7 @@
|
||||
CriteriaTriggers.f_10585_.m_70959_((ServerPlayer)this.f_35263_, this, p_35274_.m_45368_());
|
||||
}
|
||||
|
||||
+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.player.TradeWithVillagerEvent(this.f_35263_, p_35274_, this));
|
||||
}
|
||||
|
||||
protected abstract void m_8058_(MerchantOffer p_35299_);
|
||||
@@ -164,9 +_,9 @@
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.event.entity.player;
|
||||
|
||||
import net.minecraft.world.entity.npc.AbstractVillager;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.trading.MerchantOffer;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
/**
|
||||
* Fired when a player trades with an {@link AbstractVillager}.
|
||||
*
|
||||
* <p>This event is not {@linkplain Cancelable cancellable}, and does not {@linkplain Event.HasResult have a result}.</p>
|
||||
*
|
||||
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.</p>
|
||||
*/
|
||||
public class TradeWithVillagerEvent extends PlayerEvent
|
||||
{
|
||||
private final MerchantOffer offer;
|
||||
private final AbstractVillager abstractVillager;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public TradeWithVillagerEvent(Player player, MerchantOffer offer, AbstractVillager abstractVillager)
|
||||
{
|
||||
super(player);
|
||||
this.offer = offer;
|
||||
this.abstractVillager = abstractVillager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the {@link MerchantOffer} selected by the player to trade with}
|
||||
*/
|
||||
public MerchantOffer getMerchantOffer()
|
||||
{
|
||||
return offer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the villager the player traded with}
|
||||
*/
|
||||
public AbstractVillager getAbstractVillager()
|
||||
{
|
||||
return abstractVillager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.entity.player;
|
||||
|
||||
import com.mojang.logging.LogUtils;
|
||||
import net.minecraftforge.event.entity.player.TradeWithVillagerEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
|
||||
/**
|
||||
* This tests for {@link TradeWithVillagerEvent} and fires when
|
||||
* the player completes a trade with either a Villager or WanderingTrader(must inherit from AbstractVillager).
|
||||
* This test shows the player name involved along with the villager name and what the result of the trade is(Count and Item).
|
||||
*/
|
||||
@Mod("trade_with_villager_event_test")
|
||||
@Mod.EventBusSubscriber()
|
||||
public class TradeWithVillagerEventTest
|
||||
{
|
||||
|
||||
private static final boolean ENABLE = true;
|
||||
private static final Logger LOGGER = LogUtils.getLogger();
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onPlayerVillagerTrade(TradeWithVillagerEvent event)
|
||||
{
|
||||
if (!ENABLE) return;
|
||||
LOGGER.info("Player {} traded with villager {} and exchanged for {} {}.",
|
||||
event.getEntity().getName().getString(), event.getAbstractVillager().getName().getString(),
|
||||
event.getMerchantOffer().getResult().getCount(),
|
||||
event.getMerchantOffer().getResult().getDisplayName().getString());
|
||||
}
|
||||
}
|
||||
@@ -274,6 +274,8 @@ modId="grindstone_event_test"
|
||||
modId="data_pack_registries_test"
|
||||
[[mods]]
|
||||
modId="creative_mode_tab_test"
|
||||
[[mods]]
|
||||
modId="trade_with_villager_event_test"
|
||||
|
||||
|
||||
# ADD ABOVE THIS LINE
|
||||
Reference in new issue
Block a user