1
|
|
|
package de.pewpewproject.lasertag.command.lasertag.game; |
2
|
|
|
|
3
|
|
|
import com.mojang.brigadier.arguments.StringArgumentType; |
4
|
|
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
5
|
|
|
import com.mojang.brigadier.context.CommandContext; |
6
|
|
|
import de.pewpewproject.lasertag.command.CommandFeedback; |
7
|
|
|
import de.pewpewproject.lasertag.command.ServerFeedbackCommand; |
8
|
|
|
import de.pewpewproject.lasertag.command.suggestions.MapSuggestionProvider; |
9
|
|
|
import de.pewpewproject.lasertag.worldgen.chunkgen.type.ArenaType; |
10
|
|
|
import de.pewpewproject.lasertag.worldgen.chunkgen.type.ProceduralArenaType; |
11
|
|
|
import net.minecraft.server.command.ServerCommandSource; |
12
|
|
|
import net.minecraft.text.Text; |
13
|
|
|
import net.minecraft.util.Formatting; |
14
|
|
|
|
15
|
|
|
import java.util.Arrays; |
16
|
|
|
import java.util.Optional; |
17
|
|
|
|
18
|
|
|
import static com.mojang.brigadier.arguments.StringArgumentType.word; |
19
|
|
|
import static net.minecraft.server.command.CommandManager.argument; |
20
|
|
|
import static net.minecraft.server.command.CommandManager.literal; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Command to reload the arena in game |
24
|
|
|
* |
25
|
|
|
* @author Étienne Muser |
26
|
|
|
*/ |
27
|
|
|
public class LoadMapCommand extends ServerFeedbackCommand { |
28
|
|
|
@Override |
29
|
|
|
protected Optional<CommandFeedback> execute(CommandContext<ServerCommandSource> context) { |
30
|
|
|
|
31
|
|
|
// Get the game managers |
32
|
|
|
var gameManager = context.getSource().getWorld().getServerLasertagManager(); |
33
|
|
|
var arenaManager = gameManager.getArenaManager(); |
34
|
|
|
|
35
|
|
|
var mapTranslatableName = StringArgumentType.getString(context, "map"); |
36
|
|
|
|
37
|
|
|
var arenaTypeOptional = Arrays.stream(ArenaType.values()) |
38
|
|
|
.filter(m -> m.translatableName.equals(mapTranslatableName)) |
39
|
|
|
.findFirst(); |
40
|
|
|
|
41
|
|
|
var proceduralTypeOptional = Arrays.stream(ProceduralArenaType.values()) |
42
|
|
|
.filter(m -> m.translatableName.equals(mapTranslatableName)) |
43
|
|
|
.findFirst(); |
44
|
|
|
|
45
|
|
|
// If is procedural map |
46
|
|
|
if (proceduralTypeOptional.isPresent()) { |
47
|
|
|
arenaTypeOptional = Optional.of(ArenaType.PROCEDURAL); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (arenaTypeOptional.isEmpty()) { |
51
|
|
|
|
52
|
|
|
return Optional.of(new CommandFeedback(Text.literal("Could not find map.").formatted(Formatting.RED), false, false)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
arenaManager.loadArena(arenaTypeOptional.get(), proceduralTypeOptional.orElse(ProceduralArenaType.SMALL_2V2)); |
57
|
|
|
} catch (Exception e) { |
58
|
|
|
return Optional.of(new CommandFeedback(Text.literal("Unexpected error while loading map: " + e.getMessage()).formatted(Formatting.RED), false, false)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return Optional.of(new CommandFeedback(Text.literal("Map loaded."), false, false)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static void register(LiteralArgumentBuilder<ServerCommandSource> lab) { |
65
|
|
|
lab.then(literal("loadMap") |
66
|
|
|
.requires(s -> s.hasPermissionLevel(4)) |
67
|
|
|
.then(argument("map", word()) |
68
|
|
|
.suggests(MapSuggestionProvider.getInstance()) |
69
|
|
|
.executes(new LoadMapCommand()))); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|