|
| 1 | +# Dependency Injection Implementation |
| 2 | + |
| 3 | +This document describes the dependency injection (DI) implementation using Google Guice to improve the maintainability and extensibility of AntiRedstoneClock-Remastered. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The plugin has been refactored to use dependency injection patterns, making it more modular and easier to test. The implementation uses Google Guice as the DI framework. |
| 8 | + |
| 9 | +## Module Structure |
| 10 | + |
| 11 | +### ServiceModule |
| 12 | +Manages core plugin services: |
| 13 | +- `RedstoneClockService` - Core redstone clock detection logic |
| 14 | +- `UpdateService` - Plugin update checking and notifications |
| 15 | +- `CheckTPS` - TPS monitoring functionality |
| 16 | +- `TranslationService` - Internationalization support (provider method selects modern/legacy based on server version) |
| 17 | + |
| 18 | +### ExternalSupportModule |
| 19 | +Handles optional external plugin integrations: |
| 20 | +- `WorldGuardSupport` - Integration with WorldGuard plugin (provider method selects v6/v7 based on version) |
| 21 | +- `PlotsquaredSupport` - Integration with PlotSquared plugin (provider method selects v6/v7 based on version) |
| 22 | + |
| 23 | +### CommandModule |
| 24 | +Manages command classes: |
| 25 | +- `ReloadCommand` |
| 26 | +- `DisplayActiveClocksCommand` |
| 27 | +- `FeatureCommand` |
| 28 | + |
| 29 | +### ListenerModule |
| 30 | +Manages event listeners: |
| 31 | +- `PlayerListener` |
| 32 | +- `ObserverListener` |
| 33 | +- Additional listeners can be added as they get refactored |
| 34 | + |
| 35 | +## Benefits |
| 36 | + |
| 37 | +### 1. Improved Maintainability |
| 38 | +- Dependencies are explicitly declared through constructor injection |
| 39 | +- No more manual service creation scattered throughout the main class |
| 40 | +- Clear separation of concerns between different modules |
| 41 | + |
| 42 | +### 2. Enhanced Testability |
| 43 | +- Services can be easily mocked for unit testing |
| 44 | +- Dependencies are injected rather than hard-coded |
| 45 | +- Easier to create isolated test scenarios |
| 46 | + |
| 47 | +### 3. Better Extensibility for Folia |
| 48 | +- Service layer is now modular and can be easily extended |
| 49 | +- Platform-specific implementations can be swapped in via configuration |
| 50 | +- Clear interface boundaries make it easier to add platform-specific optimizations |
| 51 | + |
| 52 | +### 4. Reduced Coupling |
| 53 | +- Classes depend on interfaces rather than concrete implementations |
| 54 | +- Easier to change implementations without affecting consumers |
| 55 | +- Better adherence to SOLID principles |
| 56 | + |
| 57 | +## Usage Examples |
| 58 | + |
| 59 | +### Injecting Dependencies |
| 60 | +```java |
| 61 | +@Singleton |
| 62 | +public class SomeService { |
| 63 | + private final RedstoneClockService clockService; |
| 64 | + private final CheckTPS tpsChecker; |
| 65 | + |
| 66 | + @Inject |
| 67 | + public SomeService(RedstoneClockService clockService, CheckTPS tpsChecker) { |
| 68 | + this.clockService = clockService; |
| 69 | + this.tpsChecker = tpsChecker; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Adding New Services |
| 75 | +To add a new service: |
| 76 | +1. Create the service class with `@Inject` constructor |
| 77 | +2. Add `@Singleton` if it should be a singleton |
| 78 | +3. Bind it in the appropriate module's `configure()` method |
| 79 | +4. Inject it where needed |
| 80 | + |
| 81 | +### Creating Platform-Specific Implementations |
| 82 | +```java |
| 83 | +@Provides |
| 84 | +@Singleton |
| 85 | +public SomeService provideSomeService(AntiRedstoneClockRemastered plugin) { |
| 86 | + if (plugin.getServer().getName().equals("Folia")) { |
| 87 | + return new FoliaSomeService(plugin); |
| 88 | + } else { |
| 89 | + return new DefaultSomeService(plugin); |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Migration Notes |
| 95 | + |
| 96 | +- Legacy listeners that haven't been refactored yet still use manual instantiation |
| 97 | +- External support (WorldGuard/PlotSquared) now uses provider methods for version detection |
| 98 | +- The main plugin class is significantly simplified with most manual setup removed |
| 99 | + |
| 100 | +## Future Improvements |
| 101 | + |
| 102 | +1. Refactor remaining listeners to use DI |
| 103 | +2. Add interface abstractions for better testability |
| 104 | +3. Consider platform-specific modules for Folia support |
| 105 | +4. Add configuration-based service selection |
0 commit comments