-
Notifications
You must be signed in to change notification settings - Fork 24
/
CommandUtil.java
93 lines (86 loc) · 3.64 KB
/
CommandUtil.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
/*
* SkyClaims - A Skyblock plugin made for Sponge
* Copyright (C) 2017 Mohron
*
* 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.
*
* SkyClaims 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 SkyClaims. If not, see <http://www.gnu.org/licenses/>.
*/
package net.mohron.skyclaims.util;
import java.util.function.Consumer;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.action.TextActions;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
public class CommandUtil {
public static Consumer<CommandSource> createTeleportConsumer(CommandSource src, Location<World> location) {
return teleport -> {
if (src instanceof Player) {
Player player = (Player) src;
Location<World> safeLocation = Sponge.getGame().getTeleportHelper().getSafeLocation(location).orElse(null);
if (safeLocation == null) {
player.sendMessage(Text.of(
TextColors.RED, "Location is not safe. ",
TextColors.WHITE, "Are you sure you want to teleport here?", Text.NEW_LINE,
Text.builder("[YES]")
.onHover(TextActions.showText(Text.of("Click to teleport")))
.onClick(TextActions.executeCallback(createForceTeleportConsumer(player, location)))
.color(TextColors.GREEN)
));
} else {
player.setLocation(safeLocation);
}
}
};
}
public static Consumer<Task> createTeleportConsumer(Player player, Location<World> location) {
return teleport -> {
Location<World> safeLocation = Sponge.getGame().getTeleportHelper().getSafeLocation(location).orElse(null);
if (safeLocation == null) {
player.sendMessage(Text.of(
TextColors.RED, "Location is not safe. ",
TextColors.WHITE, "Are you sure you want to teleport here?", Text.NEW_LINE,
TextColors.WHITE, "[",
Text.builder("YES")
.color(TextColors.GREEN)
.onHover(TextActions.showText(Text.of("Click to teleport")))
.onClick(TextActions.executeCallback(createForceTeleportConsumer(player, location))),
TextColors.WHITE, "]"
));
} else {
player.setLocation(safeLocation);
}
};
}
private static Consumer<CommandSource> createForceTeleportConsumer(Player player, Location<World> location) {
return teleport -> player.setLocation(location);
}
public static Consumer<CommandSource> createCommandConsumer(CommandSource src, String command, String arguments,
Consumer<CommandSource> postConsumerTask) {
return consumer -> {
try {
Sponge.getCommandManager().get(command).get().getCallable().process(src, arguments);
} catch (CommandException e) {
src.sendMessage(e.getText());
}
if (postConsumerTask != null) {
postConsumerTask.accept(src);
}
};
}
}