package com.arckenver.mightyloot.listener;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.channel.MessageChannel;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import com.arckenver.mightyloot.DataHandler;
import com.arckenver.mightyloot.LanguageHandler;
import com.arckenver.mightyloot.object.Loot;

public class InteractListener
{
	@Listener
	public void onInteract(InteractBlockEvent event, @First Player player)
	{
		World world = player.getWorld();
		if (!event.getTargetBlock().getLocation().isPresent())
		{
			return;
		}
		Location<World> blockLoc = event.getTargetBlock().getLocation().get();
		if (DataHandler.hasLoot(world.getName()))
		{
			Loot loot = DataHandler.getLoot(world.getName());
			if (loot.getLoc().getBlockPosition().equals(blockLoc.getBlockPosition()) || loot.getLoc().getBlockPosition().add(0, -1, 0).equals(blockLoc.getBlockPosition()))
			{
				DataHandler.unregisterLoot(world.getName());
				
				String[] s1 = LanguageHandler.get("BA").split("\\{LOOT\\}");
				String[] s2 = s1[0].split("\\{PLAYER\\}");
				String[] s3 = s1[1].split("\\{PLAYER\\}");
				
				MessageChannel.TO_ALL.send(Text.builder()
						.append(Text.of(TextColors.GOLD, (s2.length > 0) ? s2[0] : ""))
						.append(Text.of(TextColors.YELLOW, (s2.length > 1) ? player.getName() : ""))
						.append(Text.of(TextColors.GOLD, (s2.length > 1) ? s2[1] : ""))
						.append(loot.getType().getDisplay())
						.append(Text.of(TextColors.GOLD, (s3.length > 0) ? s3[0] : ""))
						.append(Text.of(TextColors.YELLOW, (s3.length > 1) ? player.getName() : ""))
						.append(Text.of(TextColors.GOLD, (s3.length > 1) ? s3[1] : ""))
						.build());
			}
		}
	}
}

@@ -37,16 +37,45 @@
 tener;%0A%0A
+import java.util.ArrayList;%0A%0A
 import o
@@ -966,22 +966,43 @@
 .get();%0A
+%0A
 %09%09
-if (
+ArrayList%3CLoot%3E loots = 
 DataHand
@@ -1009,15 +1009,16 @@
 ler.
-has
+get
 Loot
+s
 (wor
@@ -1023,23 +1023,48 @@
 orld.get
-Name())
+UniqueId());%0A%09%09if (loots != null
 )%0A%09%09%7B%0A%09%09
@@ -1064,16 +1064,21 @@
 %0A%09%09%7B%0A%09%09%09
+for (
 Loot loo
@@ -1083,48 +1083,23 @@
 oot 
-= DataHandler.getLoot(world.getName());%0A
+: loots)%0A%09%09%09%7B%0A%09
 %09%09%09i
@@ -1256,26 +1256,28 @@
 tion()))%0A%09%09%09
+%09
 %7B%0A
+%09
 %09%09%09%09DataHand
@@ -1284,50 +1284,37 @@
 ler.
-unregisterLoot(world.getName()
+removeLoot(loot
 );%0A%09%09%09%09
+%09
 %0A%09%09%09%09
+%09
 Stri
@@ -1366,32 +1366,33 @@
 %7BLOOT%5C%5C%7D%22);%0A%09%09%09%09
+%09
 String%5B%5D s2 = s1
@@ -1414,24 +1414,25 @@
 LAYER%5C%5C%7D%22);%0A
+%09
 %09%09%09%09String%5B%5D
@@ -1466,25 +1466,27 @@
 R%5C%5C%7D%22);%0A%09%09%09%09
-%0A
+%09%0A%09
 %09%09%09%09MessageC
@@ -1517,32 +1517,33 @@
 builder()%0A%09%09%09%09%09%09
+%09
 .append(Text.of(
@@ -1582,32 +1582,33 @@
  ? s2%5B0%5D : %22%22))%0A
+%09
 %09%09%09%09%09%09.append(Te
@@ -1666,32 +1666,33 @@
 etName() : %22%22))%0A
+%09
 %09%09%09%09%09%09.append(Te
@@ -1743,32 +1743,33 @@
 1%5D : %22%22))%0A%09%09%09%09%09%09
+%09
 .append(loot.get
@@ -1789,16 +1789,17 @@
 play())%0A
+%09
 %09%09%09%09%09%09.a
@@ -1858,32 +1858,33 @@
 0%5D : %22%22))%0A%09%09%09%09%09%09
+%09
 .append(Text.of(
@@ -1946,24 +1946,25 @@
  %22%22))%0A%09%09%09%09%09%09
+%09
 .append(Text
@@ -2011,24 +2011,25 @@
 3%5B1%5D : %22%22))%0A
+%09
 %09%09%09%09%09%09.build
@@ -2033,16 +2033,22 @@
 ild());%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Fix InteractListener