/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/SnitchingChest.java
ViewVC logotype

Diff of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/SnitchingChest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/SnitchingChest.java revision 1769 by torben, Tue Apr 3 20:23:59 2012 UTC miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/chests/SnitchingChest.java revision 1804 by torben, Mon May 28 15:06:26 2012 UTC
# Line 1  Line 1 
1  package dk.thoerup.bukkit.hoeruputils;  package dk.thoerup.bukkit.hoeruputils.chests;
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;
# Line 18  import org.bukkit.command.CommandSender; Line 21  import org.bukkit.command.CommandSender;
21  import org.bukkit.entity.Player;  import org.bukkit.entity.Player;
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.Action;
25  import org.bukkit.event.block.BlockBreakEvent;  import org.bukkit.event.block.BlockBreakEvent;
26    import org.bukkit.event.block.BlockPlaceEvent;
27  import org.bukkit.event.inventory.InventoryCloseEvent;  import org.bukkit.event.inventory.InventoryCloseEvent;
28  import org.bukkit.event.inventory.InventoryOpenEvent;  import org.bukkit.event.inventory.InventoryOpenEvent;
29    import org.bukkit.event.player.PlayerInteractEvent;
30  import org.bukkit.inventory.InventoryHolder;  import org.bukkit.inventory.InventoryHolder;
31  import org.bukkit.inventory.ItemStack;  import org.bukkit.inventory.ItemStack;
32  import org.bukkit.plugin.Plugin;  
33    import dk.thoerup.bukkit.hoeruputils.HoerupUtilsPlugin;
34    
35    
36  public class SnitchingChest  implements Listener, CommandExecutor{  public class SnitchingChest  implements Listener, CommandExecutor{
37                    
# Line 33  public class SnitchingChest  implements Line 41  public class SnitchingChest  implements
41                    
42          HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();          HashMap<String, ItemCount> contentMap = new HashMap<String, ItemCount>();
43                    
44          HashMap<Location,String> chestMap = new HashMap<Location, String>();          HashMap<Location,SnitchingChestBean> chestMap = new HashMap<Location, SnitchingChestBean>();
45    
46                    
47          Plugin plugin;          HoerupUtilsPlugin plugin;
48            Server server;
49                    
50          public SnitchingChest(Plugin plugin) {          public SnitchingChest(HoerupUtilsPlugin plugin, Runnable r) {
51                  this.plugin = plugin;                  this.plugin = plugin;
52                                    server = plugin.getServer();
53                  loadChests();                  try {
54                            loadChests();
55                    } catch (Exception e) {
56                            e.printStackTrace();
57                            //r.run();
58                            loadChests();
59                    }
60          }          }
61                    
62    
# Line 65  public class SnitchingChest  implements Line 80  public class SnitchingChest  implements
80                                    
81                  Location loc2 = getNeighborChest(loc);                  Location loc2 = getNeighborChest(loc);
82                                    
83                  String owner = chestMap.get(loc);                  SnitchingChestBean chest = chestMap.get(loc);
84                  if (owner != null) {                  if (chest != null) {
85                          if (owner.equals(player.getName())) {                          if (chest.getOwner().equals(player.getName())) {
86                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");                                  player.sendMessage("[SnitchingChest] Removing surveillance from chest");
87                                  chestMap.remove(loc);                                  removeChest(loc);
                                 if (loc2 != null) {  
                                         chestMap.remove(loc2);                                    
                                 }  
                                 saveChests();  
88                          } else {                          } else {
89                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");                                  player.sendMessage("[SnitchingChest] Chest is already under surveillance");
90                          }                          }
# Line 81  public class SnitchingChest  implements Line 92  public class SnitchingChest  implements
92                          return true;                          return true;
93                  }                  }
94                                    
95                  chestMap.put(loc, player.getName() );                  chest = createChest(player.getName(), "", loc);        
96                  if (loc2 != null) {                  if (loc2 != null) {
97                          chestMap.put(loc2, player.getName() );                          chest.setDoublechest(true);
98                  }                  }
99                    addChest(loc, chest);
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          }          }
# Line 95  public class SnitchingChest  implements Line 108  public class SnitchingChest  implements
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);
# Line 107  public class SnitchingChest  implements Line 119  public class SnitchingChest  implements
119                          }                          }
120                  }                  }
121          }          }
122            public void addChest(Location loc, SnitchingChestBean chest) {
123                    chestMap.put(loc, chest);
124                    if (chest.isDoublechest()) {
125                            Location loc2 = getNeighborChest(loc);
126                            chestMap.put(loc2, chest);
127                    }              
128                    plugin.getDatabase().save(chest);
129                    
130                    reloadChests();
131                    
132            }
133            
134            void removeChest(Location loc) {
135                    SnitchingChestBean chest = chestMap.remove(loc);
136                    if (chest != null) {                                                                    
137                            if (chest.isDoublechest()){
138                                    Location loc2 = getNeighborChest(loc);
139                                    chestMap.remove(loc2);
140                            }
141                            plugin.getDatabase().delete(chest);
142                    }
143            }
144            
145            int loadChestsWorker() {
146                    List<SnitchingChestBean> chestlist = plugin.getDatabase().find( SnitchingChestBean.class).findList();
147                    for (SnitchingChestBean chest : chestlist) {
148                            Location loc = getChestLocation(server, chest);
149                            chestMap.put(loc, chest);      
150                            
151                            if (chest.isDoublechest()) {
152                                    Location loc2 = getNeighborChest(loc);
153                                    chestMap.put(loc2, chest);      
154                            }
155                    }
156    
157                    return chestlist.size();
158            }
159            
160            void reloadChests() {          
161                    chestMap.clear();
162                    loadChestsWorker();            
163            }
164            
165            void loadChests() {            
166                    int count = loadChestsWorker();
167                    plugin.getLogger().info("[SnitchingChest] loaded " + count + " chests");
168            }
169                    
170          void loadChests() {          
171            public SnitchingChestBean createChest(String owner, String description, Location loc) {
172                                    
173                    SnitchingChestBean chest = new SnitchingChestBean();
174                    chest.setOwner(owner);
175                    chest.setDescription(description);
176                    setChestLocation(chest, loc);
177    
178                    return chest;
179            }
180            
181            
182            public void setChestLocation(SnitchingChestBean chest, Location loc) {
183                    chest.setWorld( loc.getWorld().getName() );
184                    chest.setX( loc.getBlockX() );
185                    chest.setY( loc.getBlockY() );
186                    chest.setZ( loc.getBlockZ() );          
187            }
188            
189            public Location getChestLocation(Server server, SnitchingChestBean chest) {
190                    World wrld = server.getWorld(chest.getWorld());
191                    return new Location(wrld,chest.getX(),chest.getY(),chest.getZ());              
192          }          }
193                    
194    
195            /*
196          void saveChests() {          void saveChests() {
197                                    
198          }          }*/
199                    
200          Location getNeighborChest(Location loc) {          Location getNeighborChest(Location loc) {
201                  World world = loc.getWorld();                  World world = loc.getWorld();
# Line 146  public class SnitchingChest  implements Line 227  public class SnitchingChest  implements
227                  } else {                  } else {
228                          loc =  ( (DoubleChest)holder).getLocation();                          loc =  ( (DoubleChest)holder).getLocation();
229                  }                  }
230                    
231                    loc.setX( loc.getBlockX() ); //round to integer, since double chests apparently are placed at pos + 0.5
232                    loc.setZ( loc.getBlockZ() ); // -- // --
233                    
234                  return loc;                  return loc;
235          }          }
236                    
237          @EventHandler          @EventHandler
238            public void onChestPlaced(BlockPlaceEvent event) {
239                    Block block = event.getBlock();
240                    
241                    if (block.getType() != Material.CHEST) {
242                            return;
243                    }
244                    
245                    Location chestloc = getNeighborChest( block.getLocation() );
246                    if (chestloc != null) {
247                            SnitchingChestBean chest = chestMap.get(chestloc);
248                            
249                            if (chest != null) { //the neighbor is a snitching chest
250                                    //SnitchingChestBean newchest = createChest( chest.getOwner(), "", chestloc);
251                                    //plugin.getDatabase().save(chest);
252                                    
253                                    chest.setDoublechest(true);
254                                    addChest(chestloc, chest);
255                                    
256                                    
257                                    event.getPlayer().sendMessage( "[SnitchingChest] Chest has been expanded" );
258                            }                      
259                            
260                    }
261            }
262            
263            /*
264             * how to prevent a user from opening a chest - usefull if SnitchingChest should morph into a LockedChest
265            @EventHandler
266            public void onChestInteract(PlayerInteractEvent event) {
267                    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
268                            Block b = event.getClickedBlock();
269                            
270                            if (b.getType() == Material.CHEST) {
271                                    event.setCancelled(true);
272                            }
273                    }              
274            }
275            */
276            
277            @EventHandler
278          public void onChestOpen(InventoryOpenEvent event) {          public void onChestOpen(InventoryOpenEvent event) {
279                                    
280                  if (! (event.getPlayer() instanceof Player)) {                  if (! (event.getPlayer() instanceof Player)) {
281                          return;                          return;
282                  }                  }
283    
284    
285                                    
286                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
287                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
288                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
289                                                    
290                          String owner = chestMap.get( loc );                          SnitchingChestBean chest = chestMap.get( loc );
291                          if (owner == null) {                                                      if (chest == null) {                            
292                                  return; //chest not surveyed by this plugin                                  return; //chest not surveyed by this plugin
293                          }                          }
294                                    
295                                                                                                    
296                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
297                          if (player.getName().equals(owner)) {                          if (player.getName().equals(chest.getOwner() )) {
298                                  return; //chest is owned by it's own player                                  return; //chest is owned by it's own player
299                          }                                                }                      
300                                                    
# Line 188  public class SnitchingChest  implements Line 314  public class SnitchingChest  implements
314                  InventoryHolder holder = event.getInventory().getHolder();                  InventoryHolder holder = event.getInventory().getHolder();
315                  if (holder instanceof Chest || holder instanceof DoubleChest) {                  if (holder instanceof Chest || holder instanceof DoubleChest) {
316                          Location loc = getChestLocation(holder);                          Location loc = getChestLocation(holder);
317                          String owner = chestMap.get(loc);                          SnitchingChestBean chest = chestMap.get(loc);
318                            
319                            if (chest == null) { //chest was not a snitching chest
320                                    return;
321                            }
322                            
323                            OfflinePlayer owner = server.getOfflinePlayer( chest.getOwner() );
324                                                    
325                                                    
326                          Player player = (Player) event.getPlayer();                          Player player = (Player) event.getPlayer();
# Line 218  public class SnitchingChest  implements Line 350  public class SnitchingChest  implements
350                                                                    
351                                  int diff = Math.abs( savedcount - count);                                  int diff = Math.abs( savedcount - count);
352                                                                    
353                                  if (count > savedcount) {                                  if (diff > 0) {
354                                          String msg = player.getName() + " added " + diff + " units of " + item + " to " + owner + "'s chest";                                          String material = Material.getMaterial(item).name();
355                                          plugin.getLogger().info(msg);                                          String msg = null;
356                                  }                                          
357                                  if (count < savedcount) {                                          if (count > savedcount) {
358                                          String msg = player.getName() + " removed " + diff + " units of " + item + " from " + owner + "'s chest";                                                  msg = player.getName() + " added "   + diff + " units of " + material + "(" +item + ") to "   + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
359                                            } else { //(count < savedcount)
360                                                    msg = player.getName() + " removed " + diff + " units of " + material + "(" +item + ") from " + owner.getName() + "'s chest at " + loc.getWorld().getName() + "," + loc.getBlockX() + "," +loc.getBlockY() + "," + loc.getBlockZ();
361                                            }
362                                    
363                                    
364                                          plugin.getLogger().info(msg);                                          plugin.getLogger().info(msg);
365                                            plugin.getMessageWrapper().sendMessage("system", owner, msg);
366                                  }                                  }
367                                                                    
368                          }                          }

Legend:
Removed from v.1769  
changed lines
  Added in v.1804

  ViewVC Help
Powered by ViewVC 1.1.20