|
| 1 | +package fn10.musicexpansion.generation.features; |
| 2 | + |
| 3 | +import com.mojang.serialization.Codec; |
| 4 | +import fn10.musicexpansion.blocks.DiscMonolithBlock; |
| 5 | +import fn10.musicexpansion.items.CompactDiscItem; |
| 6 | +import fn10.musicexpansion.music.CDTracks; |
| 7 | +import net.minecraft.core.BlockPos; |
| 8 | +import net.minecraft.util.RandomSource; |
| 9 | +import net.minecraft.world.item.ItemStack; |
| 10 | +import net.minecraft.world.level.WorldGenLevel; |
| 11 | +import net.minecraft.world.level.block.state.BlockState; |
| 12 | +import net.minecraft.world.level.levelgen.feature.Feature; |
| 13 | +import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; |
| 14 | +import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class MonolithFeature extends Feature<NoneFeatureConfiguration> { |
| 20 | + |
| 21 | + public static final ArrayList<ItemStack> randomDiscs = new ArrayList<>(List.of( |
| 22 | + CompactDiscItem.makeDiscStackWithTracks(CDTracks.C418_13), |
| 23 | + CompactDiscItem.makeDiscStackWithTracks(CDTracks.C418_11), |
| 24 | + CompactDiscItem.makeDiscStackWithTracks(CDTracks.C418_CAT) |
| 25 | + )); |
| 26 | + |
| 27 | + public MonolithFeature(Codec<NoneFeatureConfiguration> codec) { |
| 28 | + super(codec); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> featurePlaceContext) { |
| 33 | + try { |
| 34 | + BlockPos origin = featurePlaceContext.origin(); |
| 35 | + WorldGenLevel level = featurePlaceContext.level(); |
| 36 | + RandomSource random = featurePlaceContext.random(); |
| 37 | + |
| 38 | + int y = origin.getY(); |
| 39 | + //y must be between -10 and 40 |
| 40 | + if (!(y < 40 && y > -10)) return false; |
| 41 | + |
| 42 | + //must be at least 3 air above |
| 43 | + for (int i = 0; i < 3; i++) { |
| 44 | + BlockPos pos = origin.atY(y + i); |
| 45 | + BlockState block = level.getBlockState(pos); |
| 46 | + if (block.isAir()) return false; |
| 47 | + } |
| 48 | + |
| 49 | + DiscMonolithBlock monlith = new DiscMonolithBlock(); |
| 50 | + ItemStack disc = randomDiscs.get(random.nextInt(randomDiscs.size())); |
| 51 | + BlockState state = monlith.defaultBlockState(); |
| 52 | + |
| 53 | + level.setBlock(origin, state, 0); |
| 54 | + monlith.useItemOn(disc, state, level.getLevel(), origin, null, null, null); |
| 55 | + |
| 56 | + return true; |
| 57 | + } catch (Exception e) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments