Skip to content

Commit 01a28e4

Browse files
committed
more work on api migration 7 => 8
1 parent 55f270a commit 01a28e4

44 files changed

Lines changed: 462 additions & 579 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api-jvm-impl/src/main/java/ch/vorburger/minecraft/storeys/japi/impl/EventsImpl.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package ch.vorburger.minecraft.storeys.japi.impl;
2020

21-
import ch.vorburger.minecraft.osgi.api.PluginInstance;
2221
import ch.vorburger.minecraft.storeys.japi.Callback;
2322
import ch.vorburger.minecraft.storeys.japi.Events;
2423
import ch.vorburger.minecraft.storeys.japi.ReadingSpeed;
@@ -30,13 +29,15 @@
3029
import java.util.Collection;
3130
import java.util.Optional;
3231
import java.util.concurrent.ConcurrentLinkedQueue;
32+
import net.kyori.adventure.audience.Audience;
3333
import org.slf4j.Logger;
3434
import org.slf4j.LoggerFactory;
3535
import org.spongepowered.api.Sponge;
36-
import org.spongepowered.api.command.CommandMapping;
36+
import org.spongepowered.api.command.Command;
37+
import org.spongepowered.api.command.CommandCause;
3738
import org.spongepowered.api.command.CommandResult;
38-
import org.spongepowered.api.command.CommandSource;
39-
import org.spongepowered.api.command.spec.CommandSpec;
39+
import org.spongepowered.api.command.registrar.CommandRegistrar;
40+
import org.spongepowered.plugin.PluginContainer;
4041

4142
/**
4243
* {@link Events} implementation.
@@ -49,7 +50,7 @@ class EventsImpl implements Events, Unregisterable {
4950

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

52-
private final PluginInstance plugin;
53+
private final PluginContainer plugin;
5354
private final EventService eventService;
5455

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

61-
EventsImpl(PluginInstance plugin, EventService eventService) {
62+
EventsImpl(PluginContainer plugin, EventService eventService) {
6263
this.plugin = plugin;
6364
this.eventService = eventService;
6465
}
6566

6667
@Override public void whenCommand(String name, Callback callback) {
67-
CommandSpec spec = CommandSpec.builder().executor((src, args) -> {
68-
CommandExceptions.doOrThrow("/" + name, () -> invokeCallback(src, callback));
68+
final Command.Parameterized spec = Command.builder().executor((src) -> {
69+
CommandExceptions.doOrThrow("/" + name, () -> invokeCallback(src.cause().audience(), callback));
6970
return CommandResult.success();
7071
}).build();
71-
Optional<CommandMapping> opt = Sponge.getCommandManager().register(plugin, spec, name);
72-
if (!opt.isPresent()) {
72+
final Optional<CommandRegistrar<Command.Parameterized>> registrar = Sponge.server().commandManager().registrar(Command.Parameterized.class);
73+
if (!registrar.isPresent()) {
7374
LOG.error("Could not register new command, because it's already present: /" + name);
7475
return;
7576
}
76-
unregistrables.add(() -> Sponge.getCommandManager().removeMapping(opt.get()));
77+
registrar.get().register(plugin, spec, name);
78+
unregistrables.add(() -> {
79+
80+
});
7781
}
7882

7983
@Override public void whenPlayerJoins(Callback callback) {
@@ -109,9 +113,9 @@ class EventsImpl implements Events, Unregisterable {
109113
}
110114
}
111115

112-
private void invokeCallback(CommandSource source, Callback callback) throws Exception {
116+
private void invokeCallback(Audience source, Callback callback) throws Exception {
113117
MinecraftJvmImpl m = new MinecraftJvmImpl(plugin, source);
114118
callback.invoke(m);
115-
player.play(new ActionContextImpl(m.player(), readingSpeed), m.getActionList());
119+
player.play(new ActionContextImpl(source, readingSpeed), m.getActionList());
116120
}
117121
}

api-jvm-impl/src/main/java/ch/vorburger/minecraft/storeys/japi/impl/MinecraftJvmImpl.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package ch.vorburger.minecraft.storeys.japi.impl;
2020

21-
import ch.vorburger.minecraft.osgi.api.PluginInstance;
2221
import ch.vorburger.minecraft.storeys.japi.Action;
2322
import ch.vorburger.minecraft.storeys.japi.Events;
2423
import ch.vorburger.minecraft.storeys.japi.Minecraft;
@@ -29,35 +28,37 @@
2928
import ch.vorburger.minecraft.storeys.japi.impl.actions.TitleAction;
3029
import java.util.ArrayList;
3130
import java.util.List;
31+
import net.kyori.adventure.audience.Audience;
32+
import net.kyori.adventure.text.Component;
3233
import org.spongepowered.api.Sponge;
33-
import org.spongepowered.api.command.CommandSource;
34+
import org.spongepowered.api.command.CommandCause;
3435
import org.spongepowered.api.entity.living.player.Player;
35-
import org.spongepowered.api.text.Text;
36+
import org.spongepowered.plugin.PluginContainer;
3637

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

47-
private final PluginInstance plugin;
48-
private final CommandSource source;
48+
private final PluginContainer plugin;
49+
private final Audience source;
4950
private final ActionWaitHelper actionWaitHelper;
5051

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

53-
MinecraftJvmImpl(PluginInstance plugin, CommandSource source) {
54+
MinecraftJvmImpl(PluginContainer plugin, Audience source) {
5455
this.source = source;
5556
this.plugin = plugin;
5657
this.actionWaitHelper = new ActionWaitHelper(plugin);
5758
}
5859

5960
@Override public void cmd(String command) {
60-
CommandAction action = new CommandAction(plugin, Sponge.getScheduler());
61+
CommandAction action = new CommandAction(plugin, Sponge.asyncScheduler());
6162
action.setCommand(command);
6263
actionList.add(action);
6364
}
@@ -72,7 +73,7 @@ class MinecraftJvmImpl implements Minecraft {
7273
Narrator narrator = new Narrator(plugin);
7374
NarrateAction action = new NarrateAction(narrator);
7475
action.setEntity(entity);
75-
action.setText(Text.of(text));
76+
action.setText(Component.text(text));
7677
actionList.add(action);
7778
}
7879

api-jvm-impl/src/main/java/ch/vorburger/minecraft/storeys/japi/impl/Scripts.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525
import java.util.concurrent.ConcurrentHashMap;
2626
import javax.inject.Inject;
2727
import javax.inject.Singleton;
28+
import org.spongepowered.plugin.PluginContainer;
2829

2930
@Singleton
3031
public class Scripts {
3132

3233
private final Map<Object, Unregisterable> unregisterables = new ConcurrentHashMap<>();
33-
private final PluginInstance plugin;
34+
private final PluginContainer plugin;
3435
private final EventService eventService;
3536

36-
@Inject public Scripts(PluginInstance plugin, EventService eventService) {
37+
@Inject public Scripts(PluginContainer plugin, EventService eventService) {
3738
this.plugin = plugin;
3839
this.eventService = eventService;
3940
}

api-jvm-impl/src/main/java/ch/vorburger/minecraft/storeys/japi/impl/actions/ActionContextImpl.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@
2020

2121
import ch.vorburger.minecraft.storeys.japi.ActionContext;
2222
import ch.vorburger.minecraft.storeys.japi.ReadingSpeed;
23+
import net.kyori.adventure.audience.Audience;
2324
import org.spongepowered.api.command.CommandCause;
2425

2526
public final class ActionContextImpl implements ActionContext {
2627

27-
private final CommandCause commandSource;
28+
private final Audience commandCause;
2829
private final ReadingSpeed readingSpeed;
2930

30-
public ActionContextImpl(CommandCause commandSource, ReadingSpeed readingSpeed) {
31+
public ActionContextImpl(Audience commandCause, ReadingSpeed readingSpeed) {
3132
super();
32-
this.commandSource = commandSource;
33+
this.commandCause = commandCause;
3334
this.readingSpeed = readingSpeed;
3435
}
3536

36-
public CommandCause getCommandCause() {
37-
return commandSource;
37+
public Audience getCommandCause() {
38+
return commandCause;
3839
}
3940

4041
public ReadingSpeed getReadingSpeed() {
@@ -44,7 +45,7 @@ public ReadingSpeed getReadingSpeed() {
4445
@Override public int hashCode() {
4546
final int prime = 31;
4647
int result = 1;
47-
result = (prime * result) + (commandSource == null ? 0 : commandSource.hashCode());
48+
result = (prime * result) + (commandCause == null ? 0 : commandCause.hashCode());
4849
result = (prime * result) + (readingSpeed == null ? 0 : readingSpeed.hashCode());
4950
return result;
5051
}
@@ -60,11 +61,11 @@ public ReadingSpeed getReadingSpeed() {
6061
return false;
6162
}
6263
ActionContextImpl other = (ActionContextImpl) obj;
63-
if (commandSource == null) {
64-
if (other.commandSource != null) {
64+
if (commandCause == null) {
65+
if (other.commandCause != null) {
6566
return false;
6667
}
67-
} else if (!commandSource.equals(other.commandSource)) {
68+
} else if (!commandCause.equals(other.commandCause)) {
6869
return false;
6970
}
7071
if (readingSpeed == null) {
@@ -74,7 +75,7 @@ public ReadingSpeed getReadingSpeed() {
7475
}
7576

7677
@Override public String toString() {
77-
return "ActionContext[commandSource=" + commandSource + ", readingSpeed=" + readingSpeed + "]";
78+
return "ActionContext[commandSource=" + commandCause + ", readingSpeed=" + readingSpeed + "]";
7879
}
7980

8081
}

api-jvm-impl/src/main/java/ch/vorburger/minecraft/storeys/japi/impl/actions/ActionException.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,39 @@
1818
*/
1919
package ch.vorburger.minecraft.storeys.japi.impl.actions;
2020

21-
import ch.vorburger.minecraft.storeys.japi.util.Texts;
22-
import org.spongepowered.api.text.Text;
23-
import org.spongepowered.api.util.TextMessageException;
21+
import net.kyori.adventure.text.Component;
22+
import net.kyori.adventure.text.TextComponent;
23+
import net.kyori.adventure.text.format.NamedTextColor;
2424

25-
public class ActionException extends TextMessageException {
25+
public class ActionException extends Exception {
2626

2727
private static final long serialVersionUID = 6261204063265579413L;
28+
private final TextComponent message;
2829

29-
public ActionException(Text message) {
30-
super(message);
30+
public ActionException(TextComponent message) {
31+
this.message = (message);
3132
}
3233

33-
public ActionException(Text message, Throwable throwable) {
34-
super(message, throwable);
34+
public ActionException(TextComponent message, Throwable throwable) {
35+
super(throwable);
36+
this.message = message;
3537
}
3638

3739
public ActionException(String message) {
38-
this(Texts.inRed(message));
40+
this(Component.text(message).color(NamedTextColor.RED));
3941
}
4042

4143
public ActionException(String message, Throwable throwable) {
42-
this(Texts.inRed(message), throwable);
44+
this(Component.text(message).color(NamedTextColor.RED), throwable);
45+
}
46+
47+
public String getMessage() {
48+
TextComponent message = getText();
49+
return message == null ? null : message.content();
50+
}
51+
52+
public TextComponent getText() {
53+
return this.message;
4354
}
4455

4556
}

api-jvm-impl/src/main/java/ch/vorburger/minecraft/storeys/japi/impl/actions/ActionWaitHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@
2020

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

23-
import ch.vorburger.minecraft.osgi.api.PluginInstance;
2423
import java.util.concurrent.Callable;
2524
import java.util.concurrent.CompletableFuture;
2625
import java.util.concurrent.CompletionStage;
2726
import javax.inject.Inject;
2827
import org.spongepowered.api.scheduler.Task;
28+
import org.spongepowered.plugin.PluginContainer;
2929

3030
public class ActionWaitHelper {
3131

32-
private final PluginInstance plugin;
32+
private final PluginContainer plugin;
3333

34-
@Inject public ActionWaitHelper(PluginInstance plugin) {
34+
@Inject public ActionWaitHelper(PluginContainer plugin) {
3535
this.plugin = plugin;
3636
}
3737

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

4444
} catch (Throwable throwable) {
4545
future.completeExceptionally(throwable);

api-jvm-impl/src/main/java/ch/vorburger/minecraft/storeys/japi/impl/actions/CommandAction.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020

2121
import static java.util.Objects.requireNonNull;
2222

23-
import ch.vorburger.minecraft.osgi.api.PluginInstance;
2423
import ch.vorburger.minecraft.storeys.japi.ActionContext;
25-
import java.util.StringJoiner;
2624
import javax.inject.Inject;
25+
import net.kyori.adventure.audience.Audience;
2726
import org.slf4j.Logger;
2827
import org.slf4j.LoggerFactory;
2928
import org.spongepowered.api.Sponge;
3029
import org.spongepowered.api.command.CommandResult;
30+
import org.spongepowered.api.command.exception.CommandException;
3131
import org.spongepowered.api.scheduler.Scheduler;
32+
import org.spongepowered.api.service.permission.Subject;
33+
import org.spongepowered.plugin.PluginContainer;
3234

3335
public class CommandAction extends MainThreadAction<CommandResult> {
3436

@@ -39,7 +41,7 @@ public class CommandAction extends MainThreadAction<CommandResult> {
3941

4042
private String commandLineWithoutSlash;
4143

42-
@Inject public CommandAction(PluginInstance plugin, Scheduler scheduler) {
44+
@Inject public CommandAction(PluginContainer plugin, Scheduler scheduler) {
4345
super(plugin, scheduler);
4446
}
4547

@@ -55,23 +57,27 @@ public CommandAction setCommand(String commandLine) {
5557
setCommand(param);
5658
}
5759

58-
@Override protected CommandResult executeInMainThread(ActionContext context) throws ActionException {
59-
CommandResult result = Sponge.getCommandManager().process(context.getCommandCause(),
60-
requireNonNull(commandLineWithoutSlash, "commandLineWithoutSlash"));
61-
LOG.info("processed command \"/{}\" from source {} with result {}", commandLineWithoutSlash, context.getCommandCause(),
62-
toString(result));
60+
@Override protected CommandResult executeInMainThread(ActionContext context) {
61+
final CommandResult result;
62+
try {
63+
result = Sponge.server().commandManager().process((Subject & Audience) context.getCommandCause(),
64+
requireNonNull(commandLineWithoutSlash, "commandLineWithoutSlash"));
65+
} catch (CommandException e) {
66+
throw new RuntimeException(e);
67+
}
68+
LOG.info("processed command \"/{}\" from source {} with result {}", commandLineWithoutSlash, context.getCommandCause(), result);
6369
return result;
6470
}
6571

66-
private String toString(CommandResult result) {
67-
StringJoiner sj = new StringJoiner(", ", "{", "}");
68-
result.getAffectedBlocks().ifPresent(affectedBlocked -> sj.add("affectedBlocked: " + affectedBlocked));
69-
result.getAffectedEntities().ifPresent(affectedEntities -> sj.add("affectedEntities: " + affectedEntities));
70-
result.getAffectedItems().ifPresent(affectedItems -> sj.add("affectedItems: " + affectedItems));
71-
result.getQueryResult().ifPresent(queryResult -> sj.add("queryResult: " + queryResult));
72-
result.getSuccessCount().ifPresent(successCount -> sj.add("successCount: " + successCount));
73-
return sj.toString();
74-
}
72+
// private String toString(CommandResult result) {
73+
// StringJoiner sj = new StringJoiner(", ", "{", "}");
74+
// result.getAffectedBlocks().ifPresent(affectedBlocked -> sj.add("affectedBlocked: " + affectedBlocked));
75+
// result.getAffectedEntities().ifPresent(affectedEntities -> sj.add("affectedEntities: " + affectedEntities));
76+
// result.getAffectedItems().ifPresent(affectedItems -> sj.add("affectedItems: " + affectedItems));
77+
// result.getQueryResult().ifPresent(queryResult -> sj.add("queryResult: " + queryResult));
78+
// result.getSuccessCount().ifPresent(successCount -> sj.add("successCount: " + successCount));
79+
// return sj.toString();
80+
// }
7581

7682
@Override public String toString() {
7783
return getClass().getSimpleName() + ": " + (commandLineWithoutSlash != null ? ("/" + commandLineWithoutSlash) : "null");

0 commit comments

Comments
 (0)