kapselpb

homeMain (command class)

Dec 9th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. package me.pluginpack;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.Location;
  9. import org.bukkit.World;
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandExecutor;
  12. import org.bukkit.command.CommandSender;
  13. import org.bukkit.configuration.file.YamlConfiguration;
  14. import org.bukkit.entity.Player;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16.  
  17. public class homeMain
  18.   extends JavaPlugin
  19.   implements CommandExecutor
  20. {
  21.   mainClass plugin;
  22.   public homeMain(mainClass passedPlugin)
  23.   {
  24.       this.plugin = passedPlugin;
  25.   }
  26.   private File homesFile = new File(getDataFolder(), "me.kapsel.yml");
  27.   private YamlConfiguration homes = YamlConfiguration.loadConfiguration(this.homesFile);
  28.  
  29.   public void onDisable()
  30.   {
  31.     saveHomesFile();
  32.   }
  33.  
  34.   public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
  35.   {
  36.     Player player = (Player)sender;
  37.     String prefixSet = ChatColor.BLUE + "Set Home > " + ChatColor.GRAY;
  38.     String prefixHome = ChatColor.BLUE + "Home > " + ChatColor.GRAY;
  39.     if (command.getName().equalsIgnoreCase("sethome"))
  40.     {
  41.       if (!player.hasPermission("home.sethome"))
  42.       {
  43.         player.sendMessage(prefixSet + ChatColor.RED + "You need permission to perform this command!");
  44.         return false;
  45.       }
  46.       double x = player.getLocation().getX();
  47.       double y = player.getLocation().getY();
  48.       double z = player.getLocation().getZ();
  49.       float yaw = player.getLocation().getYaw();
  50.       float pitch = player.getLocation().getPitch();
  51.       String worldName = player.getWorld().getName();
  52.      
  53.       this.homes.set("Homes." + player.getUniqueId().toString() + ".X", Double.valueOf(x));
  54.       this.homes.set("Homes." + player.getUniqueId().toString() + ".Y", Double.valueOf(y));
  55.       this.homes.set("Homes." + player.getUniqueId().toString() + ".Z", Double.valueOf(z));
  56.       this.homes.set("Homes." + player.getUniqueId().toString() + ".Yaw", Float.valueOf(yaw));
  57.       this.homes.set("Homes." + player.getUniqueId().toString() + ".Pitch", Float.valueOf(pitch));
  58.       this.homes.set("Homes." + player.getUniqueId().toString() + ".World", worldName);
  59.      
  60.       saveHomesFile();
  61.       player.sendMessage(prefixSet + "You have successfully set your new home!");
  62.       return false;
  63.     }
  64.     if (command.getName().equalsIgnoreCase("home"))
  65.     {
  66.       if (!player.hasPermission("home.home"))
  67.       {
  68.         player.sendMessage(prefixHome + ChatColor.RED + "You need permission to perform this command!");
  69.         return false;
  70.       }
  71.       double existCheck = (float)this.homes.getLong("Homes." + player.getUniqueId().toString() + ".Yaw");
  72.       if (existCheck != 0) {
  73.       double x = this.homes.getDouble("Homes." + player.getUniqueId().toString() + ".X");
  74.       double y = this.homes.getDouble("Homes." + player.getUniqueId().toString() + ".Y");
  75.       double z = this.homes.getDouble("Homes." + player.getUniqueId().toString() + ".Z");
  76.       float yaw = (float)this.homes.getLong("Homes." + player.getUniqueId().toString() + ".Yaw");
  77.       float pitch = (float)this.homes.getLong("Homes." + player.getUniqueId().toString() + ".Pitch");
  78.       World world = Bukkit.getWorld(this.homes.getString("Homes." + player.getUniqueId().toString() + ".World"));
  79.      
  80.       Location home = new Location(world, x, y, z, yaw, pitch);
  81.       player.teleport(home);
  82.       player.sendMessage(prefixHome + "You have been sent to your home!");
  83.       return false;
  84.       }
  85.       else {
  86.           player.sendMessage(prefixHome + "You don't have set home!");
  87.           return false;
  88.       }
  89.     }
  90.     return false;
  91.   }
  92.  
  93.   private void saveHomesFile()
  94.   {
  95.     try
  96.     {
  97.       this.homes.save(this.homesFile);
  98.     }
  99.     catch (IOException e)
  100.     {
  101.       e.printStackTrace();
  102.     }
  103.   }
  104. }
Add Comment
Please, Sign In to add comment