-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCPlayerListener.java
199 lines (164 loc) · 7.12 KB
/
LWCPlayerListener.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* This file is part of LWC (https://github.com/Hidendra/LWC)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.griefcraft.listeners;
import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCPlugin;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.Module;
import com.griefcraft.scripting.Module.Result;
import com.griefcraft.scripting.ModuleLoader.Event;
import com.griefcraft.scripting.event.LWCBlockInteractEvent;
import com.griefcraft.scripting.event.LWCDropItemEvent;
import com.griefcraft.scripting.event.LWCProtectionInteractEvent;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.ContainerBlock;
import org.bukkit.craftbukkit.CraftChunk;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class LWCPlayerListener extends PlayerListener {
/**
* The plugin instance
*/
private LWCPlugin plugin;
public LWCPlayerListener(LWCPlugin plugin) {
this.plugin = plugin;
}
@Override
public void onPlayerDropItem(PlayerDropItemEvent event) {
if (!LWC.ENABLED) {
return;
}
Player player = event.getPlayer();
Item item = event.getItemDrop();
ItemStack itemStack = item.getItemStack();
Result result = plugin.getLWC().getModuleLoader().dispatchEvent(Event.DROP_ITEM, player, item, itemStack);
LWCDropItemEvent evt = new LWCDropItemEvent(player, event);
plugin.getLWC().getModuleLoader().dispatchEvent(evt);
if (evt.isCancelled() || result == Result.CANCEL) {
event.setCancelled(true);
}
}
@Override
public void onPlayerChat(PlayerChatEvent event) {
if(!plugin.getLWC().getConfiguration().getBoolean("core.filterunlock", true)) {
return;
}
/**
* We want to block messages starting with cunlock incase someone screws up /cunlock password.
*/
String message = event.getMessage();
if(message.startsWith("cunlock")) {
event.setCancelled(true);
}
}
@Override
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.LEFT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
if (!LWC.ENABLED) {
return;
}
LWC lwc = plugin.getLWC();
Player player = event.getPlayer();
Block clickedBlock = event.getClickedBlock();
Location location = clickedBlock.getLocation();
CraftWorld craftWorld = (CraftWorld) clickedBlock.getWorld();
Block block = new CraftBlock((CraftChunk) craftWorld.getChunkAt(location), location.getBlockX(), location.getBlockY(), location.getBlockZ());
block.setTypeId(craftWorld.getBlockTypeIdAt(location));
Material material = block.getType();
/*
* Prevent players with lwc.blockinventories from opening inventories
*/
if (block.getState() instanceof ContainerBlock) {
if (!lwc.hasPermission(player, "lwc.protect") && lwc.hasPermission(player, "lwc.deny") && !lwc.isAdmin(player) && !lwc.isMod(player)) {
lwc.sendLocale(player, "protection.interact.error.blocked");
event.setCancelled(true);
return;
}
}
try {
List<String> actions = lwc.getMemoryDatabase().getActions(player.getName());
Protection protection = lwc.findProtection(block);
Module.Result result = Module.Result.CANCEL;
boolean canAccess = lwc.canAccessProtection(player, protection);
boolean canAdmin = lwc.canAdminProtection(player, protection);
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
boolean ignoreLeftClick = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(material, "ignoreLeftClick"));
if (ignoreLeftClick) {
return;
}
} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
boolean ignoreRightClick = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(material, "ignoreRightClick"));
if (ignoreRightClick) {
return;
}
}
if (protection != null) {
result = lwc.getModuleLoader().dispatchEvent(Event.INTERACT_PROTECTION, player, protection, actions, canAccess, canAdmin);
if(result == Result.DEFAULT) {
LWCProtectionInteractEvent evt = new LWCProtectionInteractEvent(event, protection, actions, canAccess, canAdmin);
lwc.getModuleLoader().dispatchEvent(evt);
result = evt.getResult();
}
} else {
result = lwc.getModuleLoader().dispatchEvent(Event.INTERACT_BLOCK, player, block, actions);
if(result == Result.DEFAULT) {
LWCBlockInteractEvent evt = new LWCBlockInteractEvent(event, block, actions);
lwc.getModuleLoader().dispatchEvent(evt);
result = evt.getResult();
}
}
if (result == Module.Result.ALLOW) {
return;
}
if (result == Module.Result.DEFAULT) {
lwc.enforceAccess(player, block);
}
if (!canAccess || result == Module.Result.CANCEL) {
event.setCancelled(true);
event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
}
} catch(Exception e) {
event.setCancelled(true);
event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
lwc.sendLocale(player, "protection.internalerror", "id", "PLAYER_INTERACT");
e.printStackTrace();
}
}
@Override
public void onPlayerQuit(PlayerQuitEvent event) {
if (!LWC.ENABLED) {
return;
}
LWC lwc = plugin.getLWC();
String player = event.getPlayer().getName();
lwc.getMemoryDatabase().unregisterPlayer(player);
lwc.getMemoryDatabase().unregisterUnlock(player);
lwc.getMemoryDatabase().unregisterPendingLock(player);
lwc.getMemoryDatabase().unregisterAllActions(player);
lwc.getMemoryDatabase().unregisterAllModes(player);
}
}