Created
March 16, 2015 22:41
-
-
Save Romain-P/ea7b38761a2709851857 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 org.heat.world; | |
| import com.google.inject.AbstractModule; | |
| import com.google.inject.multibindings.MapBinder; | |
| import org.heat.world.commands.CommandCond; | |
| import org.heat.world.commands.CommandManager; | |
| import org.heat.world.commands.CommandTree; | |
| import org.heat.world.commands.builder.CommandBuilder; | |
| import org.heat.world.commands.builder.CommandProvider; | |
| import org.heat.world.commands.impl.SimpleCommandManager; | |
| import org.heat.world.commands.impl.actions.HelpAction; | |
| import org.heat.world.commands.impl.conditions.StaffAccess; | |
| import java.util.function.Consumer; | |
| /** | |
| * Managed by romain on 08/03/2015. | |
| */ | |
| public class StdCommandsModule extends AbstractModule{ | |
| private MapBinder<String, CommandTree> commands; | |
| private MapBinder<Class, CommandCond> conditions; | |
| @Override | |
| protected void configure() { | |
| bind(CommandManager.class).to(SimpleCommandManager.class).asEagerSingleton(); | |
| commands = MapBinder.newMapBinder(binder(), String.class, CommandTree.class); | |
| conditions = MapBinder.newMapBinder(binder(), Class.class, CommandCond.class); | |
| loadConditions(); | |
| createCommands(); | |
| } | |
| private void createCommands() { | |
| createNewCommand("help", (x) -> x | |
| .to(HelpAction.class) | |
| .withSub("command", (c) -> c.to(HelpAction.class))); | |
| createNewCommand("test", (x) -> x | |
| .withSub("papa") | |
| .withSub("maman", (c) -> c.to(HelpAction.class))); | |
| } | |
| private void loadConditions() { | |
| loadNewCondition(StaffAccess.class); | |
| } | |
| private CommandTree createNewCommand(String name, Consumer<CommandBuilder> consumer) { | |
| CommandTree command = CommandProvider.create(name, consumer); | |
| commands.addBinding(name).toInstance(command); | |
| return command; | |
| } | |
| private void loadNewCondition(Class<? extends CommandCond> condClass) { | |
| conditions.addBinding(condClass).to(condClass); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment