package net.geforcemods.securitycraft.blockentities;

import java.util.Iterator;
import java.util.List;

import net.geforcemods.securitycraft.SCContent;
import net.geforcemods.securitycraft.api.CustomizableBlockEntity;
import net.geforcemods.securitycraft.api.Option;
import net.geforcemods.securitycraft.api.Option.IntOption;
import net.geforcemods.securitycraft.blocks.mines.ClaymoreBlock;
import net.geforcemods.securitycraft.misc.ModuleType;
import net.geforcemods.securitycraft.util.EntityUtils;
import net.geforcemods.securitycraft.util.ITickingBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

public class ClaymoreBlockEntity extends CustomizableBlockEntity implements ITickingBlockEntity
{
	private IntOption range = new IntOption(this::getBlockPos, "range", 5, 1, 10, 1, true);
	private double entityX = -1D;
	private double entityY = -1D;
	private double entityZ = -1D;
	private int cooldown = -1;

	public ClaymoreBlockEntity(BlockPos pos, BlockState state)
	{
		super(SCContent.beTypeClaymore, pos, state);
	}

	@Override
	public void tick(Level world, BlockPos pos, BlockState state) {  //server only as per ClaymoreBlock
		if(state.getValue(ClaymoreBlock.DEACTIVATED))
			return;

		if(cooldown > 0){
			cooldown--;
			return;
		}

		if(cooldown == 0){
			((ClaymoreBlock)state.getBlock()).explode(world, pos);
			return;
		}

		Direction dir = state.getValue(ClaymoreBlock.FACING);
		AABB area = new AABB(pos);

		if(dir == Direction.NORTH)
			area = area.contract(-0, -0, range.get());
		else if(dir == Direction.SOUTH)
			area = area.contract(-0, -0, -range.get());
		else if(dir == Direction.EAST)
			area = area.contract(-range.get(), -0, -0);
		else if(dir == Direction.WEST)
			area = area.contract(range.get(), -0, -0);

		List<LivingEntity> entities = world.getEntitiesOfClass(LivingEntity.class, area, e -> !EntityUtils.isInvisible(e) && !e.isSpectator());
		Iterator<LivingEntity> iterator = entities.iterator();
		LivingEntity entity;

		while(iterator.hasNext()){
			entity = iterator.next();

			if(EntityUtils.doesEntityOwn(entity, world, pos))
				continue;

			entityX = entity.getX();
			entityY = entity.getY();
			entityZ = entity.getZ();
			cooldown = 20;
			world.playSound(null, new BlockPos(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D), SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3F, 0.6F);
			break;
		}
	}

	/**
	 * Writes a tile entity to NBT.
	 * @return
	 */
	@Override
	public CompoundTag save(CompoundTag tag)
	{
		super.save(tag);
		writeOptions(tag);
		tag.putInt("cooldown", cooldown);
		tag.putDouble("entityX", entityX);
		tag.putDouble("entityY", entityY);
		tag.putDouble("entityZ", entityZ);
		return tag;
	}

	/**
	 * Reads a tile entity from NBT.
	 */
	@Override
	public void load(CompoundTag tag)
	{
		super.load(tag);

		readOptions(tag);
		cooldown = tag.getInt("cooldown");
		entityX = tag.getDouble("entityX");
		entityY = tag.getDouble("entityY");
		entityZ = tag.getDouble("entityZ");
	}

	@Override
	public Option<?>[] customOptions()
	{
		return new Option[]{range};
	}

	@Override
	public ModuleType[] acceptedModules()
	{
		return new ModuleType[0];
	}
}

@@ -51,59 +51,8 @@
 s;%0A%0A
-import java.util.Iterator;%0Aimport java.util.List;%0A%0A
 impo
@@ -1341,21 +1341,21 @@
 k(Level 
-world
+level
 , BlockP
@@ -1381,17 +1381,16 @@
 state) %7B
- 
  //serve
@@ -1594,21 +1594,21 @@
 explode(
-world
+level
 , pos);%0A
@@ -1715,44 +1715,45 @@
 %0A%0A%09%09
-if(dir == Direction.NORTH)%0A%09%09%09area =
+area = switch(dir) %7B%0A%09%09%09case NORTH -%3E
  are
@@ -1763,21 +1763,19 @@
 ontract(
--
 0, 
--
 0, range
@@ -1789,49 +1789,22 @@
 ;%0A%09%09
-else if(dir == Direction.SOUTH)%0A%09%09%09area =
+%09case SOUTH -%3E
  are
@@ -1814,21 +1814,19 @@
 ontract(
--
 0, 
--
 0, -rang
@@ -1841,48 +1841,21 @@
 ;%0A%09%09
-else if(dir == Direction.EAST)%0A%09%09%09area =
+%09case EAST -%3E
  are
@@ -1883,59 +1883,30 @@
 (), 
--
 0, 
--
 0);%0A%09%09
-else if(dir == Direction.WEST)%0A%09%09%09area =
+%09case WEST -%3E
  are
@@ -1933,55 +1933,48 @@
 (), 
--
 0, 
--
 0);%0A
-%0A%09%09List%3CLivingEntity%3E entities = world
+%09%09%09default -%3E area;%0A%09%09%7D;%0A%0A%09%09level
 .get
@@ -2071,218 +2071,100 @@
 or()
-);%0A%09%09Iterator%3CLivingEntity%3E iterator = entities.iterator();%0A%09%09LivingEntity entity;%0A%0A%09%09while(iterator.hasNext(
+ && !EntityUtils.doesEntityOwn(e, level, pos
 ))
-%7B
 %0A%09%09
-%09entity = iterator.next();%0A%0A%09%09%09if(EntityUtils.doesEntityOwn(entity, world, pos))%0A%09%09%09%09continue;%0A
+.stream().findFirst().ifPresent(entity -%3E %7B
 %0A%09%09%09
@@ -2265,21 +2265,21 @@
  20;%0A%09%09%09
-world
+level
 .playSou
@@ -2422,19 +2422,11 @@
 ;%0A%09%09
-%09break;%0A%09%09%7D
+%7D);
 %0A%09%7D%0A

improve claymore be code