Ported ModItems and ModBlocks. Partially ported FireflyBushBlock.
This commit is contained in:
@ -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.particle.ModParticles;
|
||||
import dev.micle.firefly_bush_backport.sound.ModSounds;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.LevelReader;
|
||||
import net.minecraft.world.level.block.BonemealableBlock;
|
||||
import net.minecraft.world.level.block.BushBlock;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.levelgen.Heightmap;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.BushBlock;
|
||||
import net.minecraft.block.IGrowable;
|
||||
import net.minecraft.util.Direction;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockReader;
|
||||
import net.minecraft.world.IWorldReader;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.gen.Heightmap;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FireflyBushBlock extends BushBlock implements BonemealableBlock {
|
||||
public class FireflyBushBlock extends BushBlock implements IGrowable {
|
||||
public FireflyBushBlock(Properties properties) {
|
||||
super(properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
@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 && (
|
||||
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(
|
||||
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()) {
|
||||
return false;
|
||||
} else {
|
||||
@ -58,37 +59,47 @@ public class FireflyBushBlock extends BushBlock implements BonemealableBlock {
|
||||
|
||||
@Override
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
@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()));
|
||||
}
|
||||
|
||||
static boolean hasSpreadableNeighbourPos(LevelReader levelReader, BlockPos blockPos, BlockState blockState) {
|
||||
return getSpreadableNeighbourPos(Direction.Plane.HORIZONTAL.stream().toList(), levelReader, blockPos, blockState).isPresent();
|
||||
private static boolean hasSpreadableNeighbourPos(IBlockReader levelReader, BlockPos blockPos, BlockState blockState) {
|
||||
return getSpreadableNeighbourPos(Direction.Plane.HORIZONTAL.stream().collect(Collectors.toList()), levelReader, blockPos, blockState).isPresent();
|
||||
}
|
||||
|
||||
static Optional<BlockPos> findSpreadableNeighbourPos(Level level, BlockPos blockPos, BlockState blockState) {
|
||||
return getSpreadableNeighbourPos(Direction.Plane.HORIZONTAL.shuffledCopy(level.random), level, blockPos, blockState);
|
||||
private static Optional<BlockPos> findSpreadableNeighbourPos(World level, BlockPos blockPos, BlockState 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) {
|
||||
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.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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,28 +1,25 @@
|
||||
package dev.micle.firefly_bush_backport.block;
|
||||
|
||||
import dev.micle.firefly_bush_backport.FireflyBushBackport;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.SoundType;
|
||||
import net.minecraft.world.level.block.state.BlockBehaviour;
|
||||
import net.minecraft.world.level.material.MapColor;
|
||||
import net.minecraft.world.level.material.PushReaction;
|
||||
import net.minecraft.block.AbstractBlock;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.SoundType;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.material.MaterialColor;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
|
||||
public class ModBlocks {
|
||||
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, FireflyBushBackport.MOD_ID);
|
||||
|
||||
public static RegistryObject<Block> FIREFLY_BUSH = BLOCKS.register("firefly_bush",
|
||||
() -> new FireflyBushBlock(BlockBehaviour.Properties.of()
|
||||
.mapColor(MapColor.PLANT)
|
||||
.ignitedByLava()
|
||||
() -> new FireflyBushBlock(AbstractBlock.Properties.of(Material.PLANT, MaterialColor.PLANT)
|
||||
.lightLevel(blockState -> 2)
|
||||
.noCollission()
|
||||
.instabreak()
|
||||
.sound(SoundType.SWEET_BERRY_BUSH)
|
||||
.pushReaction(PushReaction.DESTROY))
|
||||
.sound(SoundType.SWEET_BERRY_BUSH))
|
||||
);
|
||||
|
||||
public static void register(IEventBus modEventBus) {
|
||||
|
||||
@ -2,13 +2,13 @@ package dev.micle.firefly_bush_backport.item;
|
||||
|
||||
import dev.micle.firefly_bush_backport.FireflyBushBackport;
|
||||
import dev.micle.firefly_bush_backport.block.ModBlocks;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.BlockItem;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.RegistryObject;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user