-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEntityComponentUtils.java
More file actions
86 lines (72 loc) · 3.5 KB
/
EntityComponentUtils.java
File metadata and controls
86 lines (72 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.github.skriptdev.skript.api.hytale;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.math.vector.Location;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.entity.Entity;
import com.hypixel.hytale.server.core.entity.EntityUtils;
import com.hypixel.hytale.server.core.entity.LivingEntity;
import com.hypixel.hytale.server.core.entity.movement.MovementStatesComponent;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.modules.entity.item.ItemComponent;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatsModule;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import io.github.syst3ms.skriptparser.util.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Quick utility class for accessing entity components.
*/
@SuppressWarnings("UnusedReturnValue")
public class EntityComponentUtils {
/**
* Get the EntityStatMap component of an entity.
*
* @param entity Entity to get component from
* @return EntityStatMap component of the entity, or null if not found
*/
public static @Nullable EntityStatMap getEntityStatMap(LivingEntity entity) {
World world = entity.getWorld();
if (world == null) return null;
Store<EntityStore> store = world.getEntityStore().getStore();
Ref<EntityStore> reference = entity.getReference();
if (reference == null) return null;
return store.getComponent(reference, EntityStatsModule.get().getEntityStatMapComponentType());
}
/**
* Get the MovementStatesComponent of an entity.
*
* @param entity Entity to get component from
* @return MovementStatesComponent of the entity, or null if not found
*/
public static @Nullable MovementStatesComponent getMovementStatesComponent(Entity entity) {
Ref<EntityStore> reference = entity.getReference();
if (reference == null) return null;
Store<EntityStore> store = reference.getStore();
return store.getComponent(reference, MovementStatesComponent.getComponentType());
}
@SuppressWarnings({"DataFlowIssue", "deprecation"})
public static @NotNull Pair<Entity, ItemComponent> dropItem(Store<EntityStore> store, ItemStack itemStack, Location location, Vector3f velocity, float pickupDelay) {
if (itemStack.isEmpty() || !itemStack.isValid()) {
return new Pair<>(null, null);
}
Vector3d position = location.getPosition();
Vector3f rotation = location.getRotation();
Holder<EntityStore> itemEntityHolder = ItemComponent.generateItemDrop(store, itemStack, position, rotation, velocity.getX(), velocity.getY(), velocity.getZ());
if (itemEntityHolder == null) {
return new Pair<>(null, null);
}
;
ItemComponent itemComponent = itemEntityHolder.getComponent(ItemComponent.getComponentType());
if (itemComponent != null) {
itemComponent.setPickupDelay(pickupDelay);
}
store.addEntity(itemEntityHolder, AddReason.SPAWN);
return new Pair<>(EntityUtils.getEntity(itemEntityHolder), itemComponent);
}
}