mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-29 23:17:59 +02:00
Merge remote-tracking branch 'origin/master' into feature/item-meta
This commit is contained in:
commit
4818e29d81
@ -51,6 +51,7 @@ import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.callback.OptionalCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.entity.EntityUtils;
|
||||
import net.minestom.server.utils.instance.InstanceUtils;
|
||||
import net.minestom.server.utils.time.CooldownUtils;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
@ -1595,24 +1596,15 @@ public class Player extends LivingEntity implements CommandSender {
|
||||
});
|
||||
|
||||
// Manage entities in unchecked chunks
|
||||
final long[] chunksInRange = ChunkUtils.getChunksInRange(newChunk.toPosition(), entityViewDistance);
|
||||
EntityUtils.forEachRange(instance, newChunk.toPosition(), entityViewDistance, entity -> {
|
||||
if (entity.isAutoViewable() && !entity.viewers.contains(this)) {
|
||||
entity.addViewer(this);
|
||||
}
|
||||
|
||||
for (long chunkIndex : chunksInRange) {
|
||||
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
||||
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
||||
final Chunk chunk = instance.getChunk(chunkX, chunkZ);
|
||||
if (chunk == null)
|
||||
continue;
|
||||
instance.getChunkEntities(chunk).forEach(entity -> {
|
||||
if (isAutoViewable() && !entity.viewers.contains(this)) {
|
||||
entity.addViewer(this);
|
||||
}
|
||||
|
||||
if (entity instanceof Player && entity.isAutoViewable() && !viewers.contains(entity)) {
|
||||
addViewer((Player) entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (entity instanceof Player && isAutoViewable() && !viewers.contains(entity)) {
|
||||
addViewer((Player) entity);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package net.minestom.server.event.player;
|
||||
|
||||
import net.minestom.server.chat.RichMessage;
|
||||
import net.minestom.server.chat.JsonMessage;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.CancellableEvent;
|
||||
import net.minestom.server.event.PlayerEvent;
|
||||
@ -19,7 +19,7 @@ public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
|
||||
|
||||
private final Collection<Player> recipients;
|
||||
private String message;
|
||||
private Function<PlayerChatEvent, RichMessage> chatFormat;
|
||||
private Function<PlayerChatEvent, JsonMessage> chatFormat;
|
||||
|
||||
private boolean cancelled;
|
||||
|
||||
@ -34,7 +34,7 @@ public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
|
||||
*
|
||||
* @param chatFormat the custom chat format, null to use the default one
|
||||
*/
|
||||
public void setChatFormat(@Nullable Function<PlayerChatEvent, RichMessage> chatFormat) {
|
||||
public void setChatFormat(@Nullable Function<PlayerChatEvent, JsonMessage> chatFormat) {
|
||||
this.chatFormat = chatFormat;
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
|
||||
* @return the chat format which will be used, null if this is the default one
|
||||
*/
|
||||
@Nullable
|
||||
public Function<PlayerChatEvent, RichMessage> getChatFormatFunction() {
|
||||
public Function<PlayerChatEvent, JsonMessage> getChatFormatFunction() {
|
||||
return chatFormat;
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ import net.minestom.server.utils.PacketUtils;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.entity.EntityUtils;
|
||||
import net.minestom.server.utils.time.CooldownUtils;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
import net.minestom.server.utils.time.UpdateOption;
|
||||
@ -849,7 +850,6 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
AddEntityToInstanceEvent event = new AddEntityToInstanceEvent(this, entity);
|
||||
callCancellableEvent(AddEntityToInstanceEvent.class, event, () -> {
|
||||
final Position entityPosition = entity.getPosition();
|
||||
final long[] visibleChunksEntity = ChunkUtils.getChunksInRange(entityPosition, MinecraftServer.getEntityViewDistance());
|
||||
final boolean isPlayer = entity instanceof Player;
|
||||
|
||||
if (isPlayer) {
|
||||
@ -858,18 +858,17 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
}
|
||||
|
||||
// Send all visible entities
|
||||
for (long chunkIndex : visibleChunksEntity) {
|
||||
getEntitiesInChunk(chunkIndex).forEach(ent -> {
|
||||
if (isPlayer) {
|
||||
EntityUtils.forEachRange(this, entityPosition, MinecraftServer.getEntityViewDistance(), ent -> {
|
||||
if (isPlayer) {
|
||||
if (ent.isAutoViewable())
|
||||
ent.addViewer((Player) entity);
|
||||
}
|
||||
}
|
||||
|
||||
if (ent instanceof Player) {
|
||||
if (entity.isAutoViewable())
|
||||
entity.addViewer((Player) ent);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (ent instanceof Player) {
|
||||
if (entity.isAutoViewable())
|
||||
entity.addViewer((Player) ent);
|
||||
}
|
||||
});
|
||||
|
||||
final Chunk chunk = getChunkAt(entityPosition);
|
||||
Check.notNull(chunk, "You tried to spawn an entity in an unloaded chunk, " + entityPosition);
|
||||
|
@ -1,10 +1,7 @@
|
||||
package net.minestom.server.listener;
|
||||
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.chat.ChatClickEvent;
|
||||
import net.minestom.server.chat.ChatHoverEvent;
|
||||
import net.minestom.server.chat.ColoredText;
|
||||
import net.minestom.server.chat.RichMessage;
|
||||
import net.minestom.server.chat.*;
|
||||
import net.minestom.server.command.CommandManager;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.player.PlayerChatEvent;
|
||||
@ -35,16 +32,15 @@ public class ChatMessageListener {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
final Collection<Player> players = CONNECTION_MANAGER.getOnlinePlayers();
|
||||
PlayerChatEvent playerChatEvent = new PlayerChatEvent(player, players, message);
|
||||
|
||||
// Call the event
|
||||
player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> {
|
||||
|
||||
final Function<PlayerChatEvent, RichMessage> formatFunction = playerChatEvent.getChatFormatFunction();
|
||||
final Function<PlayerChatEvent, JsonMessage> formatFunction = playerChatEvent.getChatFormatFunction();
|
||||
|
||||
RichMessage textObject;
|
||||
JsonMessage textObject;
|
||||
|
||||
if (formatFunction != null) {
|
||||
// Custom format
|
||||
|
@ -8,6 +8,9 @@ import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class EntityUtils {
|
||||
|
||||
@ -15,7 +18,22 @@ public final class EntityUtils {
|
||||
|
||||
}
|
||||
|
||||
public static boolean areVisible(Entity ent1, Entity ent2) {
|
||||
public static void forEachRange(@NotNull Instance instance, @NotNull Position position,
|
||||
int viewDistance,
|
||||
@NotNull Consumer<Entity> consumer) {
|
||||
final long[] chunksInRange = ChunkUtils.getChunksInRange(position, viewDistance);
|
||||
|
||||
for (long chunkIndex : chunksInRange) {
|
||||
final int chunkX = ChunkUtils.getChunkCoordX(chunkIndex);
|
||||
final int chunkZ = ChunkUtils.getChunkCoordZ(chunkIndex);
|
||||
final Chunk chunk = instance.getChunk(chunkX, chunkZ);
|
||||
if (chunk == null)
|
||||
continue;
|
||||
instance.getChunkEntities(chunk).forEach(consumer::accept);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean areVisible(@NotNull Entity ent1, @NotNull Entity ent2) {
|
||||
if (ent1.getInstance() == null || ent2.getInstance() == null)
|
||||
return false;
|
||||
if (!ent1.getInstance().equals(ent2.getInstance()))
|
||||
@ -27,7 +45,6 @@ public final class EntityUtils {
|
||||
for (long visibleChunk : visibleChunksEntity) {
|
||||
final int chunkX = ChunkUtils.getChunkCoordX(visibleChunk);
|
||||
final int chunkZ = ChunkUtils.getChunkCoordZ(visibleChunk);
|
||||
|
||||
if (chunk.getChunkX() == chunkX && chunk.getChunkZ() == chunkZ)
|
||||
return true;
|
||||
}
|
||||
@ -35,7 +52,7 @@ public final class EntityUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isOnGround(Entity entity) {
|
||||
public static boolean isOnGround(@NotNull Entity entity) {
|
||||
final Instance instance = entity.getInstance();
|
||||
if (instance == null)
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user