mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-28 12:07:35 +01:00
Implemented Sponge command adapters
This commit is contained in:
parent
e95e84a89b
commit
a76932c256
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan.commands.use;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.text.TextStringBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -24,7 +25,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Utility for managing command arguments.
|
||||
* Command argument mutation and access utility.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@ -32,6 +33,10 @@ public class Arguments {
|
||||
|
||||
private final List<String> args;
|
||||
|
||||
public Arguments(String argumentsAsString) {
|
||||
this(StringUtils.split(argumentsAsString, ' '));
|
||||
}
|
||||
|
||||
public Arguments(String[] args) {
|
||||
this.args = Arrays.asList(args);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan;
|
||||
|
||||
import com.djrapitops.plan.commands.OldPlanCommand;
|
||||
import com.djrapitops.plan.commands.use.SpongeCommand;
|
||||
import com.djrapitops.plan.commands.use.Subcommand;
|
||||
import com.djrapitops.plan.exceptions.EnableException;
|
||||
import com.djrapitops.plan.gathering.ServerShutdownSave;
|
||||
@ -30,6 +30,8 @@ import org.bstats.sponge.Metrics2;
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.command.CommandManager;
|
||||
import org.spongepowered.api.command.CommandMapping;
|
||||
import org.spongepowered.api.config.ConfigDir;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
|
||||
@ -39,6 +41,9 @@ import org.spongepowered.api.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Plugin(
|
||||
id = "plan",
|
||||
@ -69,6 +74,8 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
|
||||
private Locale locale;
|
||||
private ServerShutdownSave serverShutdownSave;
|
||||
|
||||
private final Map<String, CommandMapping> commands = new HashMap<>();
|
||||
|
||||
@Listener
|
||||
public void onServerStart(GameStartedServerEvent event) {
|
||||
onEnable();
|
||||
@ -108,9 +115,7 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
|
||||
logger.error("This error should be reported at https://github.com/Rsl1122/Plan-PlayerAnalytics/issues");
|
||||
onDisable();
|
||||
}
|
||||
OldPlanCommand command = component.planCommand();
|
||||
command.registerCommands();
|
||||
registerCommand("plan", command);
|
||||
registerCommand(component.planCommand().build());
|
||||
if (system != null) {
|
||||
system.getProcessing().submitNonCritical(() -> system.getListenerSystem().callEnableEvent(this));
|
||||
}
|
||||
@ -149,8 +154,22 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerCommand(Subcommand subcommand) {
|
||||
throw new UnsupportedOperationException();
|
||||
public void registerCommand(Subcommand command) {
|
||||
if (command == null) {
|
||||
logger.warn("Attempted to register a null command!");
|
||||
return;
|
||||
}
|
||||
for (String name : command.getAliases()) {
|
||||
CommandManager commandManager = Sponge.getCommandManager();
|
||||
|
||||
CommandMapping registered = commands.get(name);
|
||||
if (registered != null) {
|
||||
commandManager.removeMapping(registered);
|
||||
}
|
||||
|
||||
Optional<CommandMapping> register = commandManager.register(this, new SpongeCommand(runnableFactory, command), name);
|
||||
register.ifPresent(commandMapping -> commands.put(name, commandMapping));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan;
|
||||
|
||||
import com.djrapitops.plan.commands.OldPlanCommand;
|
||||
import com.djrapitops.plan.commands.PlanCommand;
|
||||
import com.djrapitops.plan.gathering.ServerShutdownSave;
|
||||
import com.djrapitops.plan.modules.APFModule;
|
||||
import com.djrapitops.plan.modules.PlaceholderModule;
|
||||
@ -48,7 +48,7 @@ import javax.inject.Singleton;
|
||||
})
|
||||
public interface PlanSpongeComponent {
|
||||
|
||||
OldPlanCommand planCommand();
|
||||
PlanCommand planCommand();
|
||||
|
||||
PlanSystem system();
|
||||
|
||||
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.use;
|
||||
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.text.Text;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SpongeCMDSender implements CMDSender {
|
||||
|
||||
final CommandSource source;
|
||||
|
||||
public SpongeCMDSender(CommandSource source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageBuilder buildMessage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getPlayerName() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String permission) {
|
||||
return source.hasPermission(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<UUID> getUUID() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(String text) {
|
||||
source.sendMessage(Text.of(text));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatFormatter getFormatter() {
|
||||
return new ConsoleChatFormatter();
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.use;
|
||||
|
||||
import com.djrapitops.plugin.task.AbsRunnable;
|
||||
import com.djrapitops.plugin.task.RunnableFactory;
|
||||
import org.spongepowered.api.command.CommandCallable;
|
||||
import org.spongepowered.api.command.CommandException;
|
||||
import org.spongepowered.api.command.CommandResult;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.command.source.RconSource;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.text.Text;
|
||||
import org.spongepowered.api.world.Location;
|
||||
import org.spongepowered.api.world.World;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SpongeCommand implements CommandCallable {
|
||||
|
||||
private final RunnableFactory runnableFactory;
|
||||
private final Subcommand command;
|
||||
|
||||
public SpongeCommand(
|
||||
RunnableFactory runnableFactory,
|
||||
Subcommand command
|
||||
) {
|
||||
this.runnableFactory = runnableFactory;
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult process(CommandSource source, String arguments) throws CommandException {
|
||||
runnableFactory.create("", new AbsRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
command.getExecutor().accept(getSender(source), new Arguments(arguments));
|
||||
}
|
||||
}).runTaskAsynchronously();
|
||||
return CommandResult.success();
|
||||
}
|
||||
|
||||
private CMDSender getSender(CommandSource source) {
|
||||
if (source instanceof Player || source instanceof RconSource) {
|
||||
return new SpongePlayerCMDSender(source);
|
||||
} else {
|
||||
return new SpongeCMDSender(source);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
|
||||
return command.getArgumentResolver()
|
||||
.apply(getSender(source), new Arguments(arguments));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testPermission(CommandSource source) {
|
||||
for (String requiredPermission : command.getRequiredPermissions()) {
|
||||
if (!source.hasPermission(requiredPermission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Text> getShortDescription(CommandSource source) {
|
||||
return Optional.of(Text.of(command.getDescription()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Text> getHelp(CommandSource source) {
|
||||
return Optional.of(Text.of(command.getDescription()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Text getUsage(CommandSource source) {
|
||||
return Text.of(command.getInDepthDescription());
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.use;
|
||||
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SpongePlayerCMDSender extends SpongeCMDSender {
|
||||
|
||||
public SpongePlayerCMDSender(CommandSource source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getPlayerName() {
|
||||
return source.getFriendlyIdentifier();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<UUID> getUUID() {
|
||||
if (source instanceof Player) {
|
||||
return Optional.of(((Player) source).getUniqueId());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatFormatter getFormatter() {
|
||||
return new PlayerChatFormatter();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user