Search in sources :

Example 1 with RepositoryMyPetConverterService

use of de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService in project MyPet by xXKeyleXx.

the class CommandOptionCreate method onCommandOption.

@Override
public boolean onCommandOption(final CommandSender sender, String[] args) {
    if (args.length < 2) {
        return false;
    }
    String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
    int forceOffset = 0;
    if (args[0].equalsIgnoreCase("-f")) {
        forceOffset = 1;
        if (args.length < 3) {
            return false;
        }
    }
    try {
        MyPetType myPetType = MyPetType.byName(args[1 + forceOffset]);
        if (MyPetApi.getMyPetInfo().isLeashableEntityType(EntityType.valueOf(myPetType.getBukkitName()))) {
            Player owner = Bukkit.getPlayer(args[forceOffset]);
            if (owner == null || !owner.isOnline()) {
                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
                return true;
            }
            final MyPetPlayer newOwner;
            if (MyPetApi.getPlayerManager().isMyPetPlayer(owner)) {
                newOwner = MyPetApi.getPlayerManager().getMyPetPlayer(owner);
                if (newOwner.hasMyPet() && forceOffset == 1) {
                    MyPetApi.getMyPetManager().deactivateMyPet(newOwner, true);
                }
            } else {
                newOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(owner);
            }
            final InactiveMyPet inactiveMyPet = new InactiveMyPet(newOwner);
            inactiveMyPet.setPetType(myPetType);
            inactiveMyPet.setPetName(Translation.getString("Name." + inactiveMyPet.getPetType().name(), inactiveMyPet.getOwner().getLanguage()));
            TagCompound compound = inactiveMyPet.getInfo();
            createInfo(myPetType, Arrays.copyOfRange(args, 2 + forceOffset, args.length), compound);
            final WorldGroup wg = WorldGroup.getGroupByWorld(owner.getWorld().getName());
            inactiveMyPet.setWorldGroup(wg.getName());
            Optional<RepositoryMyPetConverterService> converter = MyPetApi.getServiceManager().getService(RepositoryMyPetConverterService.class);
            if (converter.isPresent()) {
                converter.get().convert(inactiveMyPet);
            }
            MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.AdminCommand);
            Bukkit.getServer().getPluginManager().callEvent(createEvent);
            MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
            Bukkit.getServer().getPluginManager().callEvent(saveEvent);
            MyPetApi.getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {

                @Override
                public void callback(Boolean added) {
                    if (added) {
                        if (!newOwner.hasMyPet()) {
                            inactiveMyPet.getOwner().setMyPetForWorldGroup(wg, inactiveMyPet.getUUID());
                            MyPetApi.getRepository().updateMyPetPlayer(inactiveMyPet.getOwner(), null);
                            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
                            if (myPet.isPresent()) {
                                myPet.get().createEntity();
                                sender.sendMessage(Translation.getString("Message.Command.Success", sender));
                            } else {
                                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] Can't create MyPet for " + newOwner.getName() + ". Is this player online?");
                            }
                        } else {
                            sender.sendMessage(Translation.getString("Message.Command.Success", sender));
                        }
                    }
                }
            });
        }
    } catch (MyPetTypeNotFoundException e) {
        sender.sendMessage(Translation.getString("Message.Command.PetType.Unknown", lang));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) Optional(com.google.common.base.Optional) WorldGroup(de.Keyle.MyPet.api.WorldGroup) TagCompound(de.keyle.knbt.TagCompound) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) MyPetTypeNotFoundException(de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)

Example 2 with RepositoryMyPetConverterService

use of de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService in project MyPet by xXKeyleXx.

the class NbtRepository method getMyPets.

@Override
public void getMyPets(final MyPetPlayer owner, final RepositoryCallback<List<StoredMyPet>> callback) {
    if (callback != null) {
        List<StoredMyPet> petList = new ArrayList<>();
        for (UUID petUUID : petPlayerMultiMap.get(owner.getInternalUUID())) {
            if (petTags.containsKey(petUUID)) {
                InactiveMyPet myPet = new InactiveMyPet(owner);
                myPet.load(petTags.get(petUUID));
                Optional<RepositoryMyPetConverterService> converter = MyPetApi.getServiceManager().getService(RepositoryMyPetConverterService.class);
                if (converter.isPresent()) {
                    converter.get().convert(myPet);
                }
                petList.add(myPet);
            }
        }
        callback.run(petList);
    }
}
Also used : RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 3 with RepositoryMyPetConverterService

use of de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService in project MyPet by xXKeyleXx.

the class NbtRepository method getMyPet.

@Override
public void getMyPet(final UUID uuid, final RepositoryCallback<StoredMyPet> callback) {
    if (callback != null) {
        if (petTags.containsKey(uuid)) {
            TagCompound petTag = petTags.get(uuid);
            UUID ownerUUID;
            if (petTag.containsKeyAs("Internal-Owner-UUID", TagString.class)) {
                ownerUUID = UUID.fromString(petTag.getAs("Internal-Owner-UUID", TagString.class).getStringData());
            } else {
                return;
            }
            if (!playerTags.containsKey(ownerUUID)) {
                return;
            }
            MyPetPlayer owner = MyPetApi.getPlayerManager().getMyPetPlayer(ownerUUID);
            if (owner != null) {
                InactiveMyPet myPet = new InactiveMyPet(owner);
                myPet.load(petTag);
                Optional<RepositoryMyPetConverterService> converter = MyPetApi.getServiceManager().getService(RepositoryMyPetConverterService.class);
                if (converter.isPresent()) {
                    converter.get().convert(myPet);
                }
                callback.run(myPet);
            }
        }
    }
}
Also used : RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) TagString(de.keyle.knbt.TagString) TagCompound(de.keyle.knbt.TagCompound)

Example 4 with RepositoryMyPetConverterService

use of de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService in project MyPet by xXKeyleXx.

the class SqLiteRepository method resultSetToMyPet.

// Pets ------------------------------------------------------------------------------------------------------------
private List<StoredMyPet> resultSetToMyPet(MyPetPlayer owner, ResultSet resultSet, boolean next) {
    List<StoredMyPet> pets = new ArrayList<>();
    try {
        while (!next || resultSet.next()) {
            next = true;
            InactiveMyPet pet = new InactiveMyPet(owner);
            pet.setUUID(UUID.fromString(resultSet.getString("uuid")));
            pet.setExp(resultSet.getDouble("exp"));
            pet.setHealth(resultSet.getDouble("health"));
            pet.setRespawnTime(resultSet.getInt("respawn_time"));
            pet.setPetName(new String(resultSet.getBytes("name"), StandardCharsets.UTF_8));
            pet.setPetType(MyPetType.valueOf(resultSet.getString("type")));
            pet.setLastUsed(resultSet.getLong("last_used"));
            pet.setHungerValue(resultSet.getDouble("hunger"));
            pet.setWorldGroup(resultSet.getString("world_group"));
            pet.wantsToRespawn = resultSet.getBoolean("wants_to_spawn");
            String skillTreeName = resultSet.getString("skilltree");
            if (skillTreeName != null) {
                if (SkillTreeMobType.byPetType(pet.getPetType()) != null) {
                    SkillTreeMobType mobType = SkillTreeMobType.byPetType(pet.getPetType());
                    if (mobType.hasSkillTree(skillTreeName)) {
                        pet.setSkilltree(mobType.getSkillTree(skillTreeName));
                    }
                }
            }
            pet.setSkills(TagStream.readTag(resultSet.getBytes("skills"), true));
            pet.setInfo(TagStream.readTag(resultSet.getBytes("info"), true));
            Optional<RepositoryMyPetConverterService> converter = MyPetApi.getServiceManager().getService(RepositoryMyPetConverterService.class);
            if (converter.isPresent()) {
                converter.get().convert(pet);
            }
            pets.add(pet);
        }
    } catch (SQLException | IOException e) {
        e.printStackTrace();
    }
    return pets;
}
Also used : RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) SkillTreeMobType(de.Keyle.MyPet.api.skill.skilltree.SkillTreeMobType) IOException(java.io.IOException) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Aggregations

RepositoryMyPetConverterService (de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService)4 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)4 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)2 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)2 TagCompound (de.keyle.knbt.TagCompound)2 Optional (com.google.common.base.Optional)1 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)1 MyPetCreateEvent (de.Keyle.MyPet.api.event.MyPetCreateEvent)1 MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)1 MyPetTypeNotFoundException (de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)1 SkillTreeMobType (de.Keyle.MyPet.api.skill.skilltree.SkillTreeMobType)1 TagString (de.keyle.knbt.TagString)1 IOException (java.io.IOException)1 Player (org.bukkit.entity.Player)1