Search in sources :

Example 6 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class MySqlRepository method resultSetToMyPet.

// Pets ------------------------------------------------------------------------------------------------------------
private List<StoredMyPet> resultSetToMyPet(MyPetPlayer owner, ResultSet resultSet) {
    List<StoredMyPet> pets = new ArrayList<>();
    try {
        while (resultSet.next()) {
            InactiveMyPet pet = new InactiveMyPet(owner);
            pet.setUUID(UUID.fromString(resultSet.getString("uuid")));
            pet.setWorldGroup(resultSet.getString("world_group"));
            pet.setExp(resultSet.getDouble("exp"));
            pet.setHealth(resultSet.getDouble("health"));
            pet.setRespawnTime(resultSet.getInt("respawn_time"));
            pet.setPetName(Util.toString(resultSet.getBinaryStream("name"), StandardCharsets.UTF_8));
            pet.setPetType(MyPetType.valueOf(resultSet.getString("type")));
            pet.setLastUsed(resultSet.getLong("last_used"));
            pet.setSaturation(resultSet.getDouble("hunger"));
            pet.wantsToRespawn = resultSet.getBoolean("wants_to_spawn");
            String skillTreeName = resultSet.getString("skilltree");
            if (skillTreeName != null) {
                Skilltree skilltree = MyPetApi.getSkilltreeManager().getSkilltree(skillTreeName);
                if (skilltree != null) {
                    pet.setSkilltree(skilltree);
                }
            }
            try {
                pet.setSkills(TagStream.readTag(resultSet.getBlob("skills").getBinaryStream(), true));
            } catch (ZipException exception) {
                MyPetApi.getMyPetLogger().warning("Pet skills of player \"" + pet.getOwner().getName() + "\" (" + pet.getPetName() + ") could not be loaded!");
            }
            try {
                pet.setInfo(TagStream.readTag(resultSet.getBlob("info").getBinaryStream(), true));
            } catch (ZipException exception) {
                MyPetApi.getMyPetLogger().warning("Pet info of player \"" + pet.getOwner().getName() + "\" (" + pet.getPetName() + ") could not be loaded!");
            }
            List<RepositoryMyPetConverterService> converters = MyPetApi.getServiceManager().getServices(RepositoryMyPetConverterService.class);
            for (RepositoryMyPetConverterService converter : converters) {
                converter.convert(pet);
            }
            pets.add(pet);
        }
    } catch (SQLException | IOException e) {
        e.printStackTrace();
    }
    return pets;
}
Also used : Skilltree(de.Keyle.MyPet.api.skill.skilltree.Skilltree) RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) ZipException(java.util.zip.ZipException) TagString(de.keyle.knbt.TagString) IOException(java.io.IOException) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 7 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class MySqlRepository method addMyPets.

public boolean addMyPets(List<StoredMyPet> pets) {
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement("INSERT INTO " + Configuration.Repository.MySQL.PREFIX + "pets (uuid, " + "owner_uuid, " + "exp, " + "health, " + "respawn_time, " + "name, " + "type, " + "last_used, " + "hunger, " + "world_group, " + "wants_to_spawn, " + "skilltree, " + "skills, " + "info) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
        int i = 0;
        for (StoredMyPet storedMyPet : pets) {
            statement.setString(1, storedMyPet.getUUID().toString());
            statement.setString(2, storedMyPet.getOwner().getInternalUUID().toString());
            statement.setDouble(3, storedMyPet.getExp());
            statement.setDouble(4, storedMyPet.getHealth());
            statement.setInt(5, storedMyPet.getRespawnTime());
            statement.setString(6, storedMyPet.getPetName());
            statement.setString(7, storedMyPet.getPetType().name());
            statement.setLong(8, storedMyPet.getLastUsed());
            statement.setDouble(9, storedMyPet.getSaturation());
            statement.setString(10, storedMyPet.getWorldGroup());
            statement.setBoolean(11, storedMyPet.wantsToRespawn());
            statement.setString(12, storedMyPet.getSkilltree() != null ? storedMyPet.getSkilltree().getName() : null);
            try {
                statement.setBlob(13, new ByteArrayInputStream(TagStream.writeTag(storedMyPet.getSkillInfo(), true)));
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                statement.setBlob(14, new ByteArrayInputStream(TagStream.writeTag(storedMyPet.getInfo(), true)));
            } catch (IOException e) {
                e.printStackTrace();
            }
            statement.addBatch();
            if (++i % 500 == 0 && i != pets.size()) {
                statement.executeBatch();
            }
        }
        statement.executeBatch();
        return true;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 8 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class SqLiteRepository method getAllMyPets.

@Override
public List<StoredMyPet> getAllMyPets() {
    try {
        List<MyPetPlayer> playerList = getAllMyPetPlayers();
        Map<UUID, MyPetPlayer> owners = new HashMap<>();
        for (MyPetPlayer player : playerList) {
            owners.put(player.getInternalUUID(), player);
        }
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT * FROM pets;");
        List<StoredMyPet> pets = new ArrayList<>();
        while (resultSet.next()) {
            if (!owners.containsKey(UUID.fromString(resultSet.getString("owner_uuid")))) {
                continue;
            }
            MyPetPlayer owner = owners.get(UUID.fromString(resultSet.getString("owner_uuid")));
            InactiveMyPet pet = new InactiveMyPet(owner);
            pet.setUUID(UUID.fromString(resultSet.getString("uuid")));
            pet.setWorldGroup(resultSet.getString("world_group"));
            pet.setExp(resultSet.getDouble("exp"));
            pet.setHealth(resultSet.getDouble("health"));
            pet.setRespawnTime(resultSet.getInt("respawn_time"));
            pet.setPetName(resultSet.getString("name"));
            pet.setPetType(MyPetType.valueOf(resultSet.getString("type")));
            pet.setLastUsed(resultSet.getLong("last_used"));
            pet.setSaturation(resultSet.getInt("hunger"));
            pet.wantsToRespawn = resultSet.getBoolean("wants_to_spawn");
            String skillTreeName = resultSet.getString("skilltree");
            if (skillTreeName != null) {
                Skilltree skilltree = MyPetApi.getSkilltreeManager().getSkilltree(skillTreeName);
                if (skilltree != null) {
                    pet.setSkilltree(skilltree);
                }
            }
            pet.setSkills(TagStream.readTag(resultSet.getBytes("skills"), true));
            pet.setInfo(TagStream.readTag(resultSet.getBytes("info"), true));
            pets.add(pet);
        }
        return pets;
    } catch (SQLException | IOException e) {
        e.printStackTrace();
    }
    return new ArrayList<>();
}
Also used : Skilltree(de.Keyle.MyPet.api.skill.skilltree.Skilltree) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) IOException(java.io.IOException) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 9 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class PlayerListener method on.

@EventHandler
public void on(final PlayerJoinEvent event) {
    if (WorldGroup.getGroupByWorld(event.getPlayer().getWorld()).isDisabled()) {
        return;
    }
    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:
                                                if (Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                                    joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", joinedPlayer), myPet.getPetName()));
                                                } else {
                                                    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 (Configuration.Update.SHOW_OP && event.getPlayer().isOp() && Updater.isUpdateAvailable()) {
        event.getPlayer().sendMessage(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)

Example 10 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class MongoDbRepository method getAllMyPets.

@Override
@SuppressWarnings("unchecked")
public List<StoredMyPet> getAllMyPets() {
    List<MyPetPlayer> playerList = getAllMyPetPlayers();
    final Map<UUID, MyPetPlayer> owners = new HashMap<>();
    for (MyPetPlayer player : playerList) {
        owners.put(player.getInternalUUID(), player);
    }
    MongoCollection petCollection = this.db.getCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
    final List<StoredMyPet> myPetList = new ArrayList<>();
    petCollection.find().forEach((Block<Document>) document -> {
        UUID ownerUUID = UUID.fromString(document.getString("owner_uuid"));
        if (owners.containsKey(ownerUUID)) {
            StoredMyPet storedMyPet = documentToMyPet(owners.get(ownerUUID), document);
            if (storedMyPet != null) {
                myPetList.add(storedMyPet);
            }
        }
    });
    return myPetList;
}
Also used : Document(org.bson.Document) java.util(java.util) MongoCollection(com.mongodb.client.MongoCollection) Repository(de.Keyle.MyPet.api.repository.Repository) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) MongoDatabase(com.mongodb.client.MongoDatabase) Player(org.bukkit.entity.Player) TagStream(de.keyle.knbt.TagStream) Lists(com.google.common.collect.Lists) Configuration(de.Keyle.MyPet.api.Configuration) com.mongodb(com.mongodb) MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) Binary(org.bson.types.Binary) ZipException(java.util.zip.ZipException) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetApi(de.Keyle.MyPet.MyPetApi) IOException(java.io.IOException) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) Skilltree(de.Keyle.MyPet.api.skill.skilltree.Skilltree) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) FindIterable(com.mongodb.client.FindIterable) RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetVersion(de.Keyle.MyPet.api.MyPetVersion) MongoCollection(com.mongodb.client.MongoCollection) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) Document(org.bson.Document) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Aggregations

StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)23 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)15 MyPet (de.Keyle.MyPet.api.entity.MyPet)10 Player (org.bukkit.entity.Player)9 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)8 WorldGroup (de.Keyle.MyPet.api.WorldGroup)7 IOException (java.io.IOException)7 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)7 List (java.util.List)6 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)5 Skilltree (de.Keyle.MyPet.api.skill.skilltree.Skilltree)5 UUID (java.util.UUID)5 IconMenu (de.Keyle.MyPet.api.gui.IconMenu)4 IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)4 RepositoryMyPetConverterService (de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService)4 TagCompound (de.keyle.knbt.TagCompound)4 Material (org.bukkit.Material)4 Repository (de.Keyle.MyPet.api.repository.Repository)3 FancyMessage (de.Keyle.MyPet.api.util.chat.FancyMessage)3 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)3