Solved PlayerRespawnEvent

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

Thread Status:
Not open for further replies.
  1. 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. ingame might not contain the players name, did you add the players name when the player logs in?
     
  3. What does not work? What do you expect to happen? What actually happens? Explain, please. Don't just say it doesn't work.
     
  4. 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. Normally I should to spawn here:
    ("fun"), -598, 4, 3333
    When I die while the bowpvp,but I still respawn at spawn
     
  6. You have to add at least a 1 tick delay, because that code isnt firing
     
  7. try using ingame.get(playername) != null instead of .containsKey()
     
  8. if(ingame.get(player) != null){
    you mean like this?
     

  9. how do i add delay
     
  10. Yea

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

    blm456

    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!
     


  12. Both dont work...nothing happens,the one with delay wants me to change
    Player player = event.getPlayer(); to
    final Player player = event.getPlayer();
     
  13. Then change it. It's only a final modifier
     
  14. can you add a delay to this please :)

    Code (Text):
    1. @EventHandler
    2.     public void ondeath(PlayerRespawnEvent  event){
    3.         Player player = event.getPlayer();
    4.         if(ingame.get(player) != null){
    5.                 player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));          
    6.                
    7.         }
    8.     }
     
  15. i cahnged it,it didnt work
     
  16. Why?
     
  17. There is a link above with all the code you need. If you know java, you should at least know where to put it.
     
  18. I did it already but it wont work,thats why im asking you
     
  19. 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 (Text):
    1.     @EventHandler
    2.     public void ondeath(PlayerRespawnEvent  event){
    3.         Player player = event.getPlayer();
    4.         new BukkitRunnable() {
    5.             public void run() {
    6.                 if(ingame.get(player) != null){
    7.                     player.teleport(new Location(Bukkit.getWorld("fun"), -598, 4, 3333));      
    8.                 }
    9.             }
    10.         }.runTaskLater(plugin, delay);
    11.     }

    Also, make edits from what ProtoTempus said
     
  20. 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));
     
  21. 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.
     
  22. 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.
     

  23. Thx !!
     
  24. No problem. It's a common mistake. Continue coding, asking questions (nothing wrong with asking), and learning!
     
  25. 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.
     
  26. ProtoTempus

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

    *bump*
    *bump*
    *bump*
     
  27. 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)
     
Thread Status:
Not open for further replies.

Share This Page