Search in sources :

Example 26 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class EntityActivationRange method activateEntities.

/**
 * Find what entities are in range of the players in the world and set
 * active if in range.
 *
 * @param world The world to perform activation checks in
 */
public static void activateEntities(final ServerLevel world) {
    if (((LevelBridge) world).bridge$isFake()) {
        return;
    }
    for (final ServerPlayer player : world.players()) {
        int maxRange = 0;
        for (final Integer range : EntityActivationRange.maxActivationRanges.values()) {
            if (range > maxRange) {
                maxRange = range;
            }
        }
        maxRange = Math.min((((ServerWorld) world).properties().viewDistance() << 4) - 8, maxRange);
        ((ActivationCapabilityBridge) player).activation$setActivatedTick(SpongeCommon.server().getTickCount());
        final AABB aabb = EntityActivationRange.maxBB;
        EntityActivationRange.growBb(aabb, player.getBoundingBox(), maxRange, 256, maxRange);
        final int i = Mth.floor(aabb.minX / 16.0D);
        final int j = Mth.floor(aabb.maxX / 16.0D);
        final int k = Mth.floor(aabb.minZ / 16.0D);
        final int l = Mth.floor(aabb.maxZ / 16.0D);
        for (int i1 = i; i1 <= j; ++i1) {
            for (int j1 = k; j1 <= l; ++j1) {
                final LevelChunk chunk = world.getChunkSource().getChunkNow(i1, j1);
                if (chunk != null) {
                    EntityActivationRange.activateChunkEntities(player, chunk);
                }
            }
        }
    }
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) AABB(net.minecraft.world.phys.AABB)

Example 27 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class InventoryMixin_Bridge_Inventory method bridge$setSelectedItem.

@Override
public void bridge$setSelectedItem(int itemIndex, final boolean notify) {
    itemIndex = itemIndex % 9;
    if (notify && this.player instanceof ServerPlayer) {
        final ClientboundSetCarriedItemPacket packet = new ClientboundSetCarriedItemPacket(itemIndex);
        ((ServerPlayer) this.player).connection.send(packet);
    }
    this.selected = itemIndex;
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) ClientboundSetCarriedItemPacket(net.minecraft.network.protocol.game.ClientboundSetCarriedItemPacket)

Example 28 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class EntityActivationRange method checkIfActive.

/**
 * Checks if the entity is active for this tick.
 *
 * @param entity The entity to check for activity
 * @return Whether the given entity should be active
 */
public static boolean checkIfActive(final Entity entity) {
    // Never safe to skip fireworks or entities not yet added to chunk
    if (entity instanceof Player || entity.level.isClientSide() || !entity.inChunk || entity instanceof FireworkRocketEntity) {
        return true;
    }
    final LevelChunkBridge activeChunk = ((ActiveChunkReferantBridge) entity).bridge$getActiveChunk();
    if (activeChunk == null) {
        // Should never happen but just in case for mods, always tick
        return true;
    }
    if (!activeChunk.bridge$isActive()) {
        return false;
    }
    // If in forced chunk or is player
    if (activeChunk.bridge$isPersistedChunk() || ((PlatformEntityBridge) entity).bridge$isFakePlayer() && entity instanceof ServerPlayer) {
        return true;
    }
    final long currentTick = SpongeCommon.server().getTickCount();
    final ActivationCapabilityBridge spongeEntity = (ActivationCapabilityBridge) entity;
    boolean isActive = spongeEntity.activation$getActivatedTick() >= currentTick || spongeEntity.activation$getDefaultActivationState();
    // Should this entity tick?
    if (!isActive) {
        if ((currentTick - spongeEntity.activation$getActivatedTick() - 1) % 20 == 0) {
            // Check immunities every 20 ticks.
            if (EntityActivationRange.checkEntityImmunities(entity)) {
                // Triggered some sort of immunity, give 20 full ticks before we check again.
                spongeEntity.activation$setActivatedTick(currentTick + 20);
            }
            isActive = true;
        }
    // Add a little performance juice to active entities. Skip 1/4 if not immune.
    } else if (!spongeEntity.activation$getDefaultActivationState() && entity.tickCount % 4 == 0 && !EntityActivationRange.checkEntityImmunities(entity)) {
        isActive = false;
    }
    if (isActive && !activeChunk.bridge$areNeighborsLoaded()) {
        isActive = false;
    }
    return isActive;
}
Also used : Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) PlatformEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformEntityBridge) ActiveChunkReferantBridge(org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge)

Example 29 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class ContainerBasedTransaction method handleCraftingPreview.

private void handleCraftingPreview(final Player player, final ClickContainerEvent event) {
    if (this.craftingInventory != null) {
        // TODO push event to cause?
        // TODO prevent event when there is no preview?
        final SlotTransaction previewTransaction = this.getPreviewTransaction(this.craftingInventory.result(), event.transactions());
        final Optional<CraftingRecipe> recipe = player.level.getRecipeManager().getRecipeFor(RecipeType.CRAFTING, this.craftingContainer, player.level).map(CraftingRecipe.class::cast);
        final CraftItemEvent.Preview previewEvent = SpongeEventFactory.createCraftItemEventPreview(event.cause(), (Container) this.menu, this.craftingInventory, event.cursorTransaction(), previewTransaction, recipe, Optional.empty(), event.transactions());
        SpongeCommon.post(previewEvent);
        this.handleEventResults(player, previewEvent);
        if (player instanceof ServerPlayer && previewEvent instanceof CraftItemEvent.Preview) {
            final SlotTransaction preview = previewEvent.preview();
            // Resend modified output if needed
            if (!preview.isValid() || previewEvent.isCancelled()) {
                ((ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(0, 0, ItemStackUtil.fromSnapshotToNative(previewEvent.preview().original())));
            // TODO handle preview event cancel during shift-crafting
            } else if (preview.custom().isPresent()) {
                ((ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(0, 0, ItemStackUtil.fromSnapshotToNative(previewEvent.preview().finalReplacement())));
            // TODO handle preview event modification during shift-crafting
            }
        }
    }
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) CraftItemEvent(org.spongepowered.api.event.item.inventory.CraftItemEvent) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 30 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class BookUtil method fakeBookView.

public static void fakeBookView(final Book book, final Collection<? extends Player> players) {
    // First we need to send a fake a Book ItemStack with the BookView's
    // contents to the player's hand
    // These values are localized since sending item NBT doesn't trigger translation
    final ItemStack item = ItemStack.of(ItemTypes.WRITTEN_BOOK, 1);
    Locale lastLocale = BookUtil.STARTER_LOCALE;
    for (final Player player : players) {
        if (!Objects.equals(player.locale(), lastLocale)) {
            lastLocale = player.locale();
            item.offer(Keys.CUSTOM_NAME, GlobalTranslator.render(book.title(), lastLocale));
            item.offer(Keys.AUTHOR, GlobalTranslator.render(book.author(), lastLocale));
            final Locale finalLastLocale = lastLocale;
            item.offer(Keys.PAGES, Lists.transform(book.pages(), page -> GlobalTranslator.render(page, finalLastLocale)));
        }
        final ServerPlayer mcPlayer = (ServerPlayer) player;
        final ServerGamePacketListenerImpl receiver = mcPlayer.connection;
        final Inventory inventory = mcPlayer.inventory;
        final int bookSlot = inventory.items.size() + inventory.selected;
        receiver.send(new ClientboundContainerSetSlotPacket(BookUtil.WINDOW_PLAYER_INVENTORY, bookSlot, ItemStackUtil.toNative(item)));
        // Next we tell the client to open the Book GUI
        receiver.send(new ClientboundOpenBookPacket(InteractionHand.MAIN_HAND));
        // Now we can remove the fake Book since it's contents will have already
        // been transferred to the GUI
        receiver.send(new ClientboundContainerSetSlotPacket(BookUtil.WINDOW_PLAYER_INVENTORY, bookSlot, inventory.getSelected()));
    }
}
Also used : Locale(java.util.Locale) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) Collection(java.util.Collection) ItemTypes(org.spongepowered.api.item.ItemTypes) Book(net.kyori.adventure.inventory.Book) Inventory(net.minecraft.world.entity.player.Inventory) ClientboundOpenBookPacket(net.minecraft.network.protocol.game.ClientboundOpenBookPacket) Objects(java.util.Objects) ServerPlayer(net.minecraft.server.level.ServerPlayer) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl) Keys(org.spongepowered.api.data.Keys) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Lists(com.google.common.collect.Lists) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) Locale(java.util.Locale) InteractionHand(net.minecraft.world.InteractionHand) Player(org.spongepowered.api.entity.living.player.Player) GlobalTranslator(net.kyori.adventure.translation.GlobalTranslator) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(org.spongepowered.api.entity.living.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) ClientboundOpenBookPacket(net.minecraft.network.protocol.game.ClientboundOpenBookPacket) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(net.minecraft.world.entity.player.Inventory) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)71 TamableAnimal (net.minecraft.world.entity.TamableAnimal)12 Behavior (de.Keyle.MyPet.api.skill.skills.Behavior)11 ServerLevel (net.minecraft.server.level.ServerLevel)11 ArmorStand (net.minecraft.world.entity.decoration.ArmorStand)11 ItemStack (net.minecraft.world.item.ItemStack)10 CraftPlayer (org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer)10 Player (org.bukkit.entity.Player)10 MyPet (de.Keyle.MyPet.api.entity.MyPet)8 EntityMyPet (de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet)8 CraftPlayer (org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer)8 Player (net.minecraft.world.entity.player.Player)7 EntityMyPet (de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet)6 ArrayList (java.util.ArrayList)6 ClientboundRemoveEntitiesPacket (net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket)6 ChunkMap (net.minecraft.server.level.ChunkMap)6 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 ItemEntity (net.minecraft.world.entity.item.ItemEntity)6 PlayerEntityTagMessage (com.wuest.prefab.proxy.messages.PlayerEntityTagMessage)5