package com.carrot.carrotshop.listener;

import java.util.Optional;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import com.carrot.carrotshop.CarrotShop;
import com.carrot.carrotshop.ShopsData;
import com.carrot.carrotshop.shop.Shop;


public class PlayerClickListener {
	@Listener(order=Order.FIRST)
	public void onPlayerRightClick(InteractBlockEvent.Secondary.MainHand event, @First Player player)
	{
		Optional<Location<World>> optLoc = event.getTargetBlock().getLocation();
		if (!optLoc.isPresent())
			return;

		Optional<Shop> shop = ShopsData.getShop(optLoc.get());
		if (shop.isPresent()) {
			if (optLoc.get().getBlockType() == BlockTypes.STANDING_SIGN || optLoc.get().getBlockType() == BlockTypes.WALL_SIGN) {
				event.setCancelled(true);
				shop.get().trigger(player);
				Sponge.getScheduler().createTaskBuilder().delayTicks(4).execute(
						task -> {
							shop.get().update();
							task.cancel();
						}).submit(CarrotShop.getInstance());
			}
		}
	}

	@Listener(order=Order.FIRST)
	public void onPlayerLeftClickMaster(InteractBlockEvent.Primary.MainHand event, @First Player player)
	{
		Optional<Location<World>> optLoc = event.getTargetBlock().getLocation();
		if (!optLoc.isPresent())
			return;

		Optional<Shop> shop = ShopsData.getShop(optLoc.get());
		if (shop.isPresent()) {
			shop.get().info(player);
			if (player.gameMode().get().equals(GameModes.CREATIVE)) {
				Optional<ItemStack> optItem = player.getItemInHand(HandTypes.MAIN_HAND);
				if (!optItem.isPresent() || !optItem.get().getItem().equals(ItemTypes.BEDROCK))
					event.setCancelled(true);
			}
		}		
	}

	@Listener
	public void onPlayerLeftClickNormal(InteractBlockEvent.Primary.MainHand event, @First Player player)
	{
		Optional<Location<World>> optLoc = event.getTargetBlock().getLocation();
		if (!optLoc.isPresent())
			return;

		Optional<ItemStack> optItem = player.getItemInHand(HandTypes.MAIN_HAND);
		if (optItem.isPresent() && optItem.get().getItem().equals(ItemTypes.REDSTONE)) {
			if (optLoc.get().getBlockType() == BlockTypes.CHEST || optLoc.get().getBlockType() == BlockTypes.TRAPPED_CHEST) {
				event.setCancelled(true);
				ShopsData.storeItemLocation(player, optLoc.get());
			} else if (optLoc.get().getBlockType() == BlockTypes.STANDING_SIGN || optLoc.get().getBlockType() == BlockTypes.WALL_SIGN) {
				event.setCancelled(true);
				Shop.build(player, optLoc.get());
			}
		}
	}
}

@@ -888,37 +888,69 @@
 ner(order=Order.
-FIRST
+AFTER_PRE, beforeModifications = true
 )%0A%09public void o
@@ -1359,38 +1359,8 @@
 ) %7B%0A
-%09%09%09%09event.setCancelled(true);%0A
 %09%09%09%09
@@ -1605,13 +1605,45 @@
 der.
-FIRST
+AFTER_PRE, beforeModifications = true
 )%0A%09p
@@ -1935,26 +1935,24 @@
 isPresent())
- %7B
 %0A%09%09%09shop.get
@@ -1969,22 +1969,190 @@
 ayer);%0A%09
+%7D%0A%0A%09@Listener(order=Order.FIRST, beforeModifications = true)%0A%09public void onPlayerLeftClickProtect(InteractBlockEvent.Primary.MainHand event, @First Player player)%0A%09%7B%0A
 %09%09if (
+!
 player.g
@@ -2194,20 +2194,228 @@
 EATIVE))
+%0A%09%09%09return;%0A%09%09%0A%09%09Optional%3CLocation%3CWorld%3E%3E optLoc = event.getTargetBlock().getLocation();%0A%09%09if (!optLoc.isPresent())%0A%09%09%09return;%0A%0A%09%09Optional%3CShop%3E shop = ShopsData.getShop(optLoc.get());%0A%09%09if (shop.isPresent())
  %7B%0A
-%09
 %09%09%09Optio
@@ -2603,16 +2603,9 @@
 ;%0A%09%09
-%09%7D%0A%09%09%7D%09%09
+%7D
 %0A%09%7D%0A
@@ -2615,16 +2615,44 @@
 Listener
+(beforeModifications = true)
 %0A%09public

Fix a problem with protections plugin blocking the interactions