use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.
the class MyPetAdminSelectionGui method open.
public void open(final List<StoredMyPet> pets, int page, final RepositoryCallback<StoredMyPet> callback) {
if (pets.size() > 0) {
if (page < 1 || Math.ceil(pets.size() / 45.) < page) {
page = 1;
}
final Map<Integer, StoredMyPet> petSlotList = new HashMap<>();
WorldGroup wg = WorldGroup.getGroupByWorld(petOwner.getPlayer().getWorld().getName());
Iterator<StoredMyPet> iterator = pets.iterator();
while (iterator.hasNext()) {
StoredMyPet mypet = iterator.next();
if (mypet.getWorldGroup().equals("") || !mypet.getWorldGroup().equals(wg.getName())) {
iterator.remove();
}
if (petOwner.hasMyPet() && petOwner.getMyPet().getUUID().equals(mypet.getUUID())) {
iterator.remove();
}
}
final int previousPage = page == 1 ? (int) Math.ceil(pets.size() / 45.) : page - 1;
final int nextPage = page == Math.ceil(pets.size() / 45.) ? 1 : page + 1;
IconMenu menu = new IconMenu(title, event -> {
if (event.getPosition() == 45) {
new BukkitRunnable() {
@Override
public void run() {
open(pets, previousPage, callback);
}
}.runTaskLater(MyPetApi.getPlugin(), 1L);
} else if (event.getPosition() == 53) {
new BukkitRunnable() {
@Override
public void run() {
open(pets, nextPage, callback);
}
}.runTaskLater(MyPetApi.getPlugin(), 1L);
} else if (event.getPosition() > 45) {
return;
} else if (petSlotList.containsKey(event.getPosition())) {
StoredMyPet storedMyPet = petSlotList.get(event.getPosition());
if (storedMyPet != null && callback != null) {
callback.callback(storedMyPet);
}
}
event.setWillClose(true);
event.setWillDestroy(true);
}, MyPetApi.getPlugin());
int pagePets = pets.size() - (page - 1) * 45;
for (int i = 0; i < pagePets && i <= 45; i++) {
StoredMyPet mypet = pets.get(i + ((page - 1) * 45));
List<String> lore = new ArrayList<>();
lore.add(RESET + Translation.getString("Name.Hunger", admin) + ": " + GOLD + Math.round(mypet.getSaturation()));
if (mypet.getRespawnTime() > 0) {
lore.add(RESET + Translation.getString("Name.Respawntime", admin) + ": " + GOLD + mypet.getRespawnTime() + "sec");
} else {
lore.add(RESET + Translation.getString("Name.HP", admin) + ": " + GOLD + String.format("%1.2f", mypet.getHealth()));
}
boolean levelFound = false;
if (mypet.getInfo().containsKey("storage")) {
TagCompound storage = mypet.getInfo().getAs("storage", TagCompound.class);
if (storage.containsKey("level")) {
lore.add(RESET + Translation.getString("Name.Level", admin) + ": " + GOLD + storage.getAs("level", TagInt.class).getIntData());
levelFound = true;
}
}
if (!levelFound) {
lore.add(RESET + Translation.getString("Name.Exp", admin) + ": " + GOLD + String.format("%1.2f", mypet.getExp()));
}
lore.add(RESET + Translation.getString("Name.Type", admin) + ": " + GOLD + Translation.getString("Name." + mypet.getPetType().name(), admin));
lore.add(RESET + Translation.getString("Name.Skilltree", admin) + ": " + GOLD + Colorizer.setColors(mypet.getSkilltree() != null ? mypet.getSkilltree().getDisplayName() : "-"));
IconMenuItem icon = new IconMenuItem();
icon.setTitle(RESET + mypet.getPetName());
icon.addLore(lore);
Optional<EggIconService> egg = MyPetApi.getServiceManager().getService(EggIconService.class);
egg.ifPresent(service -> service.updateIcon(mypet.getPetType(), icon));
int pos = menu.addOption(icon);
petSlotList.put(pos, mypet);
}
if (previousPage != page) {
menu.setOption(45, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "SIGN", "OAK_SIGN")).setTitle("" + previousPage + " ≪≪"));
}
if (previousPage != page) {
menu.setOption(53, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "SIGN", "OAK_SIGN")).setTitle(ChatColor.BOLD + "≫≫ " + ChatColor.RESET + nextPage));
}
menu.open(admin);
}
}
use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.
the class MySqlRepository method getAllMyPets.
@Override
public List<StoredMyPet> getAllMyPets() {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM pets;")) {
List<MyPetPlayer> playerList = getAllMyPetPlayers();
Map<UUID, MyPetPlayer> owners = new HashMap<>();
for (MyPetPlayer player : playerList) {
owners.put(player.getInternalUUID(), player);
}
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.getBlob("skills").getBinaryStream(), true));
pet.setInfo(TagStream.readTag(resultSet.getBlob("info").getBinaryStream(), 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 SqLiteRepository method addMyPets.
public boolean addMyPets(List<StoredMyPet> pets) {
try {
PreparedStatement statement = connection.prepareStatement("INSERT INTO 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.setBytes(13, TagStream.writeTag(storedMyPet.getSkillInfo(), true));
} catch (IOException e) {
e.printStackTrace();
}
try {
statement.setBytes(14, 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 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.setWorldGroup(resultSet.getString("world_group"));
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.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);
}
}
pet.setSkills(TagStream.readTag(resultSet.getBytes("skills"), true));
pet.setInfo(TagStream.readTag(resultSet.getBytes("info"), true));
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 MyPetManager method activateMyPet.
// All ----------------------------------------------------------------------
public Optional<MyPet> activateMyPet(StoredMyPet storedMyPet) {
if (storedMyPet == null) {
return Optional.empty();
}
if (!storedMyPet.getOwner().isOnline()) {
return Optional.empty();
}
if (storedMyPet.getOwner().hasMyPet()) {
if (!deactivateMyPet(storedMyPet.getOwner(), true)) {
return Optional.empty();
}
}
Event event = new MyPetLoadEvent(storedMyPet);
Bukkit.getServer().getPluginManager().callEvent(event);
MyPet myPet = MyPetClass.getByMyPetType(storedMyPet.getPetType()).getNewMyPetInstance(storedMyPet.getOwner());
myPet.setUUID(storedMyPet.getUUID());
myPet.setPetName(storedMyPet.getPetName());
myPet.setRespawnTime(storedMyPet.getRespawnTime());
myPet.setWorldGroup(storedMyPet.getWorldGroup());
myPet.setInfo(storedMyPet.getInfo());
myPet.setLastUsed(storedMyPet.getLastUsed());
myPet.setWantsToRespawn(storedMyPet.wantsToRespawn());
myPet.getExperience().setExp(storedMyPet.getExp());
myPet.setSkilltree(storedMyPet.getSkilltree());
Collection<Skill> skills = myPet.getSkills().all();
if (skills.size() > 0) {
for (Skill skill : skills) {
if (skill instanceof NBTStorage) {
NBTStorage storageSkill = (NBTStorage) skill;
if (storedMyPet.getSkillInfo().getCompoundData().containsKey(skill.getName())) {
storageSkill.load(storedMyPet.getSkillInfo().getAs(skill.getName(), TagCompound.class));
}
}
}
}
myPet.setHealth(storedMyPet.getHealth());
myPet.setSaturation(storedMyPet.getSaturation());
mActivePetsPlayer.put(myPet, myPet.getOwner());
event = new MyPetActivatedEvent(myPet);
Bukkit.getServer().getPluginManager().callEvent(event);
return Optional.of(myPet);
}
Aggregations