| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.function.Supplier;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.tags.EnchantmentTags;
|
| 9 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 10 | import net.minecraft.world.entity.EntityTypes;
|
| 11 | import net.minecraft.world.entity.monster.Silverfish;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.properties.Property;
|
| 17 | import net.minecraft.world.level.gamerules.GameRules;
|
| 18 |
|
| 19 | public class InfestedBlock extends Block {
|
| 20 | private final Block hostBlock;
|
| 21 | private static final Map<Block, Block> BLOCK_BY_HOST_BLOCK = Maps.newIdentityHashMap();
|
| 22 | private static final Map<BlockState, BlockState> HOST_TO_INFESTED_STATES = Maps.newIdentityHashMap();
|
| 23 | private static final Map<BlockState, BlockState> INFESTED_TO_HOST_STATES = Maps.newIdentityHashMap();
|
| 24 |
|
| 25 | public InfestedBlock(final Block hostBlock, final BlockBehaviour.Properties properties) {
|
| 26 | super(properties.destroyTime(hostBlock.defaultDestroyTime() / 2.0F).explosionResistance(0.75F));
|
| 27 | this.hostBlock = hostBlock;
|
| 28 | BLOCK_BY_HOST_BLOCK.put(hostBlock, this);
|
| 29 | }
|
| 30 |
|
| 31 | public Block getHostBlock() {
|
| 32 | return this.hostBlock;
|
| 33 | }
|
| 34 |
|
| 35 | public static boolean isCompatibleHostBlock(final BlockState blockState) {
|
| 36 | return BLOCK_BY_HOST_BLOCK.containsKey(blockState.getBlock());
|
| 37 | }
|
| 38 |
|
| 39 | private void spawnInfestation(final ServerLevel level, final BlockPos pos) {
|
| 40 | Silverfish silverfish = EntityTypes.SILVERFISH.create(level, EntitySpawnReason.TRIGGERED);
|
| 41 | if (silverfish != null) {
|
| 42 | silverfish.snapTo(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, 0.0F, 0.0F);
|
| 43 | level.addFreshEntity(silverfish);
|
| 44 | silverfish.spawnAnim();
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected void spawnAfterBreak(final BlockState state, final ServerLevel level, final BlockPos pos, final ItemStack tool, final boolean dropExperience) {
|
| 50 | super.spawnAfterBreak(state, level, pos, tool, dropExperience);
|
| 51 | if (level.getGameRules().get(GameRules.BLOCK_DROPS) && !EnchantmentHelper.hasTag(tool, EnchantmentTags.PREVENTS_INFESTED_SPAWNS)) {
|
| 52 | this.spawnInfestation(level, pos);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | public static BlockState infestedStateByHost(final BlockState hostState) {
|
| 57 | return getNewStateWithProperties(HOST_TO_INFESTED_STATES, hostState, () -> BLOCK_BY_HOST_BLOCK.get(hostState.getBlock()).defaultBlockState());
|
| 58 | }
|
| 59 |
|
| 60 | public BlockState hostStateByInfested(final BlockState infestedState) {
|
| 61 | return getNewStateWithProperties(INFESTED_TO_HOST_STATES, infestedState, () -> this.getHostBlock().defaultBlockState());
|
| 62 | }
|
| 63 |
|
| 64 | private static BlockState getNewStateWithProperties(
|
| 65 | final Map<BlockState, BlockState> map, final BlockState oldState, final Supplier<BlockState> newStateSupplier
|
| 66 | ) {
|
| 67 | return map.computeIfAbsent(oldState, k -> {
|
| 68 | BlockState newState = newStateSupplier.get();
|
| 69 |
|
| 70 | for (Property<?> property : k.getProperties()) {
|
| 71 | newState = BlockBehaviour.BlockStateBase.copyProperty(k, newState, property);
|
| 72 | }
|
| 73 |
|
| 74 | return newState;
|
| 75 | });
|
| 76 | }
|
| 77 | }
|
| 78 |
|