Skip to content

Commit

Permalink
feat: Add lamp in the available command APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Apr 8, 2024
1 parent a6f701a commit f69745e
Show file tree
Hide file tree
Showing 11 changed files with 524 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ repositories {
name = "xpdustry-mindustry"
mavenContent { releasesOnly() }
}
maven("https://jitpack.io") {
name = "jitpack"
mavenContent { releasesOnly() }
}
}

dependencies {
Expand Down
13 changes: 13 additions & 0 deletions distributor-command-lamp/build.gradle.kts
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)
}
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);
}
}
}
}
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(), "");
}
}
}
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();
}
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();
}
}
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);
}
Loading

0 comments on commit f69745e

Please sign in to comment.