Solved Check if player threw Ender Pearl

Discussion in 'Plugin Development' started by Xp10d3, Nov 13, 2020.

  1. Offline

    Xp10d3

    Hello. It's been a while lol. Anyways I just got back into plugin development because my friend wants me to code a quick Pot PvP plugin to test whether CPS matters in PvP (it does of course) using an autoclicker. So I coded the whole thing but am stuck on the ender pearl time part. So in Pot PvP whenever you throw an ender pearl, your experience bar fills to 15 levels then every 20 ticks or so it drains one level (so simply set a Bukkit Runnable and set the player's experience bar to one less than they have IF they have any experience). However, I need to check whether the player threw an Ender Pearl or not. How would I do that? My code isn't really relevant since I did basically nothing but here it is anyways...

    PlayerListeners.java:
    Code:java
    1.  
    2. package eltik.test.potpvp;
    3.  
    4. import org.bukkit.GameMode;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.PlayerDeathEvent;
    9.  
    10. public class PlayerListeners implements Listener {
    11. @EventHandler
    12. public void onDeath(PlayerDeathEvent event) {
    13. Player player = event.getEntity();
    14. player.spigot().respawn();
    15. player.setGameMode(GameMode.SPECTATOR);
    16. }
    17. }
    18.  


    Commands.java:
    Code:java
    1.  
    2. package eltik.test.potpvp;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.potion.Potion;
    15. import org.bukkit.potion.PotionType;
    16.  
    17. public class Commands implements CommandExecutor {
    18. // Get the main class
    19. Core core = Core.getPlugin(Core.class);
    20.  
    21. // Constructor. Register all commands
    22. public Commands(Core core) {
    23. this.core = core;
    24. Bukkit.getPluginCommand("potpvp").setExecutor(this);
    25. Bukkit.getPluginCommand("classic").setExecutor(this);
    26. }
    27.  
    28. @Override
    29. public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
    30. // If the sender of a command is NOT a player...
    31. if (!(sender instanceof Player)) {
    32. sender.sendMessage("You must be a player to access this command!");
    33. return false;
    34. }
    35.  
    36. // Get the player
    37. Player player = (Player) sender;
    38.  
    39. if (cmd.getName().equalsIgnoreCase("potpvp")) {
    40. player.getInventory().clear();
    41. ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET);
    42. ItemMeta helmetMeta = helmet.getItemMeta();
    43. helmetMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true);
    44. helmetMeta.addEnchant(Enchantment.DURABILITY, 3, true);
    45. helmet.setItemMeta(helmetMeta);
    46.  
    47. ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
    48. ItemMeta chestMeta = chestplate.getItemMeta();
    49. chestMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true);
    50. chestMeta.addEnchant(Enchantment.DURABILITY, 3, true);
    51. chestplate.setItemMeta(chestMeta);
    52.  
    53. ItemStack leggings = new ItemStack(Material.DIAMOND_LEGGINGS);
    54. ItemMeta legMeta = leggings.getItemMeta();
    55. legMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true);
    56. legMeta.addEnchant(Enchantment.DURABILITY, 3, true);
    57. leggings.setItemMeta(legMeta);
    58.  
    59. ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);
    60. ItemMeta bootMeta = boots.getItemMeta();
    61. bootMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true);
    62. bootMeta.addEnchant(Enchantment.DURABILITY, 3, true);
    63. boots.setItemMeta(bootMeta);
    64.  
    65. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    66. ItemMeta swordMeta = sword.getItemMeta();
    67. swordMeta.addEnchant(Enchantment.DAMAGE_ALL, 3, true);
    68. swordMeta.addEnchant(Enchantment.FIRE_ASPECT, 2, true);
    69. swordMeta.addEnchant(Enchantment.DURABILITY, 3, true);
    70. sword.setItemMeta(swordMeta);
    71.  
    72. ItemStack pearl = new ItemStack(Material.ENDER_PEARL, 16);
    73. ItemMeta pearlMeta = pearl.getItemMeta();
    74. pearlMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Ender Pearl");
    75. pearl.setItemMeta(pearlMeta);
    76.  
    77. Potion healing = new Potion(PotionType.INSTANT_HEAL, 2);//poison 1
    78. healing.setSplash(true);
    79. ItemStack healPot = healing.toItemStack(1);
    80.  
    81. Potion fire = new Potion(PotionType.FIRE_RESISTANCE, 1);
    82. fire.setHasExtendedDuration(true);
    83. ItemStack firePot = fire.toItemStack(1);
    84.  
    85. Potion speed = new Potion(PotionType.SPEED, 2);
    86. ItemStack speedPot = speed.toItemStack(4);
    87.  
    88. player.getInventory().setHelmet(helmet);
    89. player.getInventory().setChestplate(chestplate);
    90. player.getInventory().setLeggings(leggings);
    91. player.getInventory().setBoots(boots);
    92.  
    93. player.getInventory().setItem(0, sword);
    94. player.getInventory().setItem(1, pearl);
    95. player.getInventory().setItem(2, firePot);
    96. player.getInventory().setItem(3, speedPot);
    97. //16
    98. for (int i = 0; i <= 24; i++) {
    99. player.getInventory().addItem(healPot);
    100. }
    101. }
    102.  
    103. if (cmd.getName().equalsIgnoreCase("classic")) {
    104. player.getInventory().clear();
    105. ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET);
    106.  
    107. ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
    108.  
    109. ItemStack leggings = new ItemStack(Material.DIAMOND_LEGGINGS);
    110.  
    111. ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);
    112.  
    113. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    114.  
    115. player.getInventory().setHelmet(helmet);
    116. player.getInventory().setChestplate(chestplate);
    117. player.getInventory().setLeggings(leggings);
    118. player.getInventory().setBoots(boots);
    119.  
    120. player.getInventory().setItem(0, sword);
    121. player.getInventory().setItem(1, new ItemStack(Material.FISHING_ROD));
    122. player.getInventory().setItem(2, new ItemStack(Material.BOW));
    123. player.getInventory().setItem(3, new ItemStack(Material.ARROW, 10));
    124. player.getInventory().setItem(4, new ItemStack(Material.GOLDEN_APPLE, 6));
    125. }
    126. return false;
    127. }
    128.  
    129. }
    130.  
     
  2. Online

    xelatercero

    Maybe you can use ProjectileLaunchEvent , and just get the entity who launched the projectile check if it is a player and then check if the projectile is a ender pearl
     
  3. Offline

    Kars

    1. Have a collection with player id's of people that threw pearls.
    2. When a pearl is thrown, add player to the list & schedule a task to remove the player from the list after a certain time.
     
  4. Offline

    Xp10d3

    Never mind, I figured it out.
    Code:java
    1.  
    2. @EventHandler
    3. public void onPearl(PlayerInteractEvent event) {
    4. Player player = event.getPlayer();
    5.  
    6. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    7. if (player.getItemInHand().getType().equals(Material.ENDER_PEARL)) {
    8. core.otherLog("Player used Ender Pearl.");
    9. int exp = player.getTotalExperience();
    10. core.otherLog("Shooter was a player. Player: " + player + ". Player name: " + player.getName() + ". Exp: " + exp);
    11. if (exp > 0) {
    12. event.setCancelled(true);
    13. player.sendMessage("You have to wait before using this!");
    14. } else {
    15. player.setTotalExperience(255);
    16. for (int expe = player.getTotalExperience(); expe < 1; expe--) {
    17. player.setTotalExperience(expe - 1);
    18. player.sendMessage("Player's experience: " + expe);
    19. }
    20. }
    21. }
    22. }
    23. }
    24.  

    At least I think this works. If it doesn't I'll let people know...
     

Share This Page