1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import java.util.HashMap; |
4 |
|
5 |
import org.bukkit.Chunk; |
6 |
import org.bukkit.Location; |
7 |
import org.bukkit.command.Command; |
8 |
import org.bukkit.command.CommandExecutor; |
9 |
import org.bukkit.command.CommandSender; |
10 |
import org.bukkit.entity.Player; |
11 |
import org.bukkit.event.EventHandler; |
12 |
import org.bukkit.event.Listener; |
13 |
import org.bukkit.event.world.ChunkUnloadEvent; |
14 |
|
15 |
public class StickyChunk implements Listener, CommandExecutor { |
16 |
|
17 |
public class ChunkBean { |
18 |
|
19 |
public ChunkBean(Chunk c) { |
20 |
this.world = c.getWorld().getName(); |
21 |
this.x = c.getX(); |
22 |
this.z = c.getZ(); |
23 |
|
24 |
this.x_min = this.x - 1; |
25 |
this.x_max = this.x + 1; |
26 |
this.z_min = this.z - 1; |
27 |
this.z_max = this.z + 1; |
28 |
} |
29 |
|
30 |
public String world; |
31 |
public int x; |
32 |
public int z; |
33 |
|
34 |
public int x_min; |
35 |
public int x_max; |
36 |
|
37 |
public int z_min; |
38 |
public int z_max; |
39 |
} |
40 |
|
41 |
|
42 |
HashMap<String,ChunkBean> chunks = new HashMap<String,ChunkBean>(); |
43 |
|
44 |
|
45 |
@Override |
46 |
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
47 |
|
48 |
if ( ! (sender instanceof Player) ) { |
49 |
sender.sendMessage("stickychunk is not a console command"); |
50 |
return false; |
51 |
} |
52 |
Player p = (Player) sender; |
53 |
|
54 |
if (p.isOp()==false && p.hasPermission("hoeruputils.chunks")==false) { |
55 |
sender.sendMessage("you don't have permission to do this"); |
56 |
return false; |
57 |
} |
58 |
|
59 |
Location l = p.getLocation(); |
60 |
|
61 |
ChunkBean chunk = new ChunkBean( l.getWorld().getChunkAt( l ) ); |
62 |
|
63 |
synchronized(chunks) { |
64 |
chunks.put( p.getName(), chunk); |
65 |
} |
66 |
|
67 |
|
68 |
sender.sendMessage("chunk registeret ok"); |
69 |
|
70 |
return true; |
71 |
} |
72 |
|
73 |
|
74 |
@EventHandler |
75 |
public void chunkUnload(ChunkUnloadEvent event) { |
76 |
synchronized(chunks) { |
77 |
for (ChunkBean chunk : chunks.values()) { |
78 |
Chunk c = event.getChunk(); |
79 |
if (c.getWorld().getName().equals( chunk.world )) { |
80 |
int x = c.getX(); |
81 |
int z = c.getZ(); |
82 |
|
83 |
if (x >= chunk.x_min && x <= chunk.x_max && z>=chunk.z_min && z<=chunk.z_max) { |
84 |
//System.out.println("Cancelling unload " + x + " " + z); |
85 |
event.setCancelled(true); |
86 |
return; // |
87 |
} |
88 |
} |
89 |
|
90 |
} |
91 |
} |
92 |
} |
93 |
|
94 |
} |