/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/HomeCommand.java
ViewVC logotype

Diff of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/HomeCommand.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/HomeCommand.java revision 1592 by torben, Wed Aug 24 18:45:40 2011 UTC miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/HomeCommand.java revision 1864 by torben, Mon Oct 15 13:39:03 2012 UTC
# Line 1  Line 1 
1  package dk.thoerup.bukkit.hoeruputils;  package dk.thoerup.bukkit.hoeruputils;
2    
3  import java.io.File;  import java.io.File;
4    import java.io.IOException;
5    
6  import org.bukkit.ChatColor;  import org.bukkit.ChatColor;
7  import org.bukkit.Location;  import org.bukkit.Location;
8  import org.bukkit.World;  import org.bukkit.World;
9    import org.bukkit.OfflinePlayer;
10  import org.bukkit.command.Command;  import org.bukkit.command.Command;
11  import org.bukkit.command.CommandExecutor;  import org.bukkit.command.CommandExecutor;
12  import org.bukkit.command.CommandSender;  import org.bukkit.command.CommandSender;
13  import org.bukkit.entity.Player;  import org.bukkit.entity.Player;
14  import org.bukkit.plugin.Plugin;  import org.bukkit.plugin.Plugin;
15  import org.bukkit.util.config.Configuration;  import org.bukkit.configuration.Configuration;
16    import org.bukkit.configuration.file.YamlConfiguration;
17    
18  public class HomeCommand implements CommandExecutor {  public class HomeCommand implements CommandExecutor {
19    
20          Plugin plugin;          Plugin plugin;
21    
22          Configuration config;          YamlConfiguration config;
23    
24            File file;
25    
26          public HomeCommand(Plugin plugin) {          public HomeCommand(Plugin plugin) {
27                  this.plugin = plugin;                  this.plugin = plugin;
28    
29                  File file = new File( plugin.getDataFolder(), "homes.yml");                  file = new File( plugin.getDataFolder(), "homes.yml");
30    
31                  config = new Configuration( file );                  config = new YamlConfiguration();
32                  config.load();                  try {
33                            config.load(file);
34                    } catch (Exception e) {
35                            System.out.println("[HoerupUtils] exception loading config file: " + e.getMessage() );
36                    }
37            }
38    
39            Configuration getConfig() {
40                    return config;
41          }          }
42                    
43    
# Line 34  public class HomeCommand implements Comm Line 47  public class HomeCommand implements Comm
47                  if (!(sender instanceof Player)) {                  if (!(sender instanceof Player)) {
48                          return false;                          return false;
49                  }                  }
50                  Player p = (Player) sender;                  final Player p = (Player) sender;
51    
52                  String name = p.getName();                  String name = p.getName();
53    
54                  if ( command.getName().equals("home") ) {                  if ( command.getName().equals("home") ) {
55                          config.load();                          tpUser(p, name);
56                    }
57                          String worldName = config.getString( name + ".world");                  if ( command.getName().equals("ophome") ) {
58                          if (worldName != null) {                          if (args.length != 1) {
59                                  double yaw = config.getDouble( name + ".yaw", 0.0);                                      p.sendMessage("Usage: /ophome <player>");
60                                  double pitch = config.getDouble( name + ".pitch", 0.0);                                  return true;
61                                  double x = config.getDouble( name + ".x", 0.0);                          }
62                                  double y = config.getDouble( name + ".y", 0.0);                          if ( p.isOp() ) {
63                                  double z = config.getDouble( name + ".z", 0.0);                                  OfflinePlayer op = sender.getServer().getOfflinePlayer(args[0]);        
64                                    if (op.hasPlayedBefore() == false && op.isOnline() == false) {
65                                  World world = p.getServer().getWorld(worldName);                                          sender.sendMessage("Player not found");                                
66                                    } else {
67                                  Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch);                                          tpUser(p, op.getName() );
68                                    }
                                 p.teleport(loc);  
69                          } else {                          } else {
70                                  p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!");                                  p.sendMessage("Only operators may use this command");
71                          }                          }
72                  }                  }
73    
74                  if (command.getName().equals("sethome") ) {                  if (command.getName().equals("sethome") ) {
75                          Location loc = p.getLocation();                          Location loc = p.getLocation();
76                          config.setProperty( name + ".yaw", loc.getYaw() );                          config.set( name + ".yaw", loc.getYaw() );
77                          config.setProperty( name + ".pitch", loc.getPitch() );                          config.set( name + ".pitch", loc.getPitch() );
78                          config.setProperty( name + ".world", loc.getWorld().getName() );                          config.set( name + ".world", loc.getWorld().getName() );
79                          config.setProperty( name + ".x", loc.getX() );                          config.set( name + ".x", loc.getX() );
80                          config.setProperty( name + ".y", loc.getY() );                          config.set( name + ".y", loc.getY() );
81                          config.setProperty( name + ".z", loc.getZ() );                          config.set( name + ".z", loc.getZ() );
82    
83                          config.save();                          try {
84                          p.sendMessage(ChatColor.YELLOW + "home is set");                                  config.save(file);
85                                    p.sendMessage(ChatColor.YELLOW + "home is set");
86                            } catch (IOException e) {
87                                    System.out.println("[HoerupUtils] : /sethome save threw an IO exception: " + e.getMessage() );
88                            }
89                  }                  }
90                                    
91                  return true;                  return true;
92          }          }
93            
94            private void tpUser(final Player p, String name) {
95                    String worldName = config.getString( name + ".world");
96                    if (worldName != null) {
97                            double yaw = config.getDouble( name + ".yaw", 0.0);    
98                            double pitch = config.getDouble( name + ".pitch", 0.0);
99                            double x = config.getDouble( name + ".x", 0.0);
100                            double y = config.getDouble( name + ".y", 0.0);
101                            double z = config.getDouble( name + ".z", 0.0);
102    
103                            World world = p.getServer().getWorld(worldName);
104                            int blockX = (int) Math.floor(x);
105                            int blockZ = (int) Math.floor(z);
106                            world.loadChunk(blockX, blockZ);        
107    
108                            final Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch);
109    
110                            Runnable r = new Runnable() {
111                                    public void run() {
112                                            p.teleport(loc);
113                                    }
114                            };
115                            int tickCount = 2; // 2 ticks = 100ms
116    
117                            p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, r, tickCount);
118    
119                    } else {
120                            p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!");
121                    }
122                    
123            }
124  }  }

Legend:
Removed from v.1592  
changed lines
  Added in v.1864

  ViewVC Help
Powered by ViewVC 1.1.20