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