Search in sources :

Example 26 with IMixinEntity

use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeCommon by SpongePowered.

the class MixinWorld method onGetClosestPlayerCheck.

@Redirect(method = "getClosestPlayer(DDDDLcom/google/common/base/Predicate;)Lnet/minecraft/entity/player/EntityPlayer;", at = @At(value = "INVOKE", target = "Lcom/google/common/base/Predicate;apply(Ljava/lang/Object;)Z", remap = false))
private boolean onGetClosestPlayerCheck(com.google.common.base.Predicate<net.minecraft.entity.Entity> predicate, Object entityPlayer) {
    EntityPlayer player = (EntityPlayer) entityPlayer;
    IMixinEntity mixinEntity = (IMixinEntity) player;
    return predicate.apply(player) && !mixinEntity.isVanished();
}
Also used : IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 27 with IMixinEntity

use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeCommon by SpongePowered.

the class DisplayNameDataProcessor method set.

@Override
public DataTransactionResult set(DataHolder holder, DisplayNameData manipulator, MergeFunction function) {
    if (holder instanceof IMixinEntity && !(holder instanceof Player)) {
        final Optional<DisplayNameData> old = from(holder);
        final DisplayNameData merged = checkNotNull(function).merge(old.orElse(null), manipulator);
        final Text newValue = merged.displayName().get();
        final ImmutableValue<Text> immutableValue = merged.displayName().asImmutable();
        try {
            ((IMixinEntity) holder).setDisplayName(newValue);
            if (old.isPresent()) {
                return DataTransactionResult.successReplaceResult(old.get().displayName().asImmutable(), immutableValue);
            }
            return DataTransactionResult.successResult(immutableValue);
        } catch (Exception e) {
            SpongeImpl.getLogger().debug("An exception occurred when setting data: ", e);
            return DataTransactionResult.errorResult(immutableValue);
        }
    }
    if (holder instanceof ItemStack) {
        final Optional<DisplayNameData> prevValue = from(holder);
        final DisplayNameData merged = checkNotNull(function).merge(prevValue.orElse(null), manipulator);
        final Text newValue = merged.displayName().get();
        final ImmutableValue<Text> immutableValue = merged.displayName().asImmutable();
        ItemStack stack = (ItemStack) holder;
        if (stack.getItem() == Items.WRITTEN_BOOK) {
            NbtDataUtil.getOrCreateCompound(stack).setString(NbtDataUtil.ITEM_BOOK_TITLE, SpongeTexts.toLegacy(newValue));
        } else {
            stack.setStackDisplayName(SpongeTexts.toLegacy(newValue));
        }
        if (prevValue.isPresent()) {
            return DataTransactionResult.successReplaceResult(prevValue.get().displayName().asImmutable(), immutableValue);
        }
        return DataTransactionResult.successResult(immutableValue);
    }
    return DataTransactionResult.failResult(manipulator.getValues());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ImmutableSpongeDisplayNameData(org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData) SpongeDisplayNameData(org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData) DisplayNameData(org.spongepowered.api.data.manipulator.mutable.DisplayNameData) ImmutableDisplayNameData(org.spongepowered.api.data.manipulator.immutable.ImmutableDisplayNameData) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Text(org.spongepowered.api.text.Text) ItemStack(net.minecraft.item.ItemStack)

Example 28 with IMixinEntity

use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeCommon by SpongePowered.

the class SpongeMessageHandler method handleRequest.

public static void handleRequest(MessageTrackerDataRequest message, RemoteConnection connection, Platform.Type side) {
    if (!(connection instanceof PlayerConnection)) {
        return;
    }
    Player player = ((PlayerConnection) connection).getPlayer();
    if (!player.hasPermission("sponge.debug.block-tracking")) {
        return;
    }
    EntityPlayerMP sender = (EntityPlayerMP) player;
    BlockPos pos = new BlockPos(message.x, message.y, message.z);
    if (!sender.world.isBlockLoaded(pos)) {
        return;
    }
    String ownerName;
    String notifierName;
    Optional<User> owner = Optional.empty();
    Optional<User> notifier = Optional.empty();
    if (message.type == 0) {
        // block
        IMixinChunk spongeChunk = (IMixinChunk) sender.world.getChunkFromBlockCoords(pos);
        owner = spongeChunk.getBlockOwner(pos);
        notifier = spongeChunk.getBlockNotifier(pos);
    } else if (message.type == 1) {
        // entity
        Entity entity = sender.world.getEntityByID(message.entityId);
        if (entity != null) {
            IMixinEntity spongeEntity = (IMixinEntity) entity;
            owner = spongeEntity.getCreatorUser();
            notifier = spongeEntity.getNotifierUser();
        }
    }
    ownerName = owner.map(User::getName).orElse("");
    notifierName = notifier.map(User::getName).orElse("");
    channel.sendTo(player, new MessageTrackerDataResponse(ownerName, notifierName));
}
Also used : Entity(net.minecraft.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Player(org.spongepowered.api.entity.living.player.Player) User(org.spongepowered.api.entity.living.player.User) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPos(net.minecraft.util.math.BlockPos) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) PlayerConnection(org.spongepowered.api.network.PlayerConnection)

Example 29 with IMixinEntity

use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeForge by SpongePowered.

the class MixinItemShears method itemInteractionForEntity.

/**
 * @author gabizou - June 21st, 2016
 * @reason Rewrites the forge handling of this to properly handle
 * when sheared drops are captured by whatever current phase the
 * {@link PhaseTracker} is in.
 *
 * Returns true if the item can be used on the given entity, e.g. shears on sheep.
 */
@Overwrite
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, EntityPlayer player, EntityLivingBase entity, EnumHand hand) {
    if (entity.world.isRemote) {
        return false;
    }
    if (entity instanceof IShearable) {
        IShearable target = (IShearable) entity;
        BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
        if (target.isShearable(itemstack, entity.world, pos)) {
            List<ItemStack> drops = target.onSheared(itemstack, entity.world, pos, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, itemstack));
            // Sponge Start - Handle drops according to the current phase
            final PhaseTracker phaseTracker = PhaseTracker.getInstance();
            final PhaseData currentData = phaseTracker.getCurrentPhaseData();
            final IPhaseState<?> currentState = currentData.state;
            final PhaseContext<?> phaseContext = currentData.context;
            final Random random = EntityUtil.fromNative(entity).getRandom();
            final IMixinEntity mixinEntity = EntityUtil.toMixin(entity);
            final double posX = entity.posX;
            final double posY = entity.posY + 1.0F;
            final double posZ = entity.posZ;
            final Vector3d position = new Vector3d(posX, posY, posZ);
            // Now the real fun begins.
            for (ItemStack drop : drops) {
                final ItemStack item;
                if (!drop.isEmpty()) {
                    try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
                        // FIRST we want to throw the DropItemEvent.PRE
                        final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(drop);
                        final List<ItemStackSnapshot> original = new ArrayList<>();
                        original.add(snapshot);
                        Sponge.getCauseStackManager().pushCause(entity);
                        final DropItemEvent.Pre dropEvent = SpongeEventFactory.createDropItemEventPre(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.of(snapshot), original);
                        if (dropEvent.isCancelled()) {
                            continue;
                        }
                        // SECOND throw the ConstructEntityEvent
                        Transform<World> suggested = new Transform<>(mixinEntity.getWorld(), position);
                        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
                        ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), EntityTypes.ITEM, suggested);
                        SpongeImpl.postEvent(event);
                        item = event.isCancelled() ? null : ItemStackUtil.fromSnapshotToNative(dropEvent.getDroppedItems().get(0));
                    }
                } else {
                    continue;
                }
                if (item == null) {
                    continue;
                }
                if (!item.isEmpty()) {
                    if (!currentState.ignoresItemPreMerging() && SpongeImpl.getGlobalConfig().getConfig().getOptimizations().doDropsPreMergeItemDrops()) {
                        if (currentState.tracksEntitySpecificDrops()) {
                            final Multimap<UUID, ItemDropData> multimap = phaseContext.getCapturedEntityDropSupplier().get();
                            final Collection<ItemDropData> itemStacks = multimap.get(entity.getUniqueID());
                            SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item).motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)).position(new Vector3d(posX, posY, posZ)).build());
                            continue;
                        }
                        final List<ItemDropData> itemStacks = phaseContext.getCapturedItemStackSupplier().get();
                        SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item).position(new Vector3d(posX, posY, posZ)).motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)).build());
                        continue;
                    }
                    EntityItem entityitem = new EntityItem(entity.world, posX, posY, posZ, item);
                    entityitem.setDefaultPickupDelay();
                    entityitem.motionY += random.nextFloat() * 0.05F;
                    entityitem.motionX += (random.nextFloat() - random.nextFloat()) * 0.1F;
                    entityitem.motionZ += (random.nextFloat() - random.nextFloat()) * 0.1F;
                    // FIFTH - Capture the entity maybe?
                    if (currentState.doesCaptureEntityDrops()) {
                        if (currentState.tracksEntitySpecificDrops()) {
                            // We are capturing per entity drop
                            phaseContext.getCapturedEntityItemDropSupplier().get().put(entity.getUniqueID(), entityitem);
                        } else {
                            // We are adding to a general list - usually for EntityPhase.State.DEATH
                            phaseContext.getCapturedItemsSupplier().get().add(entityitem);
                        }
                        // Return the item, even if it wasn't spawned in the world.
                        continue;
                    }
                    // FINALLY - Spawn the entity in the world if all else didn't fail
                    entity.world.spawnEntity(entityitem);
                }
            }
            // Sponge End
            itemstack.damageItem(1, entity);
        }
        return true;
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) World(org.spongepowered.api.world.World) Random(java.util.Random) CauseStackManager(org.spongepowered.api.event.CauseStackManager) IShearable(net.minecraftforge.common.IShearable) BlockPos(net.minecraft.util.math.BlockPos) UUID(java.util.UUID) EntityItem(net.minecraft.entity.item.EntityItem) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) PhaseData(org.spongepowered.common.event.tracking.PhaseData) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Vector3d(com.flowpowered.math.vector.Vector3d) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.item.ItemStack) Transform(org.spongepowered.api.entity.Transform) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 30 with IMixinEntity

use of org.spongepowered.common.interfaces.entity.IMixinEntity in project SpongeForge by SpongePowered.

the class MixinWorld_Activation method updateEntityWithOptionalForce.

@Overwrite
public void updateEntityWithOptionalForce(Entity entityIn, boolean forceUpdate) {
    // Sponge start - area is handled in ActivationRange
    // int i = MathHelper.floor_double(entityIn.posX);
    // int j = MathHelper.floor_double(entityIn.posZ);
    // boolean isForced = getPersistentChunks().containsKey(new net.minecraft.util.math.ChunkPos(i >> 4, j >> 4));
    // int k = isForced ? 0 : 32;
    // boolean canUpdate = !forceUpdate || this.isAreaLoaded(i - k, 0, j - k, i + k, 0, j + k, true);
    boolean canUpdate = EntityActivationRange.checkIfActive(entityIn);
    // Allow forge mods to force an update
    if (!canUpdate)
        canUpdate = net.minecraftforge.event.ForgeEventFactory.canEntityUpdate(entityIn);
    if (!canUpdate) {
        entityIn.ticksExisted++;
        ((IModData_Activation) entityIn).inactiveTick();
        return;
    }
    // Sponge end
    entityIn.lastTickPosX = entityIn.posX;
    entityIn.lastTickPosY = entityIn.posY;
    entityIn.lastTickPosZ = entityIn.posZ;
    entityIn.prevRotationYaw = entityIn.rotationYaw;
    entityIn.prevRotationPitch = entityIn.rotationPitch;
    if (forceUpdate && entityIn.addedToChunk) {
        ++entityIn.ticksExisted;
        // Sponge
        ++co.aikar.timings.TimingHistory.activatedEntityTicks;
        if (entityIn.isRiding()) {
            entityIn.updateRidden();
        } else {
            entityIn.onUpdate();
        }
    }
    if (Double.isNaN(entityIn.posX) || Double.isInfinite(entityIn.posX)) {
        entityIn.posX = entityIn.lastTickPosX;
    }
    if (Double.isNaN(entityIn.posY) || Double.isInfinite(entityIn.posY)) {
        entityIn.posY = entityIn.lastTickPosY;
    }
    if (Double.isNaN(entityIn.posZ) || Double.isInfinite(entityIn.posZ)) {
        entityIn.posZ = entityIn.lastTickPosZ;
    }
    if (Double.isNaN(entityIn.rotationPitch) || Double.isInfinite(entityIn.rotationPitch)) {
        entityIn.rotationPitch = entityIn.prevRotationPitch;
    }
    if (Double.isNaN(entityIn.rotationYaw) || Double.isInfinite(entityIn.rotationYaw)) {
        entityIn.rotationYaw = entityIn.prevRotationYaw;
    }
    int l = MathHelper.floor(entityIn.posX / 16.0D);
    int i1 = MathHelper.floor(entityIn.posY / 16.0D);
    int j1 = MathHelper.floor(entityIn.posZ / 16.0D);
    if (!entityIn.addedToChunk || entityIn.chunkCoordX != l || entityIn.chunkCoordY != i1 || entityIn.chunkCoordZ != j1) {
        // Sponge start - use cached chunk
        final Chunk activeChunk = (Chunk) ((IMixinEntity) entityIn).getActiveChunk();
        if (activeChunk != null) {
            activeChunk.removeEntityAtIndex(entityIn, entityIn.chunkCoordY);
        }
        // Sponge end
        final IMixinChunk newChunk = (IMixinChunk) ((IMixinChunkProviderServer) entityIn.world.getChunkProvider()).getLoadedChunkWithoutMarkingActive(l, j1);
        final boolean isPositionDirty = entityIn.setPositionNonDirty();
        if (newChunk == null || (!isPositionDirty && newChunk.isQueuedForUnload() && !newChunk.isPersistedChunk())) {
            entityIn.addedToChunk = false;
        } else {
            ((net.minecraft.world.chunk.Chunk) newChunk).addEntity(entityIn);
        }
    }
    if (forceUpdate && entityIn.addedToChunk) {
        for (Entity entity : entityIn.getPassengers()) {
            if (!entity.isDead && entity.getRidingEntity() == entityIn) {
                this.updateEntity(entity);
            } else {
                entity.dismountRidingEntity();
            }
        }
    }
}
Also used : IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) Entity(net.minecraft.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)30 World (org.spongepowered.api.world.World)10 Entity (net.minecraft.entity.Entity)7 EntityPlayer (net.minecraft.entity.player.EntityPlayer)7 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)7 Vector3d (com.flowpowered.math.vector.Vector3d)6 User (org.spongepowered.api.entity.living.player.User)6 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)6 ArrayList (java.util.ArrayList)5 BlockPos (net.minecraft.util.math.BlockPos)5 WorldServer (net.minecraft.world.WorldServer)5 Overwrite (org.spongepowered.asm.mixin.Overwrite)5 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 Nullable (javax.annotation.Nullable)3 ItemStack (net.minecraft.item.ItemStack)3 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)3 Entity (org.spongepowered.api.entity.Entity)3 Player (org.spongepowered.api.entity.living.player.Player)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3