CustomItemEvents.java (17355B)
1 package me.night.nullvalkyrie.events.listeners; 2 3 import me.night.nullvalkyrie.entities.items.CustomItemManager; 4 import me.night.nullvalkyrie.entities.items.Pickaxe; 5 import me.night.nullvalkyrie.entities.miners.Rarity; 6 import me.night.nullvalkyrie.NullValkyrie; 7 import me.night.nullvalkyrie.packets.protocol.PacketPlayOutBlockBreakAnimation; 8 import net.md_5.bungee.api.ChatMessageType; 9 import net.md_5.bungee.api.chat.TextComponent; 10 import org.bukkit.*; 11 import org.bukkit.block.Block; 12 import org.bukkit.entity.*; 13 import org.bukkit.event.EventHandler; 14 import org.bukkit.event.Listener; 15 import org.bukkit.event.block.Action; 16 import org.bukkit.event.entity.*; 17 import org.bukkit.event.player.*; 18 import org.bukkit.inventory.ItemStack; 19 import org.bukkit.inventory.Merchant; 20 import org.bukkit.inventory.MerchantRecipe; 21 import org.bukkit.inventory.meta.ItemMeta; 22 import org.bukkit.persistence.PersistentDataContainer; 23 import org.bukkit.persistence.PersistentDataType; 24 25 import java.util.*; 26 27 public class CustomItemEvents implements Listener { 28 @EventHandler 29 public void onEntityDamageByEntity(EntityDamageByEntityEvent e) { 30 if (e.getDamager().getType().equals(EntityType.SNOWBALL)) { 31 Snowball sb = (Snowball) e.getDamager(); 32 Player pl = (Player) sb.getShooter(); 33 if (pl.getInventory().getItemInMainHand().getItemMeta() != null) { 34 String name = pl.getInventory().getItemInMainHand().getItemMeta().getDisplayName(); 35 if (name.equalsIgnoreCase(Rarity.ULTRA.getColor() + "Snow Gun")) { 36 ((Snowball) e.getDamager()).setShooter(pl.getPlayer()); 37 e.setDamage(2000); 38 } else if (name.equalsIgnoreCase("AA-12")) { 39 e.setDamage(7); 40 } else { 41 e.setDamage(0); 42 } 43 } 44 } 45 } 46 47 @EventHandler 48 public void onPlayerFish(PlayerFishEvent e) { 49 Player player = e.getPlayer(); 50 if (player.getInventory().getItemInMainHand().getItemMeta() != null) { 51 String name = player.getInventory().getItemInMainHand().getItemMeta().getDisplayName(); 52 if (name.equalsIgnoreCase(Rarity.RARE.getColor() + "Grappling Hook")) { 53 if (e.getState().equals(PlayerFishEvent.State.REEL_IN)) { 54 Location change = e.getHook().getLocation().subtract(player.getLocation()); 55 player.setVelocity(change.toVector().multiply(0.4)); 56 } 57 } 58 } 59 } 60 61 @EventHandler 62 public void onPlayerInteract(PlayerInteractEvent e) { 63 Player player = e.getPlayer(); 64 if (player.getInventory().getItemInMainHand().getItemMeta() != null) { 65 String name = player.getInventory().getItemInMainHand().getItemMeta().getDisplayName(); 66 if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { 67 if (name.equalsIgnoreCase(Rarity.GRAND.getColor() + "Teleport Door")) { 68 Block block = player.getTargetBlock(null, 12); 69 Location l = block.getLocation(); 70 l.add(0, 1, 0); 71 float yaw = player.getEyeLocation().getYaw(); 72 float pitch = player.getEyeLocation().getPitch(); 73 l.setYaw(yaw); 74 l.setPitch(pitch); 75 player.teleport(l); 76 player.playSound(player.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 10, 10); 77 } else if (name.equalsIgnoreCase(Rarity.ULTRA.getColor() + "Snow Gun")) { 78 Snowball s = player.launchProjectile(Snowball.class, player.getLocation().getDirection()); 79 s.setVelocity(player.getLocation().getDirection().multiply(10)); 80 81 ItemStack weapon = player.getInventory().getItemInMainHand(); 82 ItemMeta weaponMeta = weapon.getItemMeta(); 83 if (weaponMeta != null) { 84 PersistentDataContainer container = weaponMeta.getPersistentDataContainer(); 85 NamespacedKey ammoKey = CustomItemManager.keys.get(name + ".ammo"); 86 int ammo = container.get(ammoKey, PersistentDataType.INTEGER) != null ? container.get(ammoKey, PersistentDataType.INTEGER) : 0; 87 container.set(ammoKey, PersistentDataType.INTEGER, ammo - 1); 88 int max = container.get(CustomItemManager.keys.get(name + ".max"), PersistentDataType.INTEGER); 89 weapon.setItemMeta(weaponMeta); 90 e.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&6AK-47 ( " + (ammo - 1) + "/ " + max + " )"))); 91 92 } 93 94 } else if (name.equalsIgnoreCase(Rarity.MYTHIC.getColor() + "Terminator")) { 95 Arrow arrow = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 96 arrow.setVelocity(arrow.getVelocity().multiply(5)); 97 arrow.setPickupStatus(Arrow.PickupStatus.DISALLOWED); 98 arrow.setShooter(player); 99 arrow.setDamage(50); 100 Arrow a1 = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 101 a1.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(5)).multiply(5)); 102 a1.setPickupStatus(Arrow.PickupStatus.DISALLOWED); 103 a1.setShooter(player); 104 a1.setDamage(50); 105 Arrow a2 = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 106 a2.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(-5)).multiply(5)); 107 a2.setPickupStatus(Arrow.PickupStatus.DISALLOWED); 108 a2.setShooter(player); 109 a2.setDamage(50); 110 e.setCancelled(true); 111 } else if (name.equalsIgnoreCase(Rarity.LEGENDARY.getColor() + "Explosive Bow")) { 112 Arrow arrow = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 113 arrow.setVelocity(arrow.getVelocity().multiply(5)); 114 arrow.setShooter(player); 115 arrow.setDamage(50); 116 e.setCancelled(true); 117 } 118 } else if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) { 119 if (name.equalsIgnoreCase(Rarity.MYTHIC.getColor() + "Terminator")) { 120 Arrow arrow = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 121 arrow.setVelocity(arrow.getVelocity().multiply(5)); 122 arrow.setPickupStatus(Arrow.PickupStatus.DISALLOWED); 123 arrow.setShooter(player); 124 arrow.setDamage(50); 125 Arrow a1 = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 126 a1.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(5)).multiply(5)); 127 a1.setPickupStatus(Arrow.PickupStatus.DISALLOWED); 128 a1.setShooter(player); 129 a1.setDamage(50); 130 Arrow a2 = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 131 a2.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(-5)).multiply(5)); 132 a2.setPickupStatus(Arrow.PickupStatus.DISALLOWED); 133 a2.setShooter(player); 134 a2.setDamage(50); 135 e.setCancelled(true); 136 } else if (name.equalsIgnoreCase(Rarity.LEGENDARY.getColor() + "Explosive Bow")) { 137 Arrow arrow = player.launchProjectile(Arrow.class, player.getEyeLocation().getDirection()); 138 arrow.setVelocity(arrow.getVelocity().multiply(5)); 139 arrow.setShooter(player); 140 arrow.setDamage(50); 141 e.setCancelled(true); 142 } 143 } 144 } 145 } 146 147 @EventHandler 148 public void onEntityShoot(EntityShootBowEvent e) { 149 if (e.getProjectile() instanceof Arrow) { 150 if (e.getEntity() instanceof Player player) { 151 if (player.getInventory().getItemInMainHand().getItemMeta() != null) { 152 String name = player.getInventory().getItemInMainHand().getItemMeta().getDisplayName(); 153 if (name.equalsIgnoreCase(Rarity.MYTHIC.getColor() + "Terminator")) { 154 e.setCancelled(true); 155 } else if (name.equalsIgnoreCase(Rarity.LEGENDARY.getColor() + "Explosive Bow")) { 156 e.setCancelled(true); 157 } 158 } 159 } 160 } 161 } 162 163 @EventHandler 164 public void onProjectileHit(ProjectileHitEvent e) { 165 if (e.getEntity().getShooter() instanceof Player shooter) { 166 if (shooter.getInventory().getItemInMainHand().getItemMeta() != null) { 167 String name = shooter.getInventory().getItemInMainHand().getItemMeta().getDisplayName(); 168 if (name.equalsIgnoreCase(Rarity.LEGENDARY.getColor() + "Frag Grenade")) { 169 if (e.getHitBlock() == null) { 170 Location l = e.getHitEntity().getLocation(); 171 e.getEntity().setShooter(shooter); 172 e.getHitEntity().getWorld().createExplosion(l.getX(), l.getY(), l.getZ(), 100, false, false); 173 } else if (e.getHitEntity() == null) { 174 Location l = e.getHitBlock().getLocation(); 175 e.getHitBlock().getWorld().createExplosion(l.getX(), l.getY(), l.getZ(), 100, false, false); 176 } 177 } else if (name.equalsIgnoreCase(Rarity.LEGENDARY.getColor() + "Explosive Bow")) { 178 Arrow arrow = (Arrow) e.getEntity(); 179 Location al = arrow.getLocation(); 180 arrow.setShooter(shooter); 181 shooter.getWorld().createExplosion(al, 100, false, false); 182 } 183 } 184 185 } 186 } 187 188 @EventHandler 189 public void onCreatureSpawn(CreatureSpawnEvent event) { 190 if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.EGG) { 191 event.setCancelled(true); 192 } 193 } 194 195 @EventHandler 196 public void Projectile(ProjectileLaunchEvent e) { 197 if (e.getEntity().getShooter() instanceof Player player) { 198 if (player.getInventory().getItemInMainHand().getItemMeta() != null) { 199 String name = player.getInventory().getItemInMainHand().getItemMeta().getDisplayName(); 200 if (name.equalsIgnoreCase(Rarity.LEGENDARY.getColor() + "Frag Grenade")) { 201 Egg s = (Egg) e.getEntity(); 202 s.setVelocity(player.getLocation().getDirection().multiply(10)); 203 } 204 } 205 } 206 } 207 208 @EventHandler 209 public void onPlayerBucketEmpty(PlayerBucketEmptyEvent e) { 210 int x = e.getBlockClicked().getX() + e.getBlockFace().getModX(); 211 int y = e.getBlockClicked().getY() + e.getBlockFace().getModY(); 212 int z = e.getBlockClicked().getZ() + e.getBlockFace().getModZ(); 213 Player player = e.getPlayer(); 214 if (player.getInventory().getItemInMainHand().getItemMeta() != null) { 215 String name = player.getInventory().getItemInMainHand().getItemMeta().getDisplayName(); 216 if (name.equalsIgnoreCase(Rarity.EPIC.getColor() + "Infinite Water Bucket")) { 217 e.getPlayer().getWorld().getBlockAt(x, y, z).setType(Material.WATER); 218 e.setCancelled(true); 219 } else if (name.equalsIgnoreCase(Rarity.EPIC.getColor() + "Infinite Lava Bucket")) { 220 e.getPlayer().getWorld().getBlockAt(x, y, z).setType(Material.LAVA); 221 e.setCancelled(true); 222 } 223 } 224 } 225 226 @EventHandler 227 public void onDamage(EntityDamageByEntityEvent e) { 228 if (e.getEntity() instanceof Player player) { 229 // if ((player.getHealth() - e.getDamage()) <= 0) { 230 // e.setCancelled(true); 231 // Location loc = player.getWorld().getBlockAt(-3, 23, -3).getLocation(); 232 // player.teleport(loc); 233 // for (Player p : Bukkit.getOnlinePlayers()) { 234 // p.sendMessage(e.getDamager() instanceof Player 235 // ? ChatColor.RED + player.getName() + " has been killed by " + e.getDamager().getName() 236 // : ChatColor.RED + player.getName() + " died"); 237 // p.hidePlayer(player); 238 // } 239 // new BukkitRunnable() { 240 // @Override 241 // public void run() { 242 // for (Player p : Bukkit.getOnlinePlayers()) { 243 // p.showPlayer(player); 244 // } 245 // player.setHealth(20); 246 // player.teleport(generateRandomCoord(9, Bukkit.getWorld("world"))); 247 // } 248 // }.runTaskLater(NullValkyrie.getPlugin(NullValkyrie.class), 100L); 249 // countDown(player, new int[]{5}); 250 // } 251 } 252 253 } 254 255 private int taskID; 256 257 public void countDown(Player player, int[] a) { 258 taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(NullValkyrie.getPlugin(NullValkyrie.class), () -> { 259 player.sendTitle(ChatColor.RED + "YOU DIED!", ChatColor.GREEN + "You will revive in " + a[0] + " seconds", 0, 20, 0); 260 a[0]--; 261 if (a[0] == 0) { 262 Bukkit.getScheduler().cancelTask(taskID); 263 } 264 }, 0L, 20L); 265 } 266 267 private final Map<UUID, Merchant> villagerlist = new HashMap<>(); 268 269 @EventHandler 270 public void onClick(PlayerInteractEntityEvent e) { 271 Player p = e.getPlayer(); 272 Entity clickedEntity = e.getRightClicked(); 273 if (clickedEntity instanceof Creeper) { 274 if (p.getInventory().getItemInMainHand().getType() != Material.STICK) return; 275 clickedEntity.remove(); 276 Location loc = clickedEntity.getLocation(); 277 Villager villager = (Villager) p.getWorld().spawnEntity(loc, EntityType.VILLAGER); 278 villager.setProfession(Villager.Profession.TOOLSMITH); 279 List<MerchantRecipe> recipes = new ArrayList<>(); 280 MerchantRecipe bread = new MerchantRecipe(new ItemStack(Material.BREAD, 3), 10); 281 bread.addIngredient(new ItemStack(Material.EMERALD, 10)); 282 recipes.add(bread); 283 284 MerchantRecipe tntStick = new MerchantRecipe(CustomItemManager.produceItem("Terminator"), 10); 285 tntStick.addIngredient(CustomItemManager.produceItem("Widow Sword")); 286 recipes.add(tntStick); 287 Merchant merchant = Bukkit.createMerchant("Exchange here"); 288 merchant.setRecipes(recipes); 289 villagerlist.put(villager.getUniqueId(), merchant); 290 p.spawnParticle(Particle.END_ROD, loc, 30, 0, 1, 0, 0.2); 291 p.openMerchant(merchant, true); 292 } 293 if (e.getRightClicked() instanceof Villager) { 294 Merchant merchant = villagerlist.get(clickedEntity.getUniqueId()); 295 if (merchant == null) return; 296 e.setCancelled(true); 297 p.openMerchant(merchant, true); 298 } 299 } 300 301 private final HashMap<Location, Integer> blockStages = new HashMap<>(); 302 private final HashMap<UUID, Long> miningCooldown = new HashMap<>(); 303 304 @EventHandler 305 public void onAnimationEvent(PlayerAnimationEvent e) { //Material blockType, int mineInterval, Pickaxe x 306 Player player = e.getPlayer(); 307 UUID uuid = player.getUniqueId(); 308 if (!player.getGameMode().equals(GameMode.SURVIVAL)) return; 309 if (miningCooldown.containsKey(uuid) && (miningCooldown.get(uuid) > System.currentTimeMillis())) return; 310 else miningCooldown.remove(uuid); 311 312 if (!e.getAnimationType().equals(PlayerAnimationType.ARM_SWING)) return; 313 Block block = player.getTargetBlockExact(4); 314 if (block == null) return; 315 316 Pickaxe pickaxe = new Pickaxe(player.getInventory().getItemInMainHand()); 317 List<Material> materialsThatCanBeMinedFast = pickaxe.multimap.get(pickaxe.getMaterial()); // to get all materials that the pickaxe can mine 318 if (!materialsThatCanBeMinedFast.contains(block.getType())) return; 319 320 long miningPerPhase = pickaxe.getMiningPerPhase(block.getType()); 321 miningCooldown.put(uuid, System.currentTimeMillis() + miningPerPhase); 322 int blockStage = blockStages.getOrDefault(block.getLocation(), 0); 323 blockStage = blockStage == 10 ? 0 : blockStage + 1; 324 blockStages.put(block.getLocation(), blockStage); 325 new PacketPlayOutBlockBreakAnimation(player, block.getLocation(), blockStages.get(block.getLocation())); 326 if (blockStage == 0) { 327 blockStages.remove(block.getLocation()); 328 block.breakNaturally(); 329 } 330 } 331 }