use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.
the class CommandSwitch method onCommand.
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("You can't use this command from server console!");
return true;
}
Player player = (Player) sender;
if (!Permissions.hasLegacy(player, "MyPet.command.switch")) {
player.sendMessage(Translation.getString("Message.No.Allowed", player));
return true;
}
if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
final MyPetPlayer owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {
@Override
public void callback(List<StoredMyPet> pets) {
if (pets.size() - (owner.hasMyPet() ? 1 : 0) == 0) {
owner.sendMessage(Translation.getString("Message.Command.Switch.NoStoredPets", owner));
return;
}
if (owner.isOnline()) {
String worldGroup = WorldGroup.getGroupByWorld(owner.getPlayer().getWorld().getName()).getName();
int inactivePetCount = getInactivePetCount(pets, worldGroup);
int maxPetCount = getMaxPetCount(owner.getPlayer());
String title;
if (owner.hasMyPet()) {
inactivePetCount--;
title = Translation.getString("Message.Npc.SwitchTitle", owner);
} else {
title = Translation.getString("Message.SelectMyPet", owner);
}
String stats = "(" + inactivePetCount + "/" + maxPetCount + ")";
final MyPetSelectionGui gui = new MyPetSelectionGui(owner, title + " " + stats);
gui.open(pets, new RepositoryCallback<StoredMyPet>() {
@Override
public void callback(StoredMyPet storedMyPet) {
Optional<MyPet> activePet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
if (activePet.isPresent() && owner.isOnline()) {
Player player = owner.getPlayer();
activePet.get().getOwner().sendMessage(Util.formatText(Translation.getString("Message.Npc.ChosenPet", owner), activePet.get().getPetName()));
WorldGroup wg = WorldGroup.getGroupByWorld(player.getWorld().getName());
owner.setMyPetForWorldGroup(wg, activePet.get().getUUID());
switch(activePet.get().createEntity()) {
case Canceled:
owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", owner), activePet.get().getPetName()));
break;
case NoSpace:
owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", owner), activePet.get().getPetName()));
break;
case NotAllowed:
owner.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", owner), activePet.get().getPetName()));
break;
case Dead:
owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", owner), activePet.get().getPetName(), activePet.get().getRespawnTime()));
break;
case Spectator:
sender.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Spectator", owner), activePet.get().getPetName()));
break;
}
}
}
});
}
}
});
} else {
sender.sendMessage(Translation.getString("Message.No.HasPet", player));
}
return true;
}
use of de.Keyle.MyPet.api.player.MyPetPlayer 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.player.MyPetPlayer 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.player.MyPetPlayer in project MyPet by xXKeyleXx.
the class Timer method startTimer.
public static void startTimer() {
stopTimer();
timerIDs.add(Bukkit.getScheduler().scheduleSyncRepeatingTask(MyPetApi.getPlugin(), new Runnable() {
public void run() {
for (MyPet myPet : MyPetApi.getMyPetManager().getAllActiveMyPets()) {
myPet.schedule();
}
}
}, 0L, 20L));
timerIDs.add(Bukkit.getScheduler().scheduleSyncRepeatingTask(MyPetApi.getPlugin(), new Runnable() {
public void run() {
for (Scheduler task : tasksToSchedule) {
task.schedule();
}
}
}, 5L, 20L));
timerIDs.add(Bukkit.getScheduler().scheduleSyncRepeatingTask(MyPetApi.getPlugin(), new Runnable() {
public void run() {
for (MyPetPlayer player : MyPetApi.getPlayerManager().getMyPetPlayers()) {
player.schedule();
}
}
}, 10L, 20L));
}
use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.
the class PlayerManager method registerMyPetPlayer.
public MyPetPlayer registerMyPetPlayer(Player player) {
MyPetPlayer myPetPlayer = createMyPetPlayer(player);
MyPetApi.getRepository().addMyPetPlayer(myPetPlayer, null);
setOnline(myPetPlayer);
return myPetPlayer;
}
Aggregations