Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.77 KB | None | 0 0
  1. package org.EnderBro3D.LocTeleportator;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.logging.Level;
  7.  
  8. import org.apache.commons.io.FileUtils;
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Location;
  12. import org.bukkit.World;
  13. import org.bukkit.command.Command;
  14. import org.bukkit.command.CommandSender;
  15. import org.bukkit.configuration.file.YamlConfiguration;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. public class Loc extends JavaPlugin {
  20.     protected static YamlConfiguration config;
  21.    
  22.     public static YamlConfiguration getConfig(JavaPlugin pl, String configName) {
  23.         File file = new File(pl.getDataFolder(), configName);
  24.         if (file.isDirectory()) {
  25.             file.delete();
  26.         }
  27.         if (!file.exists()) {
  28.             try (InputStream i = pl.getClass().getClassLoader().getResourceAsStream(configName)) {
  29.                 FileUtils.copyInputStreamToFile(i, file);
  30.             } catch (IOException e) {
  31.                  pl.getLogger().log(Level.WARNING, "Ошибка при создании файла " + configName);
  32.             }
  33.             return new YamlConfiguration();
  34.         }
  35.         return YamlConfiguration.loadConfiguration(file);
  36.     }
  37.    
  38.     @Override
  39.     public void onEnable() {
  40.         config = getConfig(this, "config.yml");
  41.     }
  42.  
  43.     @Override
  44.     public void onDisable() {
  45.         try {
  46.             config.save("config.yml");
  47.        } catch (IOException ex) {
  48.             this.getLogger().log(Level.WARNING, "Ошибка при сохранении конфигов", ex);
  49.        }
  50.     }
  51.    
  52.     public static boolean isLoc(String loc) {
  53.         if(config.getConfigurationSection("Locations").isConfigurationSection(loc)) {
  54.             return true;
  55.         } else {
  56.             return false;
  57.         }
  58.     }
  59.     @Override
  60.     public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
  61.         if(cmd.getName().equalsIgnoreCase("toLoc")) {
  62.             if(args.length < 1) {
  63.                 s.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("LocTP.messages.empty")));
  64.                 return false;
  65.             }
  66.             if(args.length > 1) {
  67.                 s.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("LocTP.messages.syntax_error")));
  68.                 return false;
  69.             }
  70.             if(!isLoc(args[0])) {
  71.                 s.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("LocTP.messages.notfound")));
  72.                 s.sendMessage("\u00a7eLocation list:");
  73.                 s.sendMessage("\u00a7c"+config.getList("Locations.locationList").toString());
  74.                 return false;
  75.             }
  76.             double x = config.getDouble("Locations." + args[0] + ".x");
  77.             int y = config.getInt("Locations." + args[0] + ".y");
  78.             double z = config.getDouble("Locations." + args[0] + ".z");
  79.             World world = Bukkit.getServer().getWorld(config.getString("Locations." + args[0] + ".world"));
  80.             Location loc = new Location(world, x, y, z);
  81.             Player sender = (Player) s;
  82.             sender.teleport(loc);
  83.            
  84.             return true;
  85.         }
  86.         if(cmd.getName().equalsIgnoreCase("adminToLoc")) {
  87.             if(!s.hasPermission("toLocation.admin")) {
  88.                 s.sendMessage("\u00a7c"+config.getString("LocTP.messages.not_perms"));
  89.                 return false;
  90.             }
  91.             if(args.length == 0) {
  92.                 s.sendMessage("\u00a76Author: EnderBro3D");
  93.                 s.sendMessage("\u00a7n-----------------------------------------");
  94.                 s.sendMessage("\u00a7e/adminToLoc create <name> - новая локация");
  95.                 s.sendMessage("\u00a7n-----------------------------------------");
  96.                 return false;
  97.             }
  98.             if(args[0].equalsIgnoreCase("create")) {
  99.                 if(args.length < 2) {
  100.                     s.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("LocTP.messages.syntax_error")));
  101.                     return false;
  102.                 }
  103.                 if(config == null) {
  104.                     s.sendMessage(">> \u00a7cОшибка с конфигурацией, config = null");
  105.                     return false;
  106.                 }
  107.                 String newLoc = args[1];
  108.                 if(config.isConfigurationSection("Locations." + newLoc)) {
  109.                     s.sendMessage(">> \u00a7cЛокация уже существует");
  110.                     return false;
  111.                 }
  112.                 Player sender = (Player) s;
  113.                 config.set("Locations." + newLoc + ".x", sender.getLocation().getX());
  114.                 config.set("Locations." + newLoc + ".y", sender.getLocation().getY());
  115.                 config.set("Locations." + newLoc + ".z", sender.getLocation().getZ());
  116.                 config.set("Locations." + newLoc + ".world", sender.getLocation().getWorld().toString());
  117.                 s.sendMessage(">> \u00a7aЛокация \u00a73" + newLoc + ", \u00a7aуспешно установлена.");
  118.                 return true;
  119.             }
  120.         }
  121.         return false;
  122.     }
  123.    
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement