Solved PlayerRespawnEvent

Discussion in 'Plugin Development' started by Alex3543, Jun 21, 2013.

  1. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Why does this not work?


    Code:java
    1. @EventHandler
    2. public void ondeath(PlayerRespawnEvent event){
    3. Player player = event.getPlayer();
    4. if(ingame.containsKey(player.getName())){
    5. player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));
    6. }
    7. }




    Whole class:
    Code:java
    1. package me.alex.bowpvp;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.HashMap;
    6. import java.util.Random;
    7.  
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.GameMode;
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.block.Block;
    14. import org.bukkit.command.Command;
    15. import org.bukkit.command.CommandSender;
    16. import org.bukkit.enchantments.Enchantment;
    17. import org.bukkit.entity.Arrow;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.block.Action;
    22. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    23. import org.bukkit.event.entity.PlayerDeathEvent;
    24. import org.bukkit.event.player.AsyncPlayerChatEvent;
    25. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    26. import org.bukkit.event.player.PlayerInteractEvent;
    27. import org.bukkit.event.player.PlayerRespawnEvent;
    28. import org.bukkit.inventory.ItemStack;
    29. import org.bukkit.inventory.meta.ItemMeta;
    30. import org.bukkit.plugin.java.JavaPlugin;
    31. import org.bukkit.util.Vector;
    32.  
    33. public class bowpvp extends JavaPlugin implements Listener{
    34. public final HashMap<String, ArrayList<Block>> ingame = new HashMap<String, ArrayList<Block>>();
    35.  
    36. @Override
    37. public void onDisable() {
    38. System.out.println("bowpvp deaktiviert.");
    39. }
    40.  
    41. @Override
    42. public void onEnable() {
    43. getServer().getPluginManager().registerEvents(this,this);
    44. System.out.println("bowpvp aktiviert.");
    45. }
    46.  
    47. @Override
    48. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    49.  
    50. Player player = (Player) sender;
    51.  
    52. if(cmd.getName().equalsIgnoreCase("bp")){
    53. if(args.length == 0){
    54. player.sendMessage("§3BowPvP - made by alexmaster00:");
    55. player.sendMessage("§3/bp join");
    56. player.sendMessage("§3/bp leave");
    57. }
    58. //-----------------------------------------------------------
    59. if(args.length == 1){
    60. if(args[0].equalsIgnoreCase("join")){
    61. player.sendMessage("[§3PvP§f] §9Du bist §3BowPvP §9beigetreten.");
    62. player.sendMessage("[§3PvP§f] §9Plugin made by §4alexmaster00.");
    63. player.teleport(new Location(Bukkit.getWorld("fun"), 37, 4, 4215));
    64. ingame.put(player.getName(), null);
    65. //---------------------------------------------------------------
    66. }else if(args[0].equalsIgnoreCase("leave")){
    67. player.sendMessage("[§3PvP§f] §9Du hast das Spiel verlassen.");
    68. player.getInventory().clear();
    69. player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));
    70. ingame.remove(player.getName());
    71. }
    72. //------------------------------------------------------------------------------
    73. if(args[0].equalsIgnoreCase("normalbow")){
    74. if(ingame.containsKey(player.getName())){
    75. ItemStack bow = new ItemStack(Material.BOW, 1);
    76. ItemMeta bm = bow.getItemMeta();
    77. bm.setDisplayName("§2Normaler Bogen");
    78. bm.setLore(Arrays.asList("§4Nix besonderes."));
    79. bm.addEnchant(Enchantment.ARROW_INFINITE, 1, true);
    80. bm.addEnchant(Enchantment.ARROW_DAMAGE, 1, true);
    81. bow.setItemMeta(bm);
    82.  
    83. Random object = new Random();
    84. int spawn = object.nextInt(3);
    85.  
    86.  
    87. if(spawn == 0){
    88. Location bs = new Location(Bukkit.getWorld("fun"), 147, 5, 4196);
    89. player.teleport(bs);
    90. player.getInventory().clear();
    91. player.setGameMode(GameMode.SURVIVAL);
    92. player.getInventory().addItem(new ItemStack(bow));
    93. player.getInventory().addItem(new ItemStack(262, 1));
    94. }
    95. if(spawn == 1){
    96. Location bp = new Location(Bukkit.getWorld("fun"), 126, 6, 4201);
    97. player.teleport(bp);
    98. player.getInventory().clear();
    99. player.setGameMode(GameMode.SURVIVAL);
    100. player.getInventory().addItem(new ItemStack(bow));
    101. player.getInventory().addItem(new ItemStack(262, 1));
    102.  
    103. }
    104. if(spawn == 2){
    105. Location bn = new Location(Bukkit.getWorld("fun"), 122, 4, 4179);
    106. player.teleport(bn);
    107. player.getInventory().clear();
    108. player.setGameMode(GameMode.SURVIVAL);
    109. player.getInventory().addItem(new ItemStack(bow));
    110. player.getInventory().addItem(new ItemStack(262, 1));
    111. }
    112. }
    113. }
    114. //-------------------------------------------------------------
    115. }
    116. }
    117.  
    118.  
    119.  
    120. return true;
    121.  
    122.  
    123. }
    124.  
    125. @EventHandler
    126. public void onPlayerInteract(PlayerInteractEvent event) {
    127.  
    128. if ((event.getAction().equals(Action.RIGHT_CLICK_AIR)) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))) {
    129. Player player = event.getPlayer();
    130. ItemStack is = player.getItemInHand();
    131. if ((is.hasItemMeta()) &&
    132. (is.getItemMeta().hasDisplayName()) &&
    133. (is.getItemMeta().hasLore()) &&
    134. (is.getItemMeta().getDisplayName().contains("Sniper")) &&
    135. (is.getItemMeta().getLore().contains("Sniper!")))
    136. {
    137. Vector velo = player.getLocation().getDirection().clone().multiply(3);
    138. Arrow arrow1 = player.launchProjectile(Arrow.class);
    139. arrow1.setVelocity(velo);
    140. }
    141. }
    142. }
    143.  
    144.  
    145. @EventHandler
    146. public void ondeath(PlayerRespawnEvent event){
    147. Player player = event.getPlayer();
    148. if(ingame.containsKey(player.getName())){
    149. player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));
    150. }
    151. }
    152.  
    153. }
    154.  
  2. Offline

    pkt

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    ingame might not contain the players name, did you add the players name when the player logs in?
  3. Offline

    Rocoty

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    What does not work? What do you expect to happen? What actually happens? Explain, please. Don't just say it doesn't work.
  4. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    its a bowpvp plugin:

    you need to join with /bp join to get added to "ingame"
    even when i do that i dont spawn at the coords
  5. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Normally I should to spawn here:
    ("fun"), -598, 4, 3333
    When I die while the bowpvp,but I still respawn at spawn
  6. Offline

    ZeusAllMighty11 BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You have to add at least a 1 tick delay, because that code isnt firing
  7. Offline

    pkt

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    try using ingame.get(playername) != null instead of .containsKey()
  8. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    if(ingame.get(player) != null){
    you mean like this?
  9. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME

    how do i add delay
  10. Offline

    MCForger

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  11. Offline

    pkt

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Yea

    new BukkitRunnable() {
    public void run() {
    //code
    }
    }.runTaskLater(plugin, delayTime);
  12. Offline

    blm456

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Code:java
    1. Bukkit.getSchedualer.schedualSyncDelayedTask(this, new Runnable(){
    2. public void run(){
    3. //Java stuff
    4. }
    5. }, 20L)

    20L = 1 second and change "this" to your main plugin class!

    This post has been edited 1 time. It was last edited by TnT Jun 21, 2013.
  13. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME


    Both dont work...nothing happens,the one with delay wants me to change
    Player player = event.getPlayer(); to
    final Player player = event.getPlayer();
  14. Offline

    ZeusAllMighty11 BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Then change it. It's only a final modifier
  15. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    can you add a delay to this please :)

    Code:
    @EventHandler
        public void ondeath(PlayerRespawnEvent  event){
            Player player = event.getPlayer();
            if(ingame.get(player) != null){
                    player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));           
                   
            }
        }

    This post has been edited 1 time. It was last edited by Alex3543 Jun 21, 2013.
  16. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    i cahnged it,it didnt work
  17. Offline

    ZeusAllMighty11 BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  18. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  19. Offline

    ZeusAllMighty11 BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    There is a link above with all the code you need. If you know java, you should at least know where to put it.
  20. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I did it already but it wont work,thats why im asking you
  21. Offline

    pkt

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    No need to be rude, but When I was learning Java/Bukkit, None of the Wiki's helped me, except with yml, so I don't expect anyone else to learn from the Wiki's either.

    @Alex3543
    Code:
        @EventHandler
        public void ondeath(PlayerRespawnEvent  event){
            Player player = event.getPlayer();
            new BukkitRunnable() {
                public void run() {
                    if(ingame.get(player) != null){
                        player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));       
                    }
                }
            }.runTaskLater(plugin, delay);
        }

    Also, make edits from what @ProtoTempus said

    This post has been edited 1 time. It was last edited by pkt Jun 21, 2013.
  22. Offline

    AmShaegar

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME

    This post has been edited 2 times. It was last edited by AmShaegar Jun 21, 2013.
  23. Offline

    ProtoTempus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @Alex3543 Player is not technically alive when this code is run:
    Code:java
    1. player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));


    Instead use:
    Code:java
    1. event.setRespawnLocation(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));
  24. Offline

    ZeusAllMighty11 BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @pkt

    I wasn't being rude, I was telling the truth.

    You can't learn by being given the code.


    Think of it like you're in college taking an exam. Let's say your professor gives you a multiple choice test with 50 questions. If you somehow memorize the letter answers to those questions, and you don't know what the actual answer to the question is, and you don't know the actual reason why it's the answer, then you are just cheating yourself.

    So don't cheat yourself, learn something.

    This post has been edited 1 time. It was last edited by TheGreenGamerHD Jun 21, 2013.
  25. Offline

    ProtoTempus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You know when you're SUPER tired and you're trying to make edits to your post but instead you click delete and the popup box comes up and you click delete before you even think about it? Yeah that just happened.
  26. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME

    Thx !!
  27. Offline

    ProtoTempus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    No problem. It's a common mistake. Continue coding, asking questions (nothing wrong with asking), and learning!

    This post has been edited 1 time. It was last edited by ProtoTempus Jun 21, 2013.
  28. Offline

    ProtoTempus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I partially disagree. While I would encourage coders to spend hours figuring it out them self, some people need examples to look at to understand what is happening/being said. Everyone learns a little bit different.

    Edit: Obviously there are some occasions when a user has no intent on learning and just uses the code.

    This post has been edited 1 time. It was last edited by ProtoTempus Jun 21, 2013.
  29. Offline

    ZeusAllMighty11 BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @ProtoTempus

    ok can u giv m super craft bros pls ok i need it done fast ok ty

    *bump*
    *bump*
    *bump*
  30. Offline

    Alex3543

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I am so stupid, I actually didnt have anything wrong when I asked the question , the coords are the ones from spawn and not from the place where I want to spawn (facepalm)

Share This Page