Search in sources :

Example 1 with MyPetPlayerJoinEvent

use of de.Keyle.MyPet.api.event.MyPetPlayerJoinEvent in project MyPet by xXKeyleXx.

the class PlayerListener method on.

@EventHandler
public void on(final PlayerJoinEvent event) {
    long delay = MyPetApi.getRepository() instanceof SqLiteRepository ? 1L : Configuration.Repository.EXTERNAL_LOAD_DELAY;
    new BukkitRunnable() {

        @Override
        public void run() {
            MyPetApi.getRepository().getMyPetPlayer(event.getPlayer(), new RepositoryCallback<MyPetPlayer>() {

                @Override
                public void callback(final MyPetPlayer p) {
                    final MyPetPlayerImpl joinedPlayer = (MyPetPlayerImpl) p;
                    joinedPlayer.setLastKnownName(event.getPlayer().getName());
                    if (!event.getPlayer().getUniqueId().equals(joinedPlayer.getOfflineUUID())) {
                        if (joinedPlayer.getMojangUUID() == null) {
                            joinedPlayer.setMojangUUID(event.getPlayer().getUniqueId());
                        }
                        joinedPlayer.setOnlineMode(true);
                    }
                    MyPetApi.getPlayerManager().setOnline(joinedPlayer);
                    final WorldGroup joinGroup = WorldGroup.getGroupByWorld(event.getPlayer().getWorld().getName());
                    if (joinedPlayer.hasMyPet()) {
                        MyPet myPet = joinedPlayer.getMyPet();
                        if (!myPet.getWorldGroup().equals(joinGroup.getName())) {
                            MyPetApi.getMyPetManager().deactivateMyPet(joinedPlayer, true);
                        }
                    }
                    if (!joinedPlayer.hasMyPet() && joinedPlayer.hasMyPetInWorldGroup(joinGroup.getName())) {
                        final UUID petUUID = joinedPlayer.getMyPetForWorldGroup(joinGroup.getName());
                        MyPetApi.getRepository().getMyPet(petUUID, new RepositoryCallback<StoredMyPet>() {

                            @Override
                            public void callback(StoredMyPet storedMyPet) {
                                MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                                if (joinedPlayer.hasMyPet()) {
                                    final MyPet myPet = joinedPlayer.getMyPet();
                                    if (myPet.wantsToRespawn()) {
                                        switch(myPet.createEntity()) {
                                            case Canceled:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", joinedPlayer), myPet.getPetName()));
                                                break;
                                            case NotAllowed:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", joinedPlayer), myPet.getPetName()));
                                                break;
                                            case Dead:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", joinedPlayer), myPet.getPetName(), myPet.getRespawnTime()));
                                                break;
                                            case Flying:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Flying", joinedPlayer), myPet.getPetName()));
                                                break;
                                            case NoSpace:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", joinedPlayer), myPet.getPetName()));
                                                break;
                                        }
                                    }
                                }
                            }
                        });
                    }
                    joinedPlayer.checkForDonation();
                    Bukkit.getServer().getPluginManager().callEvent(new MyPetPlayerJoinEvent(joinedPlayer));
                }
            });
        }
    }.runTaskLater(MyPetApi.getPlugin(), delay);
    if (event.getPlayer().isOp() && Updater.isUpdateAvailable()) {
        event.getPlayer().sendMessage(Util.formatText(Translation.getString("Message.Update.Available", event.getPlayer())) + " " + Updater.getLatest());
        event.getPlayer().sendMessage(ChatColor.DARK_GREEN + "    https://mypet-plugin.de/download");
    }
}
Also used : MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) 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) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) MyPetPlayerJoinEvent(de.Keyle.MyPet.api.event.MyPetPlayerJoinEvent) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) EventHandler(org.bukkit.event.EventHandler)

Aggregations

WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 MyPet (de.Keyle.MyPet.api.entity.MyPet)1 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)1 MyPetPlayerJoinEvent (de.Keyle.MyPet.api.event.MyPetPlayerJoinEvent)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)1 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)1 MyPetPlayerImpl (de.Keyle.MyPet.util.player.MyPetPlayerImpl)1 UUID (java.util.UUID)1 EventHandler (org.bukkit.event.EventHandler)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1