use of org.bukkit.entity.Pig in project Denizen-For-Bukkit by DenizenScript.
the class EquipCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Map<String, dItem> equipment = (Map<String, dItem>) scriptEntry.getObject("equipment");
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
// Report to dB
dB.report(scriptEntry, getName(), aH.debugObj("entities", entities.toString()) + aH.debugObj("equipment", equipment.toString()));
for (dEntity entity : entities) {
if (entity.isGeneric()) {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot equip generic entity " + entity.identify() + "!");
} else if (entity.isCitizensNPC()) {
dNPC npc = entity.getDenizenNPC();
if (npc != null) {
Equipment trait = npc.getEquipmentTrait();
if (equipment.get("hand") != null) {
trait.set(0, equipment.get("hand").getItemStack());
}
if (equipment.get("head") != null) {
trait.set(1, equipment.get("head").getItemStack());
}
if (equipment.get("chest") != null) {
trait.set(2, equipment.get("chest").getItemStack());
}
if (equipment.get("legs") != null) {
trait.set(3, equipment.get("legs").getItemStack());
}
if (equipment.get("boots") != null) {
trait.set(4, equipment.get("boots").getItemStack());
}
if (equipment.get("offhand") != null) {
trait.set(5, equipment.get("offhand").getItemStack());
}
if (npc.isSpawned()) {
LivingEntity livingEntity = npc.getLivingEntity();
if (livingEntity.getType() == EntityType.HORSE) {
if (equipment.get("saddle") != null) {
((Horse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
}
if (equipment.get("horse_armor") != null) {
((Horse) livingEntity).getInventory().setArmor(equipment.get("horse_armor").getItemStack());
}
} else if (livingEntity.getType() == EntityType.PIG) {
if (equipment.get("saddle") != null) {
dItem saddle = equipment.get("saddle");
if (saddle.getItemStack().getType() == Material.SADDLE) {
((Pig) livingEntity).setSaddle(true);
} else {
((Pig) livingEntity).setSaddle(false);
}
}
}
}
}
} else {
LivingEntity livingEntity = entity.getLivingEntity();
if (livingEntity != null) {
if (livingEntity.getType() == EntityType.HORSE) {
if (equipment.get("saddle") != null) {
((Horse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
}
if (equipment.get("horse_armor") != null) {
((Horse) livingEntity).getInventory().setArmor(equipment.get("horse_armor").getItemStack());
}
} else if (livingEntity.getType() == EntityType.PIG) {
if (equipment.get("saddle") != null) {
dItem saddle = equipment.get("saddle");
if (saddle.getItemStack().getType() == Material.SADDLE) {
((Pig) livingEntity).setSaddle(true);
} else {
((Pig) livingEntity).setSaddle(false);
}
}
} else {
if (equipment.get("hand") != null) {
NMSHandler.getInstance().getEntityHelper().setItemInHand(livingEntity, equipment.get("hand").getItemStack());
}
if (equipment.get("head") != null) {
livingEntity.getEquipment().setHelmet(equipment.get("head").getItemStack());
}
if (equipment.get("chest") != null) {
livingEntity.getEquipment().setChestplate(equipment.get("chest").getItemStack());
}
if (equipment.get("legs") != null) {
livingEntity.getEquipment().setLeggings(equipment.get("legs").getItemStack());
}
if (equipment.get("boots") != null) {
livingEntity.getEquipment().setBoots(equipment.get("boots").getItemStack());
}
if (equipment.get("offhand") != null) {
NMSHandler.getInstance().getEntityHelper().setItemInOffHand(livingEntity, equipment.get("offhand").getItemStack());
}
}
}
}
}
}
use of org.bukkit.entity.Pig in project EliteMobs by MagmaGuy.
the class PigHandler method superDrops.
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void superDrops(SuperMobDamageEvent event) {
if (event.getEntityDamageEvent().getFinalDamage() < 1)
return;
if (!event.getLivingEntity().getType().equals(EntityType.PIG))
return;
Random random = new Random();
Pig pig = (Pig) event.getLivingEntity();
double damage = event.getEntityDamageEvent().getFinalDamage();
// health is hardcoded here, maybe change it at some point?
double dropChance = damage / 10;
double dropRandomizer = random.nextDouble();
// this rounds down
int dropMinAmount = (int) dropChance;
ItemStack porkchopStack = new ItemStack(PORKCHOP, random.nextInt(3) + 1);
for (int i = 0; i < dropMinAmount; i++) {
pig.getWorld().dropItem(pig.getLocation(), porkchopStack).setVelocity(ItemDropVelocity.ItemDropVelocity());
ExperienceOrb xpDrop = pig.getWorld().spawn(pig.getLocation(), ExperienceOrb.class);
xpDrop.setExperience(random.nextInt(3) + 1);
}
if (dropChance > dropRandomizer) {
pig.getWorld().dropItem(pig.getLocation(), porkchopStack).setVelocity(ItemDropVelocity.ItemDropVelocity());
ExperienceOrb xpDrop = pig.getWorld().spawn(pig.getLocation(), ExperienceOrb.class);
xpDrop.setExperience(random.nextInt(3) + 1);
}
}
use of org.bukkit.entity.Pig in project MagicPlugin by elBukkit.
the class LevitateSpell method onActivate.
@Override
public void onActivate() {
final Player player = mage.getPlayer();
if (player == null)
return;
// Prevent the player from death by fall
direction = null;
mountBoostTicksRemaining = 0;
boostTicksRemaining = 0;
controller.addFlightExemption(player);
if (stashItem) {
final PlayerInventory inventory = player.getInventory();
heldItem = inventory.getItem(heldItemSlot);
Plugin plugin = controller.getPlugin();
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
com.elmakers.mine.bukkit.api.wand.Wand wand = mage.getActiveWand();
if (wand != null) {
wand.deactivate();
}
heldItem = inventory.getItem(heldItemSlot);
inventory.setItem(heldItemSlot, null);
}
}, 1);
} else {
heldItem = null;
}
if (flySpeed > 0) {
player.setFlySpeed(flySpeed * defaultFlySpeed);
}
if (thrustSpeed > 0) {
if (thrust != null) {
thrust.stop();
}
thrust = new ThrustAction(this, thrustFrequency + flyDelay + startDelay, thrustFrequency);
} else if (deactivateFrequency > 0) {
thrust = new ThrustAction(this, deactivateFrequency + flyDelay + startDelay, deactivateFrequency);
}
if (mountType != null) {
Location location = mage.getLocation();
World world = location.getWorld();
Entity entity = null;
try {
String mountName = DeprecatedUtils.getName(mountType);
if (mountName.indexOf("Entity") != 0) {
// TODO: Look into spawning the entity via API calls.
// I think the only reason we jump through all of these hoops is to be able to modify the entity before
// spawning, such as making it invisible. There must be a better way.
mountName = mountName.substring(0, 1).toUpperCase() + mountName.substring(1);
mountName = "Entity" + mountName;
}
Class<?> mountClass = NMSUtils.getBukkitClass("net.minecraft.server." + mountName);
if (mountClass != null) {
if (worldClass == null) {
worldClass = NMSUtils.getBukkitClass("net.minecraft.server.World");
}
Constructor<? extends Object> constructor = mountClass.getConstructor(worldClass);
Object nmsWorld = NMSUtils.getHandle(world);
Object nmsEntity = constructor.newInstance(nmsWorld);
entity = NMSUtils.getBukkitEntity(nmsEntity);
if (entity != null) {
if (entity instanceof LivingEntity && mountInvisible) {
((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 2 << 24, 0));
}
if (entity instanceof Tameable) {
((Tameable) entity).setTamed(true);
}
if (mountSilent) {
CompatibilityUtils.setSilent(entity, true);
}
Method setLocationMethod = mountClass.getMethod("setLocation", Double.TYPE, Double.TYPE, Double.TYPE, Float.TYPE, Float.TYPE);
setLocationMethod.invoke(nmsEntity, location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
controller.setForceSpawn(true);
try {
CompatibilityUtils.addToWorld(world, entity, mountSpawnReason);
} catch (Exception ex) {
ex.printStackTrace();
;
}
controller.setForceSpawn(false);
} else {
mage.sendMessage("Failed to spawn entity of type: " + mountType + " (" + DeprecatedUtils.getName(mountType) + ")");
return;
}
} else {
mage.sendMessage("Invalid entity type: " + mountType + " (" + DeprecatedUtils.getName(mountType) + ")");
return;
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (entity != null) {
mountEntity = entity;
CompatibilityUtils.setInvulnerable(mountEntity);
if (entity instanceof Horse) {
Horse horse = (Horse) mountEntity;
horse.setTamed(true);
horse.setOwner(player);
horse.setAdult();
horse.setStyle(mountHorseStyle);
// horse.setVariant(mountHorseVariant);
horse.setColor(mountHorseColor);
horse.getInventory().setSaddle(new ItemStack(Material.SADDLE, 1));
if (mountItem != null) {
horse.getInventory().setArmor(new ItemStack(mountItem, 1));
}
if (mountBaby) {
horse.setBaby();
}
}
if (entity instanceof Pig) {
Pig pig = (Pig) entity;
pig.setSaddle(true);
if (mountBaby) {
pig.setBaby();
}
}
if (entity instanceof LivingEntity) {
LivingEntity living = (LivingEntity) mountEntity;
living.setHealth(0.5);
living.setMaxHealth(mountHealth);
}
if (entity instanceof ArmorStand) {
ArmorStand armorStand = (ArmorStand) entity;
configureArmorStand(armorStand);
} else if (useArmorStand) {
armorStand = CompatibilityUtils.createArmorStand(mage.getLocation());
configureArmorStand(armorStand);
armorStand.setPassenger(mountEntity);
armorStand.setMetadata("notarget", new FixedMetadataValue(controller.getPlugin(), true));
armorStand.setMetadata("broom", new FixedMetadataValue(controller.getPlugin(), true));
controller.setForceSpawn(true);
try {
CompatibilityUtils.addToWorld(mage.getLocation().getWorld(), armorStand, mountSpawnReason);
} catch (Exception ex) {
ex.printStackTrace();
}
controller.setForceSpawn(false);
} else {
armorStand = null;
}
mountEntity.setPassenger(mage.getEntity());
mountEntity.setMetadata("notarget", new FixedMetadataValue(controller.getPlugin(), true));
mountEntity.setMetadata("broom", new FixedMetadataValue(controller.getPlugin(), true));
if (listener == null) {
listener = new LevitateListener(controller);
Plugin plugin = controller.getPlugin();
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
}
}
}
grounded = false;
if (maxHeight > 0 && player.getLocation().getY() >= maxHeight) {
grounded = true;
} else if (maxHeightAboveGround > 0) {
Block block = player.getLocation().getBlock();
int height = 0;
while (height < maxHeightAboveGround && block.getType() == Material.AIR) {
block = block.getRelative(BlockFace.DOWN);
height++;
}
if (block.getType() == Material.AIR) {
grounded = true;
}
}
if (!grounded) {
Vector velocity = player.getVelocity();
velocity.setY(velocity.getY() + yBoost);
if (mountEntity != null) {
if (armorStand != null) {
SafetyUtils.setVelocity(armorStand, velocity);
} else {
SafetyUtils.setVelocity(mountEntity, velocity);
}
} else {
SafetyUtils.setVelocity(player, velocity);
}
}
if (flight) {
Bukkit.getScheduler().scheduleSyncDelayedTask(controller.getPlugin(), new Runnable() {
@Override
public void run() {
player.setAllowFlight(true);
player.setFlying(true);
}
}, flyDelay);
}
}
use of org.bukkit.entity.Pig in project MagicPlugin by elBukkit.
the class LevitateSpell method onDeactivate.
@Override
public void onDeactivate() {
if (safetyLength > 0) {
mage.enableFallProtection(safetyLength, this);
}
if (thrust != null) {
thrust.stop();
thrust = null;
}
Entity mageEntity = mage.getEntity();
if (mountEntity != null) {
Plugin plugin = controller.getPlugin();
if (mageEntity != null) {
mageEntity.eject();
}
if (armorStand != null) {
armorStand.removeMetadata("notarget", plugin);
armorStand.removeMetadata("broom", plugin);
armorStand.remove();
}
if (mountEntity instanceof Horse) {
Horse horse = (Horse) mountEntity;
horse.getInventory().clear();
}
if (mountEntity instanceof Pig) {
Pig pig = (Pig) mountEntity;
pig.setSaddle(false);
}
mountEntity.eject();
mountEntity.setPassenger(null);
mountEntity.removeMetadata("notarget", plugin);
mountEntity.removeMetadata("broom", plugin);
CompatibilityUtils.setInvulnerable(mountEntity, false);
if (mountEntity instanceof LivingEntity) {
((LivingEntity) mountEntity).setHealth(0);
}
mountEntity.remove();
mountEntity = null;
}
final Player player = mage.getPlayer();
if (player == null)
return;
controller.removeFlightExemption(player);
if (flySpeed > 0 && flight) {
player.setFlySpeed(defaultFlySpeed);
}
if (heldItem != null) {
Inventory inventory = mage.getInventory();
ItemStack current = inventory.getItem(heldItemSlot);
inventory.setItem(heldItemSlot, heldItem);
if (player.getInventory().getHeldItemSlot() == heldItemSlot && Wand.isWand(heldItem)) {
mage.checkWand();
}
if (current != null && current.getType() != Material.AIR) {
controller.giveItemToPlayer(player, current);
}
heldItem = null;
}
if (flight) {
player.setFlying(false);
player.setAllowFlight(false);
}
}
use of org.bukkit.entity.Pig in project Citizens2 by CitizensDev.
the class PigEquipper method equip.
@Override
public void equip(Player equipper, NPC toEquip) {
ItemStack hand = equipper.getInventory().getItemInHand();
Pig pig = (Pig) toEquip.getEntity();
if (hand.getType() == Material.SADDLE) {
if (!pig.hasSaddle()) {
toEquip.getTrait(Saddle.class).toggle();
hand.setAmount(0);
Messaging.sendTr(equipper, Messages.SADDLED_SET, toEquip.getName());
}
} else if (pig.hasSaddle()) {
equipper.getWorld().dropItemNaturally(pig.getLocation(), new ItemStack(Material.SADDLE, 1));
toEquip.getTrait(Saddle.class).toggle();
Messaging.sendTr(equipper, Messages.SADDLED_STOPPED, toEquip.getName());
}
equipper.getInventory().setItemInHand(hand);
}
Aggregations