VillagerInventoryMod.java 文件源码

java
阅读 25 收藏 0 点赞 0 评论 0

项目:VillagerInventory 作者:
@SubscribeEvent
public static void onPlayerEntityInteract(PlayerInteractEvent.EntityInteract event)
{
    // if the villager inventory gui is enabled, if the entity being interacted with is a villager, and if the player is sneaking...
    if ((ModConfiguration.enableInventoryGui) && (event.getTarget() instanceof EntityVillager) && (EntityUtils.getFlag(event.getEntityPlayer(), 2)))
    {
        // if opening the gui requires the player to not be holding anything, check to ensure that the player isn't holding anything
        if (!ModConfiguration.requireEmptyHand || (ModConfiguration.requireEmptyHand && event.getEntityPlayer().inventory.getCurrentItem().isEmpty()))
        {
            // if the event is running on the server
            if (!event.getWorld().isRemote)
            {
                // cast the entity to type EntityVillager
                EntityVillager villager = (EntityVillager)event.getTarget();

                // get the entity id of the villager
                int entityId = villager.getEntityId();

                // and read it's profession and career from it's nbt data
                NBTTagCompound compound = new NBTTagCompound();
                villager.writeEntityToNBT(compound);
                int professionId = compound.getInteger("Profession");
                int careerId = compound.getInteger("Career");

                // send a message to the client to tell it what the villager's career is
                NETWORK.sendTo(new VillagerCareerMessage(entityId, careerId), (EntityPlayerMP)event.getEntityPlayer());

                // open the inventory gui
                FMLNetworkHandler.openGui(event.getEntityPlayer(), VillagerInventoryMod.INSTANCE, ModGuiHandler.GUI_VILLAGER_INVENTORY, event.getWorld(), entityId, professionId, careerId);
            }

            // cancel the event which would show the trading gui
            event.setCanceled(true);
        }
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号