Teleport to map center location (X, Y, Z)

Discussion in 'Plugin Development' started by Twaia, Dec 7, 2018.

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

    Twaia

    I want to do something easy, when a player is shifting (isSneaking) and right click on an Item Frame that has a Map in it, then teleport the player to the center location of that map.

    I've got something like this:
    Code:
        @EventHandler
        public void onPlayerInteractEntity(PlayerInteractEntityEvent event){
            Player player = event.getPlayer();
            Entity entity = event.getRightClicked();
            if (entity instanceof ItemFrame) {
                ItemFrame itemFrame = (ItemFrame) entity;
                ItemStack itemStack = (ItemStack) itemFrame;
                if (itemStack.getType() == Material.MAP) {
                    short durability = itemStack.getDurability();
                    MapView map = Bukkit.getServer().getMap(durability);
                    if (player.isSneaking()) {
                        Location location = map.getWorld().getSpawnLocation(); // Instead of that, I want to take the center of that specific map
                        player.teleport(location);
                    }
                }
            }
    
        }
    But nothing happens

    Code:
    ItemStack item = itemframe.getItem(); // instead to take the map.
    The issue now is the location. I know I can put an specific location in config yaml, but I want to get the location of the center of the map, i mean the X, Y and Z position of the center of the map in the world that it pertains. I don't know if you can understand me...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 7, 2018
  2. Offline

    Ironraptor3

    So, what do you mean "Nothing happens"? Does the event never happen? If you put a debug statement before initializing player, does it print? If not, you have not registered your events class in your onEnable() method.

    For teleporting them to the center of the map, I cannot say that I know everything there is to know about map metadata. My educated guess is that it does not save where it is generated, for why would it need to, the image doesn't change after rendering it. Instead, I believe that when a player renders a map (look for the event that does this, save where they are standing to the item (through whatever means you'd like), then teleport them to the saved location on the right click whilst shifting.

    Also, you have your Entity entity, which after the first if statement, must be an ItemFrame, you cast it accordingly to ItemFrame itemFrame. You then cast it to an ItemStack itemStack and compare it to a map... but hold on, we just said it had to be the item ItemFrame. I think you mean ItemStack itemStack = itemFrame.getItem();//The Item within the Item Frame.

    Again, I'm not entirely informed about the inner workings of map, so I could be flat wrong in this criticism, but I don't think the durability value of the map corresponds to its MapViewID. Tahg me/Reply if you need further help/have more questions.
     
    Twaia likes this.
  3. Offline

    Twaia

    Ok, update:
    Code:
        @EventHandler
        public void onPlayerInteractEntity(PlayerInteractEntityEvent event){
            Player player = event.getPlayer();
            Entity entity = event.getRightClicked();
            if (entity instanceof ItemFrame) {
                final ItemFrame frame = (ItemFrame) entity;
                if (frame.getItem() != null && frame.getItem().getType() == Material.MAP) {
                    if (player.isSneaking()) {
                        final MapView mapView = Bukkit.getMap(frame.getItem().getDurability());
                        Location location = new Location(mapView.getWorld(), mapView.getCenterX(), mapView.getWorld().getHighestBlockYAt(mapView.getCenterX(), mapView.getCenterZ()), mapView.getCenterZ());
                        player.teleport(location);
                        player.sendMessage(ChatColor.GREEN + "Teleported to " + location.getX() + ", " + location.getY() + ", " + location.getZ());
                    }
                }
            }
        }
    Now is teleporting the players to same place, without relation with the map, same coordinades always.
    I paste that in case if is useful:
    Code:
       <dependency>
          <groupId>org.bukkit</groupId>
          <artifactId>bukkit</artifactId>
          <version>1.8-R0.1-SNAPSHOT</version>
          <type>jar</type>
          <scope>compile</scope>
       </dependency>
    
       <repository>
          <id>spigot-repo</id>
          <url>https://hub.spigotmc.org/nexus/content/groups/public</url>
       </repository>
    Thanks for helping me!! @Ironraptor3
     
  4. Offline

    Ironraptor3

    @Twaia could you post the coordinates? I looked around for a bit on getCenterX and couldn't find anything specific enough to go off of, but perhaps its talking about the drawing coordinates (like 0,0 is at the top left of the map) and not where the map was made?

    And btw the durability thing you are doing is correct, I was wrong.
     
  5. Offline

    Twaia

    Ok guys, solved, thanks very much!! Then, solution:

    In Listener:
    Code:
       
        @EventHandler
        public void onPlayerInteractEntity(PlayerInteractEntityEvent event){
            Player player = event.getPlayer();
            Entity entity = event.getRightClicked();
            if (entity instanceof ItemFrame) {
                final ItemFrame frame = (ItemFrame) entity;
                Rotation frameRotation = frame.getRotation().rotateCounterClockwise();
                if (frame.getItem() != null && frame.getItem().getType() == Material.FILLED_MAP) {
                    if (player.isSneaking()) {
                        frame.setRotation(frameRotation);
                        final MapMeta meta = (MapMeta)frame.getItem().getItemMeta();
                        final MapView mapView = Bukkit.getMap((short)meta.getMapId());
                        Location location = new Location(mapView.getWorld(), mapView.getCenterX(),
                                mapView.getWorld().getHighestBlockYAt(mapView.getCenterX(), mapView.getCenterZ()), mapView.getCenterZ());
                        player.teleport(location);
                        player.sendMessage(ChatColor.GREEN + "Teleported to " + location.getX() + ", " + location.getY() + ", " + location.getZ());
                    }
                }
            }
        }
    In pom.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
    [...]
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.13.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
    [...]
            <repository>
                <id>spigot-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
    
    And finally, in plugin YAML:
    Code:
    api-version: 1.13
     
Thread Status:
Not open for further replies.

Share This Page