| 1 |  |  | package de.pewpewproject.lasertag.command.suggestions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import com.mojang.brigadier.arguments.StringArgumentType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import com.mojang.brigadier.context.CommandContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import com.mojang.brigadier.suggestion.SuggestionProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import com.mojang.brigadier.suggestion.Suggestions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import com.mojang.brigadier.suggestion.SuggestionsBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import net.minecraft.server.command.ServerCommandSource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import java.util.concurrent.CompletableFuture; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Suggestion provider for the teams | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author Étienne Muser | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | public class TeamSuggestionProvider implements SuggestionProvider<ServerCommandSource> { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     private static TeamSuggestionProvider instance = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     private TeamSuggestionProvider() { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public static TeamSuggestionProvider getInstance() { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         if (instance == null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             instance = new TeamSuggestionProvider(); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         return instance; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     @Override | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         // Get the game managers | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         var gameManager = context.getSource().getWorld().getServerLasertagManager(); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         var teamsManager = gameManager.getTeamsManager(); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         var syncedState = gameManager.getSyncedState(); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         var teamsConfigState = syncedState.getTeamsConfigState(); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         boolean inputEmpty = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         // Try to get current input | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         String input = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             input = StringArgumentType.getString(context, "team"); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         } catch (IllegalArgumentException ex) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             inputEmpty = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         for (var team :  teamsConfigState.getTeams()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             if (inputEmpty || team.name().toLowerCase().startsWith(input.toLowerCase())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 builder.suggest(team.name()); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         return builder.buildFuture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  |