|
1 | 1 | package com.github.skriptdev.skript.api.hytale.utils; |
2 | 2 |
|
| 3 | +import com.hypixel.hytale.component.Ref; |
| 4 | +import com.hypixel.hytale.component.Store; |
3 | 5 | import com.hypixel.hytale.server.core.entity.entities.Player; |
| 6 | +import com.hypixel.hytale.server.core.universe.PlayerRef; |
4 | 7 | import com.hypixel.hytale.server.core.universe.Universe; |
5 | 8 | import com.hypixel.hytale.server.core.universe.world.World; |
| 9 | +import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; |
6 | 10 | import org.jetbrains.annotations.Nullable; |
7 | 11 |
|
8 | 12 | import java.util.ArrayList; |
@@ -40,4 +44,36 @@ public static List<Player> getPlayers(@Nullable World world) { |
40 | 44 | } |
41 | 45 | } |
42 | 46 |
|
| 47 | + /** |
| 48 | + * Get a PlayerRef from a Player. |
| 49 | + * |
| 50 | + * @param player Player to get PlayerRef from |
| 51 | + * @return PlayerRef from Player or null if not found |
| 52 | + */ |
| 53 | + public static @Nullable PlayerRef getPlayerRef(Player player) { |
| 54 | + World world = player.getWorld(); |
| 55 | + assert world != null; |
| 56 | + |
| 57 | + Ref<EntityStore> reference = player.getReference(); |
| 58 | + assert reference != null; |
| 59 | + |
| 60 | + Store<EntityStore> store = world.getEntityStore().getStore(); |
| 61 | + return store.getComponent(reference, PlayerRef.getComponentType()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get a Player from a PlayerRef. |
| 66 | + * |
| 67 | + * @param playerRef PlayerRef to get Player from |
| 68 | + * @param world World to get Player from |
| 69 | + * @return Player from PlayerRef or null if not found |
| 70 | + */ |
| 71 | + public static @Nullable Player getPlayer(PlayerRef playerRef, World world) { |
| 72 | + Ref<EntityStore> reference = playerRef.getReference(); |
| 73 | + if (reference == null) return null; |
| 74 | + |
| 75 | + Store<EntityStore> store = world.getEntityStore().getStore(); |
| 76 | + return store.getComponent(reference, Player.getComponentType()); |
| 77 | + } |
| 78 | + |
43 | 79 | } |
0 commit comments