Search in sources :

Example 1 with NPCRightClickEvent

use of net.citizensnpcs.api.event.NPCRightClickEvent in project Citizens2 by CitizensDev.

the class EventListen method onPlayerInteractEntity.

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    NPC npc = npcRegistry.getNPC(event.getRightClicked());
    if (npc == null || Util.isOffHand(event)) {
        return;
    }
    Player player = event.getPlayer();
    NPCRightClickEvent rightClickEvent = new NPCRightClickEvent(npc, player);
    Bukkit.getPluginManager().callEvent(rightClickEvent);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Player(org.bukkit.entity.Player) NPCRightClickEvent(net.citizensnpcs.api.event.NPCRightClickEvent) EventHandler(org.bukkit.event.EventHandler)

Example 2 with NPCRightClickEvent

use of net.citizensnpcs.api.event.NPCRightClickEvent in project MyPet by xXKeyleXx.

the class StorageTrait method onRightClick.

@EventHandler
public void onRightClick(final NPCRightClickEvent npcEvent) {
    if (this.npc != npcEvent.getNPC()) {
        return;
    }
    final Player player = npcEvent.getClicker();
    if (!Permissions.has(player, "MyPet.npc.storage")) {
        player.sendMessage(Translation.getString("Message.No.Allowed", player));
        return;
    }
    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
        final MyPetPlayer myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(player);
        assert myPetPlayer != null;
        if (myPetPlayer.hasMyPet()) {
            final NPC npc = this.npc;
            MyPetApi.getRepository().getMyPets(myPetPlayer, new RepositoryCallback<List<StoredMyPet>>() {

                @Override
                public void callback(List<StoredMyPet> pets) {
                    WorldGroup wg = WorldGroup.getGroupByWorld(myPetPlayer.getPlayer().getWorld().getName());
                    int inactivePetCount = 0;
                    UUID activePetUUID = myPetPlayer.getMyPet().getUUID();
                    for (StoredMyPet mypet : pets) {
                        if (activePetUUID.equals(mypet.getUUID()) || (!mypet.getWorldGroup().equals("") && !mypet.getWorldGroup().equals(wg.getName()))) {
                            continue;
                        }
                        inactivePetCount++;
                    }
                    int maxPetCount = 0;
                    if (!Permissions.has(player, "MyPet.admin")) {
                        for (int i = Misc.MAX_STORED_PET_COUNT; i > 0; i--) {
                            if (Permissions.hasLegacy(player, "MyPet.petstorage.limit.", i)) {
                                maxPetCount = i;
                                break;
                            }
                        }
                    } else {
                        maxPetCount = Misc.MAX_STORED_PET_COUNT;
                    }
                    if (inactivePetCount == 0 && maxPetCount == 0) {
                        player.sendMessage(Translation.getString("Message.No.Allowed", player));
                        return;
                    }
                    if (inactivePetCount >= maxPetCount) {
                        String stats = "(" + inactivePetCount + "/" + maxPetCount + ")";
                        final MyPetSelectionGui gui = new MyPetSelectionGui(myPetPlayer, stats + " " + Translation.getString("Message.Npc.SwitchTitle", player));
                        gui.open(pets, new RepositoryCallback<StoredMyPet>() {

                            @Override
                            public void callback(StoredMyPet storedMyPet) {
                                MyPetApi.getMyPetManager().deactivateMyPet(myPetPlayer, true);
                                Optional<MyPet> activePet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                                if (activePet.isPresent() && myPetPlayer.isOnline()) {
                                    Player p = myPetPlayer.getPlayer();
                                    myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Npc.ChosenPet", player), activePet.get().getPetName()));
                                    WorldGroup wg = WorldGroup.getGroupByWorld(p.getWorld().getName());
                                    myPetPlayer.setMyPetForWorldGroup(wg, activePet.get().getUUID());
                                    switch(activePet.get().createEntity()) {
                                        case Canceled:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", player), activePet.get().getPetName()));
                                            break;
                                        case NoSpace:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", player), activePet.get().getPetName()));
                                            break;
                                        case NotAllowed:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", player), activePet.get().getPetName()));
                                            break;
                                        case Dead:
                                            if (de.Keyle.MyPet.api.Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", myPetPlayer), activePet.get().getPetName()));
                                            } else {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead.Respawn", myPetPlayer), activePet.get().getPetName(), activePet.get().getRespawnTime()));
                                            }
                                            break;
                                        case Spectator:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Spectator", myPetPlayer), activePet.get().getPetName()));
                                            break;
                                    }
                                }
                            }
                        });
                    } else {
                        IconMenu menu = new IconMenu(Translation.getString("Message.Npc.HandOverTitle", myPetPlayer), event -> {
                            if (!myPetPlayer.hasMyPet()) {
                                return;
                            }
                            if (event.getPosition() == 3) {
                                boolean store = true;
                                double costs = calculateStorageCosts(myPetPlayer.getMyPet());
                                if (MyPetApi.getHookHelper().isEconomyEnabled() && costs > 0 && npc.hasTrait(WalletTrait.class)) {
                                    WalletTrait walletTrait = npc.getTrait(WalletTrait.class);
                                    if (!MyPetApi.getHookHelper().getEconomy().canPay(myPetPlayer, costs)) {
                                        player.sendMessage(Util.formatText(Translation.getString("Message.No.Money", myPetPlayer), myPetPlayer.getMyPet().getPetName(), npcEvent.getNPC().getName()));
                                        store = false;
                                    }
                                    if (MyPetApi.getHookHelper().getEconomy().pay(myPetPlayer, costs)) {
                                        walletTrait.deposit(costs);
                                    } else {
                                        store = false;
                                    }
                                }
                                if (store) {
                                    StoredMyPet storedMyPet = myPetPlayer.getMyPet();
                                    if (MyPetApi.getMyPetManager().deactivateMyPet(myPetPlayer, true)) {
                                        // remove pet from world groups
                                        String wg1 = myPetPlayer.getWorldGroupForMyPet(storedMyPet.getUUID());
                                        myPetPlayer.setMyPetForWorldGroup(wg1, null);
                                        MyPetApi.getRepository().updateMyPetPlayer(myPetPlayer, null);
                                        player.sendMessage(Util.formatText(Translation.getString("Message.Npc.HandOver", myPetPlayer), storedMyPet.getPetName(), npcEvent.getNPC().getName()));
                                    }
                                }
                            }
                            event.setWillClose(true);
                            event.setWillDestroy(true);
                        }, MyPetApi.getPlugin());
                        String[] lore;
                        double storageCosts = calculateStorageCosts(myPetPlayer.getMyPet());
                        if (MyPetApi.getPluginHookManager().isHookActive(VaultHook.class) && npc.hasTrait(WalletTrait.class) && storageCosts > 0) {
                            lore = new String[3];
                            lore[1] = "";
                            lore[2] = RESET + Translation.getString("Name.Costs", myPetPlayer) + ": " + (MyPetApi.getHookHelper().getEconomy().canPay(myPetPlayer, storageCosts) ? GREEN : RED) + storageCosts + DARK_GREEN + " " + MyPetApi.getHookHelper().getEconomy().currencyNameSingular();
                        } else {
                            lore = new String[1];
                        }
                        lore[0] = RESET + Util.formatText(Translation.getString("Message.Npc.YesHandOver", myPetPlayer), myPetPlayer.getMyPet().getPetName());
                        menu.setOption(3, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "GREEN_WOOL")).setData(5).setTitle(GREEN + Translation.getString("Name.Yes", myPetPlayer)).setLore(lore));
                        menu.setOption(5, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "RED_WOOL")).setData(14).setTitle(RED + Translation.getString("Name.No", myPetPlayer)).setLore(RESET + Util.formatText(Translation.getString("Message.Npc.NoHandOver", myPetPlayer), myPetPlayer.getMyPet().getPetName())));
                        menu.open(player);
                    }
                }
            });
        } else {
            MyPetApi.getRepository().getMyPets(myPetPlayer, new RepositoryCallback<List<StoredMyPet>>() {

                @Override
                public void callback(List<StoredMyPet> pets) {
                    if (pets.size() > 0) {
                        int maxPetCount = 0;
                        if (!Permissions.has(player, "MyPet.admin")) {
                            for (int i = Misc.MAX_STORED_PET_COUNT; i > 0; i--) {
                                if (Permissions.hasLegacy(player, "MyPet.petstorage.limit.", i)) {
                                    maxPetCount = i;
                                    break;
                                }
                            }
                        } else {
                            maxPetCount = Misc.MAX_STORED_PET_COUNT;
                        }
                        String stats = "(" + pets.size() + "/" + maxPetCount + ")";
                        MyPetSelectionGui gui = new MyPetSelectionGui(myPetPlayer, Translation.getString("Message.Npc.TakeTitle", myPetPlayer) + " " + stats);
                        gui.open(pets, new RepositoryCallback<StoredMyPet>() {

                            @Override
                            public void callback(StoredMyPet storedMyPet) {
                                Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                                if (myPet.isPresent()) {
                                    Player player = myPetPlayer.getPlayer();
                                    myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Npc.ChosenPet", myPetPlayer), myPet.get().getPetName()));
                                    WorldGroup wg = WorldGroup.getGroupByWorld(player.getWorld().getName());
                                    myPetPlayer.setMyPetForWorldGroup(wg, myPet.get().getUUID());
                                    MyPetApi.getRepository().updateMyPetPlayer(myPetPlayer, null);
                                    switch(myPet.get().createEntity()) {
                                        case Canceled:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", myPetPlayer), myPet.get().getPetName()));
                                            break;
                                        case NoSpace:
                                            myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", myPetPlayer), myPet.get().getPetName()));
                                            break;
                                        case NotAllowed:
                                            myPetPlayer.sendMessage(Translation.getString("Message.No.AllowedHere", myPetPlayer).replace("%petname%", myPet.get().getPetName()));
                                            break;
                                        case Dead:
                                            if (de.Keyle.MyPet.api.Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", myPetPlayer), myPet.get().getPetName()));
                                            } else {
                                                myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead.Respawn", myPetPlayer), myPet.get().getPetName(), myPet.get().getRespawnTime()));
                                            }
                                            break;
                                    }
                                }
                            }
                        });
                    } else {
                        myPetPlayer.sendMessage(Translation.getString("Message.No.HasPet", myPetPlayer), 5000);
                    }
                }
            });
        }
        return;
    }
    player.sendMessage(Translation.getString("Message.No.HasPet", player));
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) EnumSelector(de.Keyle.MyPet.api.util.EnumSelector) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) Player(org.bukkit.entity.Player) WorldGroup(de.Keyle.MyPet.api.WorldGroup) NPC(net.citizensnpcs.api.npc.NPC) EventHandler(org.bukkit.event.EventHandler) Misc(de.Keyle.MyPet.api.Configuration.Misc) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) NPCRightClickEvent(net.citizensnpcs.api.event.NPCRightClickEvent) Translation(de.Keyle.MyPet.api.util.locale.Translation) Trait(net.citizensnpcs.api.trait.Trait) Material(org.bukkit.Material) MyPet(de.Keyle.MyPet.api.entity.MyPet) Util(de.Keyle.MyPet.api.Util) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) MyPetApi(de.Keyle.MyPet.MyPetApi) UUID(java.util.UUID) VaultHook(de.Keyle.MyPet.util.hooks.VaultHook) CitizensHook(de.Keyle.MyPet.util.hooks.CitizensHook) List(java.util.List) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) MyPetSelectionGui(de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui) Permissions(de.Keyle.MyPet.api.player.Permissions) Optional(java.util.Optional) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) ChatColor(org.bukkit.ChatColor) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) VaultHook(de.Keyle.MyPet.util.hooks.VaultHook) MyPetSelectionGui(de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) List(java.util.List) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) EventHandler(org.bukkit.event.EventHandler)

Aggregations

NPCRightClickEvent (net.citizensnpcs.api.event.NPCRightClickEvent)2 NPC (net.citizensnpcs.api.npc.NPC)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 MyPetApi (de.Keyle.MyPet.MyPetApi)1 Misc (de.Keyle.MyPet.api.Configuration.Misc)1 Util (de.Keyle.MyPet.api.Util)1 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 MyPet (de.Keyle.MyPet.api.entity.MyPet)1 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)1 IconMenu (de.Keyle.MyPet.api.gui.IconMenu)1 IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 Permissions (de.Keyle.MyPet.api.player.Permissions)1 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)1 EnumSelector (de.Keyle.MyPet.api.util.EnumSelector)1 Translation (de.Keyle.MyPet.api.util.locale.Translation)1 MyPetSelectionGui (de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui)1 CitizensHook (de.Keyle.MyPet.util.hooks.CitizensHook)1 VaultHook (de.Keyle.MyPet.util.hooks.VaultHook)1