Private
Public Access
1
0

Ported ModItems and ModBlocks. Partially ported FireflyBushBlock.

This commit is contained in:
2026-05-25 15:38:21 +01:00
parent 0fb17e6834
commit 33412dcf93
3 changed files with 50 additions and 42 deletions

View File

@ -3,36 +3,37 @@ package dev.micle.firefly_bush_backport.block;
import dev.micle.firefly_bush_backport.config.Config; import dev.micle.firefly_bush_backport.config.Config;
import dev.micle.firefly_bush_backport.particle.ModParticles; import dev.micle.firefly_bush_backport.particle.ModParticles;
import dev.micle.firefly_bush_backport.sound.ModSounds; import dev.micle.firefly_bush_backport.sound.ModSounds;
import net.minecraft.core.BlockPos; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.core.Direction; import net.minecraft.block.BlockState;
import net.minecraft.server.level.ServerLevel; import net.minecraft.block.BushBlock;
import net.minecraft.sounds.SoundSource; import net.minecraft.block.IGrowable;
import net.minecraft.util.RandomSource; import net.minecraft.util.Direction;
import net.minecraft.world.level.Level; import net.minecraft.util.SoundCategory;
import net.minecraft.world.level.LevelReader; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.level.block.BonemealableBlock; import net.minecraft.world.IBlockReader;
import net.minecraft.world.level.block.BushBlock; import net.minecraft.world.IWorldReader;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.World;
import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.server.ServerWorld;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List; import java.util.*;
import java.util.Optional; import java.util.stream.Collectors;
public class FireflyBushBlock extends BushBlock implements BonemealableBlock { public class FireflyBushBlock extends BushBlock implements IGrowable {
public FireflyBushBlock(Properties properties) { public FireflyBushBlock(Properties properties) {
super(properties); super(properties);
} }
@Override @Override
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public void animateTick(BlockState blockState, Level level, BlockPos blockPos, RandomSource randomSource) { public void animateTick(BlockState blockState, World level, BlockPos blockPos, Random randomSource) {
if (randomSource.nextInt(Config.Client.bushFireflyAmbientSoundChanceOneIn.get()) == 0 && ( if (randomSource.nextInt(Config.Client.bushFireflyAmbientSoundChanceOneIn.get()) == 0 && (
Config.Client.bushExtraAlwaysPlayAmbientSound.get() || Config.Client.bushExtraAlwaysPlayAmbientSound.get() ||
(isMoonVisible(level) && level.getHeight(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, blockPos.getX(), blockPos.getZ()) <= blockPos.getY()) (isMoonVisible(level) && level.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, blockPos.getX(), blockPos.getZ()) <= blockPos.getY())
)) { )) {
level.playLocalSound( level.playLocalSound(
blockPos, ModSounds.FIREFLY_BUSH_IDLE.get(), SoundSource.AMBIENT, 1.0F, 1.0F, false blockPos, ModSounds.FIREFLY_BUSH_IDLE.get(), SoundCategory.AMBIENT, 1.0F, 1.0F, false
); );
} }
@ -47,7 +48,7 @@ public class FireflyBushBlock extends BushBlock implements BonemealableBlock {
} }
} }
public boolean isMoonVisible(Level level) { public boolean isMoonVisible(World level) {
if (!level.dimensionType().natural()) { if (!level.dimensionType().natural()) {
return false; return false;
} else { } else {
@ -58,37 +59,47 @@ public class FireflyBushBlock extends BushBlock implements BonemealableBlock {
@Override @Override
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public boolean isValidBonemealTarget(LevelReader level, BlockPos blockPos, BlockState blockState, boolean isClient) { public boolean isValidBonemealTarget(IBlockReader level, BlockPos blockPos, BlockState blockState, boolean isClient) {
return hasSpreadableNeighbourPos(level, blockPos, blockState); return hasSpreadableNeighbourPos(level, blockPos, blockState);
} }
@Override @Override
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public boolean isBonemealSuccess(Level level, RandomSource randomSource, BlockPos blockPos, BlockState blockState) { public boolean isBonemealSuccess(World level, Random randomSource, BlockPos blockPos, BlockState blockState) {
return true; return true;
} }
@Override @Override
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public void performBonemeal(ServerLevel level, RandomSource randomSource, BlockPos blockPos, BlockState blockState) { public void performBonemeal(ServerWorld level, Random randomSource, BlockPos blockPos, BlockState blockState) {
findSpreadableNeighbourPos(level, blockPos, blockState).ifPresent(x -> level.setBlockAndUpdate(x, this.defaultBlockState())); findSpreadableNeighbourPos(level, blockPos, blockState).ifPresent(x -> level.setBlockAndUpdate(x, this.defaultBlockState()));
} }
static boolean hasSpreadableNeighbourPos(LevelReader levelReader, BlockPos blockPos, BlockState blockState) { private static boolean hasSpreadableNeighbourPos(IBlockReader levelReader, BlockPos blockPos, BlockState blockState) {
return getSpreadableNeighbourPos(Direction.Plane.HORIZONTAL.stream().toList(), levelReader, blockPos, blockState).isPresent(); return getSpreadableNeighbourPos(Direction.Plane.HORIZONTAL.stream().collect(Collectors.toList()), levelReader, blockPos, blockState).isPresent();
} }
static Optional<BlockPos> findSpreadableNeighbourPos(Level level, BlockPos blockPos, BlockState blockState) { private static Optional<BlockPos> findSpreadableNeighbourPos(World level, BlockPos blockPos, BlockState blockState) {
return getSpreadableNeighbourPos(Direction.Plane.HORIZONTAL.shuffledCopy(level.random), level, blockPos, blockState); return getSpreadableNeighbourPos(shuffledCopy((Direction[])Direction.Plane.HORIZONTAL.stream().toArray(), level.random), level, blockPos, blockState);
} }
static Optional<BlockPos> getSpreadableNeighbourPos(List<Direction> directions, LevelReader levelReader, BlockPos blockPos, BlockState blockState) { private static Optional<BlockPos> getSpreadableNeighbourPos(List<Direction> directions, IBlockReader levelReader, BlockPos blockPos, BlockState blockState) {
for (Direction direction : directions) { for (Direction direction : directions) {
BlockPos blockPosDirection = blockPos.relative(direction); BlockPos blockPosDirection = blockPos.relative(direction);
if (levelReader.isEmptyBlock(blockPosDirection) && blockState.canSurvive(levelReader, blockPosDirection)) { if (levelReader.getBlockState(blockPosDirection).isAir() && blockState.canSurvive((IWorldReader) levelReader, blockPosDirection)) {
return Optional.of(blockPosDirection); return Optional.of(blockPosDirection);
} }
} }
return Optional.empty(); return Optional.empty();
} }
private static <T> List<T> shuffledCopy(T[] array, Random random) {
ObjectArrayList<T> objectArrayList = new ObjectArrayList<>(array);
int i = objectArrayList.size();
for (int j = i; j > 1; --j) {
int k = random.nextInt(j);
objectArrayList.set(j - 1, objectArrayList.set(k, objectArrayList.get(j - 1)));
}
return objectArrayList;
}
} }

View File

@ -1,28 +1,25 @@
package dev.micle.firefly_bush_backport.block; package dev.micle.firefly_bush_backport.block;
import dev.micle.firefly_bush_backport.FireflyBushBackport; import dev.micle.firefly_bush_backport.FireflyBushBackport;
import net.minecraft.world.level.block.Block; import net.minecraft.block.AbstractBlock;
import net.minecraft.world.level.block.SoundType; import net.minecraft.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.block.SoundType;
import net.minecraft.world.level.material.MapColor; import net.minecraft.block.material.Material;
import net.minecraft.world.level.material.PushReaction; import net.minecraft.block.material.MaterialColor;
import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public class ModBlocks { public class ModBlocks {
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, FireflyBushBackport.MOD_ID); public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, FireflyBushBackport.MOD_ID);
public static RegistryObject<Block> FIREFLY_BUSH = BLOCKS.register("firefly_bush", public static RegistryObject<Block> FIREFLY_BUSH = BLOCKS.register("firefly_bush",
() -> new FireflyBushBlock(BlockBehaviour.Properties.of() () -> new FireflyBushBlock(AbstractBlock.Properties.of(Material.PLANT, MaterialColor.PLANT)
.mapColor(MapColor.PLANT)
.ignitedByLava()
.lightLevel(blockState -> 2) .lightLevel(blockState -> 2)
.noCollission() .noCollission()
.instabreak() .instabreak()
.sound(SoundType.SWEET_BERRY_BUSH) .sound(SoundType.SWEET_BERRY_BUSH))
.pushReaction(PushReaction.DESTROY))
); );
public static void register(IEventBus modEventBus) { public static void register(IEventBus modEventBus) {

View File

@ -2,13 +2,13 @@ package dev.micle.firefly_bush_backport.item;
import dev.micle.firefly_bush_backport.FireflyBushBackport; import dev.micle.firefly_bush_backport.FireflyBushBackport;
import dev.micle.firefly_bush_backport.block.ModBlocks; import dev.micle.firefly_bush_backport.block.ModBlocks;
import net.minecraft.world.item.BlockItem; import net.minecraft.block.Block;
import net.minecraft.world.item.Item; import net.minecraft.item.BlockItem;
import net.minecraft.world.level.block.Block; import net.minecraft.item.Item;
import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import java.util.Objects; import java.util.Objects;