Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api-jvm-impl/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

dependencies {
api project(':api-jvm')
implementation 'ch.vorburger.minecraft.osgi:api:1.0.0'
implementation('com.spotify:futures-extra:4.0.0') {
exclude group: 'com.google.guava'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package ch.vorburger.minecraft.storeys.japi.impl;

import ch.vorburger.minecraft.osgi.api.PluginInstance;
import ch.vorburger.minecraft.storeys.japi.Callback;
import ch.vorburger.minecraft.storeys.japi.Events;
import ch.vorburger.minecraft.storeys.japi.ReadingSpeed;
Expand All @@ -30,13 +29,15 @@
import java.util.Collection;
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.CommandMapping;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.command.registrar.CommandRegistrar;
import org.spongepowered.plugin.PluginContainer;

/**
* {@link Events} implementation.
Expand All @@ -49,7 +50,7 @@ class EventsImpl implements Events, Unregisterable {

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

private final PluginInstance plugin;
private final PluginContainer plugin;
private final EventService eventService;

// when made modifiable, then this should be per Player
Expand All @@ -58,22 +59,28 @@ class EventsImpl implements Events, Unregisterable {
private final Collection<Unregisterable> unregistrables = new ConcurrentLinkedQueue<>();
private final ActionPlayer player = new ActionPlayer();

EventsImpl(PluginInstance plugin, EventService eventService) {
EventsImpl(PluginContainer plugin, EventService eventService) {
this.plugin = plugin;
this.eventService = eventService;
}

@Override public void whenCommand(String name, Callback callback) {
CommandSpec spec = CommandSpec.builder().executor((src, args) -> {
CommandExceptions.doOrThrow("/" + name, () -> invokeCallback(src, callback));
final Command.Parameterized spec = Command.builder().executor((src) -> {
CommandExceptions.doOrThrow("/" + name, () -> invokeCallback(src.cause().audience(), callback));
return CommandResult.success();
}).build();
Optional<CommandMapping> opt = Sponge.getCommandManager().register(plugin, spec, name);
if (!opt.isPresent()) {
final Optional<CommandRegistrar<Command.Parameterized>> registrar = Sponge.server().commandManager().registrar(Command.Parameterized.class);
if (!registrar.isPresent()) {
LOG.error("Could not register new command, because it's already present: /" + name);
return;
}
unregistrables.add(() -> Sponge.getCommandManager().removeMapping(opt.get()));
registrar.get().register(plugin, spec, name);
Sponge.server().onlinePlayers().forEach(p -> Sponge.server().commandManager().updateCommandTreeForPlayer(p));

//TODO unregister commands so that we can update them
unregistrables.add(() -> {

});
}

@Override public void whenPlayerJoins(Callback callback) {
Expand All @@ -100,7 +107,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 All @@ -109,9 +116,9 @@ class EventsImpl implements Events, Unregisterable {
}
}

private void invokeCallback(CommandSource source, Callback callback) throws Exception {
private void invokeCallback(Audience source, Callback callback) throws Exception {
MinecraftJvmImpl m = new MinecraftJvmImpl(plugin, source);
callback.invoke(m);
player.play(new ActionContextImpl(m.player(), readingSpeed), m.getActionList());
player.play(new ActionContextImpl(source, readingSpeed), m.getActionList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package ch.vorburger.minecraft.storeys.japi.impl;

import ch.vorburger.minecraft.osgi.api.PluginInstance;
import ch.vorburger.minecraft.storeys.japi.Action;
import ch.vorburger.minecraft.storeys.japi.Events;
import ch.vorburger.minecraft.storeys.japi.Minecraft;
Expand All @@ -29,35 +28,37 @@
import ch.vorburger.minecraft.storeys.japi.impl.actions.TitleAction;
import java.util.ArrayList;
import java.util.List;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.plugin.PluginContainer;

/**
* {@link Minecraft} implementation.
* Created indirectly (by EventsImpl) via {@link Scripts}.
*
* <p>The "lifecycle" of this is NOT a Singleton,
* but one for each instance (not just kind of) of an event registered on {@link Events},
* such as custom command, when right clicked, when player joined, when inside, etc.
* such as custom command, when right-clicked, when player joined, when inside, etc.
*/
class MinecraftJvmImpl implements Minecraft {

private final PluginInstance plugin;
private final CommandSource source;
private final PluginContainer plugin;
private final Audience source;
private final ActionWaitHelper actionWaitHelper;

private final List<Action<?>> actionList = new ArrayList<>();

MinecraftJvmImpl(PluginInstance plugin, CommandSource source) {
MinecraftJvmImpl(PluginContainer plugin, Audience source) {
this.source = source;
this.plugin = plugin;
this.actionWaitHelper = new ActionWaitHelper(plugin);
}

@Override public void cmd(String command) {
CommandAction action = new CommandAction(plugin, Sponge.getScheduler());
CommandAction action = new CommandAction(plugin, Sponge.asyncScheduler());
action.setCommand(command);
actionList.add(action);
}
Expand All @@ -72,7 +73,7 @@ class MinecraftJvmImpl implements Minecraft {
Narrator narrator = new Narrator(plugin);
NarrateAction action = new NarrateAction(narrator);
action.setEntity(entity);
action.setText(Text.of(text));
action.setText(Component.text(text));
actionList.add(action);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
*/
package ch.vorburger.minecraft.storeys.japi.impl;

import ch.vorburger.minecraft.osgi.api.PluginInstance;
import ch.vorburger.minecraft.storeys.japi.Script;
import ch.vorburger.minecraft.storeys.japi.impl.events.EventService;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.spongepowered.plugin.PluginContainer;

@Singleton
public class Scripts {

private final Map<Object, Unregisterable> unregisterables = new ConcurrentHashMap<>();
private final PluginInstance plugin;
private final PluginContainer plugin;
private final EventService eventService;

@Inject public Scripts(PluginInstance plugin, EventService eventService) {
@Inject public Scripts(PluginContainer plugin, EventService eventService) {
this.plugin = plugin;
this.eventService = eventService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@

import ch.vorburger.minecraft.storeys.japi.ActionContext;
import ch.vorburger.minecraft.storeys.japi.ReadingSpeed;
import org.spongepowered.api.command.CommandSource;
import net.kyori.adventure.audience.Audience;
import org.spongepowered.api.command.CommandCause;

public final class ActionContextImpl implements ActionContext {

private final CommandSource commandSource;
private final Audience commandCause;
private final ReadingSpeed readingSpeed;

public ActionContextImpl(CommandSource commandSource, ReadingSpeed readingSpeed) {
public ActionContextImpl(Audience commandCause, ReadingSpeed readingSpeed) {
super();
this.commandSource = commandSource;
this.commandCause = commandCause;
this.readingSpeed = readingSpeed;
}

public CommandSource getCommandSource() {
return commandSource;
public Audience getCommandCause() {
return commandCause;
}

public ReadingSpeed getReadingSpeed() {
Expand All @@ -44,7 +45,7 @@ public ReadingSpeed getReadingSpeed() {
@Override public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + (commandSource == null ? 0 : commandSource.hashCode());
result = (prime * result) + (commandCause == null ? 0 : commandCause.hashCode());
result = (prime * result) + (readingSpeed == null ? 0 : readingSpeed.hashCode());
return result;
}
Expand All @@ -60,11 +61,11 @@ public ReadingSpeed getReadingSpeed() {
return false;
}
ActionContextImpl other = (ActionContextImpl) obj;
if (commandSource == null) {
if (other.commandSource != null) {
if (commandCause == null) {
if (other.commandCause != null) {
return false;
}
} else if (!commandSource.equals(other.commandSource)) {
} else if (!commandCause.equals(other.commandCause)) {
return false;
}
if (readingSpeed == null) {
Expand All @@ -74,7 +75,7 @@ public ReadingSpeed getReadingSpeed() {
}

@Override public String toString() {
return "ActionContext[commandSource=" + commandSource + ", readingSpeed=" + readingSpeed + "]";
return "ActionContext[commandSource=" + commandCause + ", readingSpeed=" + readingSpeed + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,39 @@
*/
package ch.vorburger.minecraft.storeys.japi.impl.actions;

import ch.vorburger.minecraft.storeys.japi.util.Texts;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.util.TextMessageException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;

public class ActionException extends TextMessageException {
public class ActionException extends Exception {

private static final long serialVersionUID = 6261204063265579413L;
private final TextComponent message;

public ActionException(Text message) {
super(message);
public ActionException(TextComponent message) {
this.message = (message);
}

public ActionException(Text message, Throwable throwable) {
super(message, throwable);
public ActionException(TextComponent message, Throwable throwable) {
super(throwable);
this.message = message;
}

public ActionException(String message) {
this(Texts.inRed(message));
this(Component.text(message).color(NamedTextColor.RED));
}

public ActionException(String message, Throwable throwable) {
this(Texts.inRed(message), throwable);
this(Component.text(message).color(NamedTextColor.RED), throwable);
}

public String getMessage() {
TextComponent message = getText();
return message == null ? null : message.content();
}

public TextComponent getText() {
return this.message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import ch.vorburger.minecraft.osgi.api.PluginInstance;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.plugin.PluginContainer;

public class ActionWaitHelper {

private final PluginInstance plugin;
private final PluginContainer plugin;

@Inject public ActionWaitHelper(PluginInstance plugin) {
@Inject public ActionWaitHelper(PluginContainer plugin) {
this.plugin = plugin;
}

public <T> CompletionStage<T> executeAndWait(int msToWaitAfterRunning, Callable<T> callable) {
CompletableFuture<T> future = new CompletableFuture<>();
try {
T returnValue = callable.call();
Task.builder().async().execute(() -> future.complete(returnValue)).delay(msToWaitAfterRunning, MILLISECONDS).submit(plugin);
Task.builder().execute(() -> future.complete(returnValue)).delay(msToWaitAfterRunning, MILLISECONDS).plugin(plugin);

} catch (Throwable throwable) {
future.completeExceptionally(throwable);
Expand Down
Loading