Advertisement
Guest User

Home

a guest
Nov 23rd, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. package fr.simonbhb.test;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Optional;
  8.  
  9. import org.spongepowered.api.Game;
  10. import org.spongepowered.api.entity.living.player.Player;
  11. import org.spongepowered.api.text.Text;
  12. import org.spongepowered.api.text.Texts;
  13. import org.spongepowered.api.text.format.TextColors;
  14. import org.spongepowered.api.util.command.CommandCallable;
  15. import org.spongepowered.api.util.command.CommandException;
  16. import org.spongepowered.api.util.command.CommandResult;
  17. import org.spongepowered.api.util.command.CommandSource;
  18. import org.spongepowered.api.util.command.source.ConsoleSource;
  19. import org.spongepowered.api.world.World;
  20.  
  21. import ninja.leaping.configurate.ConfigurationNode;
  22. import ninja.leaping.configurate.ConfigurationOptions;
  23. import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
  24. import ninja.leaping.configurate.loader.ConfigurationLoader;
  25.  
  26. public class CommandSetHome implements CommandCallable {
  27.  
  28.     public Game game;
  29.    
  30.     public CommandSetHome(Game game) {
  31.         this.game = game;
  32.     }
  33.    
  34.     @Override
  35.     public CommandResult process(CommandSource sender, String arguments) throws CommandException {
  36.         if(sender instanceof Player) {
  37.             Player player = (Player) sender;
  38.             String[] args = arguments.split(" ");
  39.            
  40.             if(arguments == "") {
  41.                 player.sendMessage(Texts.builder("Home: default").color(TextColors.YELLOW).build());
  42.  
  43.                 File folder = new File("config/test/home/" + player.getUniqueId().toString());
  44.                 if(!folder.exists()) folder.mkdir();
  45.                
  46.                 File file = new File("config/test/home/" + player.getUniqueId().toString() + "/defaut.conf");
  47.                 ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(file).build();
  48.                 ConfigurationNode homes = manager.createEmptyNode(ConfigurationOptions.defaults());
  49.                
  50.                 try {
  51.                     String nameHome = "default";
  52.                     String playerName = player.getName();
  53.                     String world = player.getWorld().getName();                
  54.                     int cX = player.getLocation().getBlockX();
  55.                     int cY = player.getLocation().getBlockY();
  56.                     int cZ = player.getLocation().getBlockZ();
  57.                    
  58.                     if(file.exists()) {
  59.                         //file.createNewFile();
  60.                     }
  61.                     //homes = manager.load();
  62.                     homes.getNode("home", "name").setValue( nameHome );
  63.                     homes.getNode("home", "pseudo").setValue(playerName);
  64.                     homes.getNode("home", "world").setValue(world);
  65.                    
  66.                     homes.getNode("home", "x").setValue(cX);
  67.                     homes.getNode("home", "y").setValue(cY);
  68.                     homes.getNode("home", "z").setValue(cZ);
  69.                    
  70.                     manager.save(homes);           
  71.                     homes = manager.load();
  72.                     player.sendMessage(Texts.of("Sauvegarde du home: " + cX + " " + cY + " " + cZ));
  73.            
  74.                 } catch (IOException e) {
  75.                     e.printStackTrace();
  76.                 }
  77.             } else if(args.length == 1) {
  78.                 player.sendMessage(Texts.builder( "Nom du /home: " + args[0].toLowerCase() ).color(TextColors.YELLOW).build());
  79.  
  80.                 File folder = new File("config/test/home/" + player.getUniqueId().toString());
  81.                 if(!folder.exists()) folder.mkdir();
  82.                
  83.                 File file = new File("config/test/home/" + player.getUniqueId().toString() + "/" + args[0].toLowerCase() + ".conf");
  84.                 ConfigurationLoader<?> manager = HoconConfigurationLoader.builder().setFile(file).build();
  85.                 ConfigurationNode homes = manager.createEmptyNode(ConfigurationOptions.defaults());
  86.                
  87.                 try {
  88.                     String nameHome = args[0].toLowerCase();
  89.                     String playerName = player.getName();
  90.                     String world = player.getWorld().getName();                
  91.                     int cX = player.getLocation().getBlockX();
  92.                     int cY = player.getLocation().getBlockY();
  93.                     int cZ = player.getLocation().getBlockZ();
  94.                    
  95.                     if(file.exists()) {
  96.                         //file.createNewFile();                    
  97.                     }
  98.  
  99.                     homes.getNode("home", "name").setValue( nameHome );
  100.                     homes.getNode("home", "pseudo").setValue(playerName);
  101.                     homes.getNode("home", "world").setValue(world);
  102.                    
  103.                     homes.getNode("home", "x").setValue(cX);
  104.                     homes.getNode("home", "y").setValue(cY);
  105.                     homes.getNode("home", "z").setValue(cZ);
  106.                    
  107.                     manager.save(homes);
  108.                     homes = manager.load();
  109.                     player.sendMessage(Texts.of("Sauvegarde du home: " + cX + " " + cY + " " + cZ));
  110.  
  111.                 } catch (IOException e) {
  112.                     e.printStackTrace();
  113.                 }
  114.             } else {
  115.                 player.sendMessage(Texts.builder("Usage: /home ou /home set [name]").color(TextColors.RED).build());               
  116.             }
  117.         } else if(sender instanceof ConsoleSource) {
  118.             sender.sendMessage(Texts.of("La commande doit etre tape par un joueur"));
  119.         } else {
  120.            
  121.         }
  122.         return null;
  123.     }
  124.  
  125.     private final Text usage = Texts.builder("Usage: /sethome ou /sethome [name]").color(TextColors.YELLOW).build();
  126.     private final Text help = Texts.builder("Help: /sethome ou /sethome [name]").color(TextColors.YELLOW).build();
  127.     private final Text description = Texts.builder("Test | sethome Command").color(TextColors.YELLOW).build();
  128.     private List<String> suggestions = new ArrayList<String>();
  129.     private String permission = "";
  130.    
  131.     @Override
  132.     public Text getUsage(CommandSource sender) { return usage; }
  133.     @Override
  134.     public Optional<Text> getHelp(CommandSource sender) { return Optional.of(help); }
  135.     @Override
  136.     public Optional<Text> getShortDescription(CommandSource sender) { return Optional.of(description); }
  137.     @Override
  138.     public List<String> getSuggestions(CommandSource sender, String args) throws CommandException { return suggestions; }
  139.     @Override
  140.     public boolean testPermission(CommandSource sender) { return permission.equals("") ? true : sender.hasPermission(permission); }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement