summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/signs/SignPlayerListener.java
blob: 4a26f18f0e25ff20e82347603c9c8ed597d4936f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.earth2me.essentials.signs;

import net.ess3.api.IEssentials;
import java.util.logging.Level;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;


public class SignPlayerListener implements Listener
{
	private final transient IEssentials ess;

	public SignPlayerListener(final IEssentials ess)
	{
		this.ess = ess;
	}

	//This following code below listens to cancelled events to fix a bukkit issue
	//Right clicking signs with a block in hand, can now fire cancelled events.
	//This is because when the block place is cancelled (for example not enough space for the block to be placed),
	//the event will be marked as cancelled, thus preventing 30% of sign purchases.
	@EventHandler(priority = EventPriority.LOW)
	public void onPlayerInteract(final PlayerInteractEvent event)
	{
		if (ess.getSettings().areSignsDisabled())
		{
			event.getHandlers().unregister(this);
			return;
		}
		if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_AIR)
		{
			return;
		}
		final Block block;
		if (event.isCancelled() && event.getAction() == Action.RIGHT_CLICK_AIR)
		{
			Block targetBlock = null;
			try
			{
				targetBlock = event.getPlayer().getTargetBlock(null, 5);
			}
			catch (IllegalStateException ex)
			{
				if (ess.getSettings().isDebug())
				{
					ess.getLogger().log(Level.WARNING, ex.getMessage(), ex);
				}
			}
			block = targetBlock;
		}
		else
		{
			block = event.getClickedBlock();
		}
		if (block == null)
		{
			return;
		}

		final int mat = block.getTypeId();
		if (mat == Material.SIGN_POST.getId() || mat == Material.WALL_SIGN.getId())
		{
			final String csign = ((Sign)block.getState()).getLine(0);
			for (EssentialsSign sign : ess.getSettings().enabledSigns())
			{
				if (csign.equalsIgnoreCase(sign.getSuccessName()))
				{
					sign.onSignInteract(block, event.getPlayer(), ess);
					event.setCancelled(true);
					return;
				}
			}
		}
		else
		{
			for (EssentialsSign sign : ess.getSettings().enabledSigns())
			{
				if (sign.areHeavyEventRequired() && sign.getBlocks().contains(block.getType())
					&& !sign.onBlockInteract(block, event.getPlayer(), ess))
				{
					event.setCancelled(true);
					return;
				}
			}
		}
	}
}