Search in sources :

Example 1 with IMixinEntity

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

the class InvisibilityDataProcessor method set.

@Override
protected boolean set(Entity dataHolder, Map<Key<?>, Object> keyValues) {
    if (!dataHolder.world.isRemote) {
        final boolean invis = (Boolean) keyValues.get(Keys.INVISIBLE);
        final boolean collision = (Boolean) keyValues.get(Keys.VANISH_IGNORES_COLLISION);
        final boolean untargetable = (Boolean) keyValues.get(Keys.VANISH_PREVENTS_TARGETING);
        final boolean vanish = (Boolean) keyValues.get(Keys.VANISH);
        dataHolder.setInvisible(invis);
        if (vanish) {
            final IMixinEntity mixinEntity = EntityUtil.toMixin(dataHolder);
            mixinEntity.setVanished(true);
            mixinEntity.setIgnoresCollision(collision);
            mixinEntity.setUntargetable(untargetable);
        } else {
            EntityUtil.toMixin(dataHolder).setVanished(false);
        }
        return true;
    }
    return false;
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity)

Example 2 with IMixinEntity

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

the class DisplayNameDataProcessor method from.

@Override
public Optional<DisplayNameData> from(DataHolder holder) {
    if (holder instanceof Entity) {
        @Nullable Text displayName = ((IMixinEntity) holder).getDisplayNameText();
        if (displayName != null) {
            return Optional.of(new SpongeDisplayNameData(displayName));
        }
        return Optional.empty();
    } else if (holder instanceof ItemStack) {
        ItemStack stack = (ItemStack) holder;
        if (!stack.hasDisplayName()) {
            return Optional.empty();
        }
        if (stack.getItem() == Items.WRITTEN_BOOK) {
            final NBTTagCompound compound = stack.getTagCompound();
            if (compound == null) {
                // The book wasn't initialized.
                return Optional.empty();
            }
            return Optional.of(new SpongeDisplayNameData(SpongeTexts.fromLegacy(compound.getString(NbtDataUtil.ITEM_BOOK_TITLE))));
        }
        final NBTTagCompound compound = ((ItemStack) holder).getSubCompound(NbtDataUtil.ITEM_DISPLAY);
        if (compound != null && compound.hasKey(NbtDataUtil.ITEM_DISPLAY_NAME, NbtDataUtil.TAG_STRING)) {
            return Optional.of(new SpongeDisplayNameData(SpongeTexts.fromLegacy(compound.getString(NbtDataUtil.ITEM_DISPLAY_NAME))));
        }
        return Optional.empty();
    } else if (holder instanceof IWorldNameable) {
        if (((IWorldNameable) holder).hasCustomName()) {
            final String customName = ((IWorldNameable) holder).getName();
            final DisplayNameData data = new SpongeDisplayNameData(SpongeTexts.fromLegacy(customName));
            return Optional.of(data);
        }
        return Optional.empty();
    }
    return Optional.empty();
}
Also used : ImmutableSpongeDisplayNameData(org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData) SpongeDisplayNameData(org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) 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) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Text(org.spongepowered.api.text.Text) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable) IWorldNameable(net.minecraft.world.IWorldNameable)

Example 3 with IMixinEntity

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

the class MixinPlayerList method recreatePlayerEntity.

/**
 * @author Zidane - June 13th, 2015
 * @author simon816 - June 24th, 2015
 * @author Zidane - March 29th, 2016
 * @author gabizou - June 5th, 2016 - Update for teleportation changes to keep the same player.
 *
 * @reason - Direct respawning players to use Sponge events
 * and process appropriately.
 *
 * @param playerIn The player being respawned/created
 * @param targetDimension The target dimension
 * @param conqueredEnd Whether the end was conquered
 * @return The new player
 */
@Overwrite
public EntityPlayerMP recreatePlayerEntity(EntityPlayerMP playerIn, int targetDimension, boolean conqueredEnd) {
    // UNLESS comming back from the end.
    if (!conqueredEnd && targetDimension == 0) {
        targetDimension = playerIn.dimension;
    }
    if (playerIn.isBeingRidden()) {
        playerIn.removePassengers();
    }
    if (playerIn.isRiding()) {
        playerIn.dismountRidingEntity();
    }
    final Player player = (Player) playerIn;
    final Transform<World> fromTransform = player.getTransform();
    WorldServer worldServer = this.mcServer.getWorld(targetDimension);
    Transform<World> toTransform = new Transform<>(EntityUtil.getPlayerRespawnLocation(playerIn, worldServer), Vector3d.ZERO, Vector3d.ZERO);
    targetDimension = ((IMixinWorldServer) toTransform.getExtent()).getDimensionId();
    Location<World> location = toTransform.getLocation();
    // If coming from end, fire a teleport event for plugins
    if (conqueredEnd) {
        // When leaving the end, players are never placed inside the teleporter but instead "respawned" in the target world
        MoveEntityEvent.Teleport teleportEvent = EntityUtil.handleDisplaceEntityTeleportEvent(playerIn, location);
        if (teleportEvent.isCancelled()) {
            playerIn.queuedEndExit = false;
            return playerIn;
        }
        toTransform = teleportEvent.getToTransform();
        location = toTransform.getLocation();
    }
    // Keep players out of blocks
    Vector3d tempPos = player.getLocation().getPosition();
    playerIn.setPosition(location.getX(), location.getY(), location.getZ());
    while (!((WorldServer) location.getExtent()).getCollisionBoxes(playerIn, playerIn.getEntityBoundingBox()).isEmpty()) {
        playerIn.setPosition(playerIn.posX, playerIn.posY + 1.0D, playerIn.posZ);
        location = location.add(0, 1, 0);
    }
    playerIn.setPosition(tempPos.getX(), tempPos.getY(), tempPos.getZ());
    // ### PHASE 2 ### Remove player from current dimension
    playerIn.getServerWorld().getEntityTracker().removePlayerFromTrackers(playerIn);
    playerIn.getServerWorld().getEntityTracker().untrack(playerIn);
    playerIn.getServerWorld().getPlayerChunkMap().removePlayer(playerIn);
    this.playerEntityList.remove(playerIn);
    this.mcServer.getWorld(playerIn.dimension).removeEntityDangerously(playerIn);
    final BlockPos bedPos = SpongeImplHooks.getBedLocation(playerIn, targetDimension);
    // ### PHASE 3 ### Reset player (if applicable)
    // Recreate the player object in order to support Forge's PlayerEvent.Clone
    PlayerInteractionManager playerinteractionmanager;
    if (this.mcServer.isDemo()) {
        playerinteractionmanager = new DemoPlayerInteractionManager(this.mcServer.getWorld(targetDimension));
    } else {
        playerinteractionmanager = new PlayerInteractionManager(this.mcServer.getWorld(targetDimension));
    }
    EntityPlayerMP newPlayer = new EntityPlayerMP(SpongeImpl.getServer(), worldServer, playerIn.getGameProfile(), playerinteractionmanager);
    newPlayer.connection = playerIn.connection;
    newPlayer.copyFrom(playerIn, conqueredEnd);
    // set player dimension for RespawnPlayerEvent
    newPlayer.dimension = targetDimension;
    newPlayer.setEntityId(playerIn.getEntityId());
    newPlayer.setCommandStats(playerIn);
    newPlayer.setPrimaryHand(playerIn.getPrimaryHand());
    // over to the new player
    if (bedPos != null) {
        newPlayer.setSpawnPoint(bedPos, playerIn.isSpawnForced());
    }
    for (String s : playerIn.getTags()) {
        newPlayer.addTag(s);
    }
    this.setPlayerGameTypeBasedOnOther(newPlayer, playerIn, worldServer);
    newPlayer.setSneaking(false);
    // update to safe location
    toTransform = toTransform.setLocation(location);
    // ### PHASE 4 ### Fire event and set new location on the player
    Sponge.getCauseStackManager().pushCause(newPlayer);
    final RespawnPlayerEvent event = SpongeEventFactory.createRespawnPlayerEvent(Sponge.getCauseStackManager().getCurrentCause(), fromTransform, toTransform, (Player) playerIn, (Player) newPlayer, EntityUtil.tempIsBedSpawn, !conqueredEnd);
    EntityUtil.tempIsBedSpawn = false;
    SpongeImpl.postEvent(event);
    Sponge.getCauseStackManager().popCause();
    ((IMixinEntity) player).setLocationAndAngles(event.getToTransform());
    toTransform = event.getToTransform();
    location = toTransform.getLocation();
    if (!(location.getExtent() instanceof WorldServer)) {
        SpongeImpl.getLogger().warn("Location set in PlayerRespawnEvent was invalid, using original location instead");
        location = event.getFromTransform().getLocation();
    }
    worldServer = (WorldServer) location.getExtent();
    final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) worldServer;
    // Set the dimension again in case a plugin changed the target world during RespawnPlayerEvent
    newPlayer.dimension = mixinWorldServer.getDimensionId();
    newPlayer.setWorld(worldServer);
    newPlayer.interactionManager.setWorld(worldServer);
    worldServer.getChunkProvider().loadChunk((int) location.getX() >> 4, (int) location.getZ() >> 4);
    // ### PHASE 5 ### Respawn player in new world
    // Support vanilla clients logging into custom dimensions
    final int dimensionId = WorldManager.getClientDimensionId(newPlayer, worldServer);
    // Send dimension registration
    if (((IMixinEntityPlayerMP) newPlayer).usesCustomClient()) {
        WorldManager.sendDimensionRegistration(newPlayer, worldServer.provider);
    } else {
        // Force vanilla client to refresh its chunk cache if same dimension type
        if (fromTransform.getExtent() != worldServer && fromTransform.getExtent().getDimension().getType() == toTransform.getExtent().getDimension().getType()) {
            newPlayer.connection.sendPacket(new SPacketRespawn((dimensionId >= 0 ? -1 : 0), worldServer.getDifficulty(), worldServer.getWorldInfo().getTerrainType(), newPlayer.interactionManager.getGameType()));
        }
    }
    newPlayer.connection.sendPacket(new SPacketRespawn(dimensionId, worldServer.getDifficulty(), worldServer.getWorldInfo().getTerrainType(), newPlayer.interactionManager.getGameType()));
    newPlayer.connection.setPlayerLocation(location.getX(), location.getY(), location.getZ(), (float) toTransform.getYaw(), (float) toTransform.getPitch());
    final BlockPos spawnLocation = worldServer.getSpawnPoint();
    newPlayer.connection.sendPacket(new SPacketSpawnPosition(spawnLocation));
    newPlayer.connection.sendPacket(new SPacketSetExperience(newPlayer.experience, newPlayer.experienceTotal, newPlayer.experienceLevel));
    this.updateTimeAndWeatherForPlayer(newPlayer, worldServer);
    this.updatePermissionLevel(newPlayer);
    worldServer.getPlayerChunkMap().addPlayer(newPlayer);
    org.spongepowered.api.entity.Entity spongeEntity = (org.spongepowered.api.entity.Entity) newPlayer;
    ((org.spongepowered.api.world.World) worldServer).spawnEntity(spongeEntity);
    this.playerEntityList.add(newPlayer);
    this.uuidToPlayerMap.put(newPlayer.getUniqueID(), newPlayer);
    newPlayer.addSelfToInternalCraftingInventory();
    // Update reducedDebugInfo game rule
    newPlayer.connection.sendPacket(new SPacketEntityStatus(newPlayer, worldServer.getGameRules().getBoolean(DefaultGameRules.REDUCED_DEBUG_INFO) ? (byte) 22 : 23));
    for (PotionEffect potioneffect : newPlayer.getActivePotionEffects()) {
        newPlayer.connection.sendPacket(new SPacketEntityEffect(newPlayer.getEntityId(), potioneffect));
    }
    ((IMixinEntityPlayerMP) newPlayer).refreshScaledHealth();
    return newPlayer;
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) SPacketSpawnPosition(net.minecraft.network.play.server.SPacketSpawnPosition) PotionEffect(net.minecraft.potion.PotionEffect) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) World(org.spongepowered.api.world.World) SPacketSetExperience(net.minecraft.network.play.server.SPacketSetExperience) BlockPos(net.minecraft.util.math.BlockPos) SPacketRespawn(net.minecraft.network.play.server.SPacketRespawn) Player(org.spongepowered.api.entity.living.player.Player) RespawnPlayerEvent(org.spongepowered.api.event.entity.living.humanoid.player.RespawnPlayerEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SPacketEntityStatus(net.minecraft.network.play.server.SPacketEntityStatus) Vector3d(com.flowpowered.math.vector.Vector3d) DemoPlayerInteractionManager(net.minecraft.server.management.DemoPlayerInteractionManager) PlayerInteractionManager(net.minecraft.server.management.PlayerInteractionManager) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) SPacketEntityEffect(net.minecraft.network.play.server.SPacketEntityEffect) Transform(org.spongepowered.api.entity.Transform) DemoPlayerInteractionManager(net.minecraft.server.management.DemoPlayerInteractionManager) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 4 with IMixinEntity

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

the class TrackingUtil method tickRidingEntity.

public static void tickRidingEntity(net.minecraft.entity.Entity entity) {
    checkArgument(entity instanceof Entity, "Entity %s is not an instance of SpongeAPI's Entity!", entity);
    checkNotNull(entity, "Cannot capture on a null ticking entity!");
    final IMixinEntity mixinEntity = EntityUtil.toMixin(entity);
    if (!mixinEntity.shouldTick()) {
        return;
    }
    final Optional<User> notifierUser = mixinEntity.getNotifierUser();
    final Optional<User> creatorUser = mixinEntity.getCreatorUser();
    final EntityTickContext tickContext = TickPhase.Tick.ENTITY.createPhaseContext().source(entity).notifier(() -> notifierUser).owner(() -> creatorUser);
    try (final StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
        final EntityTickContext context = tickContext.buildAndSwitch();
        final Timing entityTiming = mixinEntity.getTimingsHandler().startTiming()) {
        Sponge.getCauseStackManager().pushCause(entity);
        notifierUser.ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
        creatorUser.ifPresent(notifier -> frame.addContext(EventContextKeys.OWNER, notifier));
        entity.updateRidden();
    } catch (Exception | NoClassDefFoundError e) {
        PhaseTracker.getInstance().printExceptionFromPhase(e, tickContext);
    }
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(org.spongepowered.api.entity.Entity) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) User(org.spongepowered.api.entity.living.player.User) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Timing(co.aikar.timings.Timing)

Example 5 with IMixinEntity

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

the class TrackingUtil method tickEntity.

public static void tickEntity(net.minecraft.entity.Entity entityIn) {
    checkArgument(entityIn instanceof Entity, "Entity %s is not an instance of SpongeAPI's Entity!", entityIn);
    checkNotNull(entityIn, "Cannot capture on a null ticking entity!");
    final IMixinEntity mixinEntity = EntityUtil.toMixin(entityIn);
    if (!mixinEntity.shouldTick()) {
        return;
    }
    final EntityTickContext tickContext = TickPhase.Tick.ENTITY.createPhaseContext().source(entityIn);
    try (final StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
        final EntityTickContext context = tickContext;
        final Timing entityTiming = mixinEntity.getTimingsHandler().startTiming()) {
        mixinEntity.getNotifierUser().ifPresent(notifier -> {
            frame.addContext(EventContextKeys.NOTIFIER, notifier);
            context.notifier(notifier);
        });
        mixinEntity.getCreatorUser().ifPresent(owner -> {
            if (mixinEntity instanceof EntityFallingBlock) {
                frame.pushCause(owner);
            }
            frame.addContext(EventContextKeys.OWNER, owner);
            context.owner(owner);
        });
        context.buildAndSwitch();
        entityIn.onUpdate();
    } catch (Exception | NoClassDefFoundError e) {
        PhaseTracker.getInstance().printExceptionFromPhase(e, tickContext);
    }
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(org.spongepowered.api.entity.Entity) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) EntityFallingBlock(net.minecraft.entity.item.EntityFallingBlock) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Timing(co.aikar.timings.Timing)

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