Skip to content

Commit

Permalink
refactored interface to make it easier to use with new api
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit committed Jun 27, 2023
1 parent 01a28e4 commit 574a5e9
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.registrar.CommandRegistrar;
import org.spongepowered.plugin.PluginContainer;
Expand Down Expand Up @@ -75,6 +75,7 @@ class EventsImpl implements Events, Unregisterable {
return;
}
registrar.get().register(plugin, spec, name);
//TODO Sponge.server().commandManager().updateCommandTreeForPlayer();
unregistrables.add(() -> {

});
Expand Down Expand Up @@ -104,7 +105,7 @@ class EventsImpl implements Events, Unregisterable {
ch.vorburger.minecraft.storeys.japi.impl.events.Callback otherCallback = invoker -> {
invokeCallback(invoker, callback);
};
unregistrables.add(eventService.registerInteractEntity(entityName, otherCallback));
unregistrables.add(eventService.registerInteractEntity(Component.text(entityName), otherCallback));
}

@Override public void unregister() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@
import javax.inject.Inject;
import javax.inject.Singleton;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.api.data.Key;
import org.spongepowered.api.data.Keys;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.EventManager;
import org.spongepowered.api.event.Listener;
Expand All @@ -50,7 +47,7 @@
private final Collection<Callback> onPlayerJoinCallbacks = new ConcurrentLinkedQueue<>();

private final Map<String, Collection<Callback>> onPlayerInsideCallbacks = new ConcurrentHashMap<>();
private final Map<String, Collection<Callback>> onInteractEntityEventCallbacks = new ConcurrentHashMap<>();
private final Map<Component, Collection<Callback>> onInteractEntityEventCallbacks = new ConcurrentHashMap<>();

private final EventManager eventManager;

Expand Down Expand Up @@ -94,7 +91,7 @@ public Unregisterable registerInsideLocation(String locationName, Callback callb
return add(callbacks, callback);
}

public Unregisterable registerInteractEntity(String entityName, Callback callback) {
public Unregisterable registerInteractEntity(Component entityName, Callback callback) {
Collection<Callback> callbacks = onInteractEntityEventCallbacks.computeIfAbsent(entityName, name -> new ConcurrentLinkedQueue<>());
return add(callbacks, callback);
}
Expand All @@ -111,7 +108,7 @@ private Unregisterable add(Collection<Callback> collection, Callback callback) {
Optional<Component> optEntityNameText = event.entity().get(Keys.DISPLAY_NAME);
LOG.debug("InteractEntityEvent: entityName={}; event={}", optEntityNameText, event);
optEntityNameText.ifPresent(entityNameText -> {
Collection<Callback> callbacks = onInteractEntityEventCallbacks.getOrDefault(((TextComponent) entityNameText).content(),
Collection<Callback> callbacks = onInteractEntityEventCallbacks.getOrDefault(entityNameText,
Collections.emptySet());
for (Callback callback : callbacks) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import ch.vorburger.minecraft.storeys.japi.ReadingSpeed;
import ch.vorburger.minecraft.storeys.japi.impl.actions.Narrator;
import ch.vorburger.minecraft.storeys.util.Command;
import com.google.common.collect.ImmutableList;
import java.util.List;
import javax.inject.Inject;
import net.kyori.adventure.text.Component;
import org.spongepowered.api.command.CommandResult;
Expand All @@ -42,11 +40,11 @@ public class NarrateCommand implements Command {
this.narrator = narrator;
}

@Override public List<String> aliases() {
return ImmutableList.of("narrate");
@Override public String getName() {
return "narrate";
}

@Override public org.spongepowered.api.command.Command callable() {
@Override public org.spongepowered.api.command.Command.Parameterized createCommand() {
// TODO when Sponge uses entity names instead of UUIDs:
// TODO requiringPermission()
return org.spongepowered.api.command.Command.builder().shortDescription(Component.text(("Make an entity character narrate story lines")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
import ch.vorburger.minecraft.storeys.model.parser.StoryParser;
import ch.vorburger.minecraft.storeys.model.parser.StoryRepository;
import ch.vorburger.minecraft.storeys.util.Command;
import com.google.common.collect.ImmutableList;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import javax.inject.Inject;
import net.kyori.adventure.text.Component;
import org.spongepowered.api.command.CommandResult;
Expand All @@ -56,11 +54,11 @@ public class StoryCommand implements Command {
this.storyPlayer = storyPlayer;
}

@Override public List<String> aliases() {
return ImmutableList.of("story");
@Override public String getName() {
return "story";
}

@Override public org.spongepowered.api.command.Command callable() {
@Override public org.spongepowered.api.command.Command.Parameterized createCommand() {
return org.spongepowered.api.command.Command.builder().shortDescription(Component.text("Tell a story"))
// .permission("storeys.commands.story") ?
.addParameter(STORY_NAME) // TODO requiringPermission()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.spongepowered.api.scheduler.ScheduledTask;
import org.spongepowered.api.scheduler.Scheduler;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.plugin.PluginContainer;

@ThreadSafe @Singleton public class ConditionService implements AutoCloseable {

Expand Down Expand Up @@ -90,8 +91,8 @@ private ConditionServiceRegistration(Triple<Condition, AtomicBoolean, Callback>
private final Scheduler scheduler;
private final UUID taskId;

@Inject public ConditionService(Scheduler scheduler) {
Task task = Task.builder().execute(this::run).interval(10, TimeUnit.SECONDS).build();
@Inject public ConditionService(PluginContainer plugin, Scheduler scheduler) {
Task task = Task.builder().execute(this::run).interval(10, TimeUnit.SECONDS).plugin(plugin).build();
this.scheduler = scheduler;
taskId = scheduler.submit(task).uniqueId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class LocationAction implements Action<Void> {
public LocationAction() {
}

@Inject public LocationAction(Scheduler scheduler) {
conditionService = new ConditionService(scheduler);
@Inject public LocationAction(ConditionService conditionService) {
this.conditionService = conditionService;
}

@Override public void setParameter(String param) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,40 @@
import com.google.inject.Injector;
import java.nio.file.Path;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.EventManager;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
import org.spongepowered.api.event.lifecycle.StartingEngineEvent;

// Do *NOT* annotate this class with @Plugin
public abstract class AbstractStoreysPlugin extends AbstractPlugin {

private static final Logger LOG = LoggerFactory.getLogger(AbstractStoreysPlugin.class);

@Inject @ConfigDir(sharedRoot = false) private Path configDir;

@Inject protected Injector pluginInjector;

private Injector childInjector;

@Inject private EventManager eventManager;

@Inject private EventService eventService;

@Listener public final void onGameStartingServer(StartingEngineEvent event) throws Exception {
LOG.info("See https://github.com/OASIS-learn-study/minecraft-storeys-maker for how to use /story and /narrate commands");
start(this, configDir);
}

protected void start(PluginInstance plugin, Path configDir) throws Exception {
protected void start(PluginInstance plugin, Path configDir) {
eventManager.registerListeners(plugin.getPluginContainer(), new GuardGameModeJoinListener());
eventService.setPluginContainer(plugin.getPluginContainer());

// TODO(vorburger) child injector might not actually be required, could possibly just use only pluginInjector?
childInjector = pluginInjector.createChildInjector(binder -> {
Injector childInjector = pluginInjector.createChildInjector(binder -> {
binder.bind(PluginInstance.class).toInstance(plugin);
binder.bind(Path.class).toInstance(configDir);
binder.bind(Scripts.class);
binder.bind(ScriptsLoader.class);
});
}

@Listener public void register(RegisterCommandEvent<Command.Raw> event) {
public void register(RegisterCommandEvent<Command.Parameterized> event) {
final StoryCommand storyCommand = pluginInjector.getInstance(StoryCommand.class);
final NarrateCommand narrateCommand = pluginInjector.getInstance(NarrateCommand.class);

event.register(this.getPluginContainer(), (Command.Raw) narrateCommand.callable(), narrateCommand.aliases().get(0),
narrateCommand.aliases().toArray(new String[0]));
event.register(this.getPluginContainer(), (Command.Raw) storyCommand.callable(), storyCommand.aliases().get(0),
storyCommand.aliases().toArray(new String[0]));
event.register(this.getPluginContainer(), narrateCommand.createCommand(), narrateCommand.getName(), narrateCommand.aliases());
event.register(this.getPluginContainer(), storyCommand.createCommand(), storyCommand.getName(), storyCommand.aliases());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@

public interface Command extends CommandExecutor {

org.spongepowered.api.command.Command callable();
org.spongepowered.api.command.Command.Parameterized createCommand();

List<String> aliases();
String getName();

default String[] aliases() {
return new String[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import ch.vorburger.minecraft.storeys.japi.util.CommandExceptions;
import ch.vorburger.minecraft.storeys.simple.TokenProvider;
import ch.vorburger.minecraft.storeys.util.Command;
import com.google.common.collect.ImmutableList;
import java.util.List;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
Expand All @@ -42,13 +40,13 @@ public TokenCommand(TokenProvider newTokenProvider) {
this.tokenProvider = newTokenProvider;
}

@Override public org.spongepowered.api.command.Command callable() {
@Override public org.spongepowered.api.command.Command.Parameterized createCommand() {
return org.spongepowered.api.command.Command.builder().permission("storeys.token.new")
.shortDescription(Component.text("Obtain API token for player")).executor(this).build();
}

@Override public List<String> aliases() {
return ImmutableList.of("token");
@Override public String getName() {
return "token";
}

@Override public CommandResult execute(CommandContext args) throws CommandException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@
import org.spongepowered.api.data.Keys;
import org.spongepowered.api.data.type.HandTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.event.Cause;
import org.spongepowered.api.event.EventContext;
import org.spongepowered.api.event.EventContextKeys;
import org.spongepowered.api.event.EventManager;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.lifecycle.StoppedGameEvent;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
Expand Down Expand Up @@ -93,7 +95,7 @@
}
}

@Listener public void locationToolInteraction(InteractBlockEvent event, Player player) {
@Listener public void locationToolInteraction(InteractBlockEvent.Primary event, @First ServerPlayer player) {
final Optional<ItemStackSnapshot> snapshot = event.context().get(EventContextKeys.USED_ITEM);
snapshot.ifPresent(itemStackSnapshot -> {
final ItemStack itemStack = player.itemInHand(HandTypes.MAIN_HAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
import ch.vorburger.minecraft.storeys.japi.util.CommandExceptions;
import ch.vorburger.minecraft.storeys.simple.TokenProvider;
import ch.vorburger.minecraft.storeys.util.Command;
import com.google.common.collect.ImmutableList;
import java.net.URL;
import java.util.List;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.spongepowered.api.command.Command.Parameterized;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.exception.CommandException;
import org.spongepowered.api.command.parameter.CommandContext;
Expand Down Expand Up @@ -59,13 +56,17 @@ private String getSystemPropertyEnvVarOrDefault(String propertyName, String defa
return defaultValue;
}

@Override public org.spongepowered.api.command.Command callable() {
@Override public org.spongepowered.api.command.Command.Parameterized createCommand() {
return org.spongepowered.api.command.Command.builder().shortDescription(Component.text("Open the browser and start your story"))
.executor(this).build();
}

@Override public List<String> aliases() {
return ImmutableList.of("make", "scratch");
@Override public String getName() {
return "make";
}

@Override public String[] aliases() {
return new String[] { "scratch" };
}

@Override public CommandResult execute(CommandContext args) throws CommandException {
Expand All @@ -81,6 +82,7 @@ private String getSystemPropertyEnvVarOrDefault(String propertyName, String defa
Component.text("Click here to open a browser and start MAKE actions").clickEvent(ClickEvent.openUrl(new URL(url)))
.color(NamedTextColor.GOLD));
});
return CommandResult.success();
}
return CommandResult.error(Component.text("Command source must be Player").color(NamedTextColor.RED));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.api.Server;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.command.registrar.CommandRegistrar;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
import org.spongepowered.api.event.lifecycle.StartingEngineEvent;
import org.spongepowered.api.scheduler.Scheduler;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.plugin.builtin.jvm.Plugin;
Expand All @@ -43,13 +49,17 @@
// do not extend StoreysPlugin, because we exclude that class in shadowJar

private static final Logger LOG = LoggerFactory.getLogger(StoreysWebPlugin.class);
private VertxStarter vertxStarter;
private LoginCommand loginCommand;
private TokenCommand tokenCommand;

@Inject @ConfigDir(sharedRoot = false) private Path configDir;
@Inject @DefaultConfig(sharedRoot = true) private ConfigurationLoader<CommentedConfigurationNode> configurationLoader;

@Override public void start(PluginInstance plugin, Path configDir) throws Exception {
@Listener public final void onGameStartingServer(StartingEngineEvent<Server> event) throws Exception {
LOG.info("See https://github.com/OASIS-learn-study/minecraft-storeys-maker for how to use /story and /narrate commands");
start(this, configDir);
}

@Override public void start(PluginInstance plugin, Path configDir) {
LOG.info("See https://github.com/OASIS-learn-study/minecraft-storeys-maker for how to use /story and /narrate commands");
super.start(plugin, configDir);

Injector injector = pluginInjector.createChildInjector(binder -> {
Expand All @@ -60,16 +70,21 @@
binder.bind(new TypeLiteral<ConfigurationLoader<CommentedConfigurationNode>>() {
}).toInstance(configurationLoader);
binder.bind(LocationToolListener.class);
binder.bind(Scheduler.class).toInstance(Sponge.asyncScheduler());
});
StaticWebServerVerticle staticWebServerVerticle = injector.getInstance(StaticWebServerVerticle.class);

TokenProvider tokenProvider = injector.getInstance(TokenProvider.class);
loginCommand = new LoginCommand(tokenProvider);
tokenCommand = new TokenCommand(tokenProvider);
LoginCommand loginCommand = new LoginCommand(tokenProvider);
TokenCommand tokenCommand = new TokenCommand(tokenProvider);

final Optional<CommandRegistrar<Command.Parameterized>> registrar = Sponge.server().commandManager().registrar(Command.Parameterized.class);
final CommandRegistrar<Command.Parameterized> commandRegistrar = registrar.get();
commandRegistrar.register(plugin.getPluginContainer(), loginCommand.createCommand(), loginCommand.getName(), loginCommand.aliases());
commandRegistrar.register(plugin.getPluginContainer(), tokenCommand.createCommand(), tokenCommand.getName(), tokenCommand.aliases());
try {
try {
vertxStarter = new VertxStarter();
VertxStarter vertxStarter = new VertxStarter();
vertxStarter.deployVerticle(staticWebServerVerticle).toCompletableFuture().get();

} catch (ExecutionException | InterruptedException e) {
Expand All @@ -82,9 +97,4 @@
throw e;
}
}

@Listener public void register(RegisterCommandEvent<Command.Raw> event) {
event.register(this.getPluginContainer(),
(Command.Raw) loginCommand.callable(), loginCommand.aliases().get(0), loginCommand.aliases().toArray(new String[0]));
}
}

0 comments on commit 574a5e9

Please sign in to comment.