2018-10-15 18:36:46 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2018-11-11 17:09:26 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2020-01-29 00:40:28 +01:00
|
|
|
import de.ellpeck.naturesaura.data.BlockStateGenerator;
|
2018-10-15 18:36:46 +02:00
|
|
|
import de.ellpeck.naturesaura.reg.*;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.client.color.block.BlockColor;
|
|
|
|
import net.minecraft.client.color.item.ItemColor;
|
|
|
|
import net.minecraft.client.renderer.BiomeColors;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.Block;
|
|
|
|
import net.minecraft.world.level.block.LeavesBlock;
|
|
|
|
import net.minecraft.world.level.block.SoundType;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
|
|
import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
|
|
|
import net.minecraft.world.level.material.Material;
|
|
|
|
import net.minecraft.world.level.material.MaterialColor;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2018-10-15 18:36:46 +02:00
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
2020-01-29 00:40:28 +01:00
|
|
|
public class BlockGoldenLeaves extends LeavesBlock implements IModItem, IColorProvidingBlock, IColorProvidingItem, ICustomBlockState {
|
2018-10-15 18:36:46 +02:00
|
|
|
|
2020-01-24 17:05:41 +01:00
|
|
|
public static final int HIGHEST_STAGE = 3;
|
|
|
|
public static final IntegerProperty STAGE = IntegerProperty.create("stage", 0, HIGHEST_STAGE);
|
2018-10-15 18:36:46 +02:00
|
|
|
|
|
|
|
public BlockGoldenLeaves() {
|
2021-12-22 23:29:40 +01:00
|
|
|
super(Properties.of(Material.LEAVES, MaterialColor.GOLD).strength(0.2F).randomTicks().noOcclusion().sound(SoundType.GRASS));
|
2018-11-29 17:58:47 +01:00
|
|
|
ModRegistry.add(this);
|
2018-10-15 18:36:46 +02:00
|
|
|
}
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public static boolean convert(Level level, BlockPos pos) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var state = level.getBlockState(pos);
|
2020-09-22 03:17:02 +02:00
|
|
|
if (state.getBlock() instanceof LeavesBlock && !(state.getBlock() instanceof BlockAncientLeaves || state.getBlock() instanceof BlockGoldenLeaves)) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!level.isClientSide) {
|
2021-12-15 14:26:42 +01:00
|
|
|
level.setBlockAndUpdate(pos, ModBlocks.GOLDEN_LEAVES.defaultBlockState()
|
|
|
|
.setValue(DISTANCE, state.hasProperty(DISTANCE) ? state.getValue(DISTANCE) : 1)
|
|
|
|
.setValue(PERSISTENT, state.hasProperty(PERSISTENT) ? state.getValue(PERSISTENT) : false));
|
2020-02-07 15:22:30 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-15 18:36:46 +02:00
|
|
|
@Override
|
|
|
|
public String getBaseName() {
|
|
|
|
return "golden_leaves";
|
|
|
|
}
|
|
|
|
|
2018-11-11 17:09:26 +01:00
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2021-12-04 15:40:09 +01:00
|
|
|
public void animateTick(BlockState stateIn, Level levelIn, BlockPos pos, Random rand) {
|
2021-12-15 14:26:42 +01:00
|
|
|
if (stateIn.getValue(STAGE) == HIGHEST_STAGE && rand.nextFloat() >= 0.75F)
|
2018-11-13 00:36:47 +01:00
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(
|
2018-11-11 17:09:26 +01:00
|
|
|
pos.getX() + rand.nextFloat(),
|
|
|
|
pos.getY() + rand.nextFloat(),
|
|
|
|
pos.getZ() + rand.nextFloat(),
|
|
|
|
0F, 0F, 0F,
|
|
|
|
0xF2FF00, 0.5F + rand.nextFloat(), 50, 0F, false, true);
|
|
|
|
}
|
|
|
|
|
2018-10-15 18:36:46 +02:00
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
|
|
super.createBlockStateDefinition(builder);
|
2019-11-04 19:08:49 +01:00
|
|
|
builder.add(STAGE);
|
2018-10-15 18:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2021-12-15 14:26:42 +01:00
|
|
|
public BlockColor getBlockColor() {
|
2021-12-04 15:40:09 +01:00
|
|
|
return (state, levelIn, pos, tintIndex) -> {
|
2021-12-15 16:30:22 +01:00
|
|
|
var color = 0xF2FF00;
|
2021-12-04 15:40:09 +01:00
|
|
|
if (state != null && levelIn != null && pos != null) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var foliage = BiomeColors.getAverageFoliageColor(levelIn, pos);
|
2021-12-15 14:26:42 +01:00
|
|
|
return Helper.blendColors(color, foliage, state.getValue(STAGE) / (float) HIGHEST_STAGE);
|
2018-10-15 18:36:46 +02:00
|
|
|
} else {
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2021-12-15 14:26:42 +01:00
|
|
|
public ItemColor getItemColor() {
|
2018-10-15 18:36:46 +02:00
|
|
|
return (stack, tintIndex) -> 0xF2FF00;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void randomTick(BlockState state, ServerLevel levelIn, BlockPos pos, Random random) {
|
|
|
|
super.randomTick(state, levelIn, pos, random);
|
|
|
|
if (!levelIn.isClientSide) {
|
2021-12-15 14:26:42 +01:00
|
|
|
int stage = state.getValue(STAGE);
|
2018-10-15 18:36:46 +02:00
|
|
|
if (stage < HIGHEST_STAGE) {
|
2021-12-15 14:26:42 +01:00
|
|
|
levelIn.setBlockAndUpdate(pos, state.setValue(STAGE, stage + 1));
|
2018-10-15 18:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stage > 1) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var offset = pos.relative(Direction.getRandom(random));
|
2021-12-15 14:26:42 +01:00
|
|
|
if (levelIn.isLoaded(offset))
|
2021-12-04 15:40:09 +01:00
|
|
|
convert(levelIn, offset);
|
2018-10-15 18:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 17:05:41 +01:00
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public boolean isRandomlyTicking(BlockState p_54449_) {
|
2020-01-24 17:05:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-29 00:40:28 +01:00
|
|
|
@Override
|
|
|
|
public void generateCustomBlockState(BlockStateGenerator generator) {
|
|
|
|
generator.simpleBlock(this, generator.models().getExistingFile(generator.modLoc(this.getBaseName())));
|
|
|
|
}
|
2018-10-15 18:36:46 +02:00
|
|
|
}
|