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.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;
		}
		event
		.getTransactions()
		.stream()
		.forEach(trans -> trans.getOriginal().getLocation().ifPresent(loc -> {
			if (!trans.getFinal().getState().getType().getId().equals(ConfigHandler.getNode("others", "gravestoneBlock").getString("gravestone:gravestone"))) {
				trans.setValid(DataHandler.getPerm("build", player.getUniqueId(), loc));
				if(!DataHandler.getPerm("build", player.getUniqueId(), loc))
				{
					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 -> {
			trans.setValid(DataHandler.getPerm("build", player.getUniqueId(), loc));
			if(!DataHandler.getPerm("build", player.getUniqueId(), loc))
			{
				try {
					player.sendMessage(Text.of(TextColors.RED, LanguageHandler.HH));
				} catch (Exception e) {}
			}
		}));
	}
}

@@ -34,16 +34,74 @@
 tener;%0A%0A
+import org.spongepowered.api.data.value.mutable.SetValue;%0A
 import o
@@ -146,24 +146,24 @@
 yer.Player;%0A
-
 import org.s
@@ -937,32 +937,140 @@
 %0A%09%09%09return;%0A%09%09%7D%0A
+%09%09String graveItem = ConfigHandler.getNode(%22others%22, %22gravestoneBlock%22).getString(%22gravestone:gravestone%22);%0A
 %09%09event%0A%09%09.getTr
@@ -1232,175 +1232,22 @@
 als(
-ConfigHandler.getNode(%22others%22, %22gravestoneBlock%22).getString(%22gravestone:gravestone%22
+graveItem
 ))
-)
  %7B%0A
-%09%09%09%09trans.setValid(DataHandler.getPerm(%22build%22, player.getUniqueId(), loc));%0A
 %09%09%09%09
@@ -1309,24 +1309,52 @@
 loc))%0A%09%09%09%09%7B%0A
+%09%09%09%09%09trans.setValid(false);%0A
 %09%09%09%09%09try %7B%0A%09
@@ -1926,84 +1926,8 @@
 %3E %7B%0A
-%09%09%09trans.setValid(DataHandler.getPerm(%22build%22, player.getUniqueId(), loc));%0A
 %09%09%09i
@@ -1986,21 +1986,48 @@
 , loc))%0A
-
 %09%09%09%7B%0A
+%09%09%09%09trans.setValid(false);%0A
 %09%09%09%09try 

Optimize BuildPermListener