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

70 lines
2.7 KiB
Java
Raw Normal View History

2020-09-28 16:45:37 +02:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.Helper;
2020-09-28 17:18:40 +02:00
import de.ellpeck.naturesaura.api.render.IVisualizable;
2020-09-28 16:45:37 +02:00
import de.ellpeck.naturesaura.blocks.tiles.TileEntitySlimeSplitGenerator;
2020-09-28 17:18:40 +02:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.reg.ICustomBlockState;
2020-09-28 16:45:37 +02:00
import net.minecraft.block.Blocks;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.monster.SlimeEntity;
2020-09-28 17:18:40 +02:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2020-09-28 16:45:37 +02:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
2020-09-28 17:18:40 +02:00
public class BlockSlimeSplitGenerator extends BlockContainerImpl implements IVisualizable, ICustomBlockState {
2020-09-28 16:45:37 +02:00
public BlockSlimeSplitGenerator() {
super("slime_split_generator", TileEntitySlimeSplitGenerator::new, Properties.from(Blocks.SLIME_BLOCK).hardnessAndResistance(2));
MinecraftForge.EVENT_BUS.register(new Events());
}
2020-09-28 17:18:40 +02: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")));
}
@Override
@OnlyIn(Dist.CLIENT)
public AxisAlignedBB getVisualizationBounds(World world, BlockPos pos) {
return new AxisAlignedBB(pos).grow(8);
}
@Override
@OnlyIn(Dist.CLIENT)
public int getVisualizationColor(World world, BlockPos pos) {
return 0x4da84f;
}
2020-09-28 16:45:37 +02:00
private static class Events {
@SubscribeEvent
public void onLivingDeath(LivingDeathEvent event) {
LivingEntity entity = event.getEntityLiving();
if (!(entity instanceof SlimeEntity) || entity.world.isRemote)
return;
SlimeEntity slime = (SlimeEntity) entity;
int size = slime.getSlimeSize();
if (size <= 1)
return;
Helper.getTileEntitiesInArea(entity.world, entity.getPosition(), 8, tile -> {
if (!(tile instanceof TileEntitySlimeSplitGenerator))
return false;
TileEntitySlimeSplitGenerator gen = (TileEntitySlimeSplitGenerator) tile;
if (gen.isBusy())
return false;
gen.startGenerating(slime);
return true;
});
}
}
}