|
10 | 10 | import com.hypixel.hytale.math.vector.Location; |
11 | 11 | import com.hypixel.hytale.math.vector.Vector3d; |
12 | 12 | import com.hypixel.hytale.math.vector.Vector3f; |
| 13 | +import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType; |
| 14 | +import com.hypixel.hytale.server.core.asset.type.item.config.Item; |
| 15 | +import com.hypixel.hytale.server.core.asset.type.model.config.Model; |
| 16 | +import com.hypixel.hytale.server.core.asset.type.model.config.ModelAsset; |
13 | 17 | import com.hypixel.hytale.server.core.entity.Entity; |
14 | 18 | import com.hypixel.hytale.server.core.entity.LivingEntity; |
15 | 19 | import com.hypixel.hytale.server.core.entity.UUIDComponent; |
| 20 | +import com.hypixel.hytale.server.core.entity.entities.BlockEntity; |
16 | 21 | import com.hypixel.hytale.server.core.entity.movement.MovementStatesComponent; |
17 | 22 | import com.hypixel.hytale.server.core.entity.nameplate.Nameplate; |
18 | 23 | import com.hypixel.hytale.server.core.inventory.ItemStack; |
| 24 | +import com.hypixel.hytale.server.core.modules.entity.component.EntityScaleComponent; |
| 25 | +import com.hypixel.hytale.server.core.modules.entity.component.HeadRotation; |
| 26 | +import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent; |
| 27 | +import com.hypixel.hytale.server.core.modules.entity.component.PersistentModel; |
| 28 | +import com.hypixel.hytale.server.core.modules.entity.component.PropComponent; |
| 29 | +import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent; |
19 | 30 | import com.hypixel.hytale.server.core.modules.entity.item.ItemComponent; |
| 31 | +import com.hypixel.hytale.server.core.modules.entity.item.PreventItemMerging; |
| 32 | +import com.hypixel.hytale.server.core.modules.entity.item.PreventPickup; |
| 33 | +import com.hypixel.hytale.server.core.modules.entity.tracker.NetworkId; |
20 | 34 | import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap; |
21 | 35 | import com.hypixel.hytale.server.core.modules.entitystats.EntityStatsModule; |
| 36 | +import com.hypixel.hytale.server.core.universe.Universe; |
22 | 37 | import com.hypixel.hytale.server.core.universe.world.World; |
23 | 38 | import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; |
24 | 39 | import com.hypixel.hytale.server.npc.entities.NPCEntity; |
|
29 | 44 | import org.jetbrains.annotations.NotNull; |
30 | 45 | import org.jetbrains.annotations.Nullable; |
31 | 46 |
|
| 47 | +import javax.annotation.Nonnull; |
32 | 48 | import java.util.UUID; |
33 | 49 |
|
34 | 50 | /** |
@@ -227,7 +243,7 @@ public static <ECS, T extends Component<ECS>> void tryRemoveComponent(Entity ent |
227 | 243 |
|
228 | 244 | @SuppressWarnings({"DataFlowIssue"}) |
229 | 245 | public static @NotNull Pair<Ref<EntityStore>, ItemComponent> dropItem(Store<EntityStore> store, ItemStack itemStack, |
230 | | - Location location, Vector3f velocity, float pickupDelay) { |
| 246 | + Location location, Vector3f velocity, float pickupDelay) { |
231 | 247 | if (itemStack.isEmpty() || !itemStack.isValid()) { |
232 | 248 | return new Pair<>(null, null); |
233 | 249 | } |
@@ -324,4 +340,119 @@ public static void clearMarkedEntity(NPCEntity npcEntity, @Nullable Entity targe |
324 | 340 | } |
325 | 341 | } |
326 | 342 |
|
| 343 | + private static String getItemModelId(@Nonnull Item item) { |
| 344 | + String modelId = item.getModel(); |
| 345 | + |
| 346 | + if (modelId == null && item.hasBlockType()) { |
| 347 | + BlockType blockType = BlockType.getAssetMap().getAsset(item.getId()); |
| 348 | + |
| 349 | + if (blockType != null && blockType.getCustomModel() != null) { |
| 350 | + modelId = blockType.getCustomModel(); |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + return modelId; |
| 355 | + } |
| 356 | + |
| 357 | + private static Model getItemModel(@Nonnull Item item) { |
| 358 | + String modelId = getItemModelId(item); |
| 359 | + |
| 360 | + if (modelId == null) { |
| 361 | + return null; |
| 362 | + } else { |
| 363 | + ModelAsset modelAsset = ModelAsset.getAssetMap().getAsset(modelId); |
| 364 | + |
| 365 | + return modelAsset != null ? Model.createStaticScaledModel(modelAsset, 1.0f) : null; |
| 366 | + } |
| 367 | + } |
| 368 | + |
| 369 | + public static Ref<EntityStore> spawnModel(@NotNull Object object, @NotNull Location location) { |
| 370 | + return switch (object) { |
| 371 | + case Item item -> spawnItem(item, location); |
| 372 | + case BlockType blockType -> spawnBlock(null, blockType, location); |
| 373 | + case ModelAsset modelAsset -> spawnModel(null, Model.createStaticScaledModel(modelAsset, 1.0f), location); |
| 374 | + default -> null; |
| 375 | + }; |
| 376 | + } |
| 377 | + |
| 378 | + public static Ref<EntityStore> spawnModel(@Nullable Item item, @NotNull Model model, @NotNull Location location) { |
| 379 | + World world = Universe.get().getWorld(location.getWorld()); |
| 380 | + if (world == null) return null; |
| 381 | + |
| 382 | + Store<EntityStore> store = world.getEntityStore().getStore(); |
| 383 | + Holder<EntityStore> holder = store.getRegistry().newHolder(); |
| 384 | + |
| 385 | + holder.addComponent(NetworkId.getComponentType(), new NetworkId(store.getExternalData().takeNextNetworkId())); |
| 386 | + holder.addComponent(TransformComponent.getComponentType(), new TransformComponent(location.getPosition(), location.getRotation())); |
| 387 | + holder.addComponent(ModelComponent.getComponentType(), new ModelComponent(model)); |
| 388 | + holder.addComponent(PersistentModel.getComponentType(), |
| 389 | + new PersistentModel( |
| 390 | + new Model.ModelReference(model.getModelAssetId(), 1.0f, null, true))); |
| 391 | + if (item != null) { |
| 392 | + ItemStack itemStack = new ItemStack(item.getId(), 1); |
| 393 | + itemStack.setOverrideDroppedItemAnimation(true); |
| 394 | + holder.addComponent(ItemComponent.getComponentType(), new ItemComponent(itemStack)); |
| 395 | + } |
| 396 | + holder.addComponent(EntityScaleComponent.getComponentType(), new EntityScaleComponent(1.0f)); |
| 397 | + holder.addComponent(PreventPickup.getComponentType(), PreventPickup.INSTANCE); |
| 398 | + holder.addComponent(PreventItemMerging.getComponentType(), PreventItemMerging.INSTANCE); |
| 399 | + holder.addComponent(HeadRotation.getComponentType(), new HeadRotation(location.getRotation())); |
| 400 | + holder.addComponent(PropComponent.getComponentType(), PropComponent.get()); |
| 401 | + holder.ensureComponent(UUIDComponent.getComponentType()); |
| 402 | + return store.addEntity(holder, AddReason.SPAWN); |
| 403 | + } |
| 404 | + |
| 405 | + public static Ref<EntityStore> spawnItem(@NotNull Item item, @NotNull Location location) { |
| 406 | + Model model = getItemModel(item); |
| 407 | + if (model != null) { |
| 408 | + return spawnModel(item, model, location); |
| 409 | + } |
| 410 | + if (item.hasBlockType()) { |
| 411 | + BlockType blockType = BlockType.getAssetMap().getAsset(item.getId()); |
| 412 | + if (blockType != null) { |
| 413 | + return spawnBlock(item, blockType, location); |
| 414 | + } |
| 415 | + } |
| 416 | + World world = Universe.get().getWorld(location.getWorld()); |
| 417 | + if (world == null) return null; |
| 418 | + |
| 419 | + Store<EntityStore> store = world.getEntityStore().getStore(); |
| 420 | + Holder<EntityStore> holder = store.getRegistry().newHolder(); |
| 421 | + |
| 422 | + holder.addComponent(NetworkId.getComponentType(), new NetworkId(store.getExternalData().takeNextNetworkId())); |
| 423 | + holder.addComponent(TransformComponent.getComponentType(), new TransformComponent(location.getPosition(), location.getRotation())); |
| 424 | + ItemStack itemStack = new ItemStack(item.getId(), 1); |
| 425 | + itemStack.setOverrideDroppedItemAnimation(true); |
| 426 | + holder.addComponent(ItemComponent.getComponentType(), new ItemComponent(itemStack)); |
| 427 | + holder.addComponent(EntityScaleComponent.getComponentType(), new EntityScaleComponent(1.0f)); |
| 428 | + holder.addComponent(PreventPickup.getComponentType(), PreventPickup.INSTANCE); |
| 429 | + holder.addComponent(PreventItemMerging.getComponentType(), PreventItemMerging.INSTANCE); |
| 430 | + holder.addComponent(HeadRotation.getComponentType(), new HeadRotation(location.getRotation())); |
| 431 | + holder.addComponent(PropComponent.getComponentType(), PropComponent.get()); |
| 432 | + return store.addEntity(holder, AddReason.SPAWN); |
| 433 | + } |
| 434 | + |
| 435 | + public static Ref<EntityStore> spawnBlock(@Nullable Item item, @NotNull BlockType blockType, @NotNull Location location) { |
| 436 | + World world = Universe.get().getWorld(location.getWorld()); |
| 437 | + if (world == null) return null; |
| 438 | + |
| 439 | + Store<EntityStore> store = world.getEntityStore().getStore(); |
| 440 | + Holder<EntityStore> holder = store.getRegistry().newHolder(); |
| 441 | + |
| 442 | + holder.addComponent(BlockEntity.getComponentType(), new BlockEntity(blockType.getId())); |
| 443 | + holder.addComponent(TransformComponent.getComponentType(), new TransformComponent(location.getPosition(), location.getRotation())); |
| 444 | + holder.addComponent(EntityScaleComponent.getComponentType(), new EntityScaleComponent(1.0F)); |
| 445 | + if (item != null) { |
| 446 | + ItemStack itemStack = new ItemStack(item.getId(), 1); |
| 447 | + itemStack.setOverrideDroppedItemAnimation(true); |
| 448 | + holder.addComponent(ItemComponent.getComponentType(), new ItemComponent(itemStack)); |
| 449 | + } |
| 450 | + holder.addComponent(PreventPickup.getComponentType(), PreventPickup.INSTANCE); |
| 451 | + holder.addComponent(PreventItemMerging.getComponentType(), PreventItemMerging.INSTANCE); |
| 452 | + holder.addComponent(HeadRotation.getComponentType(), new HeadRotation(location.getRotation())); |
| 453 | + holder.addComponent(PropComponent.getComponentType(), PropComponent.get()); |
| 454 | + holder.ensureComponent(UUIDComponent.getComponentType()); |
| 455 | + return store.addEntity(holder, AddReason.SPAWN); |
| 456 | + } |
| 457 | + |
327 | 458 | } |
0 commit comments