예제 #1
0
 private int getMaxPetCount(Player p) {
   int maxPetCount = 0;
   if (Permissions.has(p, "MyPet.admin")) {
     maxPetCount = Configuration.Misc.MAX_STORED_PET_COUNT;
   } else {
     for (int i = Configuration.Misc.MAX_STORED_PET_COUNT; i > 0; i--) {
       if (Permissions.hasLegacy(p, "MyPet.petstorage.limit.", i)) {
         maxPetCount = i;
         break;
       }
     }
   }
   return maxPetCount;
 }
예제 #2
0
  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;
  }