Guest User

Untitled

a guest
Mar 30th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. /*
  2. ALL OF THIS IS RUNNING IN A COMMAND
  3.  */
  4.  
  5. // Get Bukkit and PlotSquared player instances
  6. Player player = (Player) sender;
  7. PlotPlayer plotPlayer = PlotPlayer.wrap(player);
  8.  
  9. int maxPlots = plotPlayer.getAllowedPlots();
  10.  
  11. int plotsToBuy = 0;
  12. try {
  13.     plotsToBuy = Integer.parseInt(args[0]);
  14. } catch(NumberFormatException e) {
  15.     player.sendMessage(ChatUtils.prefix() + "Please only enter a whole number!");
  16.     return true;
  17. }
  18.  
  19. if(plotsToBuy == 0) {
  20.     player.sendMessage(ChatUtils.prefix() + "You must buy at least 1 plot!");
  21.     return true;
  22. }
  23.  
  24. int costOfPlot = CreativeCore.plugin.getConfig().getInt("plot-cost");
  25. // Server currency
  26. int playerCredits = Credits.getAPI().getCredits(player);
  27.  
  28. if(playerCredits >= plotsToBuy * costOfPlot) {
  29.  
  30.     if(player.hasPermission("plots.plot." + maxPlots) && maxPlots != 1) {
  31.         // Create permission nodes.
  32.         PermissionNode node = PermissionNode.builder("plots.plot." + plotsToBuy).build();
  33.         PermissionNode oldNode = PermissionNode.builder("plots.plot." + maxPlots).build();
  34.  
  35.         // Get LP API.
  36.         LuckPerms api = LuckPermsProvider.get();
  37.  
  38.         final int finalToBuy = plotsToBuy;
  39.  
  40.         // Add the new permission to the user
  41.         api.getUserManager().loadUser(player.getUniqueId()).thenApply(user ->
  42.                 user.data().add(node));
  43.  
  44.         // Remove the old permission from the user.
  45.         api.getUserManager().loadUser(player.getUniqueId()).thenApply(user ->
  46.                 user.data().remove(oldNode)).thenRun(() -> {
  47.             /*
  48.             / Is this code in .thenRun() going to run once the future is complete without blocking?
  49.              */
  50.             Credits.getAPI().setCredits(player, playerCredits - finalToBuy * costOfPlot);
  51.  
  52.             // Some database stuff on bungee using plugin channels
  53.             int newPlayerCredits = Credits.getAPI().getCredits(player);
  54.             if(finalToBuy == 1) Credits.getAPI().sendCreditsPurchaseHistory(player, "1 Plot", playerCredits, newPlayerCredits);
  55.             else Credits.getAPI().sendCreditsPurchaseHistory(player, finalToBuy + " Plots", playerCredits, newPlayerCredits);
  56.  
  57.             player.sendMessage(ChatUtils.prefix() + "Successfully bought §a" + finalToBuy + " plots");
  58.         });
  59.  
  60.         /*/ How would I check if the future failed to complete (such as user not loading, permissions not applying)
  61.         and then send the user a message saying so?
  62.          */
  63.     }
  64. } else {
  65.     player.sendMessage(ChatUtils.prefix() + "You do not have enough credits to buy " + plotsToBuy + " plots.");
  66. }
Advertisement
Add Comment
Please, Sign In to add comment