use of org.bukkit.inventory.EntityEquipment in project Glowstone by GlowstoneMC.
the class GlowHumanEntity method createSpawnMessage.
// //////////////////////////////////////////////////////////////////////////
// Internals
@Override
public List<Message> createSpawnMessage() {
List<Message> result = new LinkedList<>();
// spawn player
double x = location.getX();
double y = location.getY();
double z = location.getZ();
int yaw = Position.getIntYaw(location);
int pitch = Position.getIntPitch(location);
result.add(new SpawnPlayerMessage(entityId, profile.getId(), x, y, z, yaw, pitch, metadata.getEntryList()));
// head facing
result.add(new EntityHeadRotationMessage(entityId, yaw));
// equipment
EntityEquipment equipment = getEquipment();
result.add(new EntityEquipmentMessage(entityId, EntityEquipmentMessage.HELD_ITEM, equipment.getItemInMainHand()));
result.add(new EntityEquipmentMessage(entityId, EntityEquipmentMessage.OFF_HAND, equipment.getItemInOffHand()));
for (int i = 0; i < 4; i++) {
result.add(new EntityEquipmentMessage(entityId, EntityEquipmentMessage.BOOTS_SLOT + i, equipment.getArmorContents()[i]));
}
return result;
}
use of org.bukkit.inventory.EntityEquipment in project Glowstone by GlowstoneMC.
the class LivingEntityStore method load.
// these tags that apply to living entities only are documented as global:
// - short "Air"
// - string "CustomName"
// - bool "CustomNameVisible"
// todo: the following tags
// - float "AbsorptionAmount"
// - short "HurtTime"
// - int "HurtByTimestamp"
// - short "DeathTime"
// - bool "PersistenceRequired"
// on ActiveEffects, bool "ShowParticles"
@Override
public void load(T entity, CompoundTag compound) {
super.load(entity, compound);
compound.readShort("Air", entity::setRemainingAir);
compound.readString("CustomName", entity::setCustomName);
compound.readBoolean("CustomNameVisible", entity::setCustomNameVisible);
if (!compound.readFloat("HealF", entity::setHealth)) {
compound.readShort("Health", entity::setHealth);
}
compound.readShort("AttackTime", entity::setNoDamageTicks);
compound.readBoolean("FallFlying", entity::setFallFlying);
compound.iterateCompoundList("ActiveEffects", effect -> {
// should really always have every field, but be forgiving if possible
if (!effect.isByte("Id") || !effect.isInt("Duration")) {
return;
}
PotionEffectType type = PotionEffectType.getById(effect.getByte("Id"));
int duration = effect.getInt("Duration");
if (type == null || duration < 0) {
return;
}
final int amplifier = compound.tryGetInt("Amplifier").orElse(0);
boolean ambient = compound.getBoolean("Ambient", false);
// bool "ShowParticles"
entity.addPotionEffect(new PotionEffect(type, duration, amplifier, ambient), true);
});
EntityEquipment equip = entity.getEquipment();
if (equip != null) {
loadEquipment(entity, equip, compound);
}
compound.readBoolean("CanPickUpLoot", entity::setCanPickupItems);
AttributeManager am = entity.getAttributeManager();
compound.iterateCompoundList("Attributes", tag -> {
if (!tag.isString("Name") || !tag.isDouble("Base")) {
return;
}
List<AttributeModifier> modifiers = new ArrayList<>();
tag.iterateCompoundList("Modifiers", modifierTag -> {
if (modifierTag.isDouble("Amount") && modifierTag.isString("Name") && modifierTag.isInt("Operation") && modifierTag.isLong("UUIDLeast") && modifierTag.isLong("UUIDMost")) {
modifiers.add(new AttributeModifier(new UUID(modifierTag.getLong("UUIDLeast"), modifierTag.getLong("UUIDMost")), modifierTag.getString("Name"), modifierTag.getDouble("Amount"), AttributeModifier.Operation.values()[modifierTag.getInt("Operation")]));
}
});
AttributeManager.Key key = AttributeManager.Key.fromName(tag.getString("Name"));
am.setProperty(key, tag.getDouble("Base"), modifiers);
});
Optional<CompoundTag> maybeLeash = compound.tryGetCompound("Leash");
if (maybeLeash.isPresent()) {
CompoundTag leash = maybeLeash.get();
if (!leash.readUniqueId("UUIDMost", "UUIDLeast", entity::setLeashHolderUniqueId) && leash.isInt("X") && leash.isInt("Y") && leash.isInt("Z")) {
int x = leash.getInt("X");
int y = leash.getInt("Y");
int z = leash.getInt("Z");
LeashHitch leashHitch = GlowLeashHitch.getLeashHitchAt(new Location(entity.getWorld(), x, y, z).getBlock());
entity.setLeashHolder(leashHitch);
}
} else {
compound.readBoolean("Leashed", leashSet -> {
if (leashSet) {
// We know that there was something leashed, but not what entity it was
// This can happen, when for example Minecart got leashed
// We still have to make sure that we drop a Leash Item
entity.setLeashHolderUniqueId(UUID.randomUUID());
}
});
}
}
use of org.bukkit.inventory.EntityEquipment in project Glowstone by GlowstoneMC.
the class LivingEntityStore method save.
@Override
public void save(T entity, CompoundTag tag) {
super.save(entity, tag);
tag.putShort("Air", entity.getRemainingAir());
if (entity.getCustomName() != null && !entity.getCustomName().isEmpty()) {
tag.putString("CustomName", entity.getCustomName());
tag.putBool("CustomNameVisible", entity.isCustomNameVisible());
}
tag.putFloat("HealF", entity.getHealth());
tag.putShort("Health", (int) entity.getHealth());
tag.putShort("AttackTime", entity.getNoDamageTicks());
tag.putBool("FallFlying", entity.isFallFlying());
Map<String, Property> properties = entity.getAttributeManager().getAllProperties();
if (!properties.isEmpty()) {
List<CompoundTag> attributes = new ArrayList<>(properties.size());
properties.forEach((key, property) -> {
CompoundTag attribute = new CompoundTag();
attribute.putString("Name", key);
attribute.putDouble("Base", property.getValue());
Collection<AttributeModifier> modifiers = property.getModifiers();
if (modifiers != null && !modifiers.isEmpty()) {
List<CompoundTag> modifierTags = modifiers.stream().map(modifier -> {
CompoundTag modifierTag = new CompoundTag();
modifierTag.putDouble("Amount", modifier.getAmount());
modifierTag.putString("Name", modifier.getName());
modifierTag.putInt("Operation", modifier.getOperation().ordinal());
UUID uuid = modifier.getUniqueId();
modifierTag.putLong("UUIDLeast", uuid.getLeastSignificantBits());
modifierTag.putLong("UUIDMost", uuid.getMostSignificantBits());
return modifierTag;
}).collect(Collectors.toList());
attribute.putCompoundList("Modifiers", modifierTags);
}
attributes.add(attribute);
});
tag.putCompoundList("Attributes", attributes);
}
List<CompoundTag> effects = new LinkedList<>();
for (PotionEffect effect : entity.getActivePotionEffects()) {
CompoundTag effectTag = new CompoundTag();
effectTag.putByte("Id", effect.getType().getId());
effectTag.putByte("Amplifier", effect.getAmplifier());
effectTag.putInt("Duration", effect.getDuration());
effectTag.putBool("Ambient", effect.isAmbient());
effectTag.putBool("ShowParticles", true);
effects.add(effectTag);
}
tag.putCompoundList("ActiveEffects", effects);
EntityEquipment equip = entity.getEquipment();
if (equip != null) {
tag.putCompoundList("HandItems", Arrays.asList(NbtSerialization.writeItem(equip.getItemInMainHand(), -1), NbtSerialization.writeItem(equip.getItemInOffHand(), -1)));
tag.putCompoundList("ArmorItems", Arrays.asList(NbtSerialization.writeItem(equip.getBoots(), -1), NbtSerialization.writeItem(equip.getLeggings(), -1), NbtSerialization.writeItem(equip.getChestplate(), -1), NbtSerialization.writeItem(equip.getHelmet(), -1)));
tag.putFloatList("HandDropChances", Arrays.asList(equip.getItemInMainHandDropChance(), equip.getItemInOffHandDropChance()));
tag.putFloatList("ArmorDropChances", Arrays.asList(equip.getBootsDropChance(), equip.getLeggingsDropChance(), equip.getChestplateDropChance(), equip.getHelmetDropChance()));
}
tag.putBool("CanPickUpLoot", entity.getCanPickupItems());
tag.putBool("Leashed", entity.isLeashed());
if (entity.isLeashed()) {
Entity leashHolder = entity.getLeashHolder();
CompoundTag leash = new CompoundTag();
// The empty Leash tag is still persisted tough
if (leashHolder instanceof LeashHitch) {
Location location = leashHolder.getLocation();
leash.putInt("X", location.getBlockX());
leash.putInt("Y", location.getBlockY());
leash.putInt("Z", location.getBlockZ());
} else if (leashHolder instanceof LivingEntity) {
leash.putLong("UUIDMost", entity.getUniqueId().getMostSignificantBits());
leash.putLong("UUIDLeast", entity.getUniqueId().getLeastSignificantBits());
}
tag.putCompound("Leash", leash);
}
}
use of org.bukkit.inventory.EntityEquipment in project Essentials by EssentialsX.
the class SpawnMob method changeMobData.
private static void changeMobData(final CommandSource sender, final EntityType type, final Entity spawned, final String inputData, final User target) throws Exception {
String data = inputData;
if (data.isEmpty()) {
sender.sendMessage(tl("mobDataList", StringUtil.joinList(MobData.getValidHelp(spawned))));
}
if (spawned instanceof Zombie) {
((Zombie) spawned).setBaby(false);
} else if (spawned instanceof Ageable) {
((Ageable) spawned).setAdult();
}
if (spawned instanceof Zombie || type == EntityType.SKELETON) {
if (inputData.contains("armor") || inputData.contains("armour")) {
final EntityEquipment invent = ((LivingEntity) spawned).getEquipment();
if (inputData.contains("noarmor") || inputData.contains("noarmour")) {
invent.clear();
} else if (inputData.contains("diamond")) {
invent.setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
invent.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS, 1));
invent.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE, 1));
invent.setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
} else if (inputData.contains("gold")) {
invent.setBoots(new ItemStack(Material.GOLD_BOOTS, 1));
invent.setLeggings(new ItemStack(Material.GOLD_LEGGINGS, 1));
invent.setChestplate(new ItemStack(Material.GOLD_CHESTPLATE, 1));
invent.setHelmet(new ItemStack(Material.GOLD_HELMET, 1));
} else if (inputData.contains("leather")) {
invent.setBoots(new ItemStack(Material.LEATHER_BOOTS, 1));
invent.setLeggings(new ItemStack(Material.LEATHER_LEGGINGS, 1));
invent.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE, 1));
invent.setHelmet(new ItemStack(Material.LEATHER_HELMET, 1));
} else {
invent.setBoots(new ItemStack(Material.IRON_BOOTS, 1));
invent.setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
invent.setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
invent.setHelmet(new ItemStack(Material.IRON_HELMET, 1));
}
invent.setBootsDropChance(0f);
invent.setLeggingsDropChance(0f);
invent.setChestplateDropChance(0f);
invent.setHelmetDropChance(0f);
}
}
MobData newData = MobData.fromData(spawned, data);
while (newData != null) {
newData.setData(spawned, target.getBase(), data);
data = data.replace(newData.getMatched(), "");
newData = MobData.fromData(spawned, data);
}
}
use of org.bukkit.inventory.EntityEquipment in project Prism-Bukkit by prism.
the class PrismEntityEvents method onEntityDeath.
/**
* EntityDeathEvent.
* @param event EntityDeathEvent
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(final EntityDeathEvent event) {
final LivingEntity entity = event.getEntity();
// Mob Death
if (!(entity instanceof Player)) {
// Log item drops
if (Prism.getIgnore().event("item-drop", entity.getWorld())) {
String name = entity.getType().name().toLowerCase();
// Inventory
if (entity instanceof InventoryHolder) {
final InventoryHolder holder = (InventoryHolder) entity;
for (final ItemStack i : holder.getInventory().getContents()) {
if (checkNotNullorAir(i)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
}
}
}
// Equipment
EntityEquipment equipment = entity.getEquipment();
if (equipment != null) {
for (final ItemStack i : equipment.getArmorContents()) {
if (checkNotNullorAir(i)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
}
}
}
// Hand items not stored in "getArmorContents"
ItemStack main = entity.getEquipment().getItemInMainHand();
ItemStack off = entity.getEquipment().getItemInOffHand();
if (checkNotNullorAir(main)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", main, main.getAmount(), -1, null, entity.getLocation(), name));
}
if (checkNotNullorAir(off)) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", off, off.getAmount(), -1, null, entity.getLocation(), name));
}
}
EntityDamageEvent damageEvent = entity.getLastDamageCause();
Entity entitySource = null;
Block blockSource = null;
// Resolve source
if (damageEvent != null && !damageEvent.isCancelled()) {
if (damageEvent instanceof EntityDamageByEntityEvent) {
entitySource = ((EntityDamageByEntityEvent) damageEvent).getDamager();
if (entitySource instanceof Projectile) {
ProjectileSource ps = ((Projectile) entitySource).getShooter();
if (ps instanceof BlockProjectileSource) {
entitySource = null;
blockSource = ((BlockProjectileSource) ps).getBlock();
} else {
entitySource = (Entity) ps;
}
}
} else if (damageEvent instanceof EntityDamageByBlockEvent) {
blockSource = ((EntityDamageByBlockEvent) damageEvent).getDamager();
}
}
// Create handlers
if (entitySource instanceof Player) {
Player player = (Player) entitySource;
if (!Prism.getIgnore().event("player-kill", player)) {
return;
}
RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity, player));
} else if (entitySource != null) {
if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
return;
}
String name = entitySource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
} else if (blockSource != null) {
if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
return;
}
String name = "block:" + blockSource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
} else {
if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
return;
}
String name = "unknown";
if (damageEvent != null && !damageEvent.isCancelled()) {
name = damageEvent.getCause().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
}
RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
}
/*
* if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) { final
* EntityDamageByEntityEvent entityDamageByEntityEvent =
* (EntityDamageByEntityEvent) entity .getLastDamageCause();
*
* // Mob killed by player if (entityDamageByEntityEvent.getDamager() instanceof
* Player) { final Player player = (Player)
* entityDamageByEntityEvent.getDamager(); if
* (!Prism.getIgnore().event("player-kill", player)) return;
* RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity,
* player));
*
* } // Mob shot by an arrow from a player else if
* (entityDamageByEntityEvent.getDamager() instanceof Arrow) { final Arrow arrow
* = (Arrow) entityDamageByEntityEvent.getDamager();
*
* if (arrow.getShooter() instanceof Player) { final Player player = (Player)
* arrow.getShooter(); if (!Prism.getIgnore().event("player-kill", player))
* return; RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill",
* entity, player));
*
* } else if (arrow.getShooter() instanceof LivingEntity) { final Entity damager
* = (Entity) arrow.getShooter(); String name = "unknown"; if (damager != null)
* { name = damager.getType().name().toLowerCase(); }
*
* if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
* RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
* name)); } else if (arrow.getShooter() instanceof BlockProjectileSource) {
*
* final Block damager = (Block)
* ((BlockProjectileSource)arrow.getShooter()).getBlock();
*
* if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
*
* String name = "block:" + damager.getType().name().toLowerCase();
*
* RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
* name)); } } else { // Mob died by another mob final Entity damager =
* entityDamageByEntityEvent.getDamager(); String name = "unknown"; if (damager
* != null) { name = damager.getType().name().toLowerCase(); }
*
* if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
* RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
* name)); } } else {
*
* if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
*
* String killer = "unknown"; final EntityDamageEvent damage =
* entity.getLastDamageCause(); if (damage != null) { final DamageCause cause =
* damage.getCause(); if (cause != null) { killer = cause.name().toLowerCase();
* } }
*
* // Record the death as natural
* RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
* killer));
*
* }
*/
} else {
// Determine who died and what the exact cause was
final Player p = (Player) event.getEntity();
if (Prism.getIgnore().event("player-death", p)) {
final String cause = DeathUtils.getCauseNiceName(p);
String attacker = DeathUtils.getAttackerName(p);
if (attacker.equals("pvpwolf")) {
final String owner = DeathUtils.getTameWolfOwner(event);
attacker = owner + "'s wolf";
}
RecordingQueue.addToQueue(ActionFactory.createPlayerDeath("player-death", p, cause, attacker));
}
// Log item drops
if (Prism.getIgnore().event("item-drop", p)) {
if (!event.getDrops().isEmpty()) {
for (final ItemStack i : event.getDrops()) {
RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, p.getLocation(), p));
}
}
}
}
}
Aggregations