Jump to content

[1.14.3] Check VillagerEntity attributes before opening trade screen


Recommended Posts


I am trying to prevent a player from opening the trade screen with a villager if they have previously removed the villager's nose with a pair of shears. The trade screen should never open, and the player should be told "Leave me alone! I miss my nose". 


I have stored whether the villager has a nose by attaching a capability to all instances of VillagerEntity. But I am having difficulty obtaining the VillagerEntity instance before I open the trade screen. I tried casting the IMerchant from the MerchantContainer to an AbstractVillagerEntity (which implements IMerchant) as well as VillagerEntity, but all I get are ClassCastExceptions. I must be overlooking something.


This is what I have so far (throws ClassCastException):

    public static void guiOpenEvent(GuiScreenEvent event){
        if (event.getGui() instanceof MerchantScreen){
            IMerchant merchant = ObfuscationReflectionHelper.getPrivateValue(MerchantContainer.class, ((MerchantScreen) event.getGui()).getContainer(), "merchant");
            if (merchant instanceof VillagerEntity){
                VillagerEntity villager = (VillagerEntity) merchant;
                INose noseCapability = villager.getCapability(NOSE_CAP).orElseThrow(() -> new RuntimeException("No inventory!"));
                if (!noseCapability.getHasNose()){
                    event.getGui().getMinecraft().player.sendChatMessage(String.valueOf(new TranslationTextComponent("translation.villagersnose.trade_refusal", (Object) null)));


How can I determine which VillagerEntity instance the player is attempting to trade with?

Edited by eafooe
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.