2 |
|
|
3 |
|
|
4 |
import java.util.HashMap; |
import java.util.HashMap; |
5 |
|
import java.util.List; |
6 |
import java.util.Set; |
import java.util.Set; |
7 |
import java.util.TreeMap; |
import java.util.TreeMap; |
8 |
import java.util.TreeSet; |
import java.util.TreeSet; |
22 |
import org.bukkit.event.EventHandler; |
import org.bukkit.event.EventHandler; |
23 |
import org.bukkit.event.Listener; |
import org.bukkit.event.Listener; |
24 |
import org.bukkit.event.block.BlockBreakEvent; |
import org.bukkit.event.block.BlockBreakEvent; |
25 |
|
import org.bukkit.event.block.BlockPlaceEvent; |
26 |
import org.bukkit.event.inventory.InventoryCloseEvent; |
import org.bukkit.event.inventory.InventoryCloseEvent; |
27 |
import org.bukkit.event.inventory.InventoryOpenEvent; |
import org.bukkit.event.inventory.InventoryOpenEvent; |
28 |
import org.bukkit.inventory.InventoryHolder; |
import org.bukkit.inventory.InventoryHolder; |
37 |
|
|
38 |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
39 |
|
|
40 |
HashMap<Location,String> chestMap = new HashMap<Location, String>(); |
HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>(); |
41 |
|
|
42 |
|
|
43 |
HoerupUtilsPlugin plugin; |
HoerupUtilsPlugin plugin; |
44 |
Server server; |
Server server; |
45 |
|
|
46 |
public SnitchingChest(HoerupUtilsPlugin plugin) { |
public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) { |
47 |
this.plugin = plugin; |
this.plugin = plugin; |
48 |
server = plugin.getServer(); |
server = plugin.getServer(); |
49 |
|
try { |
50 |
loadChests(); |
loadChests(); |
51 |
|
} catch (Exception e) { |
52 |
|
e.printStackTrace(); |
53 |
|
//r.run(); |
54 |
|
loadChests(); |
55 |
|
} |
56 |
} |
} |
57 |
|
|
58 |
|
|
76 |
|
|
77 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
78 |
|
|
79 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
80 |
if (owner != null) { |
if (chest != null) { |
81 |
if (owner.equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
82 |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
83 |
chestMap.remove(loc); |
removeChest(loc); |
84 |
if (loc2 != null) { |
if (loc2 != null) { |
85 |
chestMap.remove(loc2); |
removeChest(loc2); |
86 |
} |
} |
|
saveChests(); |
|
87 |
} else { |
} else { |
88 |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
89 |
} |
} |
91 |
return true; |
return true; |
92 |
} |
} |
93 |
|
|
94 |
chestMap.put(loc, player.getName() ); |
SnitchingChestBean chest1 = createChest(player.getName(), "", loc); |
95 |
|
addChest(loc, chest1); |
96 |
if (loc2 != null) { |
if (loc2 != null) { |
97 |
chestMap.put(loc2, player.getName() ); |
|
98 |
|
SnitchingChestBean chest2 = createChest (player.getName(), "", loc2); |
99 |
|
addChest(loc, chest2); |
100 |
} |
} |
101 |
|
|
102 |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
103 |
saveChests(); |
|
104 |
|
|
105 |
return true; |
return true; |
106 |
} |
} |
108 |
@EventHandler |
@EventHandler |
109 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
110 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
111 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
112 |
if (owner != null) { |
if (chest != null) { |
113 |
if (owner.equals(event.getPlayer().getName())) { |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
114 |
chestMap.remove(loc); |
removeChest(loc); |
|
saveChests(); |
|
115 |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
116 |
} else { |
} else { |
117 |
event.setCancelled(true); |
event.setCancelled(true); |
119 |
} |
} |
120 |
} |
} |
121 |
} |
} |
122 |
|
public void addChest(Location loc, SnitchingChestBean chest) { |
123 |
|
chestMap.put(loc, chest); |
124 |
|
plugin.getDatabase().save(chest); |
125 |
|
} |
126 |
|
|
127 |
void loadChests() { |
void removeChest(Location loc) { |
128 |
|
SnitchingChestBean chest = chestMap.remove(loc); |
129 |
|
if (chest != null) { |
130 |
|
plugin.getDatabase().delete(chest); |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
|
void loadChests() { |
135 |
|
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
136 |
|
for (SnitchingChestBean chest : chestlist) { |
137 |
|
Location loc = getChestLocation(server, chest); |
138 |
|
chestMap.put(loc, chest); |
139 |
|
} |
140 |
|
|
141 |
|
plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests"); |
142 |
} |
} |
143 |
|
|
144 |
void saveChests() { |
|
145 |
|
public SnitchingChestBean createChest(String owner, String description, Location loc) { |
146 |
|
|
147 |
|
SnitchingChestBean chest = new SnitchingChestBean(); |
148 |
|
chest.setOwner(owner); |
149 |
|
chest.setDescription(description); |
150 |
|
setChestLocation(chest, loc); |
151 |
|
|
152 |
|
return chest; |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
public void setChestLocation(SnitchingChestBean chest, Location loc) { |
157 |
|
chest.setWorld( loc.getWorld().getName() ); |
158 |
|
chest.setX( loc.getBlockX() ); |
159 |
|
chest.setY( loc.getBlockY() ); |
160 |
|
chest.setZ( loc.getBlockZ() ); |
161 |
|
} |
162 |
|
|
163 |
|
public Location getChestLocation(Server server, SnitchingChestBean chest) { |
164 |
|
World wrld = server.getWorld(chest.getWorld()); |
165 |
|
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
166 |
} |
} |
167 |
|
|
168 |
|
|
169 |
|
/* |
170 |
|
void saveChests() { |
171 |
|
|
172 |
|
}*/ |
173 |
|
|
174 |
Location getNeighborChest(Location loc) { |
Location getNeighborChest(Location loc) { |
175 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
176 |
|
|
201 |
} else { |
} else { |
202 |
loc = ( (DoubleChest)holder).getLocation(); |
loc = ( (DoubleChest)holder).getLocation(); |
203 |
} |
} |
204 |
|
|
205 |
|
loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5 |
206 |
|
loc.setZ( loc.getBlockZ() ); // -- // -- |
207 |
|
|
208 |
return loc; |
return loc; |
209 |
} |
} |
210 |
|
|
211 |
@EventHandler |
@EventHandler |
212 |
|
public void onChestPlaced(BlockPlaceEvent event) { |
213 |
|
Block block = event.getBlock(); |
214 |
|
|
215 |
|
if (block.getType() != Material.CHEST) { |
216 |
|
return; |
217 |
|
} |
218 |
|
|
219 |
|
Location chestloc = getNeighborChest( block.getLocation() ); |
220 |
|
if (chestloc != null) { |
221 |
|
SnitchingChestBean chest = chestMap.get(chestloc); |
222 |
|
|
223 |
|
if (chest != null) { //the neighbor is a snitching chest |
224 |
|
SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc); |
225 |
|
addChest(chestloc, newchest); |
226 |
|
event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" ); |
227 |
|
} |
228 |
|
|
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
@EventHandler |
233 |
public void onChestOpen(InventoryOpenEvent event) { |
public void onChestOpen(InventoryOpenEvent event) { |
234 |
|
|
235 |
if (! (event.getPlayer() instanceof Player)) { |
if (! (event.getPlayer() instanceof Player)) { |
241 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
242 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
243 |
|
|
244 |
String owner = chestMap.get( loc ); |
SnitchingChestBean chest = chestMap.get( loc ); |
245 |
if (owner == null) { |
if (chest == null) { |
246 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
247 |
} |
} |
248 |
|
|
249 |
|
|
250 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
251 |
if (player.getName().equals(owner)) { |
if (player.getName().equals(chest.getOwner() )) { |
252 |
return; //chest is owned by it's own player |
return; //chest is owned by it's own player |
253 |
} |
} |
254 |
|
|
268 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
269 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
270 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
271 |
String ownerName = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
272 |
OfflinePlayer owner = server.getOfflinePlayer(ownerName); |
|
273 |
|
if (chest == null) { //chest was not a snitching chest |
274 |
|
return; |
275 |
|
} |
276 |
|
|
277 |
|
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
278 |
|
|
279 |
|
|
280 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
303 |
|
|
304 |
|
|
305 |
int diff = Math.abs( savedcount - count); |
int diff = Math.abs( savedcount - count); |
306 |
|
String msg = null; |
307 |
if (count > savedcount) { |
if (count > savedcount) { |
308 |
String msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; |
msg = player.getName() + " added " + diff + " units of " + item + " to " + owner.getName() + "'s chest"; |
|
plugin.getLogger().info(msg); |
|
|
plugin.getMessageWrapper().sendMessage(owner, msg); |
|
309 |
} |
} |
310 |
if (count < savedcount) { |
if (count < savedcount) { |
311 |
String msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; |
msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner.getName() + "'s chest"; |
312 |
|
} |
313 |
|
|
314 |
|
if (msg != null) { |
315 |
plugin.getLogger().info(msg); |
plugin.getLogger().info(msg); |
316 |
plugin.getMessageWrapper().sendMessage(owner, msg); |
plugin.getMessageWrapper().sendMessage(owner, msg); |
317 |
} |
} |