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; |
9 |
|
|
10 |
import org.bukkit.Location; |
import org.bukkit.Location; |
11 |
import org.bukkit.Material; |
import org.bukkit.Material; |
12 |
|
import org.bukkit.OfflinePlayer; |
13 |
|
import org.bukkit.Server; |
14 |
import org.bukkit.World; |
import org.bukkit.World; |
15 |
import org.bukkit.block.Block; |
import org.bukkit.block.Block; |
16 |
import org.bukkit.block.Chest; |
import org.bukkit.block.Chest; |
26 |
import org.bukkit.event.inventory.InventoryOpenEvent; |
import org.bukkit.event.inventory.InventoryOpenEvent; |
27 |
import org.bukkit.inventory.InventoryHolder; |
import org.bukkit.inventory.InventoryHolder; |
28 |
import org.bukkit.inventory.ItemStack; |
import org.bukkit.inventory.ItemStack; |
29 |
import org.bukkit.plugin.Plugin; |
|
30 |
|
|
31 |
public class SnitchingChest implements Listener, CommandExecutor{ |
public class SnitchingChest implements Listener, CommandExecutor{ |
32 |
|
|
36 |
|
|
37 |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>(); |
38 |
|
|
39 |
HashMap<Location,String> chestMap = new HashMap<Location, String>(); |
HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>(); |
40 |
|
|
41 |
|
|
42 |
Plugin plugin; |
HoerupUtilsPlugin plugin; |
43 |
|
Server server; |
44 |
|
|
45 |
public SnitchingChest(Plugin plugin) { |
public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) { |
46 |
this.plugin = plugin; |
this.plugin = plugin; |
47 |
|
server = plugin.getServer(); |
48 |
loadChests(); |
try { |
49 |
|
loadChests(); |
50 |
|
} catch (Exception e) { |
51 |
|
e.printStackTrace(); |
52 |
|
//r.run(); |
53 |
|
loadChests(); |
54 |
|
} |
55 |
} |
} |
56 |
|
|
57 |
|
|
75 |
|
|
76 |
Location loc2 = getNeighborChest(loc); |
Location loc2 = getNeighborChest(loc); |
77 |
|
|
78 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
79 |
if (owner != null) { |
if (chest != null) { |
80 |
if (owner.equals(player.getName())) { |
if (chest.getOwner().equals(player.getName())) { |
81 |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
player.sendMessage("[SnitchingChest] Removing surveillance from chest"); |
82 |
chestMap.remove(loc); |
removeChest(loc); |
83 |
if (loc2 != null) { |
if (loc2 != null) { |
84 |
chestMap.remove(loc2); |
removeChest(loc2); |
85 |
} |
} |
|
saveChests(); |
|
86 |
} else { |
} else { |
87 |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is already under surveillance"); |
88 |
} |
} |
90 |
return true; |
return true; |
91 |
} |
} |
92 |
|
|
93 |
chestMap.put(loc, player.getName() ); |
SnitchingChestBean chest1 = createChest(player.getName(), "", loc); |
94 |
|
//chestMap.put(loc, chest1); |
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 |
|
//chestMap.put(loc2, chest2 ); |
101 |
} |
} |
102 |
|
|
103 |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
player.sendMessage("[SnitchingChest] Chest is now under surveillance"); |
104 |
saveChests(); |
|
105 |
|
|
106 |
return true; |
return true; |
107 |
} |
} |
109 |
@EventHandler |
@EventHandler |
110 |
public void onBlockBreak(BlockBreakEvent event) { |
public void onBlockBreak(BlockBreakEvent event) { |
111 |
Location loc = event.getBlock().getLocation(); |
Location loc = event.getBlock().getLocation(); |
112 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
113 |
if (owner != null) { |
if (chest != null) { |
114 |
if (owner.equals(event.getPlayer().getName())) { |
if (chest.getOwner().equals(event.getPlayer().getName())) { |
115 |
chestMap.remove(loc); |
removeChest(loc); |
|
saveChests(); |
|
116 |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
event.getPlayer().sendMessage("[SnitchingChest] The destroyed chest was under surveillance"); |
117 |
} else { |
} else { |
118 |
event.setCancelled(true); |
event.setCancelled(true); |
120 |
} |
} |
121 |
} |
} |
122 |
} |
} |
123 |
|
public void addChest(Location loc, SnitchingChestBean chest) { |
124 |
|
chestMap.put(loc, chest); |
125 |
|
plugin.getDatabase().save(chest); |
126 |
|
} |
127 |
|
|
128 |
void loadChests() { |
void removeChest(Location loc) { |
129 |
|
SnitchingChestBean chest = chestMap.remove(loc); |
130 |
|
if (chest != null) { |
131 |
|
plugin.getDatabase().delete(chest); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
void loadChests() { |
136 |
|
List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList(); |
137 |
|
for (SnitchingChestBean chest : chestlist) { |
138 |
|
Location loc = getChestLocation(server, chest); |
139 |
|
chestMap.put(loc, chest); |
140 |
|
} |
141 |
|
|
142 |
|
plugin.getLogger().info("[SnitchingChest] loaded " + chestMap.size() + " chests"); |
143 |
} |
} |
144 |
|
|
145 |
void saveChests() { |
|
146 |
|
public SnitchingChestBean createChest(String owner, String description, Location loc) { |
147 |
|
|
148 |
|
SnitchingChestBean chest = new SnitchingChestBean(); |
149 |
|
chest.setOwner(owner); |
150 |
|
chest.setDescription(description); |
151 |
|
setChestLocation(chest, loc); |
152 |
|
|
153 |
|
return chest; |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
public void setChestLocation(SnitchingChestBean chest, Location loc) { |
158 |
|
chest.setWorld( loc.getWorld().getName() ); |
159 |
|
chest.setX( loc.getBlockX() ); |
160 |
|
chest.setY( loc.getBlockY() ); |
161 |
|
chest.setZ( loc.getBlockZ() ); |
162 |
|
} |
163 |
|
|
164 |
|
public Location getChestLocation(Server server, SnitchingChestBean chest) { |
165 |
|
World wrld = server.getWorld(chest.getWorld()); |
166 |
|
return new Location(wrld,chest.getX(),chest.getY(),chest.getZ()); |
167 |
} |
} |
168 |
|
|
169 |
|
|
170 |
|
/* |
171 |
|
void saveChests() { |
172 |
|
|
173 |
|
}*/ |
174 |
|
|
175 |
Location getNeighborChest(Location loc) { |
Location getNeighborChest(Location loc) { |
176 |
World world = loc.getWorld(); |
World world = loc.getWorld(); |
177 |
|
|
217 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
218 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
219 |
|
|
220 |
String owner = chestMap.get( loc ); |
SnitchingChestBean chest = chestMap.get( loc ); |
221 |
if (owner == null) { |
if (chest == null) { |
222 |
return; //chest not surveyed by this plugin |
return; //chest not surveyed by this plugin |
223 |
} |
} |
224 |
|
|
225 |
|
|
226 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
227 |
if (player.getName().equals(owner)) { |
if (player.getName().equals(chest.getOwner() )) { |
228 |
return; //chest is owned by it's own player |
return; //chest is owned by it's own player |
229 |
} |
} |
230 |
|
|
244 |
InventoryHolder holder = event.getInventory().getHolder(); |
InventoryHolder holder = event.getInventory().getHolder(); |
245 |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
if (holder instanceof Chest || holder instanceof DoubleChest) { |
246 |
Location loc = getChestLocation(holder); |
Location loc = getChestLocation(holder); |
247 |
String owner = chestMap.get(loc); |
SnitchingChestBean chest = chestMap.get(loc); |
248 |
|
|
249 |
|
if (chest == null) { //chest was not a snitching chest |
250 |
|
return; |
251 |
|
} |
252 |
|
|
253 |
|
OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() ); |
254 |
|
|
255 |
|
|
256 |
Player player = (Player) event.getPlayer(); |
Player player = (Player) event.getPlayer(); |
279 |
|
|
280 |
|
|
281 |
int diff = Math.abs( savedcount - count); |
int diff = Math.abs( savedcount - count); |
282 |
|
String msg = null; |
283 |
if (count > savedcount) { |
if (count > savedcount) { |
284 |
String msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; |
msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest"; |
|
plugin.getLogger().info(msg); |
|
285 |
} |
} |
286 |
if (count < savedcount) { |
if (count < savedcount) { |
287 |
String msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; |
msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest"; |
288 |
|
} |
289 |
|
|
290 |
|
if (msg != null) { |
291 |
plugin.getLogger().info(msg); |
plugin.getLogger().info(msg); |
292 |
|
plugin.getMessageWrapper().sendMessage(owner, msg); |
293 |
} |
} |
294 |
|
|
295 |
} |
} |