Advertisement
Guest User

HomeSpawn Code

a guest
Nov 12th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. package net.lapismc.HomeSpawn;
  2.  
  3. import java.io.File;
  4.  
  5. import java.io.IOException;
  6.  
  7. import java.io.InputStream;
  8.  
  9. import java.util.logging.Level;
  10.  
  11. import org.bukkit.Bukkit;
  12.  
  13. import org.bukkit.ChatColor;
  14.  
  15. import org.bukkit.Location;
  16.  
  17. import org.bukkit.World;
  18.  
  19. import org.bukkit.command.Command;
  20.  
  21. import org.bukkit.command.CommandSender;
  22.  
  23. import org.bukkit.configuration.file.FileConfiguration;
  24.  
  25. import org.bukkit.configuration.file.YamlConfiguration;
  26.  
  27. import org.bukkit.entity.Player;
  28.  
  29. import org.bukkit.plugin.java.JavaPlugin;
  30.  
  31. public class HomeSpawn extends JavaPlugin {
  32.  
  33.  public static HomeSpawn plugin;
  34.  
  35.  private File configFile;
  36.  
  37.  private FileConfiguration fileConfiguration;
  38.  
  39.  private YamlConfiguration Homes = null;
  40.  
  41.  private File HomesFile = null;
  42.  
  43.  private YamlConfiguration Spawn = null;
  44.  
  45.  private File SpawnFile = null;
  46.  
  47.  @Override
  48.  
  49.  public void onEnable() {
  50.  
  51.  Enable();
  52.  
  53.  Configs();
  54.  
  55.  }
  56.  
  57.  @Override
  58.  
  59.  public void onDisable() {
  60.  
  61.  Disable();
  62.  
  63.  }
  64.  
  65.  public void Enable() {
  66.  
  67.  Bukkit.getConsoleSender().sendMessage(
  68.  
  69.  "[HomeSpawn] V." + getDescription().getVersion()
  70.  
  71.  + " Has Been Enabled!");
  72.  
  73.  }
  74.  
  75.  public void Disable() {
  76.  
  77.  Bukkit.getConsoleSender().sendMessage(
  78.  
  79.  "[HomeSpawn] Plugin Has Been Disabled!");
  80.  
  81.  }
  82.  
  83.  public void Configs() {
  84.  
  85.  saveDefaultConfig();
  86.  
  87.  saveConfig();
  88.  
  89.  saveDefaultHomes();
  90.  
  91.  saveHomes();
  92.  
  93.  saveDefaultSpawn();
  94.  
  95.  saveSpawn();
  96.  
  97.  }
  98.  
  99.  @SuppressWarnings("deprecation")
  100.  
  101.  public void reloadHomes() {
  102.  
  103.  fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
  104.  
  105.  InputStream defConfigStream = plugin.getResource("Homes.yml");
  106.  
  107.  if (defConfigStream != null) {
  108.  
  109.  YamlConfiguration defConfig = YamlConfiguration
  110.  
  111.  .loadConfiguration(defConfigStream);
  112.  
  113.  fileConfiguration.setDefaults(defConfig);
  114.  
  115.  }
  116.  
  117.  }
  118.  
  119.  public void saveDefaultHomes() {
  120.  
  121.  if (HomesFile == null) {
  122.  
  123.  plugin.saveResource("Homes.yml", true);
  124.  
  125.  }
  126.  
  127.  }
  128.  
  129.  public void saveHomes() {
  130.  
  131.  if (Homes == null || HomesFile == null) {
  132.  
  133.  return;
  134.  
  135.  }
  136.  
  137.  try {
  138.  
  139.  getHomes().save(HomesFile);
  140.  
  141.  } catch (IOException ex) {
  142.  
  143.  getLogger().log(Level.SEVERE,
  144.  
  145.  "Could not save config to " + HomesFile, ex);
  146.  
  147.  }
  148.  
  149.  }
  150.  
  151.  public FileConfiguration getHomes() {
  152.  
  153.  if (Homes == null) {
  154.  
  155.  reloadHomes();
  156.  
  157.  }
  158.  
  159.  return Homes;
  160.  
  161.  }
  162.  
  163.  @SuppressWarnings("deprecation")
  164.  
  165.  public void reloadSpawn() {
  166.  
  167.  fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
  168.  
  169.  InputStream defConfigStream = plugin.getResource("Spawn.yml");
  170.  
  171.  if (defConfigStream != null) {
  172.  
  173.  YamlConfiguration defConfig = YamlConfiguration
  174.  
  175.  .loadConfiguration(defConfigStream);
  176.  
  177.  fileConfiguration.setDefaults(defConfig);
  178.  
  179.  }
  180.  
  181.  }
  182.  
  183.  public void saveDefaultSpawn() {
  184.  
  185.  if (SpawnFile == null) {
  186.  
  187.  plugin.saveResource("Spawn.yml", false);
  188.  
  189.  }
  190.  
  191.  }
  192.  
  193.  public void saveSpawn() {
  194.  
  195.  if (Homes == null || HomesFile == null) {
  196.  
  197.  return;
  198.  
  199.  }
  200.  
  201.  try {
  202.  
  203.  getSpawn().save(SpawnFile);
  204.  
  205.  } catch (IOException ex) {
  206.  
  207.  getLogger().log(Level.SEVERE,
  208.  
  209.  "Could not save config to " + HomesFile, ex);
  210.  
  211.  }
  212.  
  213.  }
  214.  
  215.  public FileConfiguration getSpawn() {
  216.  
  217.  if (Spawn == null) {
  218.  
  219.  reloadSpawn();
  220.  
  221.  }
  222.  
  223.  return Spawn;
  224.  
  225.  }
  226.  
  227.  @Override
  228.  
  229.  public boolean onCommand(CommandSender sender, Command cmd,
  230.  
  231.  String commandLabel, String[] args) {
  232.  
  233.  if (sender instanceof Player) {
  234.  
  235.  Player player = (Player) sender;
  236.  
  237.  if (commandLabel.equalsIgnoreCase("sethome")) {
  238.  
  239.  player.sendMessage(ChatColor.GOLD + "Home Set");
  240.  
  241.  getHomes().set(player.getDisplayName() + ".x",
  242.  
  243.  player.getLocation().getBlockX());
  244.  
  245.  getHomes().set(player.getDisplayName() + ".y",
  246.  
  247.  player.getLocation().getBlockY());
  248.  
  249.  getHomes().set(player.getDisplayName() + ".z",
  250.  
  251.  player.getLocation().getBlockZ());
  252.  
  253.  getHomes().set(player.getDisplayName() + ".world",
  254.  
  255.  player.getLocation().getWorld());
  256.  
  257.  getHomes().set(player.getDisplayName() + ".HasHome", "Yes");
  258.  
  259.  } else if (commandLabel.equalsIgnoreCase("home")) {
  260.  
  261.  if (getHomes().getString(player.getDisplayName() + ".HasHome")
  262.  
  263. == "Yes") {
  264.  
  265.  int x = getHomes().getInt(player.getDisplayName() + ".x");
  266.  
  267.  int y = getHomes().getInt(player.getDisplayName() + ".y");
  268.  
  269.  int z = getHomes().getInt(player.getDisplayName() + ".z");
  270.  
  271.  String cworld = getHomes().getString(
  272.  
  273.  player.getDisplayName() + ".world");
  274.  
  275.  World world = getServer().getWorld(cworld);
  276.  
  277.  Location home = new Location(world, x, y, z);
  278.  
  279.  player.sendMessage(ChatColor.GOLD + "Welcome Home");
  280.  
  281.  player.teleport(home);
  282.  
  283.  } else {
  284.  
  285.  player.sendMessage("You First Need To Set a Home With /
  286.  
  287. sethome");
  288.  
  289.  }
  290.  
  291.  } else if (commandLabel.equalsIgnoreCase("delhome")) {
  292.  
  293.  if (getHomes().getString(player.getDisplayName() + ".HasHome") == "No"
  294.  
  295.  || !getHomes().contains(
  296.  
  297.  player.getDisplayName() + ".HasHome")) {
  298.  
  299.  player.sendMessage(ChatColor.RED
  300.  
  301.  + "You Need To Set a Home Before You Can
  302.  
  303. Remove One!");
  304.  
  305.  } else if (getHomes().getString(
  306.  
  307.  player.getDisplayName() + ".HasHome") == "Yes") {
  308.  
  309.  player.sendMessage(ChatColor.GOLD + "Home Removed");
  310.  
  311.  getHomes().set(player.getDisplayName() + ".HasHome", "No");
  312.  
  313.  }
  314.  
  315.  } else if (commandLabel.equalsIgnoreCase("setspawn")) {
  316.  
  317.  player.sendMessage(ChatColor.GOLD + "Spawn Set");
  318.  
  319.  getSpawn().set("spawn.SpawnSet", "Yes");
  320.  
  321.  getSpawn().set("spawn.X", player.getLocation().getBlockX());
  322.  
  323.  getSpawn().set("spawn.Y", player.getLocation().getBlockY());
  324.  
  325.  getSpawn().set("spawn.Z", player.getLocation().getBlockZ());
  326.  
  327.  getSpawn().set("spawn.World", player.getWorld());
  328.  
  329.  } else if (commandLabel.equals("spawn")) {
  330.  
  331.  if (getSpawn().getString("spawn.SpawnSet") == "Yes") {
  332.  
  333.  int x = getSpawn().getInt("spawn.X");
  334.  
  335.  int y = getSpawn().getInt("spawn.Y");
  336.  
  337.  int z = getSpawn().getInt("spawn.Z");
  338.  
  339.  String cworld = getSpawn().getString("spawn.World");
  340.  
  341.  World world = getServer().getWorld(cworld);
  342.  
  343.  Location Spawn = new Location(world, x, y, z);
  344.  
  345.  player.sendMessage(ChatColor.GOLD + "Welcome To Spawn");
  346.  
  347.  player.teleport(Spawn);
  348.  
  349.  } else {
  350.  
  351.  player.sendMessage(ChatColor.RED + "The Spawn Isn't Set");
  352.  
  353.  }
  354.  
  355.  } else if (commandLabel.equalsIgnoreCase("delspawn")) {
  356.  
  357.  if (getSpawn().getString("spawn.SpawnSet") == "No"
  358.  
  359.  || !getSpawn().contains("spawn.SpawnSet")) {
  360.  
  361.  player.sendMessage(ChatColor.RED
  362.  
  363.  + "You Must First Set a Spawn Before You Can
  364.  
  365. Remove It!");
  366.  
  367.  } else if (getSpawn().getString("spawn.SpawnSet") == "Yes")
  368.  
  369.  getSpawn().set("spawn.SpawnSet", "No");
  370.  
  371.  player.sendMessage(ChatColor.GOLD + "Spawn Removed!");
  372.  
  373.  }
  374.  
  375.  } else {
  376.  
  377.  sender.sendMessage("You Must Be a Player To Do That");
  378.  
  379.  }
  380.  
  381.  return false;
  382.  
  383.  }
  384.  
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement