use of de.Keyle.MyPet.api.WorldGroup in project MyPet by xXKeyleXx.
the class CommandOptionClone method onCommandOption.
@Override
public boolean onCommandOption(CommandSender sender, String[] args) {
if (args.length < 2) {
return false;
}
String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
Player oldOwner = Bukkit.getPlayer(args[0]);
if (oldOwner == null || !oldOwner.isOnline()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
return true;
}
final Player newOwner = Bukkit.getPlayer(args[1]);
if (newOwner == null || !newOwner.isOnline()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
return true;
}
if (!MyPetApi.getPlayerManager().isMyPetPlayer(oldOwner)) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
return true;
}
MyPetPlayer oldPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(oldOwner);
if (!oldPetOwner.hasMyPet()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
return true;
}
final MyPetPlayer newPetOwner;
if (MyPetApi.getPlayerManager().isMyPetPlayer(newOwner)) {
newPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(newOwner);
} else {
newPetOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(newOwner);
}
if (newPetOwner.hasMyPet()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + newOwner.getName() + " has already an active MyPet!");
return true;
}
MyPet oldPet = oldPetOwner.getMyPet();
final InactiveMyPet newPet = new InactiveMyPet(newPetOwner);
newPet.setPetName(oldPet.getPetName());
newPet.setExp(oldPet.getExperience().getExp());
newPet.setHealth(oldPet.getHealth());
newPet.setHungerValue(oldPet.getHungerValue());
newPet.setRespawnTime(oldPet.getRespawnTime());
newPet.setInfo(oldPet.getInfo());
newPet.setPetType(oldPet.getPetType());
newPet.setSkilltree(oldPet.getSkilltree());
newPet.setWorldGroup(oldPet.getWorldGroup());
newPet.setSkills(oldPet.getSkillInfo());
MyPetSaveEvent event = new MyPetSaveEvent(newPet);
Bukkit.getServer().getPluginManager().callEvent(event);
MyPetApi.getRepository().addMyPet(newPet, new RepositoryCallback<Boolean>() {
@Override
public void callback(Boolean value) {
Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(newPet);
if (myPet.isPresent()) {
WorldGroup worldGroup = WorldGroup.getGroupByWorld(newPet.getOwner().getPlayer().getWorld().getName());
newPet.setWorldGroup(worldGroup.getName());
newPet.getOwner().setMyPetForWorldGroup(worldGroup, newPet.getUUID());
MyPetApi.getRepository().updateMyPetPlayer(newPetOwner, null);
newOwner.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] MyPet owned by " + newPetOwner.getName() + " successfully cloned!");
}
}
});
return true;
}
use of de.Keyle.MyPet.api.WorldGroup 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;
}
use of de.Keyle.MyPet.api.WorldGroup in project MyPet by xXKeyleXx.
the class MyPetSelectionGui 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(player.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 (player.hasMyPet() && player.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, new IconMenu.OptionClickEventHandler() {
@Override
public void onOptionClick(IconMenu.OptionClickEvent 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", player) + ": " + GOLD + Math.round(mypet.getSaturation()));
if (mypet.getRespawnTime() > 0) {
lore.add(RESET + Translation.getString("Name.Respawntime", player) + ": " + GOLD + mypet.getRespawnTime() + "sec");
} else {
lore.add(RESET + Translation.getString("Name.HP", player) + ": " + GOLD + String.format("%1.2f", mypet.getHealth()));
}
lore.add(RESET + Translation.getString("Name.Exp", player) + ": " + GOLD + String.format("%1.2f", mypet.getExp()));
if (mypet.getInfo().containsKey("storage")) {
TagCompound storage = mypet.getInfo().getAs("storage", TagCompound.class);
if (storage.containsKey("level")) {
lore.add(RESET + Translation.getString("Name.Level", player) + ": " + GOLD + storage.getAs("level", TagInt.class).getIntData());
}
}
lore.add(RESET + Translation.getString("Name.Type", player) + ": " + GOLD + Translation.getString("Name." + mypet.getPetType().name(), player));
lore.add(RESET + Translation.getString("Name.Skilltree", player) + ": " + GOLD + (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);
if (egg.isPresent()) {
egg.get().updateIcon(mypet.getPetType(), icon);
}
int pos = menu.addOption(icon);
petSlotList.put(pos, mypet);
}
if (previousPage != page) {
menu.setOption(45, new IconMenuItem().setMaterial(Material.SIGN).setTitle("" + previousPage + " ≪≪"));
}
if (previousPage != page) {
menu.setOption(53, new IconMenuItem().setMaterial(Material.SIGN).setTitle(ChatColor.BOLD + "≫≫ " + ChatColor.RESET + nextPage));
}
menu.open(player.getPlayer());
}
}
use of de.Keyle.MyPet.api.WorldGroup in project MyPet by xXKeyleXx.
the class PlayerListener method onMyPetPlayerChangeWorld.
@EventHandler
public void onMyPetPlayerChangeWorld(final PlayerChangedWorldEvent event) {
if (!event.getPlayer().isOnline()) {
return;
}
if (MyPetApi.getPlayerManager().isMyPetPlayer(event.getPlayer())) {
final MyPetPlayer myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(event.getPlayer());
final WorldGroup fromGroup = WorldGroup.getGroupByWorld(event.getFrom().getName());
final WorldGroup toGroup = WorldGroup.getGroupByWorld(event.getPlayer().getWorld().getName());
final MyPet myPet = myPetPlayer.hasMyPet() ? myPetPlayer.getMyPet() : null;
final BukkitRunnable callPet = new BukkitRunnable() {
public void run() {
if (myPetPlayer.isOnline() && myPetPlayer.hasMyPet()) {
MyPet runMyPet = myPetPlayer.getMyPet();
switch(runMyPet.createEntity()) {
case Canceled:
myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", myPetPlayer), runMyPet.getPetName()));
break;
case NoSpace:
myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", myPetPlayer), runMyPet.getPetName()));
break;
case NotAllowed:
myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", myPetPlayer), runMyPet.getPetName()));
break;
case Dead:
if (runMyPet != myPet) {
myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", myPetPlayer), runMyPet.getPetName(), runMyPet.getRespawnTime()));
}
break;
case Flying:
myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Flying", myPetPlayer), runMyPet.getPetName()));
break;
case Success:
if (runMyPet != myPet) {
myPetPlayer.sendMessage(Util.formatText(Translation.getString("Message.Command.Call.Success", myPetPlayer), runMyPet.getPetName()));
}
break;
}
}
}
};
if (fromGroup != toGroup) {
final boolean hadMyPetInFromWorld = MyPetApi.getMyPetManager().deactivateMyPet(myPetPlayer, true);
if (myPetPlayer.hasMyPetInWorldGroup(toGroup)) {
final UUID groupMyPetUUID = myPetPlayer.getMyPetForWorldGroup(toGroup);
MyPetApi.getRepository().getMyPets(myPetPlayer, new RepositoryCallback<List<StoredMyPet>>() {
@Override
public void callback(List<StoredMyPet> pets) {
for (StoredMyPet myPet : pets) {
if (myPet.getUUID().equals(groupMyPetUUID)) {
MyPetApi.getMyPetManager().activateMyPet(myPet);
break;
}
}
if (myPetPlayer.hasMyPet()) {
if (myPetPlayer.getMyPet().wantsToRespawn()) {
callPet.runTaskLater(MyPetApi.getPlugin(), 20L);
}
} else {
myPetPlayer.setMyPetForWorldGroup(toGroup, null);
}
}
});
} else if (hadMyPetInFromWorld) {
myPetPlayer.getPlayer().sendMessage(Translation.getString("Message.MultiWorld.NoActivePetInThisWorld", myPetPlayer));
}
} else if (myPet != null) {
if (myPet.wantsToRespawn()) {
callPet.runTaskLater(MyPetApi.getPlugin(), 20L);
}
}
}
}
Aggregations