1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
5 |
import java.util.HashMap; |
6 |
import java.util.List; |
7 |
|
8 |
import org.bukkit.Chunk; |
9 |
import org.bukkit.Location; |
10 |
import org.bukkit.command.Command; |
11 |
import org.bukkit.command.CommandExecutor; |
12 |
import org.bukkit.command.CommandSender; |
13 |
import org.bukkit.entity.Player; |
14 |
import org.bukkit.event.EventHandler; |
15 |
import org.bukkit.event.Listener; |
16 |
import org.bukkit.event.world.ChunkUnloadEvent; |
17 |
|
18 |
public class StickyChunk implements Listener, CommandExecutor { |
19 |
|
20 |
public class ChunkBean { |
21 |
|
22 |
public ChunkBean(Chunk c) { |
23 |
this.world = c.getWorld().getName(); |
24 |
this.x = c.getX(); |
25 |
this.z = c.getZ(); |
26 |
|
27 |
this.x_min = this.x - 1; |
28 |
this.x_max = this.x + 1; |
29 |
this.z_min = this.z - 1; |
30 |
this.z_max = this.z + 1; |
31 |
} |
32 |
|
33 |
public String world; |
34 |
public int x; |
35 |
public int z; |
36 |
|
37 |
public int x_min; |
38 |
public int x_max; |
39 |
|
40 |
public int z_min; |
41 |
public int z_max; |
42 |
} |
43 |
|
44 |
|
45 |
HashMap<String,ChunkBean> chunks = new HashMap<String,ChunkBean>(); |
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 |
|
66 |
synchronized(chunks) { |
67 |
chunks.put( p.getName(), chunk); |
68 |
} |
69 |
|
70 |
|
71 |
sender.sendMessage("chunk registeret ok"); |
72 |
|
73 |
return true; |
74 |
} |
75 |
|
76 |
|
77 |
@EventHandler |
78 |
public void chunkUnload(ChunkUnloadEvent event) { |
79 |
synchronized(chunks) { |
80 |
for (ChunkBean chunk : chunks.values()) { |
81 |
Chunk c = event.getChunk(); |
82 |
if (c.getWorld().getName().equals( chunk.world )) { |
83 |
int x = c.getX(); |
84 |
int z = c.getZ(); |
85 |
|
86 |
if (x >= chunk.x_min && x <= chunk.x_max && z>=chunk.z_min && z<=chunk.z_max) { |
87 |
//System.out.println("Cancelling unload " + x + " " + z); |
88 |
event.setCancelled(true); |
89 |
return; // |
90 |
} |
91 |
} |
92 |
|
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
} |