| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.stream.Stream;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.component.DataComponents;
|
| 11 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.nbt.CompoundTag;
|
| 14 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 17 | import net.minecraft.network.codec.StreamCodec;
|
| 18 | import net.minecraft.resources.DependantName;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.resources.ResourceKey;
|
| 21 | import net.minecraft.server.MinecraftServer;
|
| 22 | import net.minecraft.server.level.ServerLevel;
|
| 23 | import net.minecraft.tags.BlockTags;
|
| 24 | import net.minecraft.tags.TagKey;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.ProblemReporter;
|
| 27 | import net.minecraft.util.Util;
|
| 28 | import net.minecraft.util.datafix.fixes.References;
|
| 29 | import net.minecraft.world.Difficulty;
|
| 30 | import net.minecraft.world.entity.player.Player;
|
| 31 | import net.minecraft.world.flag.FeatureElement;
|
| 32 | import net.minecraft.world.flag.FeatureFlag;
|
| 33 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 34 | import net.minecraft.world.flag.FeatureFlags;
|
| 35 | import net.minecraft.world.item.ItemStack;
|
| 36 | import net.minecraft.world.item.component.TypedEntityData;
|
| 37 | import net.minecraft.world.level.Level;
|
| 38 | import net.minecraft.world.level.LevelReader;
|
| 39 | import net.minecraft.world.level.block.Block;
|
| 40 | import net.minecraft.world.level.block.Blocks;
|
| 41 | import net.minecraft.world.level.block.state.BlockState;
|
| 42 | import net.minecraft.world.level.entity.EntityTypeTest;
|
| 43 | import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
| 44 | import net.minecraft.world.level.storage.TagValueInput;
|
| 45 | import net.minecraft.world.level.storage.ValueInput;
|
| 46 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 47 | import net.minecraft.world.phys.AABB;
|
| 48 | import net.minecraft.world.phys.Vec3;
|
| 49 | import net.minecraft.world.phys.shapes.Shapes;
|
| 50 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 51 | import org.jspecify.annotations.Nullable;
|
| 52 | import org.slf4j.Logger;
|
| 53 |
|
| 54 | public class EntityType<T extends Entity> implements EntityTypeTest<Entity, T>, FeatureElement {
|
| 55 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 56 | private final Holder.Reference<EntityType<?>> builtInRegistryHolder = BuiltInRegistries.ENTITY_TYPE.createIntrusiveHolder(this);
|
| 57 | public static final Codec<EntityType<?>> CODEC = BuiltInRegistries.ENTITY_TYPE.byNameCodec();
|
| 58 | public static final StreamCodec<RegistryFriendlyByteBuf, EntityType<?>> STREAM_CODEC = ByteBufCodecs.registry(Registries.ENTITY_TYPE);
|
| 59 | public static final int NO_UPDATE_INTERVAL = Integer.MAX_VALUE;
|
| 60 | private final EntityType.EntityFactory<T> factory;
|
| 61 | private final MobCategory category;
|
| 62 | private final TagKey<Block> immuneTo;
|
| 63 | private final boolean serialize;
|
| 64 | private final boolean summon;
|
| 65 | private final boolean fireImmune;
|
| 66 | private final boolean canSpawnFarFromPlayer;
|
| 67 | private final int clientTrackingRange;
|
| 68 | private final int updateInterval;
|
| 69 | private final String descriptionId;
|
| 70 | private @Nullable Component description;
|
| 71 | private final Optional<ResourceKey<LootTable>> lootTable;
|
| 72 | private final EntityDimensions dimensions;
|
| 73 | private final float spawnDimensionsScale;
|
| 74 | private final FeatureFlagSet requiredFeatures;
|
| 75 | private final boolean allowedInPeaceful;
|
| 76 | private final boolean trackDeltas;
|
| 77 |
|
| 78 | public static Identifier getKey(final EntityType<?> type) {
|
| 79 | return BuiltInRegistries.ENTITY_TYPE.getKey(type);
|
| 80 | }
|
| 81 |
|
| 82 | public EntityType(
|
| 83 | final EntityType.EntityFactory<T> factory,
|
| 84 | final MobCategory category,
|
| 85 | final boolean serialize,
|
| 86 | final boolean summon,
|
| 87 | final boolean fireImmune,
|
| 88 | final boolean canSpawnFarFromPlayer,
|
| 89 | final TagKey<Block> immuneTo,
|
| 90 | final EntityDimensions dimensions,
|
| 91 | final float spawnDimensionsScale,
|
| 92 | final int clientTrackingRange,
|
| 93 | final int updateInterval,
|
| 94 | final String descriptionId,
|
| 95 | final Optional<ResourceKey<LootTable>> lootTable,
|
| 96 | final FeatureFlagSet requiredFeatures,
|
| 97 | final boolean allowedInPeaceful,
|
| 98 | final boolean trackDeltas
|
| 99 | ) {
|
| 100 | this.factory = factory;
|
| 101 | this.category = category;
|
| 102 | this.canSpawnFarFromPlayer = canSpawnFarFromPlayer;
|
| 103 | this.serialize = serialize;
|
| 104 | this.summon = summon;
|
| 105 | this.fireImmune = fireImmune;
|
| 106 | this.immuneTo = immuneTo;
|
| 107 | this.dimensions = dimensions;
|
| 108 | this.spawnDimensionsScale = spawnDimensionsScale;
|
| 109 | this.clientTrackingRange = clientTrackingRange;
|
| 110 | this.updateInterval = updateInterval;
|
| 111 | this.descriptionId = descriptionId;
|
| 112 | this.lootTable = lootTable;
|
| 113 | this.requiredFeatures = requiredFeatures;
|
| 114 | this.allowedInPeaceful = allowedInPeaceful;
|
| 115 | this.trackDeltas = trackDeltas;
|
| 116 | }
|
| 117 |
|
| 118 | public @Nullable T spawn(
|
| 119 | final ServerLevel level,
|
| 120 | final @Nullable ItemStack itemStack,
|
| 121 | final @Nullable LivingEntity user,
|
| 122 | final BlockPos spawnPos,
|
| 123 | final EntitySpawnReason spawnReason,
|
| 124 | final boolean tryMoveDown,
|
| 125 | final boolean movedUp
|
| 126 | ) {
|
| 127 | PostSpawnProcessor<T> postSpawnConfig;
|
| 128 | if (itemStack != null) {
|
| 129 | postSpawnConfig = createDefaultStackConfig(level, itemStack, user);
|
| 130 | } else {
|
| 131 | postSpawnConfig = PostSpawnProcessor.nop();
|
| 132 | }
|
| 133 |
|
| 134 | return this.spawn(level, postSpawnConfig, spawnPos, spawnReason, tryMoveDown, movedUp);
|
| 135 | }
|
| 136 |
|
| 137 | public static <T extends Entity> PostSpawnProcessor<T> createDefaultStackConfig(
|
| 138 | final Level level, final ItemStack itemStack, final @Nullable LivingEntity user
|
| 139 | ) {
|
| 140 | return appendDefaultStackConfig(PostSpawnProcessor.nop(), level, itemStack, user);
|
| 141 | }
|
| 142 |
|
| 143 | public static <T extends Entity> PostSpawnProcessor<T> appendDefaultStackConfig(
|
| 144 | final PostSpawnProcessor<T> initialConfig, final Level level, final ItemStack itemStack, final @Nullable LivingEntity user
|
| 145 | ) {
|
| 146 | return appendCustomEntityStackConfig(appendComponentsConfig(initialConfig, itemStack), level, itemStack, user);
|
| 147 | }
|
| 148 |
|
| 149 | public static <T extends Entity> PostSpawnProcessor<T> appendComponentsConfig(final PostSpawnProcessor<T> initialConfig, final ItemStack itemStack) {
|
| 150 | return initialConfig.andThen(entity -> entity.applyComponentsFromItemStack(itemStack));
|
| 151 | }
|
| 152 |
|
| 153 | public static <T extends Entity> PostSpawnProcessor<T> appendCustomEntityStackConfig(
|
| 154 | final PostSpawnProcessor<T> initialConfig, final Level level, final ItemStack itemStack, final @Nullable LivingEntity user
|
| 155 | ) {
|
| 156 | TypedEntityData<EntityType<?>> entityData = itemStack.get(DataComponents.ENTITY_DATA);
|
| 157 | return entityData != null ? initialConfig.andThen(entity -> updateCustomEntityTag(level, user, entity, entityData)) : initialConfig;
|
| 158 | }
|
| 159 |
|
| 160 | public @Nullable T spawn(final ServerLevel level, final BlockPos spawnPos, final EntitySpawnReason spawnReason) {
|
| 161 | return this.spawn(level, null, spawnPos, spawnReason, false, false);
|
| 162 | }
|
| 163 |
|
| 164 | public @Nullable T spawn(
|
| 165 | final ServerLevel level,
|
| 166 | final @Nullable PostSpawnProcessor<T> postSpawnConfig,
|
| 167 | final BlockPos spawnPos,
|
| 168 | final EntitySpawnReason spawnReason,
|
| 169 | final boolean tryMoveDown,
|
| 170 | final boolean movedUp
|
| 171 | ) {
|
| 172 | T entity = this.create(level, postSpawnConfig, spawnPos, spawnReason, tryMoveDown, movedUp);
|
| 173 | if (entity != null) {
|
| 174 | level.addFreshEntityWithPassengers(entity);
|
| 175 | if (entity instanceof Mob mob) {
|
| 176 | mob.playAmbientSound();
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | return entity;
|
| 181 | }
|
| 182 |
|
| 183 | public @Nullable T create(
|
| 184 | final ServerLevel level,
|
| 185 | final @Nullable PostSpawnProcessor<T> postSpawnConfig,
|
| 186 | final BlockPos spawnPos,
|
| 187 | final EntitySpawnReason spawnReason,
|
| 188 | final boolean tryMoveDown,
|
| 189 | final boolean movedUp
|
| 190 | ) {
|
| 191 | T entity = this.create(level, spawnReason);
|
| 192 | if (entity == null) {
|
| 193 | return null;
|
| 194 | }
|
| 195 |
|
| 196 | double yOff;
|
| 197 | if (tryMoveDown) {
|
| 198 | entity.setPos(spawnPos.getX() + 0.5, spawnPos.getY() + 1, spawnPos.getZ() + 0.5);
|
| 199 | yOff = getYOffset(level, spawnPos, movedUp, entity.getBoundingBox());
|
| 200 | } else {
|
| 201 | yOff = 0.0;
|
| 202 | }
|
| 203 |
|
| 204 | entity.snapTo(spawnPos.getX() + 0.5, spawnPos.getY() + yOff, spawnPos.getZ() + 0.5, Mth.wrapDegrees(level.getRandom().nextFloat() * 360.0F), 0.0F);
|
| 205 | if (entity instanceof Mob mob) {
|
| 206 | mob.yHeadRot = mob.getYRot();
|
| 207 | mob.yBodyRot = mob.getYRot();
|
| 208 | mob.finalizeSpawn(level, level.getCurrentDifficultyAt(mob.blockPosition()), spawnReason, null);
|
| 209 | }
|
| 210 |
|
| 211 | if (postSpawnConfig != null) {
|
| 212 | postSpawnConfig.apply(entity);
|
| 213 | }
|
| 214 |
|
| 215 | return entity;
|
| 216 | }
|
| 217 |
|
| 218 | protected static double getYOffset(final LevelReader level, final BlockPos spawnPos, final boolean movedUp, final AABB entityBox) {
|
| 219 | AABB aabb = new AABB(spawnPos);
|
| 220 | if (movedUp) {
|
| 221 | aabb = aabb.expandTowards(0.0, -1.0, 0.0);
|
| 222 | }
|
| 223 |
|
| 224 | Iterable<VoxelShape> shapes = level.getCollisions(null, aabb);
|
| 225 | return 1.0 + Shapes.collide(Direction.Axis.Y, entityBox, shapes, movedUp ? -2.0 : -1.0);
|
| 226 | }
|
| 227 |
|
| 228 | public static void updateCustomEntityTag(
|
| 229 | final Level level, final @Nullable LivingEntity user, final @Nullable Entity entity, final TypedEntityData<EntityType<?>> entityData
|
| 230 | ) {
|
| 231 | MinecraftServer server = level.getServer();
|
| 232 | if (server != null && entity != null) {
|
| 233 | if (entity.getType() == entityData.type()) {
|
| 234 | if (level.isClientSide() || !entity.getType().onlyOpCanSetNbt() || user instanceof Player player && server.getPlayerList().isOp(player.nameAndId())) {
|
| 235 | entityData.loadInto(entity);
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | public boolean canSerialize() {
|
| 242 | return this.serialize;
|
| 243 | }
|
| 244 |
|
| 245 | public boolean canSummon() {
|
| 246 | return this.summon;
|
| 247 | }
|
| 248 |
|
| 249 | public boolean fireImmune() {
|
| 250 | return this.fireImmune;
|
| 251 | }
|
| 252 |
|
| 253 | public boolean canSpawnFarFromPlayer() {
|
| 254 | return this.canSpawnFarFromPlayer;
|
| 255 | }
|
| 256 |
|
| 257 | public MobCategory getCategory() {
|
| 258 | return this.category;
|
| 259 | }
|
| 260 |
|
| 261 | public String getDescriptionId() {
|
| 262 | return this.descriptionId;
|
| 263 | }
|
| 264 |
|
| 265 | public Component getDescription() {
|
| 266 | if (this.description == null) {
|
| 267 | this.description = Component.translatable(this.getDescriptionId());
|
| 268 | }
|
| 269 |
|
| 270 | return this.description;
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public String toString() {
|
| 275 | return this.getDescriptionId();
|
| 276 | }
|
| 277 |
|
| 278 | public String toShortString() {
|
| 279 | int dot = this.getDescriptionId().lastIndexOf(46);
|
| 280 | return dot == -1 ? this.getDescriptionId() : this.getDescriptionId().substring(dot + 1);
|
| 281 | }
|
| 282 |
|
| 283 | public Optional<ResourceKey<LootTable>> getDefaultLootTable() {
|
| 284 | return this.lootTable;
|
| 285 | }
|
| 286 |
|
| 287 | public float getWidth() {
|
| 288 | return this.dimensions.width();
|
| 289 | }
|
| 290 |
|
| 291 | public float getHeight() {
|
| 292 | return this.dimensions.height();
|
| 293 | }
|
| 294 |
|
| 295 | @Override
|
| 296 | public FeatureFlagSet requiredFeatures() {
|
| 297 | return this.requiredFeatures;
|
| 298 | }
|
| 299 |
|
| 300 | public boolean canSpawn(final Level level) {
|
| 301 | return !this.isEnabled(level.enabledFeatures()) ? false : this.isAllowedInPeaceful() || level.getDifficulty() != Difficulty.PEACEFUL;
|
| 302 | }
|
| 303 |
|
| 304 | public @Nullable T create(final Level level, final EntitySpawnReason reason) {
|
| 305 | return this.create(level, new EntitySpawnRequest(reason, false));
|
| 306 | }
|
| 307 |
|
| 308 | public @Nullable T create(final Level level, final EntitySpawnRequest request) {
|
| 309 | return !request.ignoreChecks() && !this.canSpawn(level) ? null : this.factory.create(this, level);
|
| 310 | }
|
| 311 |
|
| 312 | public static Optional<Entity> create(final ValueInput input, final Level level, final EntitySpawnRequest request) {
|
| 313 | return Util.ifElse(
|
| 314 | by(input).map(type -> type.create(level, request)),
|
| 315 | entity -> entity.load(input),
|
| 316 | () -> LOGGER.warn("Skipping Entity with id {}", input.getStringOr("id", "[invalid]"))
|
| 317 | );
|
| 318 | }
|
| 319 |
|
| 320 | public static Optional<Entity> create(final EntityType<?> type, final ValueInput input, final Level level, final EntitySpawnReason reason) {
|
| 321 | Optional<Entity> entity = Optional.ofNullable(type.create(level, reason));
|
| 322 | entity.ifPresent(e -> e.load(input));
|
| 323 | return entity;
|
| 324 | }
|
| 325 |
|
| 326 | public AABB getSpawnAABB(final Vec3 at) {
|
| 327 | return this.getSpawnAABB(at.x, at.y, at.z);
|
| 328 | }
|
| 329 |
|
| 330 | public AABB getSpawnAABB(final double x, final double y, final double z) {
|
| 331 | float halfWidth = this.spawnDimensionsScale * this.getWidth() / 2.0F;
|
| 332 | float height = this.spawnDimensionsScale * this.getHeight();
|
| 333 | return new AABB(x - halfWidth, y, z - halfWidth, x + halfWidth, y + height, z + halfWidth);
|
| 334 | }
|
| 335 |
|
| 336 | public boolean isBlockDangerous(final BlockState state) {
|
| 337 | if (state.is(this.immuneTo)) {
|
| 338 | return false;
|
| 339 | } else {
|
| 340 | return !this.fireImmune && NodeEvaluator.isBurningBlock(state)
|
| 341 | ? true
|
| 342 | : state.is(Blocks.WITHER_ROSE) || state.is(Blocks.SWEET_BERRY_BUSH) || state.is(Blocks.CACTUS) || state.is(Blocks.POWDER_SNOW);
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | public EntityDimensions getDimensions() {
|
| 347 | return this.dimensions;
|
| 348 | }
|
| 349 |
|
| 350 | public static Optional<EntityType<?>> by(final ValueInput input) {
|
| 351 | return input.read("id", CODEC);
|
| 352 | }
|
| 353 |
|
| 354 | public static @Nullable Entity loadEntityRecursive(final CompoundTag tag, final Level level, final EntitySpawnRequest request, final EntityProcessor postLoad) {
|
| 355 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) {
|
| 356 | return loadEntityRecursive(TagValueInput.create(reporter, level.registryAccess(), tag), level, request, postLoad);
|
| 357 | }
|
| 358 | }
|
| 359 |
|
| 360 | public static @Nullable Entity loadEntityRecursive(
|
| 361 | final EntityType<?> type, final CompoundTag tag, final Level level, final EntitySpawnReason reason, final EntityProcessor postLoad
|
| 362 | ) {
|
| 363 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) {
|
| 364 | return loadEntityRecursive(type, TagValueInput.create(reporter, level.registryAccess(), tag), level, reason, postLoad);
|
| 365 | }
|
| 366 | }
|
| 367 |
|
| 368 | public static @Nullable Entity loadEntityRecursive(final ValueInput input, final Level level, final EntitySpawnReason reason, final EntityProcessor postLoad) {
|
| 369 | return loadEntityRecursive(input, level, new EntitySpawnRequest(reason, false), postLoad);
|
| 370 | }
|
| 371 |
|
| 372 | public static @Nullable Entity loadEntityRecursive(final ValueInput input, final Level level, final EntitySpawnRequest request, final EntityProcessor postLoad) {
|
| 373 | return loadStaticEntity(input, level, request)
|
| 374 | .map(postLoad::process)
|
| 375 | .map(entity -> loadPassengersRecursive(entity, input, level, request, postLoad))
|
| 376 | .orElse(null);
|
| 377 | }
|
| 378 |
|
| 379 | public static @Nullable Entity loadEntityRecursive(
|
| 380 | final EntityType<?> type, final ValueInput input, final Level level, final EntitySpawnReason reason, final EntityProcessor postLoad
|
| 381 | ) {
|
| 382 | return loadStaticEntity(type, input, level, reason)
|
| 383 | .map(postLoad::process)
|
| 384 | .map(entity -> loadPassengersRecursive(entity, input, level, new EntitySpawnRequest(reason, false), postLoad))
|
| 385 | .orElse(null);
|
| 386 | }
|
| 387 |
|
| 388 | private static Entity loadPassengersRecursive(
|
| 389 | final Entity entity, final ValueInput input, final Level level, final EntitySpawnRequest request, final EntityProcessor postLoad
|
| 390 | ) {
|
| 391 | for (ValueInput passengerTag : input.childrenListOrEmpty("Passengers")) {
|
| 392 | Entity passenger = loadEntityRecursive(passengerTag, level, request, postLoad);
|
| 393 | if (passenger != null) {
|
| 394 | passenger.startRiding(entity, true, false);
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | return entity;
|
| 399 | }
|
| 400 |
|
| 401 | public static Stream<Entity> loadEntitiesRecursive(final ValueInput.ValueInputList entities, final Level level, final EntitySpawnReason reason) {
|
| 402 | return entities.stream().mapMulti((tag, output) -> loadEntityRecursive(tag, level, reason, entity -> {
|
| 403 | output.accept(entity);
|
| 404 | return entity;
|
| 405 | }));
|
| 406 | }
|
| 407 |
|
| 408 | private static Optional<Entity> loadStaticEntity(final ValueInput input, final Level level, final EntitySpawnRequest request) {
|
| 409 | try {
|
| 410 | return create(input, level, request);
|
| 411 | } catch (RuntimeException e) {
|
| 412 | LOGGER.warn("Exception loading entity: ", e);
|
| 413 | return Optional.empty();
|
| 414 | }
|
| 415 | }
|
| 416 |
|
| 417 | private static Optional<Entity> loadStaticEntity(final EntityType<?> type, final ValueInput input, final Level level, final EntitySpawnReason reason) {
|
| 418 | try {
|
| 419 | return create(type, input, level, reason);
|
| 420 | } catch (RuntimeException e) {
|
| 421 | LOGGER.warn("Exception loading entity: ", e);
|
| 422 | return Optional.empty();
|
| 423 | }
|
| 424 | }
|
| 425 |
|
| 426 | public int clientTrackingRange() {
|
| 427 | return this.clientTrackingRange;
|
| 428 | }
|
| 429 |
|
| 430 | public int updateInterval() {
|
| 431 | return this.updateInterval;
|
| 432 | }
|
| 433 |
|
| 434 | public boolean hasUpdateInterval() {
|
| 435 | return this.updateInterval != Integer.MAX_VALUE;
|
| 436 | }
|
| 437 |
|
| 438 | public boolean trackDeltas() {
|
| 439 | return this.trackDeltas;
|
| 440 | }
|
| 441 |
|
| 442 | public @Nullable T tryCast(final Entity entity) {
|
| 443 | return (T)(entity.getType() == this ? entity : null);
|
| 444 | }
|
| 445 |
|
| 446 | @Override
|
| 447 | public Class<? extends Entity> getBaseClass() {
|
| 448 | return Entity.class;
|
| 449 | }
|
| 450 |
|
| 451 | @Deprecated
|
| 452 | public Holder.Reference<EntityType<?>> builtInRegistryHolder() {
|
| 453 | return this.builtInRegistryHolder;
|
| 454 | }
|
| 455 |
|
| 456 | public boolean isAllowedInPeaceful() {
|
| 457 | return this.allowedInPeaceful;
|
| 458 | }
|
| 459 |
|
| 460 | public boolean onlyOpCanSetNbt() {
|
| 461 | return EntityTypes.OP_ONLY_CUSTOM_DATA.contains(this);
|
| 462 | }
|
| 463 |
|
| 464 | public static class Builder<T extends Entity> implements net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityType.Builder<T> {
|
| 465 | private final EntityType.EntityFactory<T> factory;
|
| 466 | private final MobCategory category;
|
| 467 | private TagKey<Block> immuneTo = BlockTags.DEFAULT_IMMUNE_TO;
|
| 468 | private boolean serialize = true;
|
| 469 | private boolean summon = true;
|
| 470 | private boolean fireImmune;
|
| 471 | private boolean canSpawnFarFromPlayer;
|
| 472 | private int clientTrackingRange = 5;
|
| 473 | private int updateInterval = 3;
|
| 474 | private EntityDimensions dimensions = EntityDimensions.scalable(0.6F, 1.8F);
|
| 475 | private float spawnDimensionsScale = 1.0F;
|
| 476 | private EntityAttachments.Builder attachments = EntityAttachments.builder();
|
| 477 | private FeatureFlagSet requiredFeatures = FeatureFlags.VANILLA_SET;
|
| 478 | private DependantName<EntityType<?>, Optional<ResourceKey<LootTable>>> lootTable = id -> Optional.of(
|
| 479 | ResourceKey.create(Registries.LOOT_TABLE, id.identifier().withPrefix("entities/"))
|
| 480 | );
|
| 481 | private final DependantName<EntityType<?>, String> descriptionId = id -> Util.makeDescriptionId("entity", id.identifier());
|
| 482 | private boolean allowedInPeaceful = true;
|
| 483 | private boolean trackDeltas = true;
|
| 484 |
|
| 485 | private Builder(final EntityType.EntityFactory<T> factory, final MobCategory category) {
|
| 486 | this.factory = factory;
|
| 487 | this.category = category;
|
| 488 | this.canSpawnFarFromPlayer = category == MobCategory.CREATURE || category == MobCategory.MISC;
|
| 489 | }
|
| 490 |
|
| 491 | public static <T extends Entity> EntityType.Builder<T> of(final EntityType.EntityFactory<T> factory, final MobCategory category) {
|
| 492 | return new EntityType.Builder<>(factory, category);
|
| 493 | }
|
| 494 |
|
| 495 | public static <T extends Entity> EntityType.Builder<T> createNothing(final MobCategory category) {
|
| 496 | return new EntityType.Builder<>((t, l) -> null, category);
|
| 497 | }
|
| 498 |
|
| 499 | public EntityType.Builder<T> sized(final float width, final float height) {
|
| 500 | this.dimensions = EntityDimensions.scalable(width, height);
|
| 501 | return this;
|
| 502 | }
|
| 503 |
|
| 504 | public EntityType.Builder<T> spawnDimensionsScale(final float scale) {
|
| 505 | this.spawnDimensionsScale = scale;
|
| 506 | return this;
|
| 507 | }
|
| 508 |
|
| 509 | public EntityType.Builder<T> eyeHeight(final float eyeHeight) {
|
| 510 | this.dimensions = this.dimensions.withEyeHeight(eyeHeight);
|
| 511 | return this;
|
| 512 | }
|
| 513 |
|
| 514 | public EntityType.Builder<T> passengerAttachments(final float... offsetYs) {
|
| 515 | for (float offsetY : offsetYs) {
|
| 516 | this.attachments = this.attachments.attach(EntityAttachment.PASSENGER, 0.0F, offsetY, 0.0F);
|
| 517 | }
|
| 518 |
|
| 519 | return this;
|
| 520 | }
|
| 521 |
|
| 522 | public EntityType.Builder<T> passengerAttachments(final Vec3... points) {
|
| 523 | for (Vec3 point : points) {
|
| 524 | this.attachments = this.attachments.attach(EntityAttachment.PASSENGER, point);
|
| 525 | }
|
| 526 |
|
| 527 | return this;
|
| 528 | }
|
| 529 |
|
| 530 | public EntityType.Builder<T> vehicleAttachment(final Vec3 point) {
|
| 531 | return this.attach(EntityAttachment.VEHICLE, point);
|
| 532 | }
|
| 533 |
|
| 534 | public EntityType.Builder<T> ridingOffset(final float ridingOffset) {
|
| 535 | return this.attach(EntityAttachment.VEHICLE, 0.0F, -ridingOffset, 0.0F);
|
| 536 | }
|
| 537 |
|
| 538 | public EntityType.Builder<T> nameTagOffset(final float nameTagOffset) {
|
| 539 | return this.attach(EntityAttachment.NAME_TAG, 0.0F, nameTagOffset, 0.0F);
|
| 540 | }
|
| 541 |
|
| 542 | public EntityType.Builder<T> attach(final EntityAttachment attachment, final float x, final float y, final float z) {
|
| 543 | this.attachments = this.attachments.attach(attachment, x, y, z);
|
| 544 | return this;
|
| 545 | }
|
| 546 |
|
| 547 | public EntityType.Builder<T> attach(final EntityAttachment attachment, final Vec3 point) {
|
| 548 | this.attachments = this.attachments.attach(attachment, point);
|
| 549 | return this;
|
| 550 | }
|
| 551 |
|
| 552 | public EntityType.Builder<T> noSummon() {
|
| 553 | this.summon = false;
|
| 554 | return this;
|
| 555 | }
|
| 556 |
|
| 557 | public EntityType.Builder<T> noSave() {
|
| 558 | this.serialize = false;
|
| 559 | return this;
|
| 560 | }
|
| 561 |
|
| 562 | public EntityType.Builder<T> fireImmune() {
|
| 563 | this.fireImmune = true;
|
| 564 | return this;
|
| 565 | }
|
| 566 |
|
| 567 | public EntityType.Builder<T> immuneTo(final TagKey<Block> tag) {
|
| 568 | this.immuneTo = tag;
|
| 569 | return this;
|
| 570 | }
|
| 571 |
|
| 572 | public EntityType.Builder<T> canSpawnFarFromPlayer() {
|
| 573 | this.canSpawnFarFromPlayer = true;
|
| 574 | return this;
|
| 575 | }
|
| 576 |
|
| 577 | public EntityType.Builder<T> clientTrackingRange(final int clientChunkRange) {
|
| 578 | this.clientTrackingRange = clientChunkRange;
|
| 579 | return this;
|
| 580 | }
|
| 581 |
|
| 582 | public EntityType.Builder<T> updateInterval(final int updateInterval) {
|
| 583 | this.updateInterval = updateInterval;
|
| 584 | return this;
|
| 585 | }
|
| 586 |
|
| 587 | public EntityType.Builder<T> noUpdateInterval() {
|
| 588 | return this.updateInterval(Integer.MAX_VALUE);
|
| 589 | }
|
| 590 |
|
| 591 | public EntityType.Builder<T> requiredFeatures(final FeatureFlag... flags) {
|
| 592 | this.requiredFeatures = FeatureFlags.REGISTRY.subset(flags);
|
| 593 | return this;
|
| 594 | }
|
| 595 |
|
| 596 | public EntityType.Builder<T> noLootTable() {
|
| 597 | this.lootTable = DependantName.fixed(Optional.empty());
|
| 598 | return this;
|
| 599 | }
|
| 600 |
|
| 601 | public EntityType.Builder<T> notInPeaceful() {
|
| 602 | this.allowedInPeaceful = false;
|
| 603 | return this;
|
| 604 | }
|
| 605 |
|
| 606 | public EntityType.Builder<T> dontTrackDeltas() {
|
| 607 | this.trackDeltas = false;
|
| 608 | return this;
|
| 609 | }
|
| 610 |
|
| 611 | public EntityType<T> build(final ResourceKey<EntityType<?>> name) {
|
| 612 | if (this.serialize) {
|
| 613 | Util.fetchChoiceType(References.ENTITY_TREE, name.identifier().toString());
|
| 614 | }
|
| 615 |
|
| 616 | return new EntityType<>(
|
| 617 | this.factory,
|
| 618 | this.category,
|
| 619 | this.serialize,
|
| 620 | this.summon,
|
| 621 | this.fireImmune,
|
| 622 | this.canSpawnFarFromPlayer,
|
| 623 | this.immuneTo,
|
| 624 | this.dimensions.withAttachments(this.attachments),
|
| 625 | this.spawnDimensionsScale,
|
| 626 | this.clientTrackingRange,
|
| 627 | this.updateInterval,
|
| 628 | this.descriptionId.get(name),
|
| 629 | this.lootTable.get(name),
|
| 630 | this.requiredFeatures,
|
| 631 | this.allowedInPeaceful,
|
| 632 | this.trackDeltas
|
| 633 | );
|
| 634 | }
|
| 635 | }
|
| 636 |
|
| 637 | @FunctionalInterface
|
| 638 | public interface EntityFactory<T extends Entity> {
|
| 639 | @Nullable T create(final EntityType<T> entityType, final Level level);
|
| 640 | }
|
| 641 | }
|
| 642 |
|