2024-11-17 17:48:32 +01:00

90 lines
3.0 KiB
Java

package de.jottyfan.quickiemod.block;
import java.util.List;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.HostileEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager.Builder;
import net.minecraft.state.property.IntProperty;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import net.minecraft.world.tick.OrderedTick;
/**
*
* @author jotty
*
*/
public class BlockMonsterhoarder extends Block {
private static final IntProperty SUCKRADIUS = IntProperty.of("suckradius", 1, 15);
public BlockMonsterhoarder(Identifier identifier) {
super(AbstractBlock.Settings.create().hardness(2.5f).luminance(state -> state.get(BlockMonsterhoarder.SUCKRADIUS))
.registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
setDefaultState(getDefaultState().with(SUCKRADIUS, 8));
}
@Override
protected void appendProperties(Builder<Block, BlockState> builder) {
builder.add(SUCKRADIUS);
super.appendProperties(builder);
}
@Override
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (!world.isClient()) {
world.setBlockState(pos, state.cycle(SUCKRADIUS));
} else {
player.sendMessage(Text.translatable("msg.monsterhoarder.size", state.get(SUCKRADIUS)), false);
}
return ActionResult.SUCCESS;
}
@Override
protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
if (!world.isClient) {
Box box = new Box(pos).expand(Double.valueOf(state.get(SUCKRADIUS)));
List<Entity> entities = world.getOtherEntities(null, box);
for (Entity entity : entities) {
if (entity instanceof HostileEntity hostile) {
if (hostile.isFireImmune()) {
if (world instanceof ServerWorld serverWorld) {
hostile.kill(serverWorld);
}
} else {
hostile.setOnFireFor(90);
}
}
}
}
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos));
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
if (!world.isClient) {
world.getBlockTickScheduler().scheduleTick(OrderedTick.create(this, pos));
world.playSound(null, pos, SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, SoundCategory.PLAYERS, 1f, 1f);
}
super.onPlaced(world, pos, state, placer, itemStack);
}
}