-
Notifications
You must be signed in to change notification settings - Fork 17
/
MagnetModule.java
354 lines (290 loc) · 12 KB
/
MagnetModule.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.modules.flag;
import com.griefcraft.bukkit.EntityBlock;
import com.griefcraft.bukkit.StorageMinecartBlock;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.Flag;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.JavaModule;
import com.griefcraft.util.config.Configuration;
import com.narrowtux.showcase.Showcase;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.entity.CraftItem;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.StorageMinecart;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class MagnetModule extends JavaModule {
private Configuration configuration = Configuration.load("magnet.yml");
/**
* The LWC object
*/
private LWC lwc;
/**
* If this module is enabled
*/
private boolean enabled = false;
/**
* The item blacklist
*/
private List<Integer> itemBlacklist;
/**
* The radius around the container in which to suck up items
*/
private int radius;
/**
* How many items to check each time
*/
private int perSweep;
/**
* The current entity queue
*/
private final Queue<Node> items = new LinkedList<Node>();
/**
* A queue of the items that need to be worked on
*/
private final Queue<Item> work = new LinkedList<Item>();
private final class Node {
/**
* The item that is to be picked up
*/
final Item item;
/**
* The block that the item should be placed into
*/
final Block block;
public Node(Item item, Block block) {
this.item = item;
this.block = block;
}
}
/**
* Sync task, freshens the item queue when it is empty
*/
private class MagnetRefreshTask implements Runnable {
public void run() {
Server server = Bukkit.getServer();
if (work.size() == 0 && items.size() == 0) {
for (World world : server.getWorlds()) {
for (Item item : world.getEntitiesByClass(Item.class)) {
work.add(item);
}
}
} else if (work.size() > 0) {
Item item;
int checked = 0;
while ((item = work.poll()) != null) {
if (item.isDead()) {
continue;
}
// native stack handle
net.minecraft.server.ItemStack stackHandle = ((net.minecraft.server.EntityItem) ((CraftItem) item).getHandle()).itemStack;
// check if it is in the blacklist
if (itemBlacklist.contains(stackHandle.id)) {
continue;
}
// check if the item is valid
if (stackHandle.count <= 0) {
continue;
}
// has the item been living long enough?
if (item.getPickupDelay() > item.getTicksLived()) {
continue; // a player wouldn't have had a chance to pick it up yet
}
// Check for usable blocks
long start = System.currentTimeMillis();
Block block = scanForInventoryBlock(item.getLocation(), radius);
if (block == null) {
continue;
}
items.offer(new Node(item, block));
checked ++;
if (checked > 500) {
break;
}
}
}
}
}
// does all of the work
// searches the worlds for items and magnet chests nearby
private class MagnetTask implements Runnable {
public void run() {
final Server server = Bukkit.getServer();
final LWC lwc = LWC.getInstance();
// Throttle amount of items polled
int count = 0;
Node node;
while ((node = items.poll()) != null) {
final Item item = node.item;
final Block block = node.block;
final World world = item.getWorld();
if (item.isDead()) {
continue;
}
if (isShowcaseItem(item)) {
// it's being used by the Showcase plugin ... ignore it
continue;
}
if (block != null) {
Runnable runnable = new Runnable() {
public void run() {
Protection protection;
if (block instanceof EntityBlock) {
protection = lwc.getPhysicalDatabase().loadProtection(block.getWorld().getName(), block.getX(), block.getY(), block.getZ());
} else {
protection = lwc.findProtection(block);
}
if (protection == null || !protection.hasFlag(Flag.Type.MAGNET)) {
return;
}
ItemStack itemStack = item.getItemStack();
// Remove the items and suck them up :3
Map<Integer, ItemStack> remaining = lwc.depositItems(block, itemStack);
// we cancelled the item drop for some reason
if (remaining == null) {
return;
}
if (remaining.size() == 1) {
ItemStack other = remaining.values().iterator().next();
if (itemStack.getTypeId() == other.getTypeId() && itemStack.getAmount() == other.getAmount()) {
return;
}
}
// remove the item on the ground
item.remove();
// if we have a remainder, we need to drop them
if (remaining.size() > 0) {
for (ItemStack stack : remaining.values()) {
world.dropItemNaturally(item.getLocation(), stack);
}
}
}
};
server.getScheduler().scheduleSyncDelayedTask(lwc.getPlugin(), runnable);
}
// Time to throttle?
if (count > perSweep) {
break;
}
count++;
}
}
}
/**
* Check for the Showcase plugin and if it exists we also want to make sure the block doesn't have a showcase
* on it.
*
* @param item
* @return
*/
private boolean isShowcaseItem(Item item) {
if (item == null) {
return false;
}
// check for the showcase plugin
boolean hasShowcase = Bukkit.getServer().getPluginManager().getPlugin("Showcase") != null;
if (hasShowcase) {
return Showcase.instance.getItemByDrop(item) != null;
}
return false;
}
@Override
public void load(LWC lwc) {
this.lwc = lwc;
enabled = configuration.getBoolean("magnet.enabled", false);
itemBlacklist = new ArrayList<Integer>();
radius = configuration.getInt("magnet.radius", 3);
perSweep = configuration.getInt("magnet.perSweep", 20);
if (!enabled) {
return;
}
// get the item blacklist
List<String> temp = configuration.getStringList("magnet.blacklist", new ArrayList<String>());
for (String item : temp) {
Material material = Material.matchMaterial(item);
if (material != null) {
itemBlacklist.add(material.getId());
}
}
// register our search thread schedule
lwc.getPlugin().getServer().getScheduler().scheduleAsyncRepeatingTask(lwc.getPlugin(), new MagnetTask(), 50, 50);
lwc.getPlugin().getServer().getScheduler().scheduleSyncRepeatingTask(lwc.getPlugin(), new MagnetRefreshTask(), 50, 50);
}
/**
* Scan for one inventory block around the given block inside the given radius
*
* @param location
* @param radius
* @return
*/
private Block scanForInventoryBlock(Location location, int radius) {
int baseX = location.getBlockX();
int baseY = location.getBlockY();
int baseZ = location.getBlockZ();
World world = location.getWorld();
// native handle
net.minecraft.server.WorldServer worldHandle = ((CraftWorld) world).getHandle();
List<net.minecraft.server.TileEntity> entities = (List<net.minecraft.server.TileEntity>) worldHandle.getTileEntities(baseX - radius, baseY - radius, baseZ - radius, baseX + radius, baseY + radius, baseZ + radius);
for (net.minecraft.server.TileEntity entity : entities) {
Block block = world.getBlockAt(entity.x, entity.y, entity.z);
try {
if (block.getState() instanceof InventoryHolder) {
return block;
}
} catch (NullPointerException e) {
LWC lwc = LWC.getInstance();
lwc.log("Possibly invalid block found at [" + entity.x + ", " + entity.y + ", " + entity.z + "]!");
}
}
// Storage minecarts
for (Entity minecartEntity : world.getEntitiesByClass(StorageMinecart.class)) {
StorageMinecart minecart = (StorageMinecart) minecartEntity;
Location l = minecart.getLocation();
if (l.getX() >= baseX - radius && l.getX() <= baseX + radius
&& l.getY() >= baseY - radius && l.getY() <= baseY + radius
&& l.getZ() >= baseZ - radius && l.getZ() <= baseZ + radius) {
return new StorageMinecartBlock(minecart);
}
}
return null;
}
}