Search in sources :

Example 1 with IEntityOwnable

use of net.minecraft.entity.IEntityOwnable in project SpongeCommon by SpongePowered.

the class MixinEntity_Tracker method getTrackedUniqueId.

@Nullable
private UUID getTrackedUniqueId(String nbtKey) {
    if (this.creator != null && NbtDataUtil.SPONGE_ENTITY_CREATOR.equals(nbtKey)) {
        return this.creator;
    } else if (this instanceof IEntityOwnable) {
        IEntityOwnable ownable = (IEntityOwnable) this;
        final net.minecraft.entity.Entity owner = ownable.getOwner();
        if (owner != null && owner instanceof EntityPlayer) {
            this.setCreator(owner.getUniqueID());
            return owner.getUniqueID();
        }
    } else if (this.notifier != null && NbtDataUtil.SPONGE_ENTITY_NOTIFIER.equals(nbtKey)) {
        return this.notifier;
    }
    NBTTagCompound nbt = getSpongeData();
    if (!nbt.hasKey(nbtKey)) {
        return null;
    }
    NBTTagCompound creatorNbt = nbt.getCompoundTag(nbtKey);
    if (!creatorNbt.hasKey(NbtDataUtil.UUID + "Most") && !creatorNbt.hasKey(NbtDataUtil.UUID + "Least")) {
        return null;
    }
    UUID uniqueId = creatorNbt.getUniqueId(NbtDataUtil.UUID);
    if (NbtDataUtil.SPONGE_ENTITY_CREATOR.equals(nbtKey)) {
        this.creator = uniqueId;
    } else if (NbtDataUtil.SPONGE_ENTITY_NOTIFIER.equals(nbtKey)) {
        this.notifier = uniqueId;
    }
    return uniqueId;
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(org.spongepowered.api.entity.Entity) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IEntityOwnable(net.minecraft.entity.IEntityOwnable) EntityPlayer(net.minecraft.entity.player.EntityPlayer) UUID(java.util.UUID) Nullable(javax.annotation.Nullable)

Example 2 with IEntityOwnable

use of net.minecraft.entity.IEntityOwnable in project BloodMagic by WayofTime.

the class EntityAIHurtByTargetIgnoreTamed method isSuitableTarget.

@Override
public boolean isSuitableTarget(EntityLivingBase target, boolean includeInvincibles) {
    if (this.taskOwner instanceof IEntityOwnable && target instanceof IEntityOwnable) {
        UUID thisId = ((IEntityOwnable) this.taskOwner).getOwnerId();
        UUID targetId = ((IEntityOwnable) target).getOwnerId();
        if (thisId != null && targetId != null && thisId.equals(targetId)) {
            return false;
        }
    }
    return super.isSuitableTarget(target, includeInvincibles);
}
Also used : IEntityOwnable(net.minecraft.entity.IEntityOwnable) UUID(java.util.UUID)

Example 3 with IEntityOwnable

use of net.minecraft.entity.IEntityOwnable in project SpongeCommon by SpongePowered.

the class PhaseTracker method spawnEntity.

/**
 * This is the replacement of {@link WorldServer#spawnEntity(net.minecraft.entity.Entity)}
 * where it captures into phases. The causes and relations are processed by the phases.
 *
 * The difference between {@link #spawnEntityWithCause(World, Entity)} is that it bypasses
 * any phases and directly throws a spawn entity event.
 *
 * @param world The world
 * @param entity The entity
 * @return True if the entity spawn was successful
 */
public boolean spawnEntity(World world, Entity entity) {
    checkNotNull(entity, "Entity cannot be null!");
    // Sponge Start - handle construction phases
    if (((IMixinEntity) entity).isInConstructPhase()) {
        ((IMixinEntity) entity).firePostConstructEvents();
    }
    final net.minecraft.entity.Entity minecraftEntity = EntityUtil.toNative(entity);
    final WorldServer minecraftWorld = (WorldServer) world;
    final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) minecraftWorld;
    final PhaseData phaseData = this.stack.peek();
    final IPhaseState<?> phaseState = phaseData.state;
    final PhaseContext<?> context = phaseData.context;
    final boolean isForced = minecraftEntity.forceSpawn || minecraftEntity instanceof EntityPlayer;
    // Certain phases disallow entity spawns (such as block restoration)
    if (!isForced && !phaseState.allowEntitySpawns()) {
        return false;
    }
    // Sponge End - continue with vanilla mechanics
    final int chunkX = MathHelper.floor(minecraftEntity.posX / 16.0D);
    final int chunkZ = MathHelper.floor(minecraftEntity.posZ / 16.0D);
    if (!isForced && !mixinWorldServer.isMinecraftChunkLoaded(chunkX, chunkZ, true)) {
        return false;
    }
    if (minecraftEntity instanceof EntityPlayer) {
        EntityPlayer entityplayer = (EntityPlayer) minecraftEntity;
        minecraftWorld.playerEntities.add(entityplayer);
        minecraftWorld.updateAllPlayersSleepingFlag();
        SpongeImplHooks.firePlayerJoinSpawnEvent((EntityPlayerMP) entityplayer);
    } else {
        // Sponge start - check for vanilla owner
        if (minecraftEntity instanceof IEntityOwnable) {
            IEntityOwnable ownable = (IEntityOwnable) entity;
            net.minecraft.entity.Entity owner = ownable.getOwner();
            if (owner != null && owner instanceof EntityPlayer) {
                context.owner = (User) owner;
                entity.setCreator(ownable.getOwnerId());
            }
        } else if (minecraftEntity instanceof EntityThrowable) {
            EntityThrowable throwable = (EntityThrowable) minecraftEntity;
            EntityLivingBase thrower = throwable.getThrower();
            if (thrower != null) {
                User user = null;
                if (!(thrower instanceof EntityPlayer)) {
                    user = ((IMixinEntity) thrower).getCreatorUser().orElse(null);
                } else {
                    user = (User) thrower;
                }
                if (user != null) {
                    context.owner = user;
                    entity.setCreator(user.getUniqueId());
                }
            }
        }
    // Sponge end
    }
    // capture all entities until the phase is marked for completion.
    if (!isForced) {
        try {
            return ((IPhaseState) phaseState).spawnEntityOrCapture(context, entity, chunkX, chunkZ);
        } catch (Exception | NoClassDefFoundError e) {
            // Just in case something really happened, we should print a nice exception for people to
            // paste us
            this.printExceptionSpawningEntity(context, e);
            return false;
        }
    }
    // Sponge end - continue on with the checks.
    minecraftWorld.getChunkFromChunkCoords(chunkX, chunkZ).addEntity(minecraftEntity);
    minecraftWorld.loadedEntityList.add(minecraftEntity);
    // Sponge - Cannot add onEntityAdded to the access transformer because forge makes it public
    mixinWorldServer.onSpongeEntityAdded(minecraftEntity);
    return true;
}
Also used : EntityThrowable(net.minecraft.entity.projectile.EntityThrowable) User(org.spongepowered.api.entity.living.player.User) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) ReportedException(net.minecraft.util.ReportedException) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IEntityOwnable(net.minecraft.entity.IEntityOwnable)

Example 4 with IEntityOwnable

use of net.minecraft.entity.IEntityOwnable in project SpongeCommon by SpongePowered.

the class MixinEntity_Tracker method getCreatorUser.

@Override
public Optional<User> getCreatorUser() {
    if (this.creator != null) {
        return getUserForUuid(this.creator);
    }
    if (this instanceof IEntityOwnable) {
        IEntityOwnable ownable = (IEntityOwnable) this;
        final net.minecraft.entity.Entity owner = ownable.getOwner();
        if (owner != null && owner instanceof EntityPlayer) {
            this.setCreator(owner.getUniqueID());
            return Optional.of((User) owner);
        }
    }
    return getTrackedPlayer(NbtDataUtil.SPONGE_ENTITY_CREATOR);
}
Also used : IEntityOwnable(net.minecraft.entity.IEntityOwnable) EntityPlayer(net.minecraft.entity.player.EntityPlayer)

Example 5 with IEntityOwnable

use of net.minecraft.entity.IEntityOwnable in project Cavern2 by kegare.

the class ItemAxeCavenic method hitEntity.

@Override
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {
    World world = target.world;
    int amount = 1;
    for (EntityLivingBase entity : world.getEntitiesWithinAABB(EntityLivingBase.class, target.getEntityBoundingBox().grow(2.5D))) {
        if (!(entity instanceof IMob)) {
            continue;
        }
        if (entity instanceof IEntityOwnable && ((IEntityOwnable) entity).getOwner() != null) {
            continue;
        }
        double dist = target.getDistanceSq(entity);
        Vec3d vec = getSmashVector(attacker, dist <= 2.0D, (itemRand.nextDouble() + 1.0D) * 1.15D, 0.1D);
        entity.attackEntityFrom(DamageSource.causeMobDamage(attacker), 4.0F);
        entity.addVelocity(vec.x, vec.y, vec.z);
        ++amount;
    }
    if (amount > 1) {
        world.playSound(null, target.posX, target.posY + 0.85D, target.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_WEAK, SoundCategory.PLAYERS, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 0.8F) + 0.25F);
    }
    stack.damageItem(amount, attacker);
    return true;
}
Also used : IMob(net.minecraft.entity.monster.IMob) EntityLivingBase(net.minecraft.entity.EntityLivingBase) IEntityOwnable(net.minecraft.entity.IEntityOwnable) World(net.minecraft.world.World) Vec3d(net.minecraft.util.math.Vec3d)

Aggregations

IEntityOwnable (net.minecraft.entity.IEntityOwnable)6 EntityPlayer (net.minecraft.entity.player.EntityPlayer)4 UUID (java.util.UUID)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)2 CapturedMob (crazypants.enderio.util.CapturedMob)1 Nullable (javax.annotation.Nullable)1 IMob (net.minecraft.entity.monster.IMob)1 EntityThrowable (net.minecraft.entity.projectile.EntityThrowable)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ReportedException (net.minecraft.util.ReportedException)1 Vec3d (net.minecraft.util.math.Vec3d)1 World (net.minecraft.world.World)1 WorldServer (net.minecraft.world.WorldServer)1 TargetContext (net.minecraftforge.server.permission.context.TargetContext)1 Entity (org.spongepowered.api.entity.Entity)1 User (org.spongepowered.api.entity.living.player.User)1 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)1