-
Notifications
You must be signed in to change notification settings - Fork 17
/
MagnetModule.java
226 lines (182 loc) · 7.16 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
/**
* 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.modules.flag;
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 de.moritzschmale.Showcase.ShowcaseMain;
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.block.ContainerBlock;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class MagnetModule extends JavaModule {
private Configuration configuration = Configuration.load("magnet.yml");
/**
* 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 List<Entity> entities = null;
// does all of the work
// searches the worlds for items and magnet chests nearby
private class MagnetTask implements Runnable {
public void run() {
Server server = Bukkit.getServer();
LWC lwc = LWC.getInstance();
if (!LWC.ENABLED) {
server.getScheduler().cancelTask(taskId);
}
for (World world : server.getWorlds()) {
String worldName = world.getName();
if (entities == null || entities.size() == 0) {
entities = new ArrayList<Entity>(world.getEntities());
}
Iterator<Entity> iterator = entities.iterator();
int count = 0;
while (iterator.hasNext()) {
Entity entity = iterator.next();
if (!(entity instanceof Item)) {
iterator.remove();
continue;
}
if (count > perSweep) {
break;
}
Item item = (Item) entity;
ItemStack itemStack = item.getItemStack();
// check if it is in the blacklist
if (itemBlacklist.contains(itemStack.getTypeId())) {
iterator.remove();
continue;
}
Location location = item.getLocation();
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
if (isShowcaseItem(item)) {
// it's being used by the Showcase plugin ... ignore it
iterator.remove();
continue;
}
List<Protection> protections = lwc.getPhysicalDatabase().loadProtections(worldName, x, y, z, radius);
Block block;
Protection protection;
for (Protection temp : protections) {
protection = temp;
block = world.getBlockAt(protection.getX(), protection.getY(), protection.getZ());
// we only want inventory blocks
if (!(block.getState() instanceof ContainerBlock)) {
continue;
}
if (!protection.hasFlag(Flag.Type.MAGNET)) {
continue;
}
// Remove the items and suck them up :3
Map<Integer, ItemStack> remaining = lwc.depositItems(block, itemStack);
if (remaining.size() == 1) {
ItemStack other = remaining.values().iterator().next();
if (itemStack.getTypeId() == other.getTypeId() && itemStack.getAmount() == other.getAmount()) {
continue;
}
}
// 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(location, stack);
}
}
break;
}
count++;
iterator.remove();
}
}
}
}
/**
* The BukkitScheduler task id
*/
private int taskId;
/**
* 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) {
// Now we can check for the showcase item
return ShowcaseMain.instance.getItemByDrop(item) != null;
}
return false;
}
@Override
public void load(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
MagnetTask searchThread = new MagnetTask();
taskId = lwc.getPlugin().getServer().getScheduler().scheduleSyncRepeatingTask(lwc.getPlugin(), searchThread, 50, 50);
}
}