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;
}
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;
}
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<>();
}
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");
}
}
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;
}
Aggregations