use of org.bukkit.entity.LeashHitch in project Glowstone by GlowstoneMC.
the class BlockFence method blockInteract.
@Override
public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) {
super.blockInteract(player, block, face, clickedLoc);
if (!player.getLeashedEntities().isEmpty()) {
LeashHitch leashHitch = GlowLeashHitch.getLeashHitchAt(block);
ImmutableList.copyOf(player.getLeashedEntities()).stream().filter(e -> !(EventFactory.getInstance().callEvent(new PlayerLeashEntityEvent(e, leashHitch, player)).isCancelled())).forEach(e -> e.setLeashHolder(leashHitch));
return true;
}
return false;
}
use of org.bukkit.entity.LeashHitch in project Glowstone by GlowstoneMC.
the class GlowLeashHitch method getExistingLeashHitches.
/**
* Get all LeashHitch Entities in the specified block.
*
* @param block the Block to search LeashHitch Entities in
* @return a Stream of all found LeashHitch Entities
*/
private static Stream<LeashHitch> getExistingLeashHitches(Block block) {
Location location = block.getLocation().add(0.5, 0.5, 0.5);
Collection<Entity> nearbyEntities = block.getWorld().getNearbyEntities(location, 0.49, 0.49, 0.49);
return nearbyEntities.stream().filter(e -> e instanceof LeashHitch).map(e -> (LeashHitch) e);
}
use of org.bukkit.entity.LeashHitch 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.entity.LeashHitch 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);
}
}
Aggregations