Created
          March 31, 2020 17:54 
        
      - 
      
- 
        Save Lanse505/e1c145c45e85fe5d3331d3f891d54d30 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package com.teamacronymcoders.essence.util.command.argument; | |
| import com.google.common.collect.Lists; | |
| import com.mojang.brigadier.StringReader; | |
| import com.mojang.brigadier.arguments.ArgumentType; | |
| import com.mojang.brigadier.context.CommandContext; | |
| import com.mojang.brigadier.exceptions.CommandSyntaxException; | |
| import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; | |
| import com.mojang.brigadier.suggestion.Suggestions; | |
| import com.mojang.brigadier.suggestion.SuggestionsBuilder; | |
| import net.minecraft.command.ISuggestionProvider; | |
| import net.minecraft.util.ResourceLocation; | |
| import net.minecraft.util.text.TranslationTextComponent; | |
| import net.minecraftforge.registries.IForgeRegistry; | |
| import net.minecraftforge.registries.IForgeRegistryEntry; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| import java.util.Optional; | |
| import java.util.concurrent.CompletableFuture; | |
| public class EssenceRegistryArgumentType<T extends IForgeRegistryEntry<T>> implements ArgumentType<T> { | |
| private final IForgeRegistry<T> registry; | |
| private final List<String> examples; | |
| private final DynamicCommandExceptionType exceptionType = new DynamicCommandExceptionType((input) -> | |
| new TranslationTextComponent("command.argument.essence.registry.invalid", input)); | |
| public EssenceRegistryArgumentType(IForgeRegistry<T> registry) { | |
| this.registry = registry; | |
| this.examples = createExamples(registry); | |
| } | |
| private static <T extends IForgeRegistryEntry<T>> List<String> createExamples(IForgeRegistry<T> registry) { | |
| List<String> examples = Lists.newArrayList(); | |
| Iterator<ResourceLocation> iterator = registry.getKeys().iterator(); | |
| int i = 0; | |
| while (iterator.hasNext() & i < 5) { | |
| i++; | |
| examples.add(iterator.next().toString()); | |
| } | |
| return examples; | |
| } | |
| @Override | |
| public T parse(StringReader reader) throws CommandSyntaxException { | |
| ResourceLocation resourceLocation = ResourceLocation.read(reader); | |
| return Optional.ofNullable(registry.getValue(resourceLocation)) | |
| .orElseThrow(() -> exceptionType.create(resourceLocation)); | |
| } | |
| @Override | |
| public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { | |
| return ISuggestionProvider.func_212476_a(registry.getKeys().stream(), builder); | |
| } | |
| @Override | |
| public List<String> getExamples() { | |
| return examples; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment