| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.IdentityHashMap;
|
| 5 | import java.util.Map;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.Position;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.core.dispenser.BlockSource;
|
| 11 | import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
|
| 12 | import net.minecraft.core.dispenser.DispenseItemBehavior;
|
| 13 | import net.minecraft.core.dispenser.EquipmentDispenseItemBehavior;
|
| 14 | import net.minecraft.core.dispenser.ProjectileDispenseBehavior;
|
| 15 | import net.minecraft.core.dispenser.SpawnEggItemBehavior;
|
| 16 | import net.minecraft.core.dispenser.SulfurCubeBlockDispenseItemBehavior;
|
| 17 | import net.minecraft.server.level.ServerLevel;
|
| 18 | import net.minecraft.stats.Stats;
|
| 19 | import net.minecraft.tags.ItemTags;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.world.Containers;
|
| 22 | import net.minecraft.world.InteractionResult;
|
| 23 | import net.minecraft.world.entity.player.Player;
|
| 24 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 25 | import net.minecraft.world.item.Item;
|
| 26 | import net.minecraft.world.item.ItemStack;
|
| 27 | import net.minecraft.world.item.SpawnEggItem;
|
| 28 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 29 | import net.minecraft.world.level.ItemLike;
|
| 30 | import net.minecraft.world.level.Level;
|
| 31 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 32 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 33 | import net.minecraft.world.level.block.entity.DispenserBlockEntity;
|
| 34 | import net.minecraft.world.level.block.entity.DropperBlockEntity;
|
| 35 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 36 | import net.minecraft.world.level.block.state.BlockState;
|
| 37 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 38 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 39 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 40 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 41 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 42 | import net.minecraft.world.level.redstone.Orientation;
|
| 43 | import net.minecraft.world.phys.BlockHitResult;
|
| 44 | import net.minecraft.world.phys.Vec3;
|
| 45 | import org.jspecify.annotations.Nullable;
|
| 46 | import org.slf4j.Logger;
|
| 47 |
|
| 48 | /**
|
| 49 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 50 | */
|
| 51 | public class DispenserBlock extends BaseEntityBlock {
|
| 52 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 53 | public static final EnumProperty<Direction> FACING = DirectionalBlock.FACING;
|
| 54 | public static final BooleanProperty TRIGGERED = BlockStateProperties.TRIGGERED;
|
| 55 | private static final DefaultDispenseItemBehavior DEFAULT_BEHAVIOR = new DefaultDispenseItemBehavior();
|
| 56 | public static final Map<Item, DispenseItemBehavior> DISPENSER_REGISTRY = new IdentityHashMap<>();
|
| 57 | private static final int TRIGGER_DURATION = 4;
|
| 58 |
|
| 59 | public static void registerBehavior(final ItemLike item, final DispenseItemBehavior behavior) {
|
| 60 | DISPENSER_REGISTRY.put(item.asItem(), behavior);
|
| 61 | }
|
| 62 |
|
| 63 | public static void registerProjectileBehavior(final ItemLike item) {
|
| 64 | DISPENSER_REGISTRY.put(item.asItem(), new ProjectileDispenseBehavior(item.asItem()));
|
| 65 | }
|
| 66 |
|
| 67 | /**
|
| 68 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 69 | */
|
| 70 | public DispenserBlock(final BlockBehaviour.Properties properties) {
|
| 71 | super(properties);
|
| 72 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(TRIGGERED, false));
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 77 | if (!level.isClientSide() && level.getBlockEntity(pos) instanceof DispenserBlockEntity dispenser) {
|
| 78 | player.openMenu(dispenser);
|
| 79 | player.awardStat(dispenser instanceof DropperBlockEntity ? Stats.INSPECT_DROPPER : Stats.INSPECT_DISPENSER);
|
| 80 | }
|
| 81 |
|
| 82 | return InteractionResult.SUCCESS;
|
| 83 | }
|
| 84 |
|
| 85 | protected void dispenseFrom(final ServerLevel level, final BlockState state, final BlockPos pos) {
|
| 86 | DispenserBlockEntity blockEntity = level.getBlockEntity(pos, BlockEntityTypes.DISPENSER).orElse(null);
|
| 87 | if (blockEntity == null) {
|
| 88 | LOGGER.warn("Ignoring dispensing attempt for Dispenser without matching block entity at {}", pos);
|
| 89 | } else {
|
| 90 | BlockSource source = new BlockSource(level, pos, state, blockEntity);
|
| 91 | int slot = blockEntity.getRandomSlot(level.getRandom());
|
| 92 | if (slot < 0) {
|
| 93 | level.levelEvent(1001, pos, 0);
|
| 94 | level.gameEvent(GameEvent.BLOCK_ACTIVATE, pos, GameEvent.Context.of(blockEntity.getBlockState()));
|
| 95 | } else {
|
| 96 | ItemStack itemStack = blockEntity.getItem(slot);
|
| 97 | DispenseItemBehavior behavior = this.getDispenseMethod(level, itemStack);
|
| 98 | if (behavior != DispenseItemBehavior.NOOP) {
|
| 99 | blockEntity.setItem(slot, behavior.dispense(source, itemStack));
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | protected DispenseItemBehavior getDispenseMethod(final Level level, final ItemStack itemStack) {
|
| 106 | if (!itemStack.isItemEnabled(level.enabledFeatures())) {
|
| 107 | return DEFAULT_BEHAVIOR;
|
| 108 | }
|
| 109 |
|
| 110 | DispenseItemBehavior behavior = DISPENSER_REGISTRY.get(itemStack.getItem());
|
| 111 | return behavior != null ? behavior : getDefaultDispenseMethod(itemStack);
|
| 112 | }
|
| 113 |
|
| 114 | private static DispenseItemBehavior getDefaultDispenseMethod(final ItemStack itemStack) {
|
| 115 | if (itemStack.has(DataComponents.EQUIPPABLE)) {
|
| 116 | return EquipmentDispenseItemBehavior.INSTANCE;
|
| 117 | } else if (itemStack.is(ItemTags.SULFUR_CUBE_SWALLOWABLE)) {
|
| 118 | return SulfurCubeBlockDispenseItemBehavior.INSTANCE;
|
| 119 | } else {
|
| 120 | return itemStack.getItem() instanceof SpawnEggItem && itemStack.has(DataComponents.ENTITY_DATA) ? SpawnEggItemBehavior.INSTANCE : DEFAULT_BEHAVIOR;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected void neighborChanged(
|
| 126 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 127 | ) {
|
| 128 | boolean shouldTrigger = level.hasNeighborSignal(pos) || level.hasNeighborSignal(pos.above());
|
| 129 | boolean isTriggered = state.getValue(TRIGGERED);
|
| 130 | if (shouldTrigger && !isTriggered) {
|
| 131 | level.scheduleTick(pos, this, 4);
|
| 132 | level.setBlock(pos, state.setValue(TRIGGERED, true), 2);
|
| 133 | } else if (!shouldTrigger && isTriggered) {
|
| 134 | level.setBlock(pos, state.setValue(TRIGGERED, false), 2);
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 140 | this.dispenseFrom(level, state, pos);
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 145 | return new DispenserBlockEntity(worldPosition, blockState);
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 150 | return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite());
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 155 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 156 | }
|
| 157 |
|
| 158 | public static Position getDispensePosition(final BlockSource source) {
|
| 159 | return getDispensePosition(source, 0.7, Vec3.ZERO);
|
| 160 | }
|
| 161 |
|
| 162 | public static Position getDispensePosition(final BlockSource source, final double scale, final Vec3 offset) {
|
| 163 | Direction direction = source.state().getValue(FACING);
|
| 164 | return source.center().add(scale * direction.getStepX() + offset.x(), scale * direction.getStepY() + offset.y(), scale * direction.getStepZ() + offset.z());
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 169 | return true;
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 174 | return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos));
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 179 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 184 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 189 | builder.add(FACING, TRIGGERED);
|
| 190 | }
|
| 191 | }
|
| 192 |
|