NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockOakGenerator.java

90 lines
3.7 KiB
Java
Raw Normal View History

2018-11-14 19:14:03 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.api.render.IVisualizable;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityOakGenerator;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.reg.ICustomBlockState;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.data.worldgen.features.TreeFeatures;
import net.minecraft.resources.ResourceKey;
2021-12-15 16:24:53 +01:00
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.SaplingBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
2021-12-15 16:24:53 +01:00
import net.minecraft.world.phys.AABB;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.level.SaplingGrowTreeEvent;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
2018-11-14 19:14:03 +01:00
2020-01-29 01:34:58 +01:00
public class BlockOakGenerator extends BlockContainerImpl implements IVisualizable, ICustomBlockState {
2018-11-14 19:14:03 +01:00
public BlockOakGenerator() {
2023-07-08 12:32:27 +02:00
super("oak_generator", BlockEntityOakGenerator.class, Properties.of().strength(2F).sound(SoundType.WOOD));
2018-11-14 19:14:03 +01:00
2020-01-21 21:04:44 +01:00
MinecraftForge.EVENT_BUS.register(this);
2018-11-14 19:14:03 +01:00
}
@SubscribeEvent
public void onTreeGrow(SaplingGrowTreeEvent event) {
var level = event.getLevel();
2021-12-15 16:30:22 +01:00
var pos = event.getPos();
2021-12-15 16:24:53 +01:00
if (level instanceof Level && !level.isClientSide() && IAuraType.forLevel((Level) level).isSimilar(NaturesAuraAPI.TYPE_OVERWORLD)
2021-12-04 15:40:09 +01:00
&& level.getBlockState(pos).getBlock() instanceof SaplingBlock) {
2021-12-04 19:17:21 +01:00
Helper.getBlockEntitiesInArea(level, pos, 10, tile -> {
if (!(tile instanceof BlockEntityOakGenerator oak))
2018-11-14 19:14:03 +01:00
return false;
var replacement = BlockOakGenerator.getReplacement(event.getFeature());
if (replacement != null) {
oak.scheduledBigTrees.add(pos);
event.setFeature(replacement);
}
2018-11-14 19:14:03 +01:00
return true;
});
}
}
2019-01-27 13:57:34 +01:00
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
public AABB getVisualizationBounds(Level level, BlockPos pos) {
2021-12-15 16:24:53 +01:00
return new AABB(pos).inflate(10);
2019-01-27 13:57:34 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2021-12-04 15:40:09 +01:00
public int getVisualizationColor(Level level, BlockPos pos) {
2019-01-27 13:57:34 +01:00
return 0x2e7a11;
}
2020-01-29 01:34:58 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().cubeBottomTop(this.getBaseName(),
generator.modLoc("block/" + this.getBaseName()),
generator.modLoc("block/" + this.getBaseName() + "_bottom"),
generator.modLoc("block/" + this.getBaseName() + "_top")));
}
private static ResourceKey<ConfiguredFeature<?, ?>> getReplacement(Holder<ConfiguredFeature<?, ?>> holder) {
if(holder == null || !holder.unwrapKey().isPresent())
return null;
ResourceKey<ConfiguredFeature<?, ?>> feature = holder.unwrapKey().get();
if (feature == TreeFeatures.FANCY_OAK || feature == TreeFeatures.FANCY_OAK_BEES) {
return TreeFeatures.OAK;
} else if (feature == TreeFeatures.FANCY_OAK_BEES_002) {
return TreeFeatures.OAK_BEES_002;
} else if (feature == TreeFeatures.FANCY_OAK_BEES_0002) {
return TreeFeatures.OAK_BEES_0002;
} else if (feature == TreeFeatures.FANCY_OAK_BEES_005) {
return TreeFeatures.OAK_BEES_005;
} else {
return null;
}
}
2018-11-14 19:14:03 +01:00
}