mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 04:28:21 +01:00
Implement client spectate packet
This commit is contained in:
parent
a7a6210f65
commit
78ed12887d
@ -53,6 +53,7 @@ import java.util.function.Consumer;
|
|||||||
public abstract class Entity implements Viewable, EventHandler, DataContainer, PermissionHandler {
|
public abstract class Entity implements Viewable, EventHandler, DataContainer, PermissionHandler {
|
||||||
|
|
||||||
private static final Map<Integer, Entity> entityById = new ConcurrentHashMap<>();
|
private static final Map<Integer, Entity> entityById = new ConcurrentHashMap<>();
|
||||||
|
private static final Map<UUID, Entity> entityByUuid = new ConcurrentHashMap<>();
|
||||||
private static final AtomicInteger lastEntityId = new AtomicInteger();
|
private static final AtomicInteger lastEntityId = new AtomicInteger();
|
||||||
|
|
||||||
// Metadata
|
// Metadata
|
||||||
@ -137,10 +138,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
private long ticks;
|
private long ticks;
|
||||||
private final EntityTickEvent tickEvent = new EntityTickEvent(this);
|
private final EntityTickEvent tickEvent = new EntityTickEvent(this);
|
||||||
|
|
||||||
public Entity(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
|
public Entity(@NotNull EntityType entityType, @NotNull UUID uuid, @NotNull Position spawnPosition) {
|
||||||
this.id = generateId();
|
this.id = generateId();
|
||||||
this.entityType = entityType;
|
this.entityType = entityType;
|
||||||
this.uuid = UUID.randomUUID();
|
this.uuid = uuid;
|
||||||
this.position = spawnPosition.clone();
|
this.position = spawnPosition.clone();
|
||||||
|
|
||||||
setBoundingBox(0, 0, 0);
|
setBoundingBox(0, 0, 0);
|
||||||
@ -148,6 +149,11 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
setAutoViewable(true);
|
setAutoViewable(true);
|
||||||
|
|
||||||
entityById.put(id, this);
|
entityById.put(id, this);
|
||||||
|
entityByUuid.put(uuid, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entity(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
|
||||||
|
this(entityType, UUID.randomUUID(), spawnPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity(@NotNull EntityType entityType) {
|
public Entity(@NotNull EntityType entityType) {
|
||||||
@ -177,6 +183,18 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
return entityById.getOrDefault(id, null);
|
return entityById.getOrDefault(id, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an entity based on its UUID (from {@link #getUuid()}).
|
||||||
|
*
|
||||||
|
* @param uuid the entity UUID
|
||||||
|
* @return the entity having the specified uuid, null if not found
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static Entity getEntity(@NotNull UUID uuid) {
|
||||||
|
return entityByUuid.getOrDefault(uuid, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate and return a new unique entity id.
|
* Generate and return a new unique entity id.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -12,7 +12,6 @@ public class SettingsListener {
|
|||||||
|
|
||||||
PlayerSettingsChangeEvent playerSettingsChangeEvent = new PlayerSettingsChangeEvent(player);
|
PlayerSettingsChangeEvent playerSettingsChangeEvent = new PlayerSettingsChangeEvent(player);
|
||||||
player.callEvent(PlayerSettingsChangeEvent.class, playerSettingsChangeEvent);
|
player.callEvent(PlayerSettingsChangeEvent.class, playerSettingsChangeEvent);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package net.minestom.server.listener;
|
||||||
|
|
||||||
|
import net.minestom.server.entity.Entity;
|
||||||
|
import net.minestom.server.entity.Player;
|
||||||
|
import net.minestom.server.network.packet.client.play.ClientSpectatePacket;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class SpectateListener {
|
||||||
|
|
||||||
|
public static void listener(ClientSpectatePacket packet, Player player) {
|
||||||
|
final UUID targetUuid = packet.targetUuid;
|
||||||
|
final Entity target = Entity.getEntity(targetUuid);
|
||||||
|
|
||||||
|
// Check if the target is valid
|
||||||
|
if (target == null || target == player)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// TODO check if 'target' is in a different instance
|
||||||
|
player.spectate(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -53,6 +53,7 @@ public final class PacketListenerManager {
|
|||||||
setListener(ClientTeleportConfirmPacket.class, TeleportListener::listener);
|
setListener(ClientTeleportConfirmPacket.class, TeleportListener::listener);
|
||||||
setListener(ClientResourcePackStatusPacket.class, ResourcePackListener::listener);
|
setListener(ClientResourcePackStatusPacket.class, ResourcePackListener::listener);
|
||||||
setListener(ClientAdvancementTabPacket.class, AdvancementTabListener::listener);
|
setListener(ClientAdvancementTabPacket.class, AdvancementTabListener::listener);
|
||||||
|
setListener(ClientSpectatePacket.class, SpectateListener::listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user