Saving and loading of a Location

Discussion in 'Plugin Development' started by Linschlager, Sep 6, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Linschlager

    My problem is that I want to set a Location and then look for a play who clicked on the block on that Location: My try was:
    Code:java
    1. @EventHandler
    2. public void onClick (PlayerInteractEvent event) {
    3.  
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. Player p = event.getPlayer();
    6. Location loc = event.getClickedBlock().getLocation();
    7. if (event.getPlayer().getItemInHand().getType() == Material.GOLD_INGOT) {
    8. Utils.SetPoint(loc, p, "Yellow", Core.locations, Core.locationFile);
    9. p.sendMessage(Core.prefix + " §bYellow Location successfully set!");
    10. }
    11. }
    12. }


    Code:java
    1. public static void SetPoint(Location loc, Player p, String path, FileConfiguration cfg, File file){
    2. if(!p.isOp()||!p.hasPermission("SetPoint.*")){
    3. p.sendMessage("§4No Permission §c(SetPoint.*)");
    4. return;
    5. } else {
    6. cfg.set(path+".world", loc.getWorld().getName());
    7. cfg.set(path+".X", loc.getX());
    8. cfg.set(path+".Y", loc.getY());
    9. cfg.set(path+".Z", loc.getZ());
    10. try {
    11. cfg.save(file);
    12. } catch (IOException e) {
    13. System.err.println(e.getMessage());
    14. }
    15. }
    16. }


    and:

    Code:java
    1. public static Location GetLocation(Plugin pl, String path, FileConfiguration cfg){
    2. if(!cfg.contains(path)){
    3. return null;
    4. } else {
    5. World world = pl.getServer().getWorld((String) cfg.get(path+".world"));
    6. int X = cfg.getInt(path+".X");
    7. int Y = cfg.getInt(path+".Y");
    8. int Z = cfg.getInt(path+".Z");
    9.  
    10. if(world == null){
    11. return null;
    12. }
    13. Location point = new Location(world, X, Y, Z);
    14. return point;
    15. }
    16. }


    And at last the method of clicking:

    Code:java
    1. public void onInteract (PlayerInteractEvent event) {
    2. Player p = event.getPlayer();
    3. Block clicked = event.getClickedBlock();
    4. Location bloc = clicked.getLocation();
    5. try {
    6. blueloc = Utils.GetLocation(plugin, "BlueObsidian", Core.locs);
    7. redloc = Utils.GetLocation(plugin, "RedObsidian", Core.locs);
    8. greenloc = Utils.GetLocation(plugin, "GreenObsidian", Core.locs);
    9. yellowloc = Utils.GetLocation(plugin, "YellowObsidian", Core.locs);
    10. } catch (Exception ex ) {
    11. ex.printStackTrace();
    12. }
    13.  
    14. if (event.getAction() == Action.RIGHT_CLICK_BLOCK||event.getAction() == Action.LEFT_CLICK_BLOCK) {
    15. if (clicked.getType() == Material.ENCHANTMENT_TABLE) {
    16. if (p.getItemInHand().getType() != Material.STICK) {
    17. event.setCancelled(true);
    18. event.getPlayer().sendMessage(Core.prefix + " §bPlease click with a stick");
    19. return;
    20. }
    21. if (yloc == yellowloc) {
    22. Utils.destroyStick(plugin, bloc, "Yellow");
    23. event.setCancelled(true);
    24. p.getInventory().remove(Material.STICK);
    25. p.updateInventory();
    26. } else {
    27. event.setCancelled(true);
    28. event.getPlayer().sendMessage(Core.prefix + " §cPlease destroy the stick on an altar");
    29. }
    30. }
    31. }
    32. }
     
  2. Offline

    StevenMG

    I think Material.GOLD_INGOT() returns an integer value. Instead of comparing it to getItemInHand.getType() compare it to getItemInHand.getTypeId(). I am not at my development PC though, so I am not able to test what I just said.
     
  3. Offline

    Linschlager

    StevenMG there is no java error or any in the console, its just not working at clicking on an enchantment table
     
Thread Status:
Not open for further replies.

Share This Page