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; |
11 |
import org.bukkit.command.CommandSender; |
import org.bukkit.command.CommandSender; |
12 |
import org.bukkit.entity.Player; |
import org.bukkit.entity.Player; |
13 |
import org.bukkit.plugin.Plugin; |
import org.bukkit.plugin.Plugin; |
14 |
import org.bukkit.util.config.Configuration; |
import org.bukkit.configuration.file.YamlConfiguration; |
15 |
|
|
16 |
public class HomeCommand implements CommandExecutor { |
public class HomeCommand implements CommandExecutor { |
17 |
|
|
18 |
Plugin plugin; |
Plugin plugin; |
19 |
|
|
20 |
Configuration config; |
YamlConfiguration config; |
21 |
|
|
22 |
|
File file; |
23 |
|
|
24 |
public HomeCommand(Plugin plugin) { |
public HomeCommand(Plugin plugin) { |
25 |
this.plugin = plugin; |
this.plugin = plugin; |
26 |
|
|
27 |
File file = new File( plugin.getDataFolder(), "homes.yml"); |
file = new File( plugin.getDataFolder(), "homes.yml"); |
28 |
|
|
29 |
config = new Configuration( file ); |
config = new YamlConfiguration(); |
30 |
config.load(); |
try { |
31 |
|
config.load(file); |
32 |
|
} catch (Exception e) { |
33 |
|
System.out.println("[HoerupUtils] exception loading config file: " + e.getMessage() ); |
34 |
|
} |
35 |
} |
} |
36 |
|
|
37 |
|
|
41 |
if (!(sender instanceof Player)) { |
if (!(sender instanceof Player)) { |
42 |
return false; |
return false; |
43 |
} |
} |
44 |
Player p = (Player) sender; |
final Player p = (Player) sender; |
45 |
|
|
46 |
String name = p.getName(); |
String name = p.getName(); |
47 |
|
|
48 |
if ( command.getName().equals("home") ) { |
if ( command.getName().equals("home") ) { |
|
config.load(); |
|
49 |
|
|
50 |
String worldName = config.getString( name + ".world"); |
String worldName = config.getString( name + ".world"); |
51 |
if (worldName != null) { |
if (worldName != null) { |
56 |
double z = config.getDouble( name + ".z", 0.0); |
double z = config.getDouble( name + ".z", 0.0); |
57 |
|
|
58 |
World world = p.getServer().getWorld(worldName); |
World world = p.getServer().getWorld(worldName); |
59 |
|
int blockX = (int) Math.floor(x); |
60 |
|
int blockZ = (int) Math.floor(z); |
61 |
|
world.loadChunk(blockX, blockZ); |
62 |
|
|
63 |
|
final Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch); |
64 |
|
|
65 |
|
Runnable r = new Runnable() { |
66 |
|
public void run() { |
67 |
|
p.teleport(loc); |
68 |
|
} |
69 |
|
}; |
70 |
|
int tickCount = 2; // 2 ticks = 100ms |
71 |
|
|
72 |
Location loc = new Location( world, x, y, z, (float)yaw, (float)pitch); |
p.getServer().getScheduler().scheduleSyncDelayedTask(plugin, r, tickCount); |
73 |
|
|
|
p.teleport(loc); |
|
74 |
} else { |
} else { |
75 |
p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!"); |
p.sendMessage(ChatColor.YELLOW + "You haven't set a home yet!"); |
76 |
} |
} |
78 |
|
|
79 |
if (command.getName().equals("sethome") ) { |
if (command.getName().equals("sethome") ) { |
80 |
Location loc = p.getLocation(); |
Location loc = p.getLocation(); |
81 |
config.setProperty( name + ".yaw", loc.getYaw() ); |
config.set( name + ".yaw", loc.getYaw() ); |
82 |
config.setProperty( name + ".pitch", loc.getPitch() ); |
config.set( name + ".pitch", loc.getPitch() ); |
83 |
config.setProperty( name + ".world", loc.getWorld().getName() ); |
config.set( name + ".world", loc.getWorld().getName() ); |
84 |
config.setProperty( name + ".x", loc.getX() ); |
config.set( name + ".x", loc.getX() ); |
85 |
config.setProperty( name + ".y", loc.getY() ); |
config.set( name + ".y", loc.getY() ); |
86 |
config.setProperty( name + ".z", loc.getZ() ); |
config.set( name + ".z", loc.getZ() ); |
87 |
|
|
88 |
config.save(); |
try { |
89 |
p.sendMessage(ChatColor.YELLOW + "home is set"); |
config.save(file); |
90 |
|
p.sendMessage(ChatColor.YELLOW + "home is set"); |
91 |
|
} catch (IOException e) { |
92 |
|
System.out.println("[HoerupUtils] : /sethome save threw an IO exception: " + e.getMessage() ); |
93 |
|
} |
94 |
} |
} |
95 |
|
|
96 |
return true; |
return true; |