Skip to content

Commit 62f0a75

Browse files
committed
Added an option to select between BlockChanger and FAWE for arena cleanup
1 parent f636ebb commit 62f0a75

6 files changed

Lines changed: 67 additions & 35 deletions

File tree

Plugin/src/main/java/dev/lrxh/neptune/Neptune.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private void initAPI() {
118118

119119
private void loadManager() {
120120
ConfigService.get().load();
121-
arenaGenerationDisabled = !SettingsLocale.ARENA_GENERATION.getBoolean();
121+
arenaGenerationDisabled = !SettingsLocale.DYNAMIC_ARENA_GENERATION.getBoolean();
122122

123123
loadExtensions();
124124
if (!isEnabled())
@@ -136,7 +136,7 @@ private void loadManager() {
136136
ArenaService.get().setupDuplicatesWorld();
137137
ArenaService.get().loadDuplicates();
138138
} else {
139-
ServerUtils.error("FastAsyncWorldEdit is not installed - arena duplicates are disabled. Falling back to using the original arenas.");
139+
ServerUtils.error("FastAsyncWorldEdit is not installed - arena duplicates are disabled.");
140140
}
141141
}
142142
KitService.get().load();

Plugin/src/main/java/dev/lrxh/neptune/configs/impl/SettingsLocale.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ public enum SettingsLocale implements IDataAccessor {
1616
FRIENDLY_FIRE("FRIENDLY_FIRE", DataType.BOOLEAN, "false"),
1717
MATCH_START_COUNTDOWN("MATCH_START_COUNTDOWN", "The countdown in seconds before a match starts.", DataType.INT, "3"),
1818
FFA_MATCH_START_COUNTDOWN("FFA_MATCH_START_COUNTDOWN", "The countdown in seconds before an FFA match starts.", DataType.INT, "5"),
19-
ARENA_GENERATION("ARENA_GENERATION",
19+
DYNAMIC_ARENA_GENERATION("DYNAMIC_ARENA_GENERATION",
2020
"If disabled, matches reuse the original arena (flagged in use) instead of generating a copy. Requires a server restart to apply.",
2121
DataType.BOOLEAN, "false"),
2222
DUPLICATE_WORLD("DUPLICATE.WORLD", "World that holds all pre-generated arena duplicates (used when ARENA_GENERATION is disabled).", DataType.STRING, "neptune_duplicates"),
2323
DUPLICATE_DISTANCE("DUPLICATE.DISTANCE", "Blocks between each duplicate arena on the grid. Must be larger than your largest arena's footprint.", DataType.INT, "500"),
24+
ARENA_CLEANUP_METHOD("ARENA_CLEANUP_METHOD", "Engine used to reset arenas after a match: BLOCKCHANGER (default) or FAWE (requires FastAsyncWorldEdit). Requires a server restart to apply.", DataType.STRING, "BLOCKCHANGER"),
2425
COMMANDS_AFTER_MATCH_WINNER("COMMAND_AFTER_MATCH.WINNER", DataType.STRING_LIST, "NONE"),
2526
COMMANDS_AFTER_MATCH_LOSER("COMMAND_AFTER_MATCH.LOSER", DataType.STRING_LIST, "NONE"),
2627
SPAWN_LOCATION("SPAWN.LOCATION", DataType.STRING, "NONE"),

Plugin/src/main/java/dev/lrxh/neptune/game/arena/Arena.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.lrxh.blockChanger.BlockChanger;
55
import dev.lrxh.blockChanger.snapshot.CuboidSnapshot;
66
import dev.lrxh.neptune.Neptune;
7+
import dev.lrxh.neptune.configs.impl.SettingsLocale;
78
import dev.lrxh.neptune.game.kit.KitService;
89
import dev.lrxh.neptune.providers.manager.ConfigData;
910
import dev.lrxh.neptune.utils.LocationUtil;
@@ -32,6 +33,7 @@ public class Arena implements IArena, ConfigData {
3233
private long time;
3334
private List<Material> whitelistedBlocks;
3435
private CuboidSnapshot snapshot;
36+
private Object faweClipboard;
3537
private Arena owner;
3638
private boolean doneLoading;
3739
private boolean inUse;
@@ -60,14 +62,7 @@ public Arena(String name, String displayName, Location redSpawn, Location blueSp
6062
this.buildLimit = buildLimit;
6163
this.whitelistedBlocks = (whitelistedBlocks != null ? whitelistedBlocks : new ArrayList<>());
6264

63-
if (min != null && max != null) {
64-
this.doneLoading = false;
65-
CuboidSnapshot.create(min, max).thenAccept(cuboidSnapshot -> {
66-
this.snapshot = cuboidSnapshot;
67-
this.doneLoading = true;
68-
});
69-
}
70-
65+
capture();
7166
}
7267

7368
public Arena(String name, String displayName, Location redSpawn, Location blueSpawn,
@@ -216,31 +211,44 @@ public static Arena read(String name, ConfigurationSection s) {
216211
}
217212

218213
public void restore() {
219-
if (snapshot != null) {
214+
if (faweClipboard != null) {
215+
Bukkit.getScheduler().runTaskAsynchronously(Neptune.get(),
216+
() -> ArenaDuplicator.restore(min.getWorld(), faweClipboard));
217+
} else if (snapshot != null) {
220218
snapshot.restore(true);
221219
}
222220
}
223221

224-
public void setMin(Location min) {
225-
this.min = min;
226-
if (min != null && max != null) {
227-
this.doneLoading = false;
222+
private static boolean useFawe() {
223+
return "FAWE".equalsIgnoreCase(SettingsLocale.ARENA_CLEANUP_METHOD.getString()) && ArenaDuplicator.isAvailable();
224+
}
225+
226+
public void capture() {
227+
if (min == null || max == null) return;
228+
this.doneLoading = false;
229+
if (useFawe()) {
230+
this.snapshot = null;
231+
Bukkit.getScheduler().runTaskAsynchronously(Neptune.get(), () -> {
232+
this.faweClipboard = ArenaDuplicator.capture(min.getWorld(), min, max);
233+
this.doneLoading = true;
234+
});
235+
} else {
236+
this.faweClipboard = null;
228237
CuboidSnapshot.create(min, max).thenAccept(cuboidSnapshot -> {
229238
this.snapshot = cuboidSnapshot;
230239
this.doneLoading = true;
231240
});
232241
}
233242
}
234243

244+
public void setMin(Location min) {
245+
this.min = min;
246+
capture();
247+
}
248+
235249
public void setMax(Location max) {
236250
this.max = max;
237-
if (min != null && max != null) {
238-
this.doneLoading = false;
239-
CuboidSnapshot.create(min, max).thenAccept(cuboidSnapshot -> {
240-
this.snapshot = cuboidSnapshot;
241-
this.doneLoading = true;
242-
});
243-
}
251+
capture();
244252
}
245253

246254
public void setRedSpawn(Location redSpawn) {

Plugin/src/main/java/dev/lrxh/neptune/game/arena/ArenaDuplicator.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.sk89q.worldedit.WorldEdit;
55
import com.sk89q.worldedit.bukkit.BukkitAdapter;
66
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
7+
import com.sk89q.worldedit.extent.clipboard.Clipboard;
78
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
89
import com.sk89q.worldedit.function.operation.Operations;
910
import com.sk89q.worldedit.math.BlockVector3;
@@ -43,4 +44,36 @@ public static void copyPaste(World sourceWorld, Location min, Location max, Worl
4344
.build());
4445
}
4546
}
47+
48+
/**
49+
* Snapshots [min, max] into an in-memory clipboard for later cleanup. Returned as Object so callers stay FAWE-free.
50+
*/
51+
public static Object capture(World world, Location min, Location max) {
52+
CuboidRegion region = new CuboidRegion(BukkitAdapter.adapt(world),
53+
BlockVector3.at(min.getBlockX(), min.getBlockY(), min.getBlockZ()),
54+
BlockVector3.at(max.getBlockX(), max.getBlockY(), max.getBlockZ()));
55+
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
56+
clipboard.setOrigin(region.getMinimumPoint());
57+
58+
try (EditSession source = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(world))) {
59+
ForwardExtentCopy copy = new ForwardExtentCopy(source, region, clipboard, region.getMinimumPoint());
60+
copy.setCopyingEntities(false);
61+
Operations.complete(copy);
62+
}
63+
return clipboard;
64+
}
65+
66+
/**
67+
* Pastes a clipboard from {@link #capture} back to its original location, resetting the arena.
68+
*/
69+
public static void restore(World world, Object clipboard) {
70+
Clipboard clip = (Clipboard) clipboard;
71+
try (EditSession target = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(world))) {
72+
Operations.complete(new ClipboardHolder(clip)
73+
.createPaste(target)
74+
.to(clip.getOrigin())
75+
.ignoreAirBlocks(false)
76+
.build());
77+
}
78+
}
4679
}

Plugin/src/main/java/dev/lrxh/neptune/game/arena/ArenaService.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import dev.lrxh.api.arena.IArena;
44
import dev.lrxh.api.arena.IArenaService;
5-
import dev.lrxh.blockChanger.snapshot.CuboidSnapshot;
65
import dev.lrxh.neptune.Neptune;
76
import dev.lrxh.neptune.configs.ConfigService;
87
import dev.lrxh.neptune.configs.impl.SettingsLocale;
@@ -168,11 +167,7 @@ public void recopyDuplicates(Arena owner) {
168167
}
169168
Bukkit.getScheduler().runTask(Neptune.get(), () -> {
170169
for (Arena dup : dups) {
171-
dup.setDoneLoading(false);
172-
CuboidSnapshot.create(dup.getMin(), dup.getMax()).thenAccept(s -> {
173-
dup.setSnapshot(s);
174-
dup.setDoneLoading(true);
175-
});
170+
dup.capture();
176171
}
177172
});
178173
});

Plugin/src/main/java/dev/lrxh/neptune/game/arena/menu/ArenaManagementMenu.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.lrxh.neptune.game.arena.menu;
22

3-
import dev.lrxh.blockChanger.snapshot.CuboidSnapshot;
43
import dev.lrxh.neptune.Neptune;
54
import dev.lrxh.neptune.game.arena.Arena;
65
import dev.lrxh.neptune.game.arena.impl.EdgeType;
@@ -65,11 +64,7 @@ public ItemStack getItemStack(Player player) {
6564

6665
@Override
6766
public void onClick(ClickType type, Player player) {
68-
arena.setDoneLoading(false);
69-
CuboidSnapshot.create(arena.getMin(), arena.getMax()).thenAccept(snapshot -> {
70-
arena.setSnapshot(snapshot);
71-
arena.setDoneLoading(true);
72-
});
67+
arena.capture();
7368
}
7469
});
7570
}

0 commit comments

Comments
 (0)