Home > other >  Minecraft Bukkit - Custom GUI when right click on a villager
Minecraft Bukkit - Custom GUI when right click on a villager

Time:10-31

I'm making a [Bed Wars][1] plugin for my Minecraft server. Actually I have to make a custom villager shop.

I have made my GUI (with implements InventoryHolder). It's working with a commands. I have searched all over the Internet, but I have not found anything on a system that makes, when you right click on a villager (I know how to spawn it) it's is showing my GUI. What would be an idea for that?

edit : I tried to use PlayerInteractAtEntityEvent, i maked a class, register it and make this code :

    @EventHandler
    public void interactAtEntity(PlayerInteractAtEntityEvent e) {
        if (e.getRightClicked() == ShopVillager.villager1) {
            System.out.println("UwU");
            Player player = e.getPlayer();
            FastShop shop = new FastShop(player);
            player.openInventory(shop.getInventory());
            e.setCancelled(true);
            return;
        }
    }

It show the gui like 0.2sec but after i close it and show the original trade gui, and i got the uwu in my console.

CodePudding user response:

It depend of how you make the PNJ (villager).

  1. Spawn PNJ as default entity

If you spawn it with world.spawnEntity, you can use default spigot event.

For example, with PlayerInteractAtEntityEvent, you can get the entity.

  1. Spawn entity with Packet

Personnally, I used packet to detect PacketPlayInUseEntity and get villager with the ID.

  1. Globally, with multiple interact event and by checking location/nearest PNJ, you will be able to find the which one you are looking for.

CodePudding user response:

If you want to have greater versatility and use things other than villagers, consider using the Citizens2 API, which lets you easily spawn entities and attach traits to them.

Adding Citizens to your Maven project

    <repository>
        <id>everything</id>
        <url>https://repo.citizensnpcs.co/</url>
    </repository>
    <dependency>
        <groupId>net.citizensnpcs</groupId>
        <artifactId>citizens-main</artifactId>
        <version>VERSION</version>
        <type>jar</type>
        <scope>provided</scope>
    </dependency>

Custom Shop NPC

You can spawn an NPC, and attach a custom Trait to it. Here's how I implemented it in a minigame I made:


public class ItemShopTrait extends Trait
{
    public ItemShopTrait()
    {
        super("itemshoptrait");
    }
   
    
    @EventHandler
    public void rightClick(net.citizensnpcs.api.event.NPCRightClickEvent rightClickEvent)
    {
        if (rightClickEvent.getNPC().equals(this.getNPC()))
        {
            this.openShop(rightClickEvent.getClicker());
        }
    }
    
    @EventHandler
    public void leftClick(net.citizensnpcs.api.event.NPCLeftClickEvent leftClickEvent)
    {
        if (leftClickEvent.getNPC().equals(this.getNPC()))
        {
            this.openShop(leftClickEvent.getClicker());
        }
    }
    
    public void openShop(Player player)
    {
        // Open shop inventory, etc.
    }
}

And then to spawn the NPC and attach the trait:

Location shopLocation = /* Location where you want the NPC to spawn */;

// shopList is an ArrayList of usernames that have skins you want to use. This randomly chooses one of the skins from the list.
Random random = new Random();
String randomName = MapBase.shopList.get(random.nextInt(shopList.size())); 

// EntityType.PLAYER can be changed to whatever EntityType you want, like a VILLAGER
// "Shop" is the name of the NPC
NPC npc = CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, "Shop");

// Sets the NPC's skin to always stay the same
npc.data().setPersistent(NPC.PLAYER_SKIN_UUID_METADATA, randomName);

// Add the right/left click traits to the NPC
npc.addTrait(new ItemShopTrait());

// No capes
SkinLayers trait = npc.getTrait(SkinLayers.class);
trait.hideCape();
trait.setVisible(SkinLayers.Layer.CAPE, false);

// Spawn the NPC in the world
npc.spawn(shopLocation);
  • Related