/
MechanicCommands.java
181 lines (159 loc) · 8.26 KB
/
MechanicCommands.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
/*
* CraftBook Copyright (C) EngineHub and Contributors <https://enginehub.org/>
*
* 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 org.enginehub.craftbook.mechanic;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.util.formatting.component.InvalidComponentException;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.enginehub.craftbook.CraftBook;
import org.enginehub.craftbook.bukkit.BukkitCraftBookPlatform;
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
import org.enginehub.craftbook.mechanic.exception.MechanicInitializationException;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandManagerService;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.annotation.param.ArgFlag;
import org.enginehub.piston.part.SubCommandPart;
import java.util.Optional;
import java.util.stream.Collectors;
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
public class MechanicCommands {
public static void register(CommandManagerService service, CommandManager commandManager, CommandRegistrationHandler registration) {
commandManager.register("mechanic", builder -> {
builder.aliases(Lists.newArrayList("mech", "mechs", "mechanics"));
builder.description(TextComponent.of("Mechanic Commands"));
CommandManager innerManager = service.newCommandManager();
registration.register(
innerManager,
MechanicCommandsRegistration.builder(),
new MechanicCommands()
);
builder.addPart(SubCommandPart
.builder(TranslatableComponent.of("worldedit.argument.action"), TextComponent.of("Sub-command to run."))
.withCommands(innerManager.getAllCommands().collect(Collectors.toList()))
.required()
.build());
});
}
public MechanicCommands() {
}
@Command(name = "enable", desc = "Enable a mechanic")
@CommandPermissions({ "craftbook.mechanic.enable" })
public void enable(Actor actor,
@Arg(desc = "The mechanic to enable")
MechanicType<?> mechanicType,
@ArgFlag(name = 'l', desc = "Show the list box", def = "0")
int listBoxPage) throws InvalidComponentException {
CraftBookPlugin plugin = CraftBookPlugin.inst();
try {
CraftBook.getInstance().getPlatform().getMechanicManager().enableMechanic(mechanicType);
CraftBook.getInstance().getPlatform().getConfiguration().enabledMechanics.add(mechanicType.getId());
CraftBook.getInstance().getPlatform().getConfiguration().save();
if (plugin.getCommandManager().getMechanicRegistrar().isDirty()) {
((BukkitCraftBookPlatform) CraftBook.getInstance().getPlatform()).resetCommandRegistration(plugin);
Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
}
actor.printInfo(TranslatableComponent.of(
"craftbook.mechanisms.enable-success",
TextComponent.of(mechanicType.getName(), TextColor.WHITE)
));
} catch (MechanicInitializationException e) {
actor.printError(TranslatableComponent.of(
"craftbook.mechanisms.enable-failed",
TextComponent.of(mechanicType.getName(), TextColor.WHITE),
e.getRichMessage()
));
}
if (listBoxPage > 0) {
showListBox(actor, listBoxPage);
}
}
@Command(name = "disable", desc = "Disable a mechanic")
@CommandPermissions({ "craftbook.mechanic.disable" })
public void disable(Actor actor,
@Arg(desc = "The mechanic to disable")
MechanicType<?> mechanicType,
@ArgFlag(name = 'l', desc = "Show the list box", def = "0")
int listBoxPage) throws InvalidComponentException {
CraftBookPlugin plugin = CraftBookPlugin.inst();
Optional<?> mech = CraftBook.getInstance().getPlatform().getMechanicManager().getMechanic(mechanicType);
if (mech.isPresent() && CraftBook.getInstance().getPlatform().getMechanicManager().disableMechanic((CraftBookMechanic) mech.get())) {
CraftBook.getInstance().getPlatform().getConfiguration().enabledMechanics.remove(mechanicType.getId());
CraftBook.getInstance().getPlatform().getConfiguration().save();
if (plugin.getCommandManager().getMechanicRegistrar().isDirty()) {
((BukkitCraftBookPlatform) CraftBook.getInstance().getPlatform()).resetCommandRegistration(plugin);
Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
}
actor.printInfo(TranslatableComponent.of(
"craftbook.mechanisms.disable-success",
TextComponent.of(mechanicType.getName(), TextColor.WHITE)
));
} else {
actor.printError(TranslatableComponent.of(
"craftbook.mechanisms.not-enabled",
TextComponent.of(mechanicType.getName(), TextColor.WHITE)
));
}
if (listBoxPage > 0) {
showListBox(actor, listBoxPage);
}
}
@Command(name = "reload", desc = "Reload a mechanic's configuration")
@CommandPermissions({ "craftbook.mechanic.reload" })
public void reload(Actor actor,
@Arg(desc = "The mechanic to reload")
MechanicType<?> mechanicType) {
try {
MechanicManager mechanicManager = CraftBook.getInstance().getPlatform().getMechanicManager();
Optional<? extends CraftBookMechanic> mechanic = mechanicManager.getMechanic(mechanicType);
if (!mechanic.isPresent()) {
actor.printError(TranslatableComponent.of(
"craftbook.mechanisms.not-enabled",
TextComponent.of(mechanicType.getName(), TextColor.WHITE)
));
return;
}
mechanicManager.reloadMechanic(mechanic.get());
actor.printInfo(TranslatableComponent.of(
"craftbook.mechanisms.reload-success",
TextComponent.of(mechanicType.getName(), TextColor.WHITE)
));
} catch (MechanicInitializationException e) {
actor.printError(TranslatableComponent.of(
"craftbook.mechanisms.reload-failed",
TextComponent.of(mechanicType.getName(), TextColor.WHITE),
e.getRichMessage()
));
}
}
@Command(name = "list", desc = "List mechanics")
@CommandPermissions({ "craftbook.mechanic.list" })
public void list(Actor actor, @ArgFlag(name = 'p', desc = "The page number", def = "1") int page) throws InvalidComponentException {
showListBox(actor, page);
}
private void showListBox(Actor actor, int page) throws InvalidComponentException {
MechanicListBox mechanicListBox = new MechanicListBox(actor);
actor.print(mechanicListBox.create(page));
}
}