Search in sources :

Example 1 with LeashHitch

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;
}
Also used : Vector(org.bukkit.util.Vector) GlowBlock(net.glowstone.block.GlowBlock) ImmutableList(com.google.common.collect.ImmutableList) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) MaterialMatcher(net.glowstone.inventory.MaterialMatcher) EventFactory(net.glowstone.EventFactory) PlayerLeashEntityEvent(org.bukkit.event.entity.PlayerLeashEntityEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) LeashHitch(org.bukkit.entity.LeashHitch) Material(org.bukkit.Material) PlayerLeashEntityEvent(org.bukkit.event.entity.PlayerLeashEntityEvent) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) LeashHitch(org.bukkit.entity.LeashHitch)

Example 2 with LeashHitch

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);
}
Also used : BlockType(net.glowstone.block.blocktype.BlockType) ItemTable(net.glowstone.block.ItemTable) Message(com.flowpowered.network.Message) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) PlayerUnleashEntityEvent(org.bukkit.event.player.PlayerUnleashEntityEvent) LeashHitch(org.bukkit.entity.LeashHitch) EntityNetworkUtil(net.glowstone.entity.EntityNetworkUtil) GameMode(org.bukkit.GameMode) Lists(com.google.common.collect.Lists) Block(org.bukkit.block.Block) ImmutableList(com.google.common.collect.ImmutableList) Location(org.bukkit.Location) InteractEntityMessage(net.glowstone.net.message.play.player.InteractEntityMessage) Material(org.bukkit.Material) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) BlockFence(net.glowstone.block.blocktype.BlockFence) Comparator.comparingInt(java.util.Comparator.comparingInt) EntityMetadataMessage(net.glowstone.net.message.play.entity.EntityMetadataMessage) Entity(org.bukkit.entity.Entity) EventFactory(net.glowstone.EventFactory) Collection(java.util.Collection) PlayerLeashEntityEvent(org.bukkit.event.entity.PlayerLeashEntityEvent) GlowEntity(net.glowstone.entity.GlowEntity) EntityType(org.bukkit.entity.EntityType) GlowHangingEntity(net.glowstone.entity.GlowHangingEntity) Action(net.glowstone.net.message.play.player.InteractEntityMessage.Action) ItemStack(org.bukkit.inventory.ItemStack) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) SpawnObjectMessage(net.glowstone.net.message.play.entity.SpawnObjectMessage) Entity(org.bukkit.entity.Entity) GlowEntity(net.glowstone.entity.GlowEntity) GlowHangingEntity(net.glowstone.entity.GlowHangingEntity) LeashHitch(org.bukkit.entity.LeashHitch) Location(org.bukkit.Location)

Example 3 with LeashHitch

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());
            }
        });
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) PotionEffectType(org.bukkit.potion.PotionEffectType) ArrayList(java.util.ArrayList) AttributeModifier(org.bukkit.attribute.AttributeModifier) AttributeManager(net.glowstone.entity.AttributeManager) EntityEquipment(org.bukkit.inventory.EntityEquipment) LeashHitch(org.bukkit.entity.LeashHitch) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) UUID(java.util.UUID) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 4 with LeashHitch

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);
    }
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag) Property(net.glowstone.entity.AttributeManager.Property) AttributeModifier(org.bukkit.attribute.AttributeModifier) Arrays(java.util.Arrays) Player(org.bukkit.entity.Player) LeashHitch(org.bukkit.entity.LeashHitch) InventoryUtil(net.glowstone.util.InventoryUtil) NbtSerialization(net.glowstone.io.nbt.NbtSerialization) ArrayList(java.util.ArrayList) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) Location(org.bukkit.Location) Map(java.util.Map) LinkedList(java.util.LinkedList) AttributeManager(net.glowstone.entity.AttributeManager) Entity(org.bukkit.entity.Entity) Collection(java.util.Collection) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) Collectors(java.util.stream.Collectors) ItemStack(org.bukkit.inventory.ItemStack) PotionEffect(org.bukkit.potion.PotionEffect) List(java.util.List) Optional(java.util.Optional) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) EntityEquipment(org.bukkit.inventory.EntityEquipment) PotionEffectType(org.bukkit.potion.PotionEffectType) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) PotionEffect(org.bukkit.potion.PotionEffect) ArrayList(java.util.ArrayList) AttributeModifier(org.bukkit.attribute.AttributeModifier) LinkedList(java.util.LinkedList) LivingEntity(org.bukkit.entity.LivingEntity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) EntityEquipment(org.bukkit.inventory.EntityEquipment) LeashHitch(org.bukkit.entity.LeashHitch) GlowLeashHitch(net.glowstone.entity.objects.GlowLeashHitch) UUID(java.util.UUID) Property(net.glowstone.entity.AttributeManager.Property) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Aggregations

LeashHitch (org.bukkit.entity.LeashHitch)4 GlowLeashHitch (net.glowstone.entity.objects.GlowLeashHitch)3 Location (org.bukkit.Location)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 EventFactory (net.glowstone.EventFactory)2 AttributeManager (net.glowstone.entity.AttributeManager)2 GlowPlayer (net.glowstone.entity.GlowPlayer)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 Material (org.bukkit.Material)2 AttributeModifier (org.bukkit.attribute.AttributeModifier)2 BlockFace (org.bukkit.block.BlockFace)2 Entity (org.bukkit.entity.Entity)2 EntityType (org.bukkit.entity.EntityType)2 PlayerLeashEntityEvent (org.bukkit.event.entity.PlayerLeashEntityEvent)2 EntityEquipment (org.bukkit.inventory.EntityEquipment)2