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);
}
}
}
}
}
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;
}
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;
}
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
}
}
}
}
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()));
}
}
Aggregations