hassansyyid

Untitled

Aug 25th, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package io.github.hsyyid.spongeessentialcmds.cmdexecutors;
  2.  
  3. import io.github.hsyyid.spongeessentialcmds.utils.Utils;
  4.  
  5. import org.spongepowered.api.entity.player.Player;
  6. import org.spongepowered.api.service.permission.Subject;
  7. import org.spongepowered.api.service.permission.option.OptionSubject;
  8. import org.spongepowered.api.text.Texts;
  9. import org.spongepowered.api.text.format.TextColors;
  10. import org.spongepowered.api.util.command.CommandException;
  11. import org.spongepowered.api.util.command.CommandResult;
  12. import org.spongepowered.api.util.command.CommandSource;
  13. import org.spongepowered.api.util.command.args.CommandContext;
  14. import org.spongepowered.api.util.command.source.CommandBlockSource;
  15. import org.spongepowered.api.util.command.source.ConsoleSource;
  16. import org.spongepowered.api.util.command.spec.CommandExecutor;
  17.  
  18. public class SetHomeExecutor implements CommandExecutor
  19. {
  20.  
  21.     public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
  22.     {
  23.         String homeName = ctx.<String> getOne("home name").get();
  24.         if (src instanceof Player)
  25.         {
  26.             Player player = (Player) src;
  27.             Subject subject = player.getContainingCollection().get(player.getIdentifier());
  28.             String homesAllowed = null;
  29.             if (subject instanceof OptionSubject)
  30.             {
  31.                 homesAllowed = ((OptionSubject) subject).getOption("homes").or("");
  32.             }
  33.             if (homesAllowed != null && !(homesAllowed.equals("")))
  34.             {
  35.                 if (homesAllowed.equals("unlimited"))
  36.                 {
  37.                     Utils.setHome(player.getUniqueId(), player.getLocation(), player.getWorld().getName(), homeName);
  38.                     src.sendMessage(Texts.of(TextColors.GREEN, "Success: ", TextColors.YELLOW, "Home set."));
  39.                 }
  40.                 else
  41.                 {
  42.                     Integer allowedHomes = Integer.parseInt(homesAllowed);
  43.                     try
  44.                     {
  45.                         if(allowedHomes > Utils.getHomes(player.getUniqueId()).size())
  46.                         {
  47.                             Utils.setHome(player.getUniqueId(), player.getLocation(), player.getWorld().getName(), homeName);
  48.                             src.sendMessage(Texts.of(TextColors.GREEN, "Success: ", TextColors.YELLOW, "Home set."));
  49.                         }
  50.                         else
  51.                         {
  52.                             src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You have reached the maximum number of homes you are allowed!"));
  53.                         }
  54.                     }
  55.                     catch(NullPointerException e)
  56.                     {
  57.                         if(allowedHomes > 0)
  58.                         {
  59.                             Utils.setHome(player.getUniqueId(), player.getLocation(), player.getWorld().getName(), homeName);
  60.                             src.sendMessage(Texts.of(TextColors.GREEN, "Success: ", TextColors.YELLOW, "Home set."));
  61.                         }
  62.                         else
  63.                         {
  64.                             src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You have reached the maximum number of homes you are allowed!"));
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.             else
  70.             {
  71.                 Utils.setHome(player.getUniqueId(), player.getLocation(), player.getWorld().getName(), homeName);
  72.                 src.sendMessage(Texts.of(TextColors.GREEN, "Success: ", TextColors.YELLOW, "Home set."));
  73.             }
  74.         }
  75.         else if (src instanceof ConsoleSource)
  76.         {
  77.             src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /sethome!"));
  78.         }
  79.         else if (src instanceof CommandBlockSource)
  80.         {
  81.             src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /sethome!"));
  82.         }
  83.  
  84.         return CommandResult.success();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment