62 lines
1.9 KiB
Java
62 lines
1.9 KiB
Java
package de.jottyfan.minecraft.block;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import de.jottyfan.minecraft.item.QuicklyItems;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.resources.Identifier;
|
|
import net.minecraft.sounds.SoundEvent;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.world.entity.projectile.Projectile;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.storage.loot.LootParams.Builder;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
|
|
/**
|
|
*
|
|
* @author jotty
|
|
*
|
|
*/
|
|
public class BlockOre extends Block {
|
|
|
|
private SoundEvent soundEvent;
|
|
private Identifier[] dropItems;
|
|
|
|
public BlockOre(Properties properties, Identifier... dropItems) {
|
|
super(properties.requiresCorrectToolForDrops());
|
|
this.dropItems = dropItems;
|
|
}
|
|
|
|
public BlockOre(Properties properties, SoundEvent soundEvent, Identifier... dropItems) {
|
|
super(properties.requiresCorrectToolForDrops());
|
|
this.soundEvent = soundEvent;
|
|
this.dropItems = dropItems;
|
|
}
|
|
|
|
@Override
|
|
protected List<ItemStack> getDrops(BlockState state, Builder builder) {
|
|
List<ItemStack> list = new ArrayList<>();
|
|
if (dropItems == null || dropItems.length < 1) {
|
|
list.add(new ItemStack(state.getBlock().asItem()));
|
|
} else {
|
|
for (Identifier identifier : dropItems) {
|
|
list.add(new ItemStack(QuicklyItems.of(identifier)));
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
@Override
|
|
protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult hitResult,
|
|
final Projectile projectile) {
|
|
if (!level.isClientSide() && soundEvent != null) {
|
|
BlockPos hitPos = hitResult.getBlockPos();
|
|
level.playSound(null, hitPos, soundEvent, SoundSource.BLOCKS, 1.0F, 0.5F + level.getRandom().nextFloat() * 1.2F);
|
|
}
|
|
}
|
|
}
|