Guest User

SLAPI attempt

a guest
Apr 12th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. package me.dr_madman.hungercraft;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.util.ArrayList;
  10.  
  11. import org.bukkit.Location;
  12. import org.bukkit.Material;
  13. import org.bukkit.block.Block;
  14. import org.bukkit.entity.Player;
  15. import org.bukkit.event.Event;
  16. import org.bukkit.event.EventHandler;
  17. import org.bukkit.event.Listener;
  18. import org.bukkit.event.block.Action;
  19. import org.bukkit.event.player.PlayerInteractEvent;
  20.  
  21. public class HungerListener implements Listener {
  22. public static HungerCraft plugin;
  23. private ArrayList<Location> spawnlocations = new ArrayList<Location>();
  24. public static void createFile() throws IOException{
  25. File f;
  26. f=new File("location.bin");
  27. if(!f.exists()){
  28. f.createNewFile();
  29.  
  30. }
  31. }
  32. public HungerListener(HungerCraft instance) {plugin = instance;}
  33. /** SLAPI = Saving/Loading API
  34. * API for Saving and Loading Objects.
  35. * @author Tomsik68
  36. */
  37. public static class SLAPI {
  38. public static void save(Object obj,String path) throws Exception
  39. {
  40. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
  41. oos.writeObject(obj);
  42. oos.flush();
  43. oos.close();
  44. }
  45. public static Object load(String path) throws Exception
  46. {
  47. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
  48. Object result = ois.readObject();
  49. ois.close();
  50. return result;
  51. }
  52. }
  53. public boolean checkRightClickGoldBlock(Event event){
  54. if(((PlayerInteractEvent) event).getAction() == Action.RIGHT_CLICK_BLOCK ){
  55. if(((PlayerInteractEvent) event).getClickedBlock().getType() == Material.GOLD_BLOCK){
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61.  
  62. @SuppressWarnings("unchecked")
  63. @EventHandler
  64. public void blockInteract(PlayerInteractEvent event) throws Exception{
  65. Player player = event.getPlayer();
  66. Block block = event.getClickedBlock();
  67. if(checkRightClickGoldBlock(event)){
  68. if(player.isOp() || event.getPlayer().hasPermission("hc.setspawn")){
  69. createFile();
  70. spawnlocations = (ArrayList<Location>)SLAPI.load("location.bin");
  71. spawnlocations.add(block.getLocation().add(0, 1, 0));
  72. SLAPI.save(spawnlocations, "location.bin");
  73. player.sendMessage("Location saved");
  74. }
  75. }
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment