Code Request

Discussion in 'Plugin Development' started by Amrefinium, May 15, 2013.

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

    Amrefinium

    can some one help me with a little problem of mine.
    i don't know what code i need to be able to teleport my player to where his cross air is.
    can someone help, can you post up the code im looking for please.
    Many thanks
     
  2. Offline

    chasechocolate

    Code:java
    1. Block block = player.getTargetBlock(null, <max distance>);
    2. Location teleportLoc = block.getLocation();
    3. teleportLoc.setY(teleportLoc.getY() + 1.5); //Prevent teleporting into the block
    4. player.teleport(teleportLoc);
     
  3. Offline

    Amrefinium

    thanks, can i have the whole page iv tried to paste it in but its not working properly
     
  4. Do you have any Java knowledge?
     
  5. Offline

    Amrefinium

    a bit, but im still a bit of a noob
     
  6. I suggest you to learn some more, if you can't even use that little piece of code, but I will help you, show us your whole code please.
     
  7. Offline

    Amrefinium

    Code:
    package me.BlubBlubx.teleport;
     
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Teleport extends JavaPlugin{
        public static Teleport plugin;
       
        [USER=90830436]EventHandler[/USER]
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            int blockId = player.getItemInHand().getType().getId();
            if(blockId == 280){
            Block block = player.getTargetBlock(null, 200);
            Location teleportLoc = block.getLocation();
            teleportLoc.setY(teleportLoc.getY() + 1.5);
            Player.teleport(teleportLoc);
            }
        }
    }    
     
  8. And where does it give errors? I would suggest to not use id's but materials/blocks...
     
  9. Offline

    Amrefinium

    Player.teleport(teleportLoc);
    thats underlined red, and ill try that, ill send you the code when im done

    Code:
    package me.BlubBlubx.teleport;
     
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Teleport extends JavaPlugin{
        public static Teleport plugin;
       
        [USER=90830436]EventHandler[/USER]
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            int blockId = player.getItemInHand().getMaterial(id)(){
            if(blockId == 280){
            Block block = player.getTargetBlock(null, 200);
            Location teleportLoc = block.getLocation();
            teleportLoc.setY(teleportLoc.getY() + 1.5);
            Player.teleport(teleportLoc);
            }
            }
        }
    }    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  10. Offline

    chasechocolate

    Amrefinium ah, it's thinking that teleport() is a static method in the player class. Change "Player" to "player".
     
  11. Offline

    Amrefinium

    okaii, done that but theres still an error, do i use Override or EventHandler or something else
     
  12. Offline

    chasechocolate

    Exactly how you have it should work fine. And yes, you would always use the EventHandler annotation above each event method.
     
  13. Offline

    ZeusAllMighty11

  14. Offline

    Amrefinium

    okaii, but the last line is redlined, how to i fix this??

    iv done that

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  15. Offline

    NoLiver92

    wheres the onEnable and onDisable in the code that was posted?
     
  16. Offline

    chasechocolate

    Code:java
    1. [USER=90830436]EventHandler[/USER]
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. int blockId = player.getItemInHand().getMaterial(id)(){
    5. if(blockId == 280){
    6. Block block = player.getTargetBlock(null, 200);
    7. Location teleportLoc = block.getLocation();
    8. teleportLoc.setY(teleportLoc.getY() + 1.5);
    9. player.teleport(teleportLoc);
    10. }
    11. }
    12. }
     
  17. Offline

    Amrefinium

    oh, that helps, sorry i didn't click straight off, now there parts of text that are highlighted grey is that fine??
     
  18. Offline

    chasechocolate

    What parts? You will need to change the top part, someone's name is "EventHandler" so it will tag/tahg the user.
     
  19. Offline

    Amrefinium

    it's all good now just testing it out ill give you feedback in 5 mins
     
  20. Offline

    Amrefinium

    Code:
        [USER=90830436]EventHandler[/USER]
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            int blockId = player.getItemInHand().getType().getId();
            if(blockId == 280){
                Block block = player.getTargetBlock(null, 200);
                Location location = block.getLocation();
                World world = player.getWorld();
                world.createExplosion(location, 5);
            } else if(blockId == 352){
                Block block = player.getTargetBlock(null, 200);
                Location location = block.getLocation();
                World world = player.getWorld();
                world.strikeLightning(location);
            } else if(blockId == 345){
                Block block = player.getTargetBlock(null, 200);
                Location teleportLoc = block.getLocation();
                teleportLoc.setY(teleportLoc.getY() + 1.5);
                player.teleport(teleportLoc);
            }
        }   
    }
    This is the code that works for me, before the event handler use public static <name of plugin> plugin;
     
Thread Status:
Not open for further replies.

Share This Page