| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.cache.CacheBuilder;
|
| 4 | import com.google.common.cache.CacheLoader;
|
| 5 | import com.google.common.cache.LoadingCache;
|
| 6 | import com.google.common.collect.ImmutableMap;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import it.unimi.dsi.fastutil.objects.Object2ByteLinkedOpenHashMap;
|
| 9 | import java.lang.annotation.ElementType;
|
| 10 | import java.lang.annotation.Retention;
|
| 11 | import java.lang.annotation.RetentionPolicy;
|
| 12 | import java.lang.annotation.Target;
|
| 13 | import java.util.Arrays;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Map.Entry;
|
| 17 | import java.util.function.BiConsumer;
|
| 18 | import java.util.function.Function;
|
| 19 | import java.util.function.IntFunction;
|
| 20 | import java.util.function.Supplier;
|
| 21 | import java.util.stream.Collectors;
|
| 22 | import java.util.stream.IntStream;
|
| 23 | import net.fabricmc.fabric.api.block.v1.FabricBlock;
|
| 24 | import net.minecraft.SharedConstants;
|
| 25 | import net.minecraft.core.BlockPos;
|
| 26 | import net.minecraft.core.Direction;
|
| 27 | import net.minecraft.core.Holder;
|
| 28 | import net.minecraft.core.IdMapper;
|
| 29 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 30 | import net.minecraft.network.chat.Component;
|
| 31 | import net.minecraft.network.chat.MutableComponent;
|
| 32 | import net.minecraft.resources.ResourceKey;
|
| 33 | import net.minecraft.server.level.ServerLevel;
|
| 34 | import net.minecraft.server.level.ServerPlayer;
|
| 35 | import net.minecraft.stats.Stats;
|
| 36 | import net.minecraft.tags.BlockTags;
|
| 37 | import net.minecraft.util.Mth;
|
| 38 | import net.minecraft.util.RandomSource;
|
| 39 | import net.minecraft.util.valueproviders.IntProvider;
|
| 40 | import net.minecraft.world.entity.Entity;
|
| 41 | import net.minecraft.world.entity.EntityTypes;
|
| 42 | import net.minecraft.world.entity.ExperienceOrb;
|
| 43 | import net.minecraft.world.entity.LivingEntity;
|
| 44 | import net.minecraft.world.entity.item.ItemEntity;
|
| 45 | import net.minecraft.world.entity.monster.piglin.PiglinAi;
|
| 46 | import net.minecraft.world.entity.player.Player;
|
| 47 | import net.minecraft.world.item.BlockItem;
|
| 48 | import net.minecraft.world.item.Item;
|
| 49 | import net.minecraft.world.item.ItemInstance;
|
| 50 | import net.minecraft.world.item.ItemStack;
|
| 51 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 52 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 53 | import net.minecraft.world.level.BlockGetter;
|
| 54 | import net.minecraft.world.level.Explosion;
|
| 55 | import net.minecraft.world.level.ItemLike;
|
| 56 | import net.minecraft.world.level.Level;
|
| 57 | import net.minecraft.world.level.LevelAccessor;
|
| 58 | import net.minecraft.world.level.LevelReader;
|
| 59 | import net.minecraft.world.level.biome.Biome;
|
| 60 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 61 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 62 | import net.minecraft.world.level.block.state.BlockState;
|
| 63 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 64 | import net.minecraft.world.level.block.state.StateHolder;
|
| 65 | import net.minecraft.world.level.block.state.properties.Property;
|
| 66 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 67 | import net.minecraft.world.level.gamerules.GameRules;
|
| 68 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 69 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 70 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 71 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 72 | import net.minecraft.world.phys.Vec3;
|
| 73 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 74 | import net.minecraft.world.phys.shapes.Shapes;
|
| 75 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 76 | import org.jspecify.annotations.Nullable;
|
| 77 | import org.slf4j.Logger;
|
| 78 |
|
| 79 | public class Block extends BlockBehaviour implements ItemLike, FabricBlock {
|
| 80 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 81 | private final Holder.Reference<Block> builtInRegistryHolder = BuiltInRegistries.BLOCK.createIntrusiveHolder(this);
|
| 82 | public static final IdMapper<BlockState> BLOCK_STATE_REGISTRY = new IdMapper<>();
|
| 83 | private static final LoadingCache<VoxelShape, Boolean> SHAPE_FULL_BLOCK_CACHE = CacheBuilder.newBuilder()
|
| 84 | .maximumSize(512L)
|
| 85 | .weakKeys()
|
| 86 | .build(new CacheLoader<VoxelShape, Boolean>() {
|
| 87 | public Boolean load(final VoxelShape shape) {
|
| 88 | return !Shapes.joinIsNotEmpty(Shapes.block(), shape, BooleanOp.NOT_SAME);
|
| 89 | }
|
| 90 | });
|
| 91 | public static final int UPDATE_NEIGHBORS = 1;
|
| 92 | public static final int UPDATE_CLIENTS = 2;
|
| 93 | public static final int UPDATE_INVISIBLE = 4;
|
| 94 | public static final int UPDATE_IMMEDIATE = 8;
|
| 95 | public static final int UPDATE_KNOWN_SHAPE = 16;
|
| 96 | public static final int UPDATE_SUPPRESS_DROPS = 32;
|
| 97 | public static final int UPDATE_MOVE_BY_PISTON = 64;
|
| 98 | public static final int UPDATE_SKIP_SHAPE_UPDATE_ON_WIRE = 128;
|
| 99 | public static final int UPDATE_SKIP_BLOCK_ENTITY_SIDEEFFECTS = 256;
|
| 100 | public static final int UPDATE_SKIP_ON_PLACE = 512;
|
| 101 | public static final @Block.UpdateFlags int UPDATE_NONE = 260;
|
| 102 | public static final @Block.UpdateFlags int UPDATE_ALL = 3;
|
| 103 | public static final @Block.UpdateFlags int UPDATE_ALL_IMMEDIATE = 11;
|
| 104 | public static final @Block.UpdateFlags int UPDATE_SKIP_ALL_SIDEEFFECTS = 816;
|
| 105 | public static final float INDESTRUCTIBLE = -1.0F;
|
| 106 | public static final float INSTANT = 0.0F;
|
| 107 | public static final int UPDATE_LIMIT = 512;
|
| 108 | protected final StateDefinition<Block, BlockState> stateDefinition;
|
| 109 | private BlockState defaultBlockState;
|
| 110 | private @Nullable Item item;
|
| 111 | private static final int CACHE_SIZE = 256;
|
| 112 | private static final ThreadLocal<Object2ByteLinkedOpenHashMap<Block.ShapePairKey>> OCCLUSION_CACHE = ThreadLocal.withInitial(() -> {
|
| 113 | Object2ByteLinkedOpenHashMap<Block.ShapePairKey> map = new Object2ByteLinkedOpenHashMap<Block.ShapePairKey>(256, 0.25F) {
|
| 114 | @Override
|
| 115 | protected void rehash(final int newN) {
|
| 116 | }
|
| 117 | };
|
| 118 | map.defaultReturnValue((byte)127);
|
| 119 | return map;
|
| 120 | });
|
| 121 |
|
| 122 | public static int getId(final @Nullable BlockState blockState) {
|
| 123 | if (blockState == null) {
|
| 124 | return 0;
|
| 125 | }
|
| 126 |
|
| 127 | int id = BLOCK_STATE_REGISTRY.getId(blockState);
|
| 128 | return id == -1 ? 0 : id;
|
| 129 | }
|
| 130 |
|
| 131 | public static BlockState stateById(final int idWithData) {
|
| 132 | BlockState state = BLOCK_STATE_REGISTRY.byId(idWithData);
|
| 133 | return state == null ? Blocks.AIR.defaultBlockState() : state;
|
| 134 | }
|
| 135 |
|
| 136 | public static Block byItem(final @Nullable Item item) {
|
| 137 | return item instanceof BlockItem blockItem ? blockItem.getBlock() : Blocks.AIR;
|
| 138 | }
|
| 139 |
|
| 140 | public static BlockState pushEntitiesUp(final BlockState state, final BlockState newState, final LevelAccessor level, final BlockPos pos) {
|
| 141 | VoxelShape offsetShape = Shapes.joinUnoptimized(state.getCollisionShape(level, pos), newState.getCollisionShape(level, pos), BooleanOp.ONLY_SECOND).move(pos);
|
| 142 | if (offsetShape.isEmpty()) {
|
| 143 | return newState;
|
| 144 | }
|
| 145 |
|
| 146 | for (Entity collidingEntity : level.getEntities(null, offsetShape.bounds())) {
|
| 147 | double offset = Shapes.collide(Direction.Axis.Y, collidingEntity.getBoundingBox().move(0.0, 1.0, 0.0), List.of(offsetShape), -1.0);
|
| 148 | collidingEntity.teleportRelative(0.0, 1.0 + offset, 0.0);
|
| 149 | }
|
| 150 |
|
| 151 | return newState;
|
| 152 | }
|
| 153 |
|
| 154 | public static VoxelShape box(final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ) {
|
| 155 | return Shapes.box(minX / 16.0, minY / 16.0, minZ / 16.0, maxX / 16.0, maxY / 16.0, maxZ / 16.0);
|
| 156 | }
|
| 157 |
|
| 158 | public static VoxelShape[] boxes(final int endInclusive, final IntFunction<VoxelShape> voxelShapeFactory) {
|
| 159 | return IntStream.rangeClosed(0, endInclusive).mapToObj(voxelShapeFactory).toArray(VoxelShape[]::new);
|
| 160 | }
|
| 161 |
|
| 162 | public static VoxelShape cube(final double size) {
|
| 163 | return cube(size, size, size);
|
| 164 | }
|
| 165 |
|
| 166 | public static VoxelShape cube(final double sizeX, final double sizeY, final double sizeZ) {
|
| 167 | double halfY = sizeY / 2.0;
|
| 168 | return column(sizeX, sizeZ, 8.0 - halfY, 8.0 + halfY);
|
| 169 | }
|
| 170 |
|
| 171 | public static VoxelShape column(final double sizeXZ, final double minY, final double maxY) {
|
| 172 | return column(sizeXZ, sizeXZ, minY, maxY);
|
| 173 | }
|
| 174 |
|
| 175 | public static VoxelShape column(final double sizeX, final double sizeZ, final double minY, final double maxY) {
|
| 176 | double halfX = sizeX / 2.0;
|
| 177 | double halfZ = sizeZ / 2.0;
|
| 178 | return box(8.0 - halfX, minY, 8.0 - halfZ, 8.0 + halfX, maxY, 8.0 + halfZ);
|
| 179 | }
|
| 180 |
|
| 181 | public static VoxelShape boxZ(final double sizeXY, final double minZ, final double maxZ) {
|
| 182 | return boxZ(sizeXY, sizeXY, minZ, maxZ);
|
| 183 | }
|
| 184 |
|
| 185 | public static VoxelShape boxZ(final double sizeX, final double sizeY, final double minZ, final double maxZ) {
|
| 186 | double halfY = sizeY / 2.0;
|
| 187 | return boxZ(sizeX, 8.0 - halfY, 8.0 + halfY, minZ, maxZ);
|
| 188 | }
|
| 189 |
|
| 190 | public static VoxelShape boxZ(final double sizeX, final double minY, final double maxY, final double minZ, final double maxZ) {
|
| 191 | double halfX = sizeX / 2.0;
|
| 192 | return box(8.0 - halfX, minY, minZ, 8.0 + halfX, maxY, maxZ);
|
| 193 | }
|
| 194 |
|
| 195 | public static BlockState updateFromNeighbourShapes(final BlockState state, final LevelAccessor level, final BlockPos pos) {
|
| 196 | BlockState newState = state;
|
| 197 | BlockPos.MutableBlockPos neighbourPos = new BlockPos.MutableBlockPos();
|
| 198 |
|
| 199 | for (Direction direction : UPDATE_SHAPE_ORDER) {
|
| 200 | neighbourPos.setWithOffset(pos, direction);
|
| 201 | newState = newState.updateShape(level, level, pos, direction, neighbourPos, level.getBlockState(neighbourPos), level.getRandom());
|
| 202 | }
|
| 203 |
|
| 204 | return newState;
|
| 205 | }
|
| 206 |
|
| 207 | public static void updateOrDestroy(
|
| 208 | final BlockState blockState, final BlockState newState, final LevelAccessor level, final BlockPos blockPos, final @Block.UpdateFlags int updateFlags
|
| 209 | ) {
|
| 210 | updateOrDestroy(blockState, newState, level, blockPos, updateFlags, 512);
|
| 211 | }
|
| 212 |
|
| 213 | public static void updateOrDestroy(
|
| 214 | final BlockState blockState,
|
| 215 | final BlockState newState,
|
| 216 | final LevelAccessor level,
|
| 217 | final BlockPos blockPos,
|
| 218 | final @Block.UpdateFlags int updateFlags,
|
| 219 | final int updateLimit
|
| 220 | ) {
|
| 221 | if (newState != blockState) {
|
| 222 | if (newState.isAir()) {
|
| 223 | if (!level.isClientSide()) {
|
| 224 | level.destroyBlock(blockPos, (updateFlags & 32) == 0, null, updateLimit);
|
| 225 | }
|
| 226 | } else {
|
| 227 | level.setBlock(blockPos, newState, updateFlags & -33, updateLimit);
|
| 228 | }
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | public Block(final BlockBehaviour.Properties properties) {
|
| 233 | super(properties);
|
| 234 | StateDefinition.Builder<Block, BlockState> builder = new StateDefinition.Builder<>(this);
|
| 235 | this.createBlockStateDefinition(builder);
|
| 236 | this.stateDefinition = builder.create(Block::defaultBlockState, BlockState::new);
|
| 237 | this.registerDefaultState(this.stateDefinition.any());
|
| 238 | if (SharedConstants.IS_RUNNING_IN_IDE) {
|
| 239 | String className = this.getClass().getSimpleName();
|
| 240 | if (!className.endsWith("Block")) {
|
| 241 | LOGGER.error("Block classes should end with Block and {} doesn't.", className);
|
| 242 | }
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | public static boolean isExceptionForConnection(final BlockState state) {
|
| 247 | return state.getBlock() instanceof LeavesBlock
|
| 248 | || state.is(Blocks.BARRIER)
|
| 249 | || state.is(Blocks.CARVED_PUMPKIN)
|
| 250 | || state.is(Blocks.JACK_O_LANTERN)
|
| 251 | || state.is(Blocks.MELON)
|
| 252 | || state.is(Blocks.PUMPKIN)
|
| 253 | || state.is(BlockTags.SHULKER_BOXES);
|
| 254 | }
|
| 255 |
|
| 256 | public static boolean dropFromBlockInteractLootTable(
|
| 257 | final ServerLevel level,
|
| 258 | final ResourceKey<LootTable> key,
|
| 259 | final BlockPos interactedBlockPos,
|
| 260 | final BlockState interactedBlockState,
|
| 261 | final @Nullable BlockEntity interactedBlockEntity,
|
| 262 | final @Nullable ItemInstance tool,
|
| 263 | final @Nullable Entity interactingEntity,
|
| 264 | final BiConsumer<ServerLevel, ItemStack> consumer
|
| 265 | ) {
|
| 266 | return dropFromLootTable(
|
| 267 | level,
|
| 268 | key,
|
| 269 | params -> params.withParameter(LootContextParams.BLOCK_STATE, interactedBlockState)
|
| 270 | .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(interactedBlockPos))
|
| 271 | .withOptionalParameter(LootContextParams.BLOCK_ENTITY, interactedBlockEntity)
|
| 272 | .withOptionalParameter(LootContextParams.INTERACTING_ENTITY, interactingEntity)
|
| 273 | .withOptionalParameter(LootContextParams.TOOL, tool)
|
| 274 | .create(LootContextParamSets.BLOCK_INTERACT),
|
| 275 | consumer
|
| 276 | );
|
| 277 | }
|
| 278 |
|
| 279 | protected static boolean dropFromLootTable(
|
| 280 | final ServerLevel level,
|
| 281 | final ResourceKey<LootTable> key,
|
| 282 | final Function<LootParams.Builder, LootParams> paramsBuilder,
|
| 283 | final BiConsumer<ServerLevel, ItemStack> consumer
|
| 284 | ) {
|
| 285 | LootTable lootTable = level.getServer().reloadableRegistries().getLootTable(key);
|
| 286 | LootParams params = paramsBuilder.apply(new LootParams.Builder(level));
|
| 287 | List<ItemStack> drops = lootTable.getRandomItems(params);
|
| 288 | if (!drops.isEmpty()) {
|
| 289 | drops.forEach(stack -> consumer.accept(level, stack));
|
| 290 | return true;
|
| 291 | } else {
|
| 292 | return false;
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | public static boolean shouldRenderFace(final BlockState state, final BlockState neighborState, final Direction direction) {
|
| 297 | VoxelShape occluder = neighborState.getFaceOcclusionShape(direction.getOpposite());
|
| 298 | if (occluder == Shapes.block()) {
|
| 299 | return false;
|
| 300 | }
|
| 301 |
|
| 302 | if (state.skipRendering(neighborState, direction)) {
|
| 303 | return false;
|
| 304 | }
|
| 305 |
|
| 306 | if (occluder == Shapes.empty()) {
|
| 307 | return true;
|
| 308 | }
|
| 309 |
|
| 310 | VoxelShape shape = state.getFaceOcclusionShape(direction);
|
| 311 | if (shape == Shapes.empty()) {
|
| 312 | return true;
|
| 313 | }
|
| 314 |
|
| 315 | Block.ShapePairKey key = new Block.ShapePairKey(shape, occluder);
|
| 316 | Object2ByteLinkedOpenHashMap<Block.ShapePairKey> cache = OCCLUSION_CACHE.get();
|
| 317 | byte cached = cache.getAndMoveToFirst(key);
|
| 318 | if (cached != 127) {
|
| 319 | return cached != 0;
|
| 320 | }
|
| 321 |
|
| 322 | boolean result = Shapes.joinIsNotEmpty(shape, occluder, BooleanOp.ONLY_FIRST);
|
| 323 | if (cache.size() == 256) {
|
| 324 | cache.removeLastByte();
|
| 325 | }
|
| 326 |
|
| 327 | cache.putAndMoveToFirst(key, (byte)(result ? 1 : 0));
|
| 328 | return result;
|
| 329 | }
|
| 330 |
|
| 331 | public static boolean canSupportRigidBlock(final BlockGetter level, final BlockPos below) {
|
| 332 | return level.getBlockState(below).isFaceSturdy(level, below, Direction.UP, SupportType.RIGID);
|
| 333 | }
|
| 334 |
|
| 335 | public static boolean canSupportCenter(final LevelReader level, final BlockPos belowPos, final Direction direction) {
|
| 336 | BlockState state = level.getBlockState(belowPos);
|
| 337 | return direction == Direction.DOWN && state.is(BlockTags.UNSTABLE_BOTTOM_CENTER) ? false : state.isFaceSturdy(level, belowPos, direction, SupportType.CENTER);
|
| 338 | }
|
| 339 |
|
| 340 | public static boolean isFaceFull(final VoxelShape shape, final Direction direction) {
|
| 341 | VoxelShape faceShape = shape.getFaceShape(direction);
|
| 342 | return isShapeFullBlock(faceShape);
|
| 343 | }
|
| 344 |
|
| 345 | public static boolean isShapeFullBlock(final VoxelShape shape) {
|
| 346 | return SHAPE_FULL_BLOCK_CACHE.getUnchecked(shape);
|
| 347 | }
|
| 348 |
|
| 349 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 350 | }
|
| 351 |
|
| 352 | public void destroy(final LevelAccessor level, final BlockPos pos, final BlockState state) {
|
| 353 | }
|
| 354 |
|
| 355 | public static List<ItemStack> getDrops(final BlockState state, final ServerLevel level, final BlockPos pos, final @Nullable BlockEntity blockEntity) {
|
| 356 | LootParams.Builder params = new LootParams.Builder(level)
|
| 357 | .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
|
| 358 | .withParameter(LootContextParams.TOOL, ItemStack.EMPTY)
|
| 359 | .withOptionalParameter(LootContextParams.BLOCK_ENTITY, blockEntity);
|
| 360 | return state.getDrops(params);
|
| 361 | }
|
| 362 |
|
| 363 | public static List<ItemStack> getDrops(
|
| 364 | final BlockState state,
|
| 365 | final ServerLevel level,
|
| 366 | final BlockPos pos,
|
| 367 | final @Nullable BlockEntity blockEntity,
|
| 368 | final @Nullable Entity breaker,
|
| 369 | final ItemInstance tool
|
| 370 | ) {
|
| 371 | LootParams.Builder params = new LootParams.Builder(level)
|
| 372 | .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
|
| 373 | .withParameter(LootContextParams.TOOL, tool)
|
| 374 | .withOptionalParameter(LootContextParams.THIS_ENTITY, breaker)
|
| 375 | .withOptionalParameter(LootContextParams.BLOCK_ENTITY, blockEntity);
|
| 376 | return state.getDrops(params);
|
| 377 | }
|
| 378 |
|
| 379 | public static void dropResources(final BlockState state, final Level level, final BlockPos pos) {
|
| 380 | if (level instanceof ServerLevel serverLevel) {
|
| 381 | getDrops(state, serverLevel, pos, null).forEach(stack -> popResource(level, pos, stack));
|
| 382 | state.spawnAfterBreak(serverLevel, pos, ItemStack.EMPTY, true);
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | public static void dropResources(final BlockState state, final LevelAccessor level, final BlockPos pos, final @Nullable BlockEntity blockEntity) {
|
| 387 | if (level instanceof ServerLevel serverLevel) {
|
| 388 | getDrops(state, serverLevel, pos, blockEntity).forEach(stack -> popResource(serverLevel, pos, stack));
|
| 389 | state.spawnAfterBreak(serverLevel, pos, ItemStack.EMPTY, true);
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | public static void dropResources(
|
| 394 | final BlockState state, final Level level, final BlockPos pos, final @Nullable BlockEntity blockEntity, final @Nullable Entity breaker, final ItemStack tool
|
| 395 | ) {
|
| 396 | if (level instanceof ServerLevel serverLevel) {
|
| 397 | getDrops(state, serverLevel, pos, blockEntity, breaker, tool).forEach(stack -> popResource(level, pos, stack));
|
| 398 | state.spawnAfterBreak(serverLevel, pos, tool, true);
|
| 399 | }
|
| 400 | }
|
| 401 |
|
| 402 | public static void popResource(final Level level, final BlockPos pos, final ItemStack itemStack) {
|
| 403 | double halfHeight = EntityTypes.ITEM.getHeight() / 2.0;
|
| 404 | RandomSource random = level.getRandom();
|
| 405 | double x = pos.getX() + 0.5 + Mth.nextDouble(random, -0.25, 0.25);
|
| 406 | double y = pos.getY() + 0.5 + Mth.nextDouble(random, -0.25, 0.25) - halfHeight;
|
| 407 | double z = pos.getZ() + 0.5 + Mth.nextDouble(random, -0.25, 0.25);
|
| 408 | popResource(level, () -> new ItemEntity(level, x, y, z, itemStack), itemStack);
|
| 409 | }
|
| 410 |
|
| 411 | public static void popResourceFromFace(final Level level, final BlockPos pos, final Direction face, final ItemStack itemStack) {
|
| 412 | int stepX = face.getStepX();
|
| 413 | int stepY = face.getStepY();
|
| 414 | int stepZ = face.getStepZ();
|
| 415 | double halfWidth = EntityTypes.ITEM.getWidth() / 2.0;
|
| 416 | double halfHeight = EntityTypes.ITEM.getHeight() / 2.0;
|
| 417 | RandomSource random = level.getRandom();
|
| 418 | double x = pos.getX() + 0.5 + (stepX == 0 ? Mth.nextDouble(random, -0.25, 0.25) : stepX * (0.5 + halfWidth));
|
| 419 | double y = pos.getY() + 0.5 + (stepY == 0 ? Mth.nextDouble(random, -0.25, 0.25) : stepY * (0.5 + halfHeight)) - halfHeight;
|
| 420 | double z = pos.getZ() + 0.5 + (stepZ == 0 ? Mth.nextDouble(random, -0.25, 0.25) : stepZ * (0.5 + halfWidth));
|
| 421 | double deltaX = stepX == 0 ? Mth.nextDouble(random, -0.1, 0.1) : stepX * 0.1;
|
| 422 | double deltaY = stepY == 0 ? Mth.nextDouble(random, 0.0, 0.1) : stepY * 0.1 + 0.1;
|
| 423 | double deltaZ = stepZ == 0 ? Mth.nextDouble(random, -0.1, 0.1) : stepZ * 0.1;
|
| 424 | popResource(level, () -> new ItemEntity(level, x, y, z, itemStack, deltaX, deltaY, deltaZ), itemStack);
|
| 425 | }
|
| 426 |
|
| 427 | private static void popResource(final Level level, final Supplier<ItemEntity> entityFactory, final ItemStack itemStack) {
|
| 428 | if (level instanceof ServerLevel serverLevel && !itemStack.isEmpty() && serverLevel.getGameRules().get(GameRules.BLOCK_DROPS)) {
|
| 429 | ItemEntity entity = entityFactory.get();
|
| 430 | entity.setDefaultPickUpDelay();
|
| 431 | level.addFreshEntity(entity);
|
| 432 | }
|
| 433 | }
|
| 434 |
|
| 435 | protected void popExperience(final ServerLevel level, final BlockPos pos, final int amount) {
|
| 436 | if (level.getGameRules().get(GameRules.BLOCK_DROPS)) {
|
| 437 | ExperienceOrb.award(level, Vec3.atCenterOf(pos), amount);
|
| 438 | }
|
| 439 | }
|
| 440 |
|
| 441 | public float getExplosionResistance() {
|
| 442 | return this.explosionResistance;
|
| 443 | }
|
| 444 |
|
| 445 | public void wasExploded(final ServerLevel level, final BlockPos pos, final Explosion explosion) {
|
| 446 | }
|
| 447 |
|
| 448 | public void stepOn(final Level level, final BlockPos pos, final BlockState onState, final Entity entity) {
|
| 449 | }
|
| 450 |
|
| 451 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 452 | return this.defaultBlockState();
|
| 453 | }
|
| 454 |
|
| 455 | public void playerDestroy(
|
| 456 | final ServerLevel level,
|
| 457 | final ServerPlayer player,
|
| 458 | final BlockPos pos,
|
| 459 | final BlockState state,
|
| 460 | final @Nullable BlockEntity blockEntity,
|
| 461 | final ItemStack destroyedWith
|
| 462 | ) {
|
| 463 | player.awardStat(Stats.BLOCK_MINED.get(this));
|
| 464 | player.causeFoodExhaustion(0.005F);
|
| 465 | dropResources(state, level, pos, blockEntity, player, destroyedWith);
|
| 466 | }
|
| 467 |
|
| 468 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 469 | }
|
| 470 |
|
| 471 | public boolean isPossibleToRespawnInThis(final BlockState state) {
|
| 472 | return !state.isSolid() && !state.liquid();
|
| 473 | }
|
| 474 |
|
| 475 | public MutableComponent getName() {
|
| 476 | return Component.translatable(this.getDescriptionId());
|
| 477 | }
|
| 478 |
|
| 479 | public void fallOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance) {
|
| 480 | double reducedFallDistance = fallDistance * (1.0F - this.getFallDistanceReduction());
|
| 481 | entity.causeFallDamage(reducedFallDistance, 1.0F, entity.damageSources().fall());
|
| 482 | }
|
| 483 |
|
| 484 | public void bounceOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance) {
|
| 485 | }
|
| 486 |
|
| 487 | public float getBounceRestitution() {
|
| 488 | return this.bounceRestitution;
|
| 489 | }
|
| 490 |
|
| 491 | public float getFallDistanceReduction() {
|
| 492 | return this.fallDistanceReduction;
|
| 493 | }
|
| 494 |
|
| 495 | public float getFriction() {
|
| 496 | return this.friction;
|
| 497 | }
|
| 498 |
|
| 499 | public float getSpeedFactor() {
|
| 500 | return this.speedFactor;
|
| 501 | }
|
| 502 |
|
| 503 | public float getJumpFactor() {
|
| 504 | return this.jumpFactor;
|
| 505 | }
|
| 506 |
|
| 507 | public void spawnDestroyByEntityParticles(final Level level, final @Nullable Entity entity, final BlockPos pos, final BlockState state) {
|
| 508 | level.levelEvent(entity, 2001, pos, getId(state));
|
| 509 | }
|
| 510 |
|
| 511 | public void spawnDestroyParticles(final Level level, final BlockPos pos, final BlockState state) {
|
| 512 | this.spawnDestroyByEntityParticles(level, null, pos, state);
|
| 513 | }
|
| 514 |
|
| 515 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 516 | this.spawnDestroyByEntityParticles(level, player, pos, state);
|
| 517 | if (state.is(BlockTags.GUARDED_BY_PIGLINS) && level instanceof ServerLevel serverLevel) {
|
| 518 | PiglinAi.angerNearbyPiglins(serverLevel, player, false);
|
| 519 | }
|
| 520 |
|
| 521 | level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(player, state));
|
| 522 | return state;
|
| 523 | }
|
| 524 |
|
| 525 | public void handlePrecipitation(final BlockState state, final Level level, final BlockPos pos, final Biome.Precipitation precipitation) {
|
| 526 | }
|
| 527 |
|
| 528 | public boolean dropFromExplosion(final Explosion explosion) {
|
| 529 | return true;
|
| 530 | }
|
| 531 |
|
| 532 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 533 | }
|
| 534 |
|
| 535 | public StateDefinition<Block, BlockState> getStateDefinition() {
|
| 536 | return this.stateDefinition;
|
| 537 | }
|
| 538 |
|
| 539 | protected final void registerDefaultState(final BlockState state) {
|
| 540 | this.defaultBlockState = state;
|
| 541 | }
|
| 542 |
|
| 543 | public final BlockState defaultBlockState() {
|
| 544 | return this.defaultBlockState;
|
| 545 | }
|
| 546 |
|
| 547 | public final BlockState withPropertiesOf(final BlockState source) {
|
| 548 | BlockState result = this.defaultBlockState();
|
| 549 |
|
| 550 | for (Property<?> property : source.getBlock().getStateDefinition().getProperties()) {
|
| 551 | result = BlockBehaviour.BlockStateBase.copyProperty(source, result, property);
|
| 552 | }
|
| 553 |
|
| 554 | return result;
|
| 555 | }
|
| 556 |
|
| 557 | @Override
|
| 558 | public Item asItem() {
|
| 559 | if (this.item == null) {
|
| 560 | this.item = Item.byBlock(this);
|
| 561 | }
|
| 562 |
|
| 563 | return this.item;
|
| 564 | }
|
| 565 |
|
| 566 | public boolean hasDynamicShape() {
|
| 567 | return this.dynamicShape;
|
| 568 | }
|
| 569 |
|
| 570 | @Override
|
| 571 | public String toString() {
|
| 572 | return "Block{" + BuiltInRegistries.BLOCK.wrapAsHolder(this).getRegisteredName() + "}";
|
| 573 | }
|
| 574 |
|
| 575 | @Override
|
| 576 | protected Block asBlock() {
|
| 577 | return this;
|
| 578 | }
|
| 579 |
|
| 580 | protected Function<BlockState, VoxelShape> getShapeForEachState(final Function<BlockState, VoxelShape> shapeCalculator) {
|
| 581 | return this.stateDefinition.getPossibleStates().stream().collect(ImmutableMap.toImmutableMap(Function.identity(), shapeCalculator))::get;
|
| 582 | }
|
| 583 |
|
| 584 | protected Function<BlockState, VoxelShape> getShapeForEachState(final Function<BlockState, VoxelShape> shapeCalculator, final Property<?>... ignoredProperties) {
|
| 585 | Map<? extends Property<?>, Object> defaults = Arrays.stream(ignoredProperties).collect(Collectors.toMap(k -> k, k -> k.getPossibleValues().getFirst()));
|
| 586 | ImmutableMap<BlockState, VoxelShape> map = this.stateDefinition
|
| 587 | .getPossibleStates()
|
| 588 | .stream()
|
| 589 | .filter(state -> defaults.entrySet().stream().allMatch(entry -> state.getValue((Property<?>)entry.getKey()) == entry.getValue()))
|
| 590 | .collect(ImmutableMap.toImmutableMap(Function.identity(), shapeCalculator));
|
| 591 | return blockState -> {
|
| 592 | for (Entry<? extends Property<?>, Object> entry : defaults.entrySet()) {
|
| 593 | blockState = setValueHelper(blockState, (Property<?>)entry.getKey(), entry.getValue());
|
| 594 | }
|
| 595 |
|
| 596 | return map.get(blockState);
|
| 597 | };
|
| 598 | }
|
| 599 |
|
| 600 | private static <S extends StateHolder<?, S>, T extends Comparable<T>> S setValueHelper(final S state, final Property<T> property, final Object value) {
|
| 601 | return state.setValue(property, (Comparable)value);
|
| 602 | }
|
| 603 |
|
| 604 | @Deprecated
|
| 605 | public Holder.Reference<Block> builtInRegistryHolder() {
|
| 606 | return this.builtInRegistryHolder;
|
| 607 | }
|
| 608 |
|
| 609 | protected void tryDropExperience(final ServerLevel level, final BlockPos pos, final ItemStack tool, final IntProvider xpRange) {
|
| 610 | int experience = EnchantmentHelper.processBlockExperience(level, tool, xpRange.sample(level.getRandom()));
|
| 611 | if (experience > 0) {
|
| 612 | this.popExperience(level, pos, experience);
|
| 613 | }
|
| 614 | }
|
| 615 |
|
| 616 | private record ShapePairKey(VoxelShape first, VoxelShape second) {
|
| 617 | @Override
|
| 618 | public boolean equals(final Object o) {
|
| 619 | return o instanceof Block.ShapePairKey that && this.first == that.first && this.second == that.second;
|
| 620 | }
|
| 621 |
|
| 622 | @Override
|
| 623 | public int hashCode() {
|
| 624 | return System.identityHashCode(this.first) * 31 + System.identityHashCode(this.second);
|
| 625 | }
|
| 626 | }
|
| 627 |
|
| 628 | @Retention(RetentionPolicy.CLASS)
|
| 629 | @Target(ElementType.TYPE_USE)
|
| 630 | public @interface UpdateFlags {
|
| 631 | }
|
| 632 | }
|
| 633 |
|