-
Notifications
You must be signed in to change notification settings - Fork 1
/
RegionServicesManagerImpl.java
257 lines (205 loc) · 8.64 KB
/
RegionServicesManagerImpl.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
package com.github.sanctum.labyrinth.data;
import com.github.sanctum.labyrinth.Labyrinth;
import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.github.sanctum.labyrinth.data.container.Region;
import com.github.sanctum.labyrinth.event.CuboidSelectEvent;
import com.github.sanctum.labyrinth.event.LabyrinthVentCall;
import com.github.sanctum.labyrinth.event.RegionBuildEvent;
import com.github.sanctum.labyrinth.event.RegionDestroyEvent;
import com.github.sanctum.labyrinth.event.RegionInteractionEvent;
import com.github.sanctum.labyrinth.event.RegionPVPEvent;
import com.github.sanctum.labyrinth.event.DefaultEvent;
import com.github.sanctum.labyrinth.library.Items;
import com.github.sanctum.labyrinth.task.TaskScheduler;
import com.github.sanctum.panther.container.ImmutablePantherCollection;
import com.github.sanctum.panther.container.PantherCollection;
import com.github.sanctum.panther.container.PantherList;
import com.github.sanctum.panther.event.Vent;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.ServicePriority;
import org.jetbrains.annotations.NotNull;
/**
* Default {@link RegionServicesManager} implementation.
*/
public final class RegionServicesManagerImpl extends RegionServicesManager {
final PantherCollection<Region> cache = new PantherList<>();
final FlagManager flagManager = new FlagManager(this);
private ItemStack wand = new ItemStack(Material.STICK);
public static void initialize(Labyrinth instance) {
RegionServicesManager servicesManager = new RegionServicesManagerImpl();
Bukkit.getServicesManager().register(RegionServicesManager.class, servicesManager, instance, ServicePriority.Normal);
instance.getEventMap().subscribe(new Vent.Subscription<>(DefaultEvent.BlockBreak.class, instance, Vent.Priority.HIGH, (e, subscription) -> {
Optional<Region> r = CompletableFuture.supplyAsync(() -> Optional.ofNullable(getInstance().get(e.getBlock().getLocation()))).join();
if (r.isPresent()) {
RegionDestroyEvent event = new LabyrinthVentCall<>(new RegionDestroyEvent(e.getPlayer(), r.get(), e.getBlock())).run();
if (event.isCancelled()) {
e.setCancelled(true);
}
}
}));
instance.getEventMap().subscribe(new Vent.Subscription<>(DefaultEvent.BlockPlace.class, instance, Vent.Priority.HIGH, (e, subscription) -> {
Optional<Region> r = CompletableFuture.supplyAsync(() -> Optional.ofNullable(getInstance().get(e.getBlock().getLocation()))).join();
if (r.isPresent()) {
RegionBuildEvent event = new LabyrinthVentCall<>(new RegionBuildEvent(e.getPlayer(), r.get(), e.getBlock())).run();
if (event.isCancelled()) {
e.setCancelled(true);
}
}
}));
instance.getEventMap().subscribe(new Vent.Subscription<>(DefaultEvent.Join.class, "region-spawn", instance, Vent.Priority.MEDIUM, (event, subscription) -> {
Region.Resident r = Region.Resident.get(event.getPlayer());
if (!event.getPlayer().hasPlayedBefore()) {
TaskScheduler.of(() -> {
Region region = getInstance().get("spawn");
if (region != null) {
r.setSpawnTagged(true);
r.setPastSpawn(false);
}
}).scheduleLater(2);
} else {
r.setSpawnTagged(false);
}
}));
instance.getEventMap().subscribe(new Vent.Subscription<>(DefaultEvent.Interact.class, "region-interact", instance, Vent.Priority.MEDIUM, (e, subscription) -> {
if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
Optional<Region> r = CompletableFuture.supplyAsync(() -> e.getBlock().map(Block::getLocation).flatMap(location -> Optional.ofNullable(getInstance().get(location)))).join();
if (r.isPresent()) {
// TODO: rewrite the optional#get call to safely obtain the value
//noinspection OptionalGetWithoutIsPresent
RegionInteractionEvent event = new LabyrinthVentCall<>(new RegionInteractionEvent(e.getPlayer(), r.get(), e.getBlock().get(), RegionInteractionEvent.ClickType.LEFT)).run();
if (event.isCancelled()) {
e.setCancelled(true);
}
}
if (e.getItem() == null) {
return;
}
if (!e.getPlayer().hasPermission("labyrinth.selection"))
return;
CuboidSelection selection = CuboidSelection.get(e.getPlayer());
if (e.getItem().getType() == selection.getWand().getType()) {
if (e.getResult() != Event.Result.DENY) {
e.setResult(Event.Result.DENY);
}
// TODO: decide how to handle optional (map setPos1?)
//noinspection OptionalGetWithoutIsPresent
selection.setPos1(e.getBlock().get().getLocation());
new LabyrinthVentCall<>(new CuboidSelectEvent(selection) {
}).run();
}
}
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
Optional<Region> r = CompletableFuture.supplyAsync(() -> e.getBlock().map(Block::getLocation).flatMap(location -> Optional.ofNullable(getInstance().get(location)))).join();
if (r.isPresent()) {
// TODO: rewrite the optional#get call to safely obtain the value
//noinspection OptionalGetWithoutIsPresent
RegionInteractionEvent event = new LabyrinthVentCall<>(new RegionInteractionEvent(e.getPlayer(), r.get(), e.getBlock().get(), RegionInteractionEvent.ClickType.RIGHT)).run();
if (event.isCancelled()) {
e.setCancelled(true);
}
}
if (e.getItem() == null)
return;
if (!e.getPlayer().hasPermission("labyrinth.selection"))
return;
boolean isOld = LabyrinthProvider.getInstance().isLegacy();
Material mat = Items.findMaterial("WOODEN_AXE");
if (isOld) {
mat = Items.findMaterial("WOOD_AXE");
}
if (e.getItem().getType() == mat) {
CuboidSelection selection = CuboidSelection.get(e.getPlayer());
if (e.getResult() != Event.Result.DENY) {
e.setResult(Event.Result.DENY);
}
// TODO: decide how to handle optional (map setPos2?)
//noinspection OptionalGetWithoutIsPresent
selection.setPos2(e.getBlock().get().getLocation());
new LabyrinthVentCall<>(new CuboidSelectEvent(selection) {
}).run();
}
}
}));
instance.getEventMap().subscribe(new Vent.Subscription<>(DefaultEvent.PlayerDamagePlayer.class, "region-pvp", instance, Vent.Priority.MEDIUM, (event, subscription) -> {
Player p = event.getPlayer();
Player target = event.getVictim();
Region.Resident r = Region.Resident.get(target);
if (event.isPhysical()) {
if (r.getRegion().isPresent()) {
Region region = r.getRegion().get();
RegionPVPEvent e = new LabyrinthVentCall<>(new RegionPVPEvent(p, target, region)).run();
if (e.isCancelled()) {
event.setCancelled(true);
}
}
} else {
if (r.getRegion().isPresent()) {
Region region = r.getRegion().get();
RegionPVPEvent e = new LabyrinthVentCall<>(new RegionPVPEvent(p, target, region)).run();
if (e.isCancelled()) {
event.setCancelled(true);
}
}
}
}));
}
@Override
public PantherCollection<Region> getAll() {
return ImmutablePantherCollection.of(cache);
}
@Override
public Region get(@NotNull Location location) {
return cache.stream().filter(region -> region.contains(location)).findFirst().orElse(null);
}
@Override
public Region get(@NotNull Player player) {
return cache.stream().filter(region -> region.contains(player)).findFirst().orElse(null);
}
@Override
public Region get(@NotNull String name) {
return cache.stream().filter(region -> region.getName().equals(name)).findFirst().orElse(null);
}
@Override
public Region get(@NotNull Location location, boolean passthrough) {
return cache.stream().filter(region -> region.contains(location) && region.isDominant() == passthrough).findFirst().orElse(null);
}
@Override
public Region get(@NotNull Player player, boolean passthrough) {
return cache.stream().filter(region -> region.contains(player) && region.isDominant() == passthrough).findFirst().orElse(null);
}
@Override
public Region get(@NotNull String name, boolean passthrough) {
return cache.stream().filter(region -> region.getName().equals(name) && region.isDominant() == passthrough).findFirst().orElse(null);
}
@Override
public boolean load(@NotNull Region region) {
if (cache.contains(region)) return false;
return cache.add(region);
}
@Override
public boolean unload(@NotNull Region region) {
if (!cache.contains(region)) return false;
return cache.remove(region);
}
@Override
public FlagManager getFlagManager() {
return flagManager;
}
@Override
public ItemStack getWand() {
return wand;
}
@Override
public void setWand(@NotNull ItemStack itemStack) {
this.wand = itemStack;
}
}