| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Arrays;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.core.component.DataComponentGetter;
|
| 13 | import net.minecraft.core.component.DataComponentMap;
|
| 14 | import net.minecraft.core.component.DataComponents;
|
| 15 | import net.minecraft.nbt.CompoundTag;
|
| 16 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 17 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 18 | import net.minecraft.network.codec.StreamCodec;
|
| 19 | import net.minecraft.server.level.ServerLevel;
|
| 20 | import net.minecraft.sounds.SoundEvents;
|
| 21 | import net.minecraft.sounds.SoundSource;
|
| 22 | import net.minecraft.tags.BlockTags;
|
| 23 | import net.minecraft.tags.EntityTypeTags;
|
| 24 | import net.minecraft.util.ProblemReporter;
|
| 25 | import net.minecraft.util.RandomSource;
|
| 26 | import net.minecraft.util.VisibleForDebug;
|
| 27 | import net.minecraft.util.debug.DebugHiveInfo;
|
| 28 | import net.minecraft.util.debug.DebugSubscriptions;
|
| 29 | import net.minecraft.util.debug.DebugValueSource;
|
| 30 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 31 | import net.minecraft.world.entity.Entity;
|
| 32 | import net.minecraft.world.entity.EntityProcessor;
|
| 33 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 34 | import net.minecraft.world.entity.EntityType;
|
| 35 | import net.minecraft.world.entity.EntityTypes;
|
| 36 | import net.minecraft.world.entity.animal.bee.Bee;
|
| 37 | import net.minecraft.world.entity.player.Player;
|
| 38 | import net.minecraft.world.item.component.Bees;
|
| 39 | import net.minecraft.world.item.component.TypedEntityData;
|
| 40 | import net.minecraft.world.level.Level;
|
| 41 | import net.minecraft.world.level.block.BeehiveBlock;
|
| 42 | import net.minecraft.world.level.block.CampfireBlock;
|
| 43 | import net.minecraft.world.level.block.FireBlock;
|
| 44 | import net.minecraft.world.level.block.state.BlockState;
|
| 45 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 46 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 47 | import net.minecraft.world.level.storage.ValueInput;
|
| 48 | import net.minecraft.world.level.storage.ValueOutput;
|
| 49 | import org.jspecify.annotations.Nullable;
|
| 50 | import org.slf4j.Logger;
|
| 51 |
|
| 52 | public class BeehiveBlockEntity extends BlockEntity {
|
| 53 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 54 | private static final String TAG_FLOWER_POS = "flower_pos";
|
| 55 | private static final String BEES = "bees";
|
| 56 | private static final List<String> IGNORED_BEE_TAGS = Arrays.asList(
|
| 57 | "Air",
|
| 58 | "drop_chances",
|
| 59 | "equipment",
|
| 60 | "Brain",
|
| 61 | "CanPickUpLoot",
|
| 62 | "DeathTime",
|
| 63 | "fall_distance",
|
| 64 | "FallFlying",
|
| 65 | "Fire",
|
| 66 | "HurtTime",
|
| 67 | "LeftHanded",
|
| 68 | "Motion",
|
| 69 | "NoGravity",
|
| 70 | "OnGround",
|
| 71 | "PortalCooldown",
|
| 72 | "Pos",
|
| 73 | "Rotation",
|
| 74 | "sleeping_pos",
|
| 75 | "CannotEnterHiveTicks",
|
| 76 | "TicksSincePollination",
|
| 77 | "CropsGrownSincePollination",
|
| 78 | "hive_pos",
|
| 79 | "Passengers",
|
| 80 | "leash",
|
| 81 | "UUID"
|
| 82 | );
|
| 83 | public static final int MAX_OCCUPANTS = 3;
|
| 84 | private static final int MIN_TICKS_BEFORE_REENTERING_HIVE = 400;
|
| 85 | private static final int MIN_OCCUPATION_TICKS_NECTAR = 2400;
|
| 86 | public static final int MIN_OCCUPATION_TICKS_NECTARLESS = 600;
|
| 87 | private final List<BeehiveBlockEntity.BeeData> stored = Lists.newArrayList();
|
| 88 | private @Nullable BlockPos savedFlowerPos;
|
| 89 |
|
| 90 | public BeehiveBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 91 | super(BlockEntityTypes.BEEHIVE, worldPosition, blockState);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void setChanged() {
|
| 96 | if (this.isFireNearby()) {
|
| 97 | this.emptyAllLivingFromHive(null, this.level.getBlockState(this.getBlockPos()), BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY);
|
| 98 | }
|
| 99 |
|
| 100 | super.setChanged();
|
| 101 | }
|
| 102 |
|
| 103 | public boolean isFireNearby() {
|
| 104 | return this.level == null
|
| 105 | ? false
|
| 106 | : this.level
|
| 107 | .findBlocksIn(this.worldPosition.offset(-1, -1, -1), this.worldPosition.offset(1, 1, 1))
|
| 108 | .filterState(state -> state.getBlock() instanceof FireBlock)
|
| 109 | .anyMatched();
|
| 110 | }
|
| 111 |
|
| 112 | public boolean isEmpty() {
|
| 113 | return this.stored.isEmpty();
|
| 114 | }
|
| 115 |
|
| 116 | public boolean isFull() {
|
| 117 | return this.stored.size() == 3;
|
| 118 | }
|
| 119 |
|
| 120 | public void emptyAllLivingFromHive(final @Nullable Player player, final BlockState state, final BeehiveBlockEntity.BeeReleaseStatus releaseReason) {
|
| 121 | List<Entity> releasedFromHive = this.releaseAllOccupants(state, releaseReason);
|
| 122 | if (player != null) {
|
| 123 | for (Entity released : releasedFromHive) {
|
| 124 | if (released instanceof Bee bee && player.position().distanceToSqr(released.position()) <= 16.0) {
|
| 125 | if (!this.isSedated()) {
|
| 126 | bee.setTarget(player);
|
| 127 | } else {
|
| 128 | bee.setStayOutOfHiveCountdown(400);
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | private List<Entity> releaseAllOccupants(final BlockState state, final BeehiveBlockEntity.BeeReleaseStatus releaseStatus) {
|
| 136 | List<Entity> spawned = Lists.newArrayList();
|
| 137 | this.stored
|
| 138 | .removeIf(occupantEntry -> releaseOccupant(this.level, this.worldPosition, state, occupantEntry.toOccupant(), spawned, releaseStatus, this.savedFlowerPos));
|
| 139 | if (!spawned.isEmpty()) {
|
| 140 | super.setChanged();
|
| 141 | }
|
| 142 |
|
| 143 | return spawned;
|
| 144 | }
|
| 145 |
|
| 146 | @VisibleForDebug
|
| 147 | public int getOccupantCount() {
|
| 148 | return this.stored.size();
|
| 149 | }
|
| 150 |
|
| 151 | public static int getHoneyLevel(final BlockState blockState) {
|
| 152 | return blockState.getValue(BeehiveBlock.HONEY_LEVEL);
|
| 153 | }
|
| 154 |
|
| 155 | @VisibleForDebug
|
| 156 | public boolean isSedated() {
|
| 157 | return CampfireBlock.isSmokeyPos(this.level, this.getBlockPos());
|
| 158 | }
|
| 159 |
|
| 160 | public void addOccupant(final Bee bee) {
|
| 161 | if (this.stored.size() < 3) {
|
| 162 | bee.stopRiding();
|
| 163 | bee.ejectPassengers();
|
| 164 | bee.dropLeash();
|
| 165 | this.storeBee(BeehiveBlockEntity.Occupant.of(bee));
|
| 166 | if (this.level != null) {
|
| 167 | if (bee.hasSavedFlowerPos() && (!this.hasSavedFlowerPos() || this.level.getRandom().nextBoolean())) {
|
| 168 | this.savedFlowerPos = bee.getSavedFlowerPos();
|
| 169 | }
|
| 170 |
|
| 171 | BlockPos blockPos = this.getBlockPos();
|
| 172 | this.level.playSound(null, blockPos.getX(), blockPos.getY(), blockPos.getZ(), SoundEvents.BEEHIVE_ENTER, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 173 | this.level.gameEvent(GameEvent.BLOCK_CHANGE, blockPos, GameEvent.Context.of(bee, this.getBlockState()));
|
| 174 | }
|
| 175 |
|
| 176 | bee.discard();
|
| 177 | super.setChanged();
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | public void storeBee(final BeehiveBlockEntity.Occupant occupant) {
|
| 182 | this.stored.add(new BeehiveBlockEntity.BeeData(occupant));
|
| 183 | }
|
| 184 |
|
| 185 | private static boolean releaseOccupant(
|
| 186 | final Level level,
|
| 187 | final BlockPos blockPos,
|
| 188 | final BlockState state,
|
| 189 | final BeehiveBlockEntity.Occupant beeData,
|
| 190 | final @Nullable List<Entity> spawned,
|
| 191 | final BeehiveBlockEntity.BeeReleaseStatus releaseStatus,
|
| 192 | final @Nullable BlockPos savedFlowerPos
|
| 193 | ) {
|
| 194 | if (level.environmentAttributes().getValue(EnvironmentAttributes.BEES_STAY_IN_HIVE, blockPos)
|
| 195 | && releaseStatus != BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY) {
|
| 196 | return false;
|
| 197 | }
|
| 198 |
|
| 199 | Direction facing = state.getValue(BeehiveBlock.FACING);
|
| 200 | BlockPos facingPos = blockPos.relative(facing);
|
| 201 | boolean frontBlocked = !level.getBlockState(facingPos).getCollisionShape(level, facingPos).isEmpty();
|
| 202 | if (frontBlocked && releaseStatus != BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY) {
|
| 203 | return false;
|
| 204 | }
|
| 205 |
|
| 206 | Entity entity = beeData.createEntity(level, blockPos);
|
| 207 | if (entity != null) {
|
| 208 | if (entity instanceof Bee bee) {
|
| 209 | RandomSource random = level.getRandom();
|
| 210 | if (savedFlowerPos != null && !bee.hasSavedFlowerPos() && random.nextFloat() < 0.9F) {
|
| 211 | bee.setSavedFlowerPos(savedFlowerPos);
|
| 212 | }
|
| 213 |
|
| 214 | if (releaseStatus == BeehiveBlockEntity.BeeReleaseStatus.HONEY_DELIVERED) {
|
| 215 | bee.dropOffNectar();
|
| 216 | if (state.is(BlockTags.BEEHIVES, s -> s.hasProperty(BeehiveBlock.HONEY_LEVEL))) {
|
| 217 | int honeyLevel = getHoneyLevel(state);
|
| 218 | if (honeyLevel < 5) {
|
| 219 | int levelIncrease = random.nextInt(100) == 0 ? 2 : 1;
|
| 220 | if (honeyLevel + levelIncrease > 5) {
|
| 221 | levelIncrease--;
|
| 222 | }
|
| 223 |
|
| 224 | level.setBlockAndUpdate(blockPos, state.setValue(BeehiveBlock.HONEY_LEVEL, honeyLevel + levelIncrease));
|
| 225 | }
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | if (spawned != null) {
|
| 230 | spawned.add(bee);
|
| 231 | }
|
| 232 |
|
| 233 | float bbWidth = entity.getBbWidth();
|
| 234 | double delta = frontBlocked ? 0.0 : 0.55 + bbWidth / 2.0F;
|
| 235 | double spawnX = blockPos.getX() + 0.5 + delta * facing.getStepX();
|
| 236 | double spawnY = blockPos.getY() + 0.5 - entity.getBbHeight() / 2.0F;
|
| 237 | double spawnZ = blockPos.getZ() + 0.5 + delta * facing.getStepZ();
|
| 238 | entity.snapTo(spawnX, spawnY, spawnZ, entity.getYRot(), entity.getXRot());
|
| 239 | }
|
| 240 |
|
| 241 | level.playSound(null, blockPos, SoundEvents.BEEHIVE_EXIT, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 242 | level.gameEvent(GameEvent.BLOCK_CHANGE, blockPos, GameEvent.Context.of(entity, level.getBlockState(blockPos)));
|
| 243 | return level.addFreshEntity(entity);
|
| 244 | } else {
|
| 245 | return false;
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | private boolean hasSavedFlowerPos() {
|
| 250 | return this.savedFlowerPos != null;
|
| 251 | }
|
| 252 |
|
| 253 | private static void tickOccupants(
|
| 254 | final Level level, final BlockPos pos, final BlockState state, final List<BeehiveBlockEntity.BeeData> stored, final @Nullable BlockPos savedFlowerPos
|
| 255 | ) {
|
| 256 | boolean changed = false;
|
| 257 | Iterator<BeehiveBlockEntity.BeeData> iterator = stored.iterator();
|
| 258 |
|
| 259 | while (iterator.hasNext()) {
|
| 260 | BeehiveBlockEntity.BeeData data = iterator.next();
|
| 261 | if (data.tick()) {
|
| 262 | BeehiveBlockEntity.BeeReleaseStatus releaseStatus = data.hasNectar()
|
| 263 | ? BeehiveBlockEntity.BeeReleaseStatus.HONEY_DELIVERED
|
| 264 | : BeehiveBlockEntity.BeeReleaseStatus.BEE_RELEASED;
|
| 265 | if (releaseOccupant(level, pos, state, data.toOccupant(), null, releaseStatus, savedFlowerPos)) {
|
| 266 | changed = true;
|
| 267 | iterator.remove();
|
| 268 | }
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | if (changed) {
|
| 273 | setChanged(level, pos, state);
|
| 274 | }
|
| 275 | }
|
| 276 |
|
| 277 | public static void serverTick(final Level level, final BlockPos blockPos, final BlockState state, final BeehiveBlockEntity entity) {
|
| 278 | tickOccupants(level, blockPos, state, entity.stored, entity.savedFlowerPos);
|
| 279 | if (!entity.stored.isEmpty() && level.getRandom().nextDouble() < 0.005) {
|
| 280 | double x = blockPos.getX() + 0.5;
|
| 281 | double y = blockPos.getY();
|
| 282 | double z = blockPos.getZ() + 0.5;
|
| 283 | level.playSound(null, x, y, z, SoundEvents.BEEHIVE_WORK, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | @Override
|
| 288 | protected void loadAdditional(final ValueInput input) {
|
| 289 | super.loadAdditional(input);
|
| 290 | this.stored.clear();
|
| 291 | input.read("bees", BeehiveBlockEntity.Occupant.LIST_CODEC).orElse(List.of()).forEach(this::storeBee);
|
| 292 | this.savedFlowerPos = input.read("flower_pos", BlockPos.CODEC).orElse(null);
|
| 293 | }
|
| 294 |
|
| 295 | @Override
|
| 296 | protected void saveAdditional(final ValueOutput output) {
|
| 297 | super.saveAdditional(output);
|
| 298 | output.store("bees", BeehiveBlockEntity.Occupant.LIST_CODEC, this.getBees());
|
| 299 | output.storeNullable("flower_pos", BlockPos.CODEC, this.savedFlowerPos);
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 304 | super.applyImplicitComponents(components);
|
| 305 | this.stored.clear();
|
| 306 | List<BeehiveBlockEntity.Occupant> bees = components.getOrDefault(DataComponents.BEES, Bees.EMPTY).bees();
|
| 307 | bees.forEach(this::storeBee);
|
| 308 | }
|
| 309 |
|
| 310 | @Override
|
| 311 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 312 | super.collectImplicitComponents(components);
|
| 313 | components.set(DataComponents.BEES, new Bees(this.getBees()));
|
| 314 | }
|
| 315 |
|
| 316 | @Override
|
| 317 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 318 | super.removeComponentsFromTag(output);
|
| 319 | output.discard("bees");
|
| 320 | }
|
| 321 |
|
| 322 | private List<BeehiveBlockEntity.Occupant> getBees() {
|
| 323 | return this.stored.stream().map(BeehiveBlockEntity.BeeData::toOccupant).toList();
|
| 324 | }
|
| 325 |
|
| 326 | @Override
|
| 327 | public void registerDebugValues(final ServerLevel level, final DebugValueSource.Registration registration) {
|
| 328 | registration.register(DebugSubscriptions.BEE_HIVES, () -> DebugHiveInfo.pack(this));
|
| 329 | }
|
| 330 |
|
| 331 | private static class BeeData {
|
| 332 | private final BeehiveBlockEntity.Occupant occupant;
|
| 333 | private int ticksInHive;
|
| 334 |
|
| 335 | private BeeData(final BeehiveBlockEntity.Occupant occupant) {
|
| 336 | this.occupant = occupant;
|
| 337 | this.ticksInHive = occupant.ticksInHive();
|
| 338 | }
|
| 339 |
|
| 340 | public boolean tick() {
|
| 341 | return this.ticksInHive++ > this.occupant.minTicksInHive;
|
| 342 | }
|
| 343 |
|
| 344 | public BeehiveBlockEntity.Occupant toOccupant() {
|
| 345 | return new BeehiveBlockEntity.Occupant(this.occupant.entityData, this.ticksInHive, this.occupant.minTicksInHive);
|
| 346 | }
|
| 347 |
|
| 348 | public boolean hasNectar() {
|
| 349 | return this.occupant.entityData.getUnsafe().getBooleanOr("HasNectar", false);
|
| 350 | }
|
| 351 | }
|
| 352 |
|
| 353 | public enum BeeReleaseStatus {
|
| 354 | HONEY_DELIVERED,
|
| 355 | BEE_RELEASED,
|
| 356 | EMERGENCY;
|
| 357 | }
|
| 358 |
|
| 359 | public record Occupant(TypedEntityData<EntityType<?>> entityData, int ticksInHive, int minTicksInHive) {
|
| 360 | public static final Codec<BeehiveBlockEntity.Occupant> CODEC = RecordCodecBuilder.create(
|
| 361 | i -> i.group(
|
| 362 | TypedEntityData.codec(EntityType.CODEC).fieldOf("entity_data").forGetter(BeehiveBlockEntity.Occupant::entityData),
|
| 363 | Codec.INT.fieldOf("ticks_in_hive").forGetter(BeehiveBlockEntity.Occupant::ticksInHive),
|
| 364 | Codec.INT.fieldOf("min_ticks_in_hive").forGetter(BeehiveBlockEntity.Occupant::minTicksInHive)
|
| 365 | )
|
| 366 | .apply(i, BeehiveBlockEntity.Occupant::new)
|
| 367 | );
|
| 368 | public static final Codec<List<BeehiveBlockEntity.Occupant>> LIST_CODEC = CODEC.listOf();
|
| 369 | public static final StreamCodec<RegistryFriendlyByteBuf, BeehiveBlockEntity.Occupant> STREAM_CODEC = StreamCodec.composite(
|
| 370 | TypedEntityData.streamCodec(EntityType.STREAM_CODEC),
|
| 371 | BeehiveBlockEntity.Occupant::entityData,
|
| 372 | ByteBufCodecs.VAR_INT,
|
| 373 | BeehiveBlockEntity.Occupant::ticksInHive,
|
| 374 | ByteBufCodecs.VAR_INT,
|
| 375 | BeehiveBlockEntity.Occupant::minTicksInHive,
|
| 376 | BeehiveBlockEntity.Occupant::new
|
| 377 | );
|
| 378 |
|
| 379 | public static BeehiveBlockEntity.Occupant of(final Entity entity) {
|
| 380 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(entity.problemPath(), BeehiveBlockEntity.LOGGER)) {
|
| 381 | TagValueOutput output = TagValueOutput.createWithContext(reporter, entity.registryAccess());
|
| 382 | entity.save(output);
|
| 383 | BeehiveBlockEntity.IGNORED_BEE_TAGS.forEach(output::discard);
|
| 384 | CompoundTag entityTag = output.buildResult();
|
| 385 | boolean hasNectar = entityTag.getBooleanOr("HasNectar", false);
|
| 386 | return new BeehiveBlockEntity.Occupant(TypedEntityData.of(entity.getType(), entityTag), 0, hasNectar ? 2400 : 600);
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | public static BeehiveBlockEntity.Occupant create(final int ticksInHive) {
|
| 391 | return new BeehiveBlockEntity.Occupant(TypedEntityData.of(EntityTypes.BEE, new CompoundTag()), ticksInHive, 600);
|
| 392 | }
|
| 393 |
|
| 394 | public @Nullable Entity createEntity(final Level level, final BlockPos hivePos) {
|
| 395 | CompoundTag entityTag = this.entityData.copyTagWithoutId();
|
| 396 | BeehiveBlockEntity.IGNORED_BEE_TAGS.forEach(entityTag::remove);
|
| 397 | Entity entity = EntityType.loadEntityRecursive(this.entityData.type(), entityTag, level, EntitySpawnReason.LOAD, EntityProcessor.NOP);
|
| 398 | if (entity != null && entity.is(EntityTypeTags.BEEHIVE_INHABITORS)) {
|
| 399 | entity.setNoGravity(true);
|
| 400 | if (entity instanceof Bee bee) {
|
| 401 | bee.setHivePos(hivePos);
|
| 402 | setBeeReleaseData(this.ticksInHive, bee);
|
| 403 | }
|
| 404 |
|
| 405 | return entity;
|
| 406 | } else {
|
| 407 | return null;
|
| 408 | }
|
| 409 | }
|
| 410 |
|
| 411 | private static void setBeeReleaseData(final int ticksInHive, final Bee bee) {
|
| 412 | updateBeeAge(ticksInHive, bee);
|
| 413 | bee.setInLoveTime(Math.max(0, bee.getInLoveTime() - ticksInHive));
|
| 414 | }
|
| 415 |
|
| 416 | private static void updateBeeAge(final int ticksInHive, final Bee bee) {
|
| 417 | if (!bee.isAgeLocked()) {
|
| 418 | int age = bee.getAge();
|
| 419 | if (age < 0) {
|
| 420 | bee.setAge(Math.min(0, age + ticksInHive));
|
| 421 | } else if (age > 0) {
|
| 422 | bee.setAge(Math.max(0, age - ticksInHive));
|
| 423 | }
|
| 424 | }
|
| 425 | }
|
| 426 | }
|
| 427 | }
|
| 428 |
|