generated from xpdustry/template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add lamp in the available command APIs
- Loading branch information
Showing
11 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plugins { | ||
id("distributor.base-conventions") | ||
id("distributor.publish-conventions") | ||
} | ||
|
||
dependencies { | ||
api(libs.lamp.common) | ||
compileOnlyApi(projects.distributorCommonApi) | ||
compileOnlyApi(libs.bundles.mindustry) | ||
compileOnlyApi(libs.immutables.annotations) | ||
compileOnly(libs.jetbrains.annotations) | ||
annotationProcessor(libs.immutables.processor) | ||
} |
130 changes: 130 additions & 0 deletions
130
...r-command-lamp/src/main/java/com/xpdustry/distributor/command/lamp/LampCommandFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.command.lamp; | ||
|
||
import arc.util.CommandHandler; | ||
import com.xpdustry.distributor.command.CommandFacade; | ||
import com.xpdustry.distributor.command.CommandHelp; | ||
import com.xpdustry.distributor.command.CommandSender; | ||
import com.xpdustry.distributor.command.DescriptionFacade; | ||
import com.xpdustry.distributor.plugin.MindustryPlugin; | ||
import java.util.Map; | ||
import mindustry.gen.Player; | ||
import org.jspecify.annotations.Nullable; | ||
import revxrsal.commands.core.CommandPath; | ||
|
||
final class LampCommandFacade extends CommandHandler.Command implements CommandFacade { | ||
|
||
final MindustryCommandHandler handler; | ||
private final String name; | ||
private final DescriptionFacade description; | ||
private final boolean alias; | ||
private final boolean prefixed; | ||
|
||
LampCommandFacade( | ||
final MindustryCommandHandler handler, | ||
final String name, | ||
final DescriptionFacade description, | ||
final boolean alias, | ||
final boolean prefixed) { | ||
super( | ||
prefixed ? handler.getPlugin().getMetadata().getName() + ":" + name : name, | ||
"[args...]", | ||
description.getText(), | ||
new LampCommandRunner(name, handler)); | ||
this.handler = handler; | ||
this.name = name; | ||
this.description = description; | ||
this.alias = alias; | ||
this.prefixed = prefixed; | ||
} | ||
|
||
@Override | ||
public String getRealName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.text; | ||
} | ||
|
||
@Override | ||
public DescriptionFacade getDescription() { | ||
return this.description; | ||
} | ||
|
||
@Override | ||
public boolean isAlias() { | ||
return this.alias; | ||
} | ||
|
||
@Override | ||
public boolean isPrefixed() { | ||
return this.prefixed; | ||
} | ||
|
||
@Override | ||
public boolean isVisible(final CommandSender sender) { | ||
final var actor = this.handler.wrap(sender); | ||
final var root = CommandPath.get(this.name); | ||
return this.handler.getCommands().entrySet().stream() | ||
.filter(entry -> entry.getKey().isChildOf(root)) | ||
.map(Map.Entry::getValue) | ||
.anyMatch(command -> command.hasPermission(actor)); | ||
} | ||
|
||
@Override | ||
public CommandHelp getHelp(final CommandSender sender, final String query) { | ||
return CommandHelp.Empty.getInstance(); | ||
} | ||
|
||
@Override | ||
public MindustryPlugin getPlugin() { | ||
return this.handler.getPlugin(); | ||
} | ||
|
||
@SuppressWarnings("ClassCanBeRecord") | ||
private static class LampCommandRunner implements CommandHandler.CommandRunner<Player> { | ||
|
||
private final String name; | ||
private final MindustryCommandHandler handler; | ||
|
||
private LampCommandRunner(final String name, final MindustryCommandHandler handler) { | ||
this.name = name; | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
public void accept(final String[] args, final @Nullable Player player) { | ||
final var actor = this.handler.wrap(player != null ? CommandSender.player(player) : CommandSender.server()); | ||
|
||
final var input = new StringBuilder(this.name); | ||
for (final var arg : args) { | ||
input.append(' ').append(arg); | ||
} | ||
|
||
try { | ||
this.handler.dispatch(actor, input.toString()); | ||
} catch (final Throwable throwable) { | ||
this.handler.getExceptionHandler().handleException(throwable, actor); | ||
} | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...ributor-command-lamp/src/main/java/com/xpdustry/distributor/command/lamp/LampElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.command.lamp; | ||
|
||
import com.xpdustry.distributor.internal.DistributorDataClass; | ||
import java.util.Objects; | ||
import org.immutables.value.Value; | ||
import revxrsal.commands.command.CommandCategory; | ||
import revxrsal.commands.command.CommandParameter; | ||
import revxrsal.commands.command.ExecutableCommand; | ||
|
||
public sealed interface LampElement { | ||
|
||
String getName(); | ||
|
||
String getDescription(); | ||
|
||
@DistributorDataClass | ||
@Value.Immutable | ||
sealed interface Category extends LampElement permits CategoryImpl { | ||
|
||
static Category of(final CommandCategory category) { | ||
return CategoryImpl.of(category); | ||
} | ||
|
||
CommandCategory getCategory(); | ||
|
||
@Override | ||
default String getName() { | ||
return getCategory().getName(); | ||
} | ||
|
||
@Override | ||
default String getDescription() { | ||
return ""; | ||
} | ||
} | ||
|
||
@DistributorDataClass | ||
@Value.Immutable | ||
sealed interface Command extends LampElement permits CommandImpl { | ||
|
||
static Command of(final ExecutableCommand command) { | ||
return CommandImpl.of(command); | ||
} | ||
|
||
ExecutableCommand getCommand(); | ||
|
||
@Override | ||
default String getName() { | ||
return getCommand().getName(); | ||
} | ||
|
||
@Override | ||
default String getDescription() { | ||
return Objects.requireNonNullElse(getCommand().getDescription(), ""); | ||
} | ||
} | ||
|
||
@DistributorDataClass | ||
@Value.Immutable | ||
sealed interface Parameter extends LampElement permits ParameterImpl { | ||
|
||
static Parameter of(final CommandParameter parameter) { | ||
return ParameterImpl.of(parameter); | ||
} | ||
|
||
CommandParameter getParameter(); | ||
|
||
@Override | ||
default String getName() { | ||
return getParameter().getName(); | ||
} | ||
|
||
@Override | ||
default String getDescription() { | ||
return Objects.requireNonNullElse(getParameter().getDescription(), ""); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...mmand-lamp/src/main/java/com/xpdustry/distributor/command/lamp/MindustryCommandActor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.command.lamp; | ||
|
||
import com.xpdustry.distributor.command.CommandSender; | ||
import revxrsal.commands.command.CommandActor; | ||
|
||
public interface MindustryCommandActor extends CommandActor { | ||
|
||
CommandSender getCommandSender(); | ||
} |
75 changes: 75 additions & 0 deletions
75
...d-lamp/src/main/java/com/xpdustry/distributor/command/lamp/MindustryCommandActorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.command.lamp; | ||
|
||
import com.xpdustry.distributor.command.CommandSender; | ||
import com.xpdustry.distributor.player.MUUID; | ||
import java.util.Locale; | ||
import java.util.UUID; | ||
import revxrsal.commands.CommandHandler; | ||
|
||
final class MindustryCommandActorImpl implements MindustryCommandActor { | ||
|
||
private static final UUID CONSOLE_UUID = new UUID(0, 0); | ||
|
||
private final MindustryCommandHandler handler; | ||
private final CommandSender sender; | ||
|
||
MindustryCommandActorImpl(final MindustryCommandHandler handler, final CommandSender sender) { | ||
this.handler = handler; | ||
this.sender = sender; | ||
} | ||
|
||
@Override | ||
public CommandSender getCommandSender() { | ||
return this.sender; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.sender.getName(); | ||
} | ||
|
||
@Override | ||
public UUID getUniqueId() { | ||
return this.sender.isServer() | ||
? CONSOLE_UUID | ||
: MUUID.from(this.sender.getPlayer()).toRealUUID(); | ||
} | ||
|
||
@Override | ||
public void reply(final String message) { | ||
this.sender.sendMessage(message); | ||
} | ||
|
||
@Override | ||
public void error(final String message) { | ||
this.sender.sendWarning(message); | ||
} | ||
|
||
@Override | ||
public CommandHandler getCommandHandler() { | ||
return this.handler; | ||
} | ||
|
||
@Override | ||
public Locale getLocale() { | ||
return this.sender.getLocale(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...and-lamp/src/main/java/com/xpdustry/distributor/command/lamp/MindustryCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.command.lamp; | ||
|
||
import com.xpdustry.distributor.command.CommandSender; | ||
import com.xpdustry.distributor.command.DescriptionMapper; | ||
import com.xpdustry.distributor.plugin.MindustryPlugin; | ||
import com.xpdustry.distributor.plugin.PluginAware; | ||
import revxrsal.commands.CommandHandler; | ||
|
||
public interface MindustryCommandHandler extends CommandHandler, PluginAware { | ||
|
||
static MindustryCommandHandler create( | ||
final MindustryPlugin plugin, final DescriptionMapper<LampElement> descriptionMapper) { | ||
return new MindustryCommandHandlerImpl(plugin, descriptionMapper); | ||
} | ||
|
||
MindustryCommandActor wrap(final CommandSender sender); | ||
|
||
void initialize(final arc.util.CommandHandler handler); | ||
} |
Oops, something went wrong.