generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package rs.onako2.block; | ||
|
||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.MossBlock; | ||
import net.minecraft.registry.RegistryKeys; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.random.Random; | ||
import net.minecraft.world.gen.feature.ConfiguredFeature; | ||
import rs.onako2.Init; | ||
|
||
public class PaleMossBlock extends MossBlock { | ||
public PaleMossBlock(Settings settings) { | ||
super(settings); | ||
} | ||
|
||
@Override | ||
public void grow(ServerWorld world, Random random, BlockPos pos, BlockState state) { | ||
world.getRegistryManager().getOptional(RegistryKeys.CONFIGURED_FEATURE).flatMap((key) -> { | ||
return key.getEntry(Init.PALE_MOSS_PATCH_BONEMEAL_FEATURE_ID); | ||
}).ifPresent((entry) -> { | ||
((ConfiguredFeature)entry.value()).generate(world, world.getChunkManager().getChunkGenerator(), random, pos.up()); | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/rs/onako2/feature/pale_moss_patch_bonemeal/PaleMossPatchBonemealConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package rs.onako2.feature.pale_moss_patch_bonemeal; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.dynamic.Codecs; | ||
import net.minecraft.world.gen.feature.FeatureConfig; | ||
|
||
public record PaleMossPatchBonemealConfig(int number, Identifier blockId) implements FeatureConfig { | ||
public static final Codec<PaleMossPatchBonemealConfig> CODEC = RecordCodecBuilder.create( | ||
instance -> instance.group( | ||
// you can add as many of these as you want, one for each parameter | ||
Codecs.POSITIVE_INT.fieldOf("number").forGetter(PaleMossPatchBonemealConfig::number), | ||
Identifier.CODEC.fieldOf("blockID").forGetter(PaleMossPatchBonemealConfig::blockId)) | ||
.apply(instance, PaleMossPatchBonemealConfig::new)); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/rs/onako2/feature/pale_moss_patch_bonemeal/PaleMossPatchBonemealFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package rs.onako2.feature.pale_moss_patch_bonemeal; | ||
|
||
import com.mojang.serialization.Codec; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.registry.tag.BlockTags; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.random.Random; | ||
import net.minecraft.world.StructureWorldAccess; | ||
import net.minecraft.world.gen.feature.Feature; | ||
import net.minecraft.world.gen.feature.util.FeatureContext; | ||
|
||
public class PaleMossPatchBonemealFeature extends Feature<PaleMossPatchBonemealConfig> { | ||
public PaleMossPatchBonemealFeature(Codec<PaleMossPatchBonemealConfig> configCodec) { | ||
super(configCodec); | ||
} | ||
|
||
@Override | ||
public boolean generate(FeatureContext<PaleMossPatchBonemealConfig> context) { | ||
StructureWorldAccess world = context.getWorld(); | ||
// the origin is the place where the game starts trying to place the feature | ||
BlockPos origin = context.getOrigin(); | ||
// we won't use the random here, but we could if we wanted to | ||
Random random = context.getRandom(); | ||
PaleMossPatchBonemealConfig config = context.getConfig(); | ||
|
||
// don't worry about where these come from-- we'll implement these methods soon | ||
int number = config.number(); | ||
Identifier blockId = config.blockId(); | ||
|
||
BlockState blockState = Registries.BLOCK.get(blockId).getDefaultState(); | ||
// ensure the ID is okay | ||
if (blockState == null) | ||
throw new IllegalStateException(blockId + " could not be parsed to a valid block identifier!"); | ||
|
||
// find the surface of the world | ||
BlockPos testPos = new BlockPos(origin); | ||
for (int y = 0; y < world.getHeight(); y++) { | ||
testPos = testPos.up(); | ||
// the tag name is dirt, but includes grass, mud, podzol, etc. | ||
if (world.getBlockState(testPos).isIn(BlockTags.DIRT)) { | ||
if (world.getBlockState(testPos.up()).isOf(Blocks.AIR)) { | ||
for (int i = 0; i < number; i++) { | ||
// create a simple pillar of blocks | ||
world.setBlockState(testPos, blockState, 0x10); | ||
testPos = testPos.up(); | ||
|
||
// ensure we don't try to place blocks outside the world | ||
if (testPos.getY() >= world.getTopY()) break; | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
// the game couldn't find a place to put the pillar | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"replace": false, | ||
"values": [ | ||
"iwie:pale_oak_log" | ||
] | ||
} |