| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.stream.Collectors;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.world.entity.ContainerUser;
|
| 7 | import net.minecraft.world.entity.Entity;
|
| 8 | import net.minecraft.world.entity.LivingEntity;
|
| 9 | import net.minecraft.world.entity.player.Player;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 13 | import net.minecraft.world.phys.AABB;
|
| 14 |
|
| 15 | public abstract class ContainerOpenersCounter {
|
| 16 | private static final int CHECK_TICK_DELAY = 5;
|
| 17 | private int openCount;
|
| 18 | private double maxInteractionRange;
|
| 19 |
|
| 20 | protected abstract void onOpen(final Level level, final BlockPos pos, final BlockState blockState);
|
| 21 |
|
| 22 | protected abstract void onClose(final Level level, final BlockPos pos, final BlockState blockState);
|
| 23 |
|
| 24 | protected abstract void openerCountChanged(final Level level, final BlockPos pos, final BlockState blockState, int previous, int current);
|
| 25 |
|
| 26 | public abstract boolean isOwnContainer(final Player player);
|
| 27 |
|
| 28 | public void incrementOpeners(final LivingEntity entity, final Level level, final BlockPos pos, final BlockState blockState, final double maxInteractionRange) {
|
| 29 | int previous = this.openCount++;
|
| 30 | if (previous == 0) {
|
| 31 | this.onOpen(level, pos, blockState);
|
| 32 | level.gameEvent(entity, GameEvent.CONTAINER_OPEN, pos);
|
| 33 | scheduleRecheck(level, pos, blockState);
|
| 34 | }
|
| 35 |
|
| 36 | this.openerCountChanged(level, pos, blockState, previous, this.openCount);
|
| 37 | this.maxInteractionRange = Math.max(maxInteractionRange, this.maxInteractionRange);
|
| 38 | }
|
| 39 |
|
| 40 | public void decrementOpeners(final LivingEntity entity, final Level level, final BlockPos pos, final BlockState blockState) {
|
| 41 | int previous = this.openCount--;
|
| 42 | if (this.openCount == 0) {
|
| 43 | this.onClose(level, pos, blockState);
|
| 44 | level.gameEvent(entity, GameEvent.CONTAINER_CLOSE, pos);
|
| 45 | this.maxInteractionRange = 0.0;
|
| 46 | }
|
| 47 |
|
| 48 | this.openerCountChanged(level, pos, blockState, previous, this.openCount);
|
| 49 | }
|
| 50 |
|
| 51 | public List<ContainerUser> getEntitiesWithContainerOpen(final Level level, final BlockPos pos) {
|
| 52 | double range = this.maxInteractionRange + 4.0;
|
| 53 | AABB searchBox = new AABB(pos).inflate(range);
|
| 54 | return level.getEntities((Entity)null, searchBox, entity -> this.hasContainerOpen(entity, pos))
|
| 55 | .stream()
|
| 56 | .map(entity -> (ContainerUser)entity)
|
| 57 | .collect(Collectors.toList());
|
| 58 | }
|
| 59 |
|
| 60 | private boolean hasContainerOpen(final Entity entity, final BlockPos blockPos) {
|
| 61 | return entity instanceof ContainerUser containerUser && !containerUser.getLivingEntity().isSpectator()
|
| 62 | ? containerUser.hasContainerOpen(this, blockPos)
|
| 63 | : false;
|
| 64 | }
|
| 65 |
|
| 66 | public void recheckOpeners(final Level level, final BlockPos pos, final BlockState blockState) {
|
| 67 | List<ContainerUser> containerUsers = this.getEntitiesWithContainerOpen(level, pos);
|
| 68 | this.maxInteractionRange = 0.0;
|
| 69 |
|
| 70 | for (ContainerUser containerUser : containerUsers) {
|
| 71 | this.maxInteractionRange = Math.max(containerUser.getContainerInteractionRange(), this.maxInteractionRange);
|
| 72 | }
|
| 73 |
|
| 74 | int openCount = containerUsers.size();
|
| 75 | int prevCount = this.openCount;
|
| 76 | if (prevCount != openCount) {
|
| 77 | boolean isOpen = openCount != 0;
|
| 78 | boolean wasOpen = prevCount != 0;
|
| 79 | if (isOpen && !wasOpen) {
|
| 80 | this.onOpen(level, pos, blockState);
|
| 81 | level.gameEvent(null, GameEvent.CONTAINER_OPEN, pos);
|
| 82 | } else if (!isOpen) {
|
| 83 | this.onClose(level, pos, blockState);
|
| 84 | level.gameEvent(null, GameEvent.CONTAINER_CLOSE, pos);
|
| 85 | }
|
| 86 |
|
| 87 | this.openCount = openCount;
|
| 88 | }
|
| 89 |
|
| 90 | this.openerCountChanged(level, pos, blockState, prevCount, openCount);
|
| 91 | if (openCount > 0) {
|
| 92 | scheduleRecheck(level, pos, blockState);
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public int getOpenerCount() {
|
| 97 | return this.openCount;
|
| 98 | }
|
| 99 |
|
| 100 | private static void scheduleRecheck(final Level level, final BlockPos blockPos, final BlockState blockState) {
|
| 101 | level.scheduleTick(blockPos, blockState.getBlock(), 5);
|
| 102 | }
|
| 103 | }
|
| 104 |
|