PlayerRespawnEvent not working

Discussion in 'Plugin Development' started by MinecraftMarc3l, Oct 24, 2014.

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

    MinecraftMarc3l

    Code:java
    1. package duell;
    2.  
    3. import java.util.HashMap;
    4. import net.milkbowl.vault.economy.Economy;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.EventPriority;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.PlayerDeathEvent;
    15. import org.bukkit.event.player.PlayerRespawnEvent;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.plugin.RegisteredServiceProvider;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. /**
    21. *
    22. * @author Marcel
    23. */
    24. public class Duell extends JavaPlugin implements Listener {
    25.  
    26. @Override
    27. public void onEnable() {
    28. getServer().getPluginManager().registerEvents(this, this);
    29. RegisteredServiceProvider economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
    30. if(economyProvider != null) {
    31. economy = (Economy)economyProvider.getProvider();
    32. }
    33. }
    34. public static HashMap<Player, Integer> induell = new HashMap<>();
    35. public static HashMap<Player, Integer> hatanfrage = new HashMap<>();
    36. public static HashMap<Player, Player> anfrage = new HashMap<>();
    37. public static HashMap<Player, String> invent = new HashMap<>();
    38. public Location LastDeath;
    39. private HashMap<String, ItemStack[]> mySavedItems = new HashMap<String, ItemStack[]>();
    40. public static Economy economy = null;
    41.  
    42. @Override
    43. public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
    44. final Player p = (Player) sender;
    45. if(cmd.getName().equalsIgnoreCase("duell")) {
    46. if(args.length == 1) {
    47. if(args[0].equalsIgnoreCase("annehmen")) {
    48. try {
    49. if (!anfrage.get(p).isOnline()) {
    50. throw new NullPointerException();
    51. }
    52. } catch (NullPointerException e) {
    53. p.sendMessage("§6[EE1BSystem | Duell]§e" + args[0] + "§b ist nicht mehr online.");
    54. return false;
    55. }
    56. p.sendMessage("§6[EE1BSystem | Duell]§bDu hast das Duell angenommen");
    57. anfrage.get(p).sendMessage("§6[EE1BSystem | Duell]§e" + p.getName() + "§b hat deine Duell Herrausforderung angenommen.");
    58. induell.put(p, 1);
    59. induell.put(anfrage.get(p),1);
    60. hatanfrage.put(anfrage.get(p),0);
    61. hatanfrage.put(p,0);
    62. Location p1 = new Location(p.getWorld(), -234, 163, 308);
    63. Location p2 = new Location(p.getWorld(), -306, 163, 308);
    64. p.teleport(p1);
    65. anfrage.get(p).teleport(p2);
    66. p.getInventory().clear();
    67. anfrage.get(p).getInventory().clear();
    68. ItemStack sword1 = new ItemStack(Material.STONE_SWORD, 1);
    69. p.getInventory().addItem(sword1);
    70. anfrage.get(p).getInventory().addItem(sword1);
    71. p.setFoodLevel(20);
    72. anfrage.get(p).setFoodLevel(20);
    73. p.setHealth(20);
    74. anfrage.get(p).setHealth(20);
    75. ItemStack helmet = new ItemStack(Material.LEATHER_HELMET, 1);
    76. ItemStack chest = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    77. p.getEquipment().setHelmet(helmet);
    78. p.getEquipment().setChestplate(chest);
    79. anfrage.get(p).getEquipment().setHelmet(helmet);
    80. anfrage.get(p).getEquipment().setChestplate(chest);
    81. } else if(args[0].equalsIgnoreCase("ablehnen")) {
    82. anfrage.remove(anfrage.get(p));
    83. hatanfrage.put(anfrage.get(p),0);
    84. hatanfrage.put(p,0);
    85. anfrage.remove(p);
    86. } else if(args[0].equalsIgnoreCase("abbrechen")) {
    87. p.sendMessage("§6[EE1BSystem | Duell]§bDu hast deine Duell Herrausforderung zurückgezogen");
    88. anfrage.get(p).sendMessage("§6[EE1BSystem | Duell]§bHat seine Duell Herrausforderung zurückgezogen.");
    89. anfrage.remove(anfrage.get(p));
    90. hatanfrage.put(anfrage.get(p),0);
    91. hatanfrage.put(p,0);
    92. anfrage.remove(p);
    93. } else {
    94. final Player target = p.getServer().getPlayerExact(args[0]);
    95. if (sender.getName().equalsIgnoreCase(args[0])) {
    96. p.sendMessage("§6[EE1BSystem | Duell]§bDu kannst dich nicht selber zu einem Duell herrausfordern.");
    97. return false;
    98. }
    99. try {
    100. if (!target.isOnline()) {
    101. throw new NullPointerException();
    102. }
    103. } catch (NullPointerException e) {
    104. p.sendMessage("§6[EE1BSystem | Duell]§e" + args[0] + " §bist nicht Online.");
    105. return false;
    106. }
    107. try {
    108. if(induell.get(target) == 1) {
    109. p.sendMessage("§6[EE1BSystem | Duell]§bDieser Spieler ist bereits in einem Duell!");
    110. return false;
    111. }
    112. if(induell.get(p) == 1) {
    113. p.sendMessage("§6[EE1BSystem | Duell]§bDu bist bereits in einem Duell!");
    114. return false;
    115. }
    116. } catch (NullPointerException ex) {
    117.  
    118. }
    119. try {
    120. if(hatanfrage.get(target) == 1) {
    121. p.sendMessage("§6[EE1BSystem | Duell]§bDieser Spieler hat bereits eine Anfrage!");
    122. return false;
    123. }
    124. if(hatanfrage.get(p) == 1) {
    125. p.sendMessage("§6[EE1BSystem | Duell]§bDu hast bereits eine Anfrage!");
    126. return false;
    127. }
    128. } catch (NullPointerException ex) {
    129.  
    130. }
    131. anfrage.put(p, target);
    132. anfrage.put(target, p);
    133. hatanfrage.put(p,1);
    134. hatanfrage.put(target,1);
    135. p.sendMessage("§6[EE1BSystem | Duell]§bDu hast den Spieler " + target.getDisplayName() + "§b zu einem Duell herrausgefordert.");
    136. target.sendMessage("§6[EE1BSystem | Duell]§bDer Spieler " + p.getDisplayName() + "§b fordert dich zu einem Duell herraus.");
    137. target.sendMessage("§6[EE1BSystem | Duell]§e/duell annehmen/ablehnen!");
    138. }
    139. return true;
    140. }
    141. } else {
    142. p.sendMessage("§6[EE1BSystem | Duell]§bCommands:");
    143. p.sendMessage("§b/duell annehmen");
    144. p.sendMessage("§b/duell ablehnen");
    145. p.sendMessage("§b/duell abbrechen");
    146. p.sendMessage("§b/duell [Spieler]");
    147. }
    148. return false;
    149. }
    150.  
    151. @EventHandler(priority = EventPriority.HIGHEST)
    152. public void onPlayerRespawn(PlayerRespawnEvent eve) {
    153. final Player p = eve.getPlayer();
    154. if(induell.get(p) == 1) {
    155. induell.put(p,0);
    156. anfrage.remove(anfrage.get(p));
    157. anfrage.remove(p);
    158. }
    159. p.sendMessage("§6[EE1BSystem]&bLetze Position vor dem Tod: X=" + LastDeath.getBlockX() + " Y=" + LastDeath.getBlockY() + " Z=" + LastDeath.getBlockZ());
    160. Bukkit.broadcastMessage("Test");
    161. }
    162.  
    163. @EventHandler
    164. public void onPlayerDeath(PlayerDeathEvent event) {
    165. Bukkit.getServer().broadcastMessage("§6[EE1BSystem]§b" + event.getEntity().getName() + " starb.");
    166. Player p = event.getEntity().getPlayer();
    167. LastDeath = p.getLocation();
    168. Player killer = anfrage.get(p);
    169. if(induell.get(p) == 1) {
    170. p.sendMessage("§6[EE1BSystem | Duell]§bDu hast das Duell gegen " + killer.getName() + " verloren.");
    171. killer.sendMessage("§6[EE1BSystem | Duell]§bDu hast das Duell gegen " + p.getName() + " gewonnen.");
    172. killer.getInventory().clear();
    173. induell.put(killer,0);
    174. }
    175. }
    176. }

    Hello,

    My PlayerRespawnEvent just won't work i do not know why everything seems fine to me ... here is my whole Plugin, I hope someone can help me.
     
  2. Offline

    xepisolonxx

    MinecraftMarc3l Replace
    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);

    with
    Code:java
    1. PluginManager pm = Bukkit.getPluginManager();
    2. pm.registerEvents(this, this);
     
  3. Offline

    MinecraftMarc3l

    My other Event is working so that is not the Matter i think but ill try

    The Server is not working anymore if i do what u said ^^

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

    acer5999

    First off, Ouch. That code hurts my eyes
    Second off, xepisolonxx Your code is the exact same except saving the first method to a variable, the reason MinecraftMarc3l s plugin broke is because you forgot getServer(), Your code should be

    Code:java
    1. PluginManager pm = getServer().getPluginManager();
    2. pm.registerEvents(this, this);


    Is the console throwing errors?
    What part of the plugin isn't working?
     
    mine-care likes this.
  5. Offline

    mine-care

    Umm as with @acer5999's yes mine hurt too
    Please stop abusing static!
    2. Read my signature about casting sender to player
    3. I would sugest the use of more than one classes for listeners so code doesn't pile up in the main class but that's an option
    4. Avoid storage of player object in hashmap... (Google it for the reason why)
    5. Why in the command, if the player is not online, you throw a null pointer exception, then catch it and then send the message? You could use !=null and if this is true do whatever else send the message

    And about your main problem, have you tried debugging ?

    Hope I helped
     
  6. Offline

    MinecraftMarc3l

    What should i use instead of Hashmaps?
     
  7. Offline

    imLore70

    i use this code and it work fine with me :/



    @EventHandler
    public void onRespawn(PlayerRespawnEvent e)
    {
    e.getPlayer().getInventory().clear();
    e.getPlayer().getInventory().addItem(new ItemStack[] { new ItemStack(Material.STONE_SWORD) });
    }
     
  8. Offline

    MinecraftMarc3l

    imLore70 it is the same it just Looks diffrent :)

    by the way it is still not working ^^

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

    teej107

    MinecraftMarc3l Don't catch NullPointers. Check for null and stop it before the error happens. Get rid of those static variables, and use encapsulation.
     
  10. Offline

    MinecraftMarc3l

    I dont get what These ancapsulation shit is but i do not really care i just want this Event to be called
     
Thread Status:
Not open for further replies.

Share This Page