refactor: DIP — core services & listeners use constructor injection, ServiceRegistry removed

This commit is contained in:
loki5512344 2026-07-05 16:16:18 +02:00
parent a66d2b54d5
commit 09689ae799
14 changed files with 44 additions and 34 deletions

View file

@ -1,5 +1,9 @@
package com.loki.lochat; package com.loki.lochat;
import com.loki.lochat.api.service.MessageService;
import com.loki.lochat.api.service.MessagingService;
import com.loki.lochat.api.service.NickService;
import com.loki.lochat.api.service.PlayerService;
import com.loki.lochat.api.service.PunishmentService; import com.loki.lochat.api.service.PunishmentService;
import com.loki.lochat.commands.CommandManager; import com.loki.lochat.commands.CommandManager;
import com.loki.lochat.config.ConfigManager; import com.loki.lochat.config.ConfigManager;
@ -65,7 +69,12 @@ public final class LoChat extends JavaPlugin {
// Команды и слушатели // Команды и слушатели
commandManager = new CommandManager(this); commandManager = new CommandManager(this);
commandManager.registerCommands(); commandManager.registerCommands();
initializer.registerListeners(serviceRegistry, discordIntegration); initializer.registerListeners(serviceRegistry, discordIntegration,
serviceRegistry.get(MessageService.class),
serviceRegistry.get(PlayerService.class),
serviceRegistry.get(MessagingService.class),
serviceRegistry.get(NickService.class),
serviceRegistry.get(PunishmentService.class));
initializer.registerPluginChannels(); initializer.registerPluginChannels();
// Автосообщения // Автосообщения

View file

@ -1,6 +1,11 @@
package com.loki.lochat.core; package com.loki.lochat.core;
import com.loki.lochat.LoChat; import com.loki.lochat.LoChat;
import com.loki.lochat.api.service.MessageService;
import com.loki.lochat.api.service.MessagingService;
import com.loki.lochat.api.service.NickService;
import com.loki.lochat.api.service.PlayerService;
import com.loki.lochat.api.service.PunishmentService;
import com.loki.lochat.config.ConfigManager; import com.loki.lochat.config.ConfigManager;
import com.loki.lochat.config.MessageConfig; import com.loki.lochat.config.MessageConfig;
import com.loki.lochat.core.filter.AdvancedMessageFilter; import com.loki.lochat.core.filter.AdvancedMessageFilter;
@ -96,18 +101,20 @@ public class PluginInitializer {
/** /**
* Зарегистрировать слушатели событий * Зарегистрировать слушатели событий
*/ */
public AdvancedMessageFilter registerListeners(ServiceRegistry serviceRegistry, DiscordIntegration discordIntegration) { public AdvancedMessageFilter registerListeners(ServiceRegistry serviceRegistry, DiscordIntegration discordIntegration,
// Создаём один экземпляр фильтра и передаём в оба листенера MessageService messageService, PlayerService playerService,
MessagingService messagingService, NickService nickService,
PunishmentService punishmentService) {
AdvancedMessageFilter filter = new AdvancedMessageFilter(plugin.getConfig(), plugin); AdvancedMessageFilter filter = new AdvancedMessageFilter(plugin.getConfig(), plugin);
plugin.getServer().getPluginManager().registerEvents( plugin.getServer().getPluginManager().registerEvents(
new ChatEventListener(plugin, serviceRegistry, filter), plugin new ChatEventListener(plugin, messageService, playerService, filter), plugin
); );
plugin.getServer().getPluginManager().registerEvents( plugin.getServer().getPluginManager().registerEvents(
new PlayerEventListener(plugin, serviceRegistry, filter), plugin new PlayerEventListener(plugin, playerService, messagingService, nickService, filter), plugin
); );
plugin.getServer().getPluginManager().registerEvents( plugin.getServer().getPluginManager().registerEvents(
new ModerationListener(serviceRegistry), plugin new ModerationListener(punishmentService), plugin
); );
// Discord listener // Discord listener

View file

@ -41,16 +41,15 @@ public class ServiceRegistry {
} }
private void registerServicesWithDeps(ConfigManager configManager, MessageConfig messageConfig) { private void registerServicesWithDeps(ConfigManager configManager, MessageConfig messageConfig) {
register(ChatService.class, new ChatServiceImpl(plugin, this)); MuteService muteService = ServiceFactory.createMuteService(plugin);
register(MuteService.class, ServiceFactory.createMuteService(plugin)); register(MuteService.class, muteService);
// ✅ NEW: Объединённый PlayerService (Cooldown + PlayerData)
PlayerService playerService = new PlayerServiceImpl(plugin); PlayerService playerService = new PlayerServiceImpl(plugin);
register(PlayerService.class, playerService); register(PlayerService.class, playerService);
register(MessageService.class, new MessageServiceImpl(plugin, this)); register(ChatService.class, new ChatServiceImpl(plugin));
register(MessageService.class, new MessageServiceImpl(plugin, muteService, playerService));
// ✅ NEW: Объединённый MessagingService (PM + Spy + Ignore) через фабрику
PrivateMessageServiceImpl pmService = new PrivateMessageServiceImpl((LoChat) plugin); PrivateMessageServiceImpl pmService = new PrivateMessageServiceImpl((LoChat) plugin);
MessagingService messagingService = ServiceFactory.createMessagingService(plugin, messageConfig, pmService); MessagingService messagingService = ServiceFactory.createMessagingService(plugin, messageConfig, pmService);
pmService.init(messagingService); pmService.init(messagingService);

View file

@ -1,7 +1,6 @@
package com.loki.lochat.core.service; package com.loki.lochat.core.service;
import com.loki.lochat.api.service.ChatService; import com.loki.lochat.api.service.ChatService;
import com.loki.lochat.core.registry.ServiceRegistry;
import com.loki.lochat.renderer.EnhancedChatRenderer; import com.loki.lochat.renderer.EnhancedChatRenderer;
import com.loki.lochat.utils.format.ChatFormatter; import com.loki.lochat.utils.format.ChatFormatter;
import com.loki.lochat.utils.persistence.FilePersistence; import com.loki.lochat.utils.persistence.FilePersistence;
@ -29,7 +28,7 @@ public class ChatServiceImpl implements ChatService {
private final JavaPlugin plugin; private final JavaPlugin plugin;
private final Set<UUID> globalChatDisabled = ConcurrentHashMap.newKeySet(); private final Set<UUID> globalChatDisabled = ConcurrentHashMap.newKeySet();
public ChatServiceImpl(JavaPlugin plugin, ServiceRegistry registry) { public ChatServiceImpl(JavaPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
loadDisabled(); loadDisabled();
} }

View file

@ -6,7 +6,6 @@ import com.loki.lochat.api.service.MuteService;
import com.loki.lochat.api.service.PlayerService; import com.loki.lochat.api.service.PlayerService;
import com.loki.lochat.core.filter.CooldownFilter; import com.loki.lochat.core.filter.CooldownFilter;
import com.loki.lochat.core.filter.MuteFilter; import com.loki.lochat.core.filter.MuteFilter;
import com.loki.lochat.core.registry.ServiceRegistry;
import com.loki.lochat.data.model.ChatMessage; import com.loki.lochat.data.model.ChatMessage;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -21,13 +20,10 @@ import java.util.List;
public class MessageServiceImpl implements MessageService { public class MessageServiceImpl implements MessageService {
private final List<MessageFilter> filters; private final List<MessageFilter> filters;
public MessageServiceImpl(JavaPlugin plugin, ServiceRegistry registry) { public MessageServiceImpl(JavaPlugin plugin, MuteService muteService, PlayerService playerService) {
this.filters = new ArrayList<>(); this.filters = new ArrayList<>();
filters.add(new MuteFilter(muteService, plugin));
// Регистрируем фильтры в порядке применения filters.add(new CooldownFilter(playerService, plugin));
// MuteFilter теперь получает plugin для доступа к HardcodedMessages
filters.add(new MuteFilter(registry.get(MuteService.class), plugin));
filters.add(new CooldownFilter(registry.get(PlayerService.class), plugin));
} }
@Override @Override

View file

@ -2,9 +2,9 @@ package com.loki.lochat.listener;
import com.loki.lochat.LoChat; import com.loki.lochat.LoChat;
import com.loki.lochat.api.service.MessageService; import com.loki.lochat.api.service.MessageService;
import com.loki.lochat.api.service.PlayerService;
import com.loki.lochat.core.filter.AdvancedMessageFilter; import com.loki.lochat.core.filter.AdvancedMessageFilter;
import com.loki.lochat.core.filter.FilterResult; import com.loki.lochat.core.filter.FilterResult;
import com.loki.lochat.core.registry.ServiceRegistry;
import com.loki.lochat.renderer.EnhancedChatRenderer; import com.loki.lochat.renderer.EnhancedChatRenderer;
import com.loki.lochat.utils.format.ChatFormatter; import com.loki.lochat.utils.format.ChatFormatter;
@ -22,11 +22,14 @@ import io.papermc.paper.event.player.AsyncChatEvent;
public class ChatEventListener implements Listener { public class ChatEventListener implements Listener {
private final JavaPlugin plugin; private final JavaPlugin plugin;
private final MessageService messageService; private final MessageService messageService;
private final PlayerService playerService;
private final AdvancedMessageFilter advancedFilter; private final AdvancedMessageFilter advancedFilter;
public ChatEventListener(JavaPlugin plugin, ServiceRegistry registry, com.loki.lochat.core.filter.AdvancedMessageFilter filter) { public ChatEventListener(JavaPlugin plugin, MessageService messageService, PlayerService playerService,
AdvancedMessageFilter filter) {
this.plugin = plugin; this.plugin = plugin;
this.messageService = registry.get(MessageService.class); this.messageService = messageService;
this.playerService = playerService;
this.advancedFilter = filter; this.advancedFilter = filter;
} }
@ -148,8 +151,6 @@ public class ChatEventListener implements Listener {
} }
private void recordStatistics(LoChat loChat, Player sender, boolean isGlobal) { private void recordStatistics(LoChat loChat, Player sender, boolean isGlobal) {
com.loki.lochat.api.service.PlayerService playerService =
loChat.getServiceRegistry().get(com.loki.lochat.api.service.PlayerService.class);
playerService.recordMessage(sender.getUniqueId(), isGlobal ? "global" : "local"); playerService.recordMessage(sender.getUniqueId(), isGlobal ? "global" : "local");
} }

View file

@ -1,7 +1,6 @@
package com.loki.lochat.listener; package com.loki.lochat.listener;
import com.loki.lochat.api.service.PunishmentService; import com.loki.lochat.api.service.PunishmentService;
import com.loki.lochat.core.registry.ServiceRegistry;
import com.loki.lochat.data.model.BanRecord; import com.loki.lochat.data.model.BanRecord;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -16,8 +15,8 @@ public class ModerationListener implements Listener {
private final PunishmentService punishmentService; private final PunishmentService punishmentService;
public ModerationListener(ServiceRegistry registry) { public ModerationListener(PunishmentService punishmentService) {
this.punishmentService = registry.get(PunishmentService.class); this.punishmentService = punishmentService;
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)

View file

@ -6,7 +6,6 @@ import com.loki.lochat.api.service.NickService;
import com.loki.lochat.api.service.PlayerService; import com.loki.lochat.api.service.PlayerService;
import com.loki.lochat.config.ConfigManager; import com.loki.lochat.config.ConfigManager;
import com.loki.lochat.core.filter.AdvancedMessageFilter; import com.loki.lochat.core.filter.AdvancedMessageFilter;
import com.loki.lochat.core.registry.ServiceRegistry;
import com.loki.lochat.utils.format.ChatFormatter; import com.loki.lochat.utils.format.ChatFormatter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -32,11 +31,12 @@ public class PlayerEventListener implements Listener {
private final AdvancedMessageFilter advancedFilter; private final AdvancedMessageFilter advancedFilter;
private final ConfigManager configManager; private final ConfigManager configManager;
public PlayerEventListener(LoChat plugin, ServiceRegistry registry, AdvancedMessageFilter advancedFilter) { public PlayerEventListener(LoChat plugin, PlayerService playerService, MessagingService messagingService,
NickService nickService, AdvancedMessageFilter advancedFilter) {
this.plugin = plugin; this.plugin = plugin;
this.playerService = registry.get(PlayerService.class); this.playerService = playerService;
this.messagingService = registry.get(MessagingService.class); this.messagingService = messagingService;
this.nickService = registry.get(NickService.class); this.nickService = nickService;
this.advancedFilter = advancedFilter; this.advancedFilter = advancedFilter;
this.configManager = plugin.getConfigManager(); this.configManager = plugin.getConfigManager();
} }