package com.arckenver.nations.listener;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.cause.entity.spawn.EntitySpawnCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import com.arckenver.nations.ConfigHandler;
import com.arckenver.nations.DataHandler;
import com.arckenver.nations.LanguageHandler;

public class BuildPermListener
{

	@Listener(order=Order.FIRST)
	public void onPlayerPlacesBlock(ChangeBlockEvent.Place event, @First Player player)
	{
		if (!ConfigHandler.getNode("worlds").getNode(event.getTargetWorld().getName()).getNode("enabled").getBoolean())
		{
			return;
		}
		if (player.hasPermission("nations.admin.bypass.perm.build"))
		{
			return;
		}
		String graveItem = ConfigHandler.getNode("others", "gravestoneBlock").getString("gravestone:gravestone");
		event
		.getTransactions()
		.stream()
		.forEach(trans -> trans.getOriginal().getLocation().ifPresent(loc -> {
			if (!trans.getFinal().getState().getType().getId().equals(graveItem)) {
				if(!DataHandler.getPerm("build", player.getUniqueId(), loc))
				{
					trans.setValid(false);
					try {
						player.sendMessage(Text.of(TextColors.RED, LanguageHandler.HH));
					} catch (Exception e) {}
				}
			}
		}));
	}

	@Listener(order=Order.FIRST)
	public void onPlayerBreaksBlock(ChangeBlockEvent.Break event, @First Player player)
	{
		if (!ConfigHandler.getNode("worlds").getNode(event.getTargetWorld().getName()).getNode("enabled").getBoolean())
		{
			return;
		}
		if (player.hasPermission("nations.admin.bypass.perm.build"))
		{
			return;
		}
		event
		.getTransactions()
		.stream()
		.forEach(trans -> trans.getOriginal().getLocation().ifPresent(loc -> {
			if(!DataHandler.getPerm("build", player.getUniqueId(), loc))
			{
				trans.setValid(false);
				try {
					player.sendMessage(Text.of(TextColors.RED, LanguageHandler.HH));
				} catch (Exception e) {}
			}
		}));
	}
	
	@Listener(order=Order.FIRST)
	public void onEntitySpawn(SpawnEntityEvent event, @First Player player, @First EntitySpawnCause entitySpawnCause)
	{
		if (!ConfigHandler.getNode("worlds").getNode(event.getTargetWorld().getName()).getNode("enabled").getBoolean())
		{
			return;
		}
		if (player.hasPermission("nations.admin.bypass.perm.build"))
		{
			return;
		}
		if (entitySpawnCause.getType() == SpawnTypes.PLACEMENT)
		{
			try {
			if (!DataHandler.getPerm("build", player.getUniqueId(), event.getEntities().get(0).getLocation()))
				event.setCancelled(true);
			} catch (IndexOutOfBoundsException e) {}
		}
	}
}

@@ -88,24 +88,80 @@
 yer.Player;%0A
+import org.spongepowered.api.entity.living.player.User;%0A
 import org.s
@@ -290,24 +290,93 @@
 BlockEvent;%0A
+import org.spongepowered.api.event.block.tileentity.ChangeSignEvent;%0A
 import org.s
@@ -2407,17 +2407,526 @@
 %7D));%0A%09%7D%0A
-%09
+%0A%09@Listener(order = Order.FIRST, beforeModifications = true)%0A%09public void onSignChanged(ChangeSignEvent event, @First User player)%0A%09%7B%0A%09%09if (!ConfigHandler.getNode(%22worlds%22).getNode(event.getTargetTile().getLocation().getExtent().getName()).getNode(%22enabled%22).getBoolean())%0A%09%09%7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09if (player.hasPermission(%22nations.admin.bypass.perm.build%22))%0A%09%09%7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09if (!DataHandler.getPerm(%22build%22, player.getUniqueId(), event.getTargetTile().getLocation()))%0A%09%09%7B%0A%09%09%09event.setCancelled(true);%0A%09%09%7D%0A%09%7D%0A
 %0A%09@Liste
@@ -3351,24 +3351,25 @@
 %09%09%09try %7B%0A%09%09%09
+%09
 if (!DataHan
@@ -3455,16 +3455,17 @@
 ion()))%0A
+%09
 %09%09%09%09even

Add extra protection to cancel sign edits that can occurs because of network lag