dev.alangomes.springspigot.command.CommandInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spigot-spring-boot-starter Show documentation
Show all versions of spigot-spring-boot-starter Show documentation
Spring support for spigot plugins
package dev.alangomes.springspigot.command;
import dev.alangomes.springspigot.context.Context;
import lombok.val;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;
import picocli.CommandLine;
@Component
@ConditionalOnBean(annotation = CommandLine.Command.class)
class CommandInterceptor implements Listener {
@Autowired
private Context context;
@Autowired
private CommandExecutor commandExecutor;
@Autowired
private CommandService commandService;
@EventHandler
void onPlayerCommand(PlayerCommandPreprocessEvent event) {
if (event.isCancelled() || commandService.isRegistered()) return;
val player = context.getPlayer();
val result = commandExecutor.execute(event.getMessage().substring(1).split(" "));
event.setCancelled(result.isExists());
result.getOutput().forEach(player::sendMessage);
}
@EventHandler
void onServerCommand(ServerCommandEvent event) {
if (event.isCancelled() || commandService.isRegistered()) return;
val sender = context.getSender();
val result = commandExecutor.execute(event.getCommand().split(" "));
event.setCancelled(result.isExists());
result.getOutput().forEach(sender::sendMessage);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy