Breeding support

This commit is contained in:
Brianna O'Keefe 2018-12-17 13:32:57 -05:00
parent 8efa264ddf
commit 76ec78023e
3 changed files with 82 additions and 8 deletions

View File

@ -11,7 +11,7 @@ public class EntityStack {
public EntityStack(Entity entity, int amount) { public EntityStack(Entity entity, int amount) {
this.entity = entity; this.entity = entity;
this.amount = amount; this.setAmount(amount);
} }
public void updateStack() { public void updateStack() {

View File

@ -7,13 +7,18 @@ import org.bukkit.Material;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityBreedEvent;
import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class InteractListeners implements Listener { public class InteractListeners implements Listener {
@ -24,6 +29,12 @@ public class InteractListeners implements Listener {
this.instance = instance; this.instance = instance;
} }
@EventHandler
public void onBread(EntityBreedEvent event) {
event.getFather().removeMetadata("inLove", instance);
event.getMother().removeMetadata("inLove", instance);
}
@EventHandler @EventHandler
public void onInteract(PlayerInteractAtEntityEvent event) { public void onInteract(PlayerInteractAtEntityEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -31,18 +42,19 @@ public class InteractListeners implements Listener {
ItemStack item = player.getInventory().getItemInHand(); ItemStack item = player.getInventory().getItemInHand();
if (item.getType() != Material.NAME_TAG if (!instance.getEntityStackManager().isStacked(entity)) return;
|| !instance.getEntityStackManager().isStacked(entity)) return;
if (item.getType() != Material.NAME_TAG && !correctFood(item, entity)) return;
EntityStack stack = instance.getEntityStackManager().getStack(entity); EntityStack stack = instance.getEntityStackManager().getStack(entity);
if (stack.getAmount() == 1) return; if (stack.getAmount() == 1) return;
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> stack.setAmount(stack.getAmount() - 1), 1L); if (item.getType() == Material.NAME_TAG)
event.setCancelled(true);
Entity newEntity = entity.getWorld().spawnEntity(entity.getLocation(), entity.getType()); Entity newEntity = entity.getWorld().spawnEntity(entity.getLocation(), entity.getType());
newEntity.setVelocity(getRandomVector()); entity.setVelocity(getRandomVector());
if (entity instanceof Ageable) { if (entity instanceof Ageable) {
if (((Ageable) entity).isAdult()) { if (((Ageable) entity).isAdult()) {
@ -62,9 +74,70 @@ public class InteractListeners implements Listener {
Villager villager = ((Villager) newEntity); Villager villager = ((Villager) newEntity);
villager.setProfession(villager.getProfession()); villager.setProfession(villager.getProfession());
} }
newEntity.setCustomName(item.getItemMeta().getDisplayName());
instance.getEntityStackManager().addStack(new EntityStack(newEntity, stack.getAmount() - 1));
stack.setAmount(1);
instance.getEntityStackManager().removeStack(entity);
if (item.getType() == Material.NAME_TAG) {
entity.setCustomName(item.getItemMeta().getDisplayName());
} else {
entity.setMetadata("inLove", new FixedMetadataValue(instance, true));
Bukkit.getScheduler().runTaskLaterAsynchronously(instance, () -> {
if (entity.isDead()) return;
entity.removeMetadata("inLove", instance);
}, 20 * 20);
}
} }
private Vector getRandomVector() { private Vector getRandomVector() {
return new Vector(ThreadLocalRandom.current().nextDouble(-1, 1.01), 0, ThreadLocalRandom.current().nextDouble(-1, 1.01)).normalize().multiply(0.5); return new Vector(ThreadLocalRandom.current().nextDouble(-1, 1.01), 0, ThreadLocalRandom.current().nextDouble(-1, 1.01)).normalize().multiply(0.5);
} }
private boolean correctFood(ItemStack is, Entity entity) {
Material type = is.getType();
switch (entity.getType()) {
case COW:
case SHEEP:
return type == Material.WHEAT;
case PIG:
return (type == Material.CARROT || type == Material.BEETROOT || type == Material.POTATO);
case CHICKEN:
return type == Material.WHEAT_SEEDS
|| type == Material.MELON_SEEDS
|| type == Material.BEETROOT_SEEDS
|| type == Material.PUMPKIN_SEEDS;
case HORSE:
return type == Material.GOLDEN_APPLE || type == Material.GOLDEN_CARROT;
case WOLF:
return type == Material.BEEF
|| type == Material.CHICKEN
|| type == Material.COD
|| type == Material.MUTTON
|| type == Material.PORKCHOP
|| type == Material.RABBIT
|| type == Material.SALMON
|| type == Material.COOKED_BEEF
|| type == Material.COOKED_CHICKEN
|| type == Material.COOKED_COD
|| type == Material.COOKED_MUTTON
|| type == Material.COOKED_PORKCHOP
|| type == Material.COOKED_RABBIT
|| type == Material.COOKED_SALMON;
case OCELOT:
return type == Material.SALMON
|| type == Material.COD
|| type == Material.PUFFERFISH
|| type == Material.TROPICAL_FISH;
case RABBIT:
return type == Material.CARROT || type == Material.GOLDEN_CARROT || type == Material.DANDELION;
case LLAMA:
return type == Material.HAY_BLOCK;
case TURTLE:
return type == Material.SEAGRASS;
}
return false;
}
} }

View File

@ -93,7 +93,8 @@ public class StackingTask extends BukkitRunnable {
if (initalEntity.isDead() if (initalEntity.isDead()
|| !initalEntity.isValid() || !initalEntity.isValid()
|| initalEntity instanceof ArmorStand) continue; || initalEntity instanceof ArmorStand
|| initalEntity.hasMetadata("inLove")) continue;
EntityStack initialStack = stackManager.getStack(initalEntity); EntityStack initialStack = stackManager.getStack(initalEntity);
if (initialStack == null && initalEntity.getCustomName() != null) continue; if (initialStack == null && initalEntity.getCustomName() != null) continue;