package com.gmail.stefvanschiedev.buildinggame.events.player;

import com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager;
import com.gmail.stefvanschiedev.buildinggame.utils.arena.Arena;
import com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.PlayerInventory;

/**
 * An event that makes it so players can place blocks through spectators. This makes it easier for players to build and
 * people now can't troll others by blocking their view.
 * 
 * @since 5.0.4
 */
public class PlaceIgnoreSpectators implements Listener {

    /**
     * Handles players trying to place a block where a spectator is in the way
     *
     * @param e the event used for this algorithm
     */
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        Player player = e.getPlayer();
        Arena arena = ArenaManager.getInstance().getArena(player);
        Block clickedBlock = e.getClickedBlock();

        //check if the player is actually playing and the clicked entity is a player
        if (arena == null || clickedBlock == null)
            return;

        boolean nearbySpectator = false;
        Block relativeBlock = clickedBlock.getRelative(e.getBlockFace());

        //check if there's a spectator nearby
        for (GamePlayer spectator : arena.getPlot(player).getSpectators()) {
            //the value 1.8 is the result of the pythagorean theorem applied in 3 dimensional space.
            //The actual value is a little lower than 1.8 (1.771...), but overestimating the value isn't a problem in
            //this case, as the result will be the same (the player won't place the block, but we'll do it for them)
            if (spectator.getPlayer().getLocation().distanceSquared(relativeBlock.getLocation()) <= 1.8)
                nearbySpectator = true;
        }

        if (!nearbySpectator)
            return;

        PlayerInventory inventory = player.getInventory();

        //set block
        relativeBlock.setType((e.getHand() == EquipmentSlot.HAND ? inventory.getItemInMainHand() :
                inventory.getItemInOffHand()).getType());
        relativeBlock.setData((byte) (e.getHand() == EquipmentSlot.HAND ? inventory.getItemInMainHand() :
                inventory.getItemInOffHand()).getDurability());
    }
}
@@ -264,32 +264,60 @@
 yer.GamePlayer;%0A
+import org.bukkit.Location;%0A
 import org.bukki
@@ -1533,16 +1533,78 @@
 Face());
+%0A        Location blockLocation = relativeBlock.getLocation();
 %0A%0A      
@@ -1716,24 +1716,281 @@
 tators()) %7B%0A
+            Location location = spectator.getPlayer().getLocation();%0A%0A            //check if the locations are in the same world before measuring the distance%0A            if (!location.getWorld().equals(blockLocation.getWorld()))%0A                continue;%0A%0A
             
@@ -2333,43 +2333,16 @@
 if (
-spectator.getPlayer().getL
+l
 ocation
-()
 .dis
@@ -2358,33 +2358,21 @@
 red(
-relativeBlock.get
+block
 Location
 ()) 
@@ -2367,18 +2367,16 @@
 Location
-()
 ) %3C= 1.8

Fix issues with spectators in different worlds