package ovh.sad.animalrp.animals; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.UUID; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Sound; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.components.FoodComponent; import org.bukkit.persistence.PersistentDataType; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitRunnable; import ovh.sad.animalrp.AnimalRP; import ovh.sad.animalrp.util.Mood; import ovh.sad.animalrp.util.TextDestroyer; public class Bee extends Animal { class Row { Material mat; Integer times; } private static Material[] _allFlowers = { Material.ALLIUM, Material.AZURE_BLUET, Material.BLUE_ORCHID, Material.CORNFLOWER, Material.DANDELION, Material.LILY_OF_THE_VALLEY, Material.OXEYE_DAISY, Material.POPPY, Material.TORCHFLOWER, Material.ORANGE_TULIP, Material.PINK_TULIP, Material.RED_TULIP, Material.WHITE_TULIP }; public static List allFlowers = Arrays.asList(_allFlowers); public static NamespacedKey beeFoodKey = NamespacedKey.fromString("animalrp:bee_food"); public static HashMap inARow = new HashMap(); TextDestroyer destroyer = new TextDestroyer(new String[] { ">_<", "*buzz*", ";3", ":3", "εწз", " ≧◠◡◠≦ ", "*stings you*", "*humms*", "*i'm a bee*" }, new String[][] { { "e", "ee" }, { "b", "bzz" }, { "h", "hh" }, { "ie", "ee" }, { "be", "bee" }, { "E", "EE" }, { "B", "BZZ" }, { "H", "HH" }, { "IE", "EE" }, { "BE", "BEE" } }); ArrayList sneakers = new ArrayList(); public Bee() { super("bee", "Buzz...", "#FFFF00"); this.moodSounds.put(Mood.HAPPY, Sound.ENTITY_BEE_LOOP); this.moodSounds.put(Mood.CUTE, Sound.ENTITY_BEE_LOOP); this.moodSounds.put(Mood.SAD, Sound.ENTITY_BEE_HURT); this.moodSounds.put(Mood.STRESSED, Sound.ENTITY_BEE_STING); this.moodSounds.put(Mood.ANGRY, Sound.ENTITY_BEE_LOOP_AGGRESSIVE); } @EventHandler public void onConsume(PlayerItemConsumeEvent event) { Player player = event.getPlayer(); Animal animal = AnimalRP.users.get(player.getUniqueId()); ItemStack item = event.getItem(); if (!allFlowers.contains(item.getType())) { // not a flower return; } if (animal == null) return; if (animal.name != this.name) return; Row row = inARow.get(player.getUniqueId()); // make a new row if (row == null) { // none yet row = new Row(); row.mat = item.getType(); row.times = 1; } else { if (row.mat.equals(item.getType())) { // mat is same as in row, increase time row.times += 1; } else { row.mat = item.getType(); // mat not same, change mat, reset time row.times = 1; } } if (row.times > 20) { player.addPotionEffect(new PotionEffect(PotionEffectType.NAUSEA, 20 * 10, 1, true)); } if (row.times > 30) { if (row.times > 40) { player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 20 * 10, 3, true)); } else { player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 20 * 5, 2, true)); } } inARow.put(player.getUniqueId(), row); } @EventHandler public void onInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); Animal animal = AnimalRP.users.get(player.getUniqueId()); ItemStack item = event.getItem(); if (item == null) // air interact return; if (!allFlowers.contains(item.getType())) { // not a flower return; } ItemMeta meta = item.getItemMeta(); Boolean incorrect = false; if (animal == null) { incorrect = true; } else { if (animal.name != this.name) { incorrect = true; } } if (incorrect) { if (meta.getPersistentDataContainer().has(beeFoodKey)) { meta.getPersistentDataContainer().remove(beeFoodKey); meta.setFood(null); item.setItemMeta(meta); event.setCancelled(true); } return; } if (meta.getPersistentDataContainer().has(beeFoodKey)) { // correct animal, but foodkey already set return; } FoodComponent food = meta.getFood(); food.addEffect(new PotionEffect(PotionEffectType.SPEED, 20 * 4, 1, true), 1); food.setCanAlwaysEat(true); food.setNutrition(4); // these values match the 'superfood' of animalrps food.setSaturation(9.4f); meta.getPersistentDataContainer().set(beeFoodKey, PersistentDataType.BOOLEAN, true); meta.setFood(food); item.setItemMeta(meta); } @EventHandler public void onSneak(PlayerToggleSneakEvent event) { Animal animal = AnimalRP.users.get(event.getPlayer().getUniqueId()); if (animal == null) return; if (animal.name != this.name) return; Material type = event.getPlayer().getLocation().getBlock().getRelative(BlockFace.DOWN).getType(); if (event.isSneaking() && type != Material.AIR && type != Material.WATER) { Player player = event.getPlayer(); if (!sneakers.contains(player.getUniqueId())) { sneakers.add(player.getUniqueId()); new BukkitRunnable() { @Override public void run() { if (sneakers.contains(player.getUniqueId())) sneakers.remove(player.getUniqueId()); } }.runTaskLater(AnimalRP.getProvidingPlugin(Animal.class), 20); } else { sneakers.remove(player.getUniqueId()); player.addPotionEffect(new PotionEffect(PotionEffectType.LEVITATION, 20 * 1, 5, true)); player.getWorld().playSound(player.getLocation(), this.moodSounds.get(Mood.HAPPY), 1F, 1); } } } @Override public String chatTransformations(String message) { return destroyer.destroy(message); } }