Skip to content

Commit

Permalink
feat(paper): add EntitySource and rename *Sender types to *Source
Browse files Browse the repository at this point in the history
  • Loading branch information
broccolai committed Aug 3, 2024
1 parent b77c2f1 commit b1ee8d1
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
import org.checkerframework.checker.nullness.qual.NonNull;

@SuppressWarnings("UnstableApiUsage")
public final class ConsoleSender extends GenericSender {
public final class ConsoleSource extends GenericSource {

ConsoleSender(final CommandSourceStack commandSourceStack) {
ConsoleSource(final CommandSourceStack commandSourceStack) {
super(commandSourceStack);
}

@Override
public @NonNull ConsoleCommandSender sender() {
return (ConsoleCommandSender) super.sender();
public @NonNull ConsoleCommandSender source() {
return (ConsoleCommandSender) super.source();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.paper.sender;

import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.entity.Entity;
import org.checkerframework.checker.nullness.qual.NonNull;

@SuppressWarnings("UnstableApiUsage")
public final class EntitySource extends GenericSource {

EntitySource(final CommandSourceStack commandSourceStack) {
super(commandSourceStack);
}

@Override
public @NonNull Entity source() {
return (Entity) super.source();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,36 @@
package org.incendo.cloud.paper.sender;

import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.kyori.adventure.audience.Audience;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.checkerframework.checker.nullness.qual.NonNull;

@SuppressWarnings("UnstableApiUsage")
public class GenericSender implements Sender {
public class GenericSource implements Source {

private final CommandSourceStack commandSourceStack;

GenericSender(final @NonNull CommandSourceStack commandSourceStack) {
GenericSource(final @NonNull CommandSourceStack commandSourceStack) {
this.commandSourceStack = commandSourceStack;
}

@Override
public final @NonNull CommandSourceStack commandSourceStack() {
public final @NonNull CommandSourceStack stack() {
return this.commandSourceStack;
}

/**
* @see Sender#sender()
* @see Source#source()
*/
@Override
public @NonNull CommandSender sender() {
public @NonNull CommandSender source() {
return this.commandSourceStack.getSender();
}

@Override
public final @NonNull Audience audience() {
Entity executor = this.commandSourceStack.getExecutor();
return executor == null ? this.commandSourceStack.getSender() : executor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,47 @@
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.SenderMapper;

@SuppressWarnings("UnstableApiUsage")
public final class ModernProvidedSenderMapper implements SenderMapper<CommandSourceStack, Sender> {
public final class PaperSimpleSenderMapper implements SenderMapper<CommandSourceStack, Source> {

/**
* Create a new instance of {@link ModernProvidedSenderMapper}.
* Create a new instance of {@link PaperSimpleSenderMapper}.
*
* @return a new instance of {@link ModernProvidedSenderMapper}
* @return a new instance of {@link PaperSimpleSenderMapper}
*/
public static @NonNull ModernProvidedSenderMapper providedSenderMapper() {
return new ModernProvidedSenderMapper();
public static @NonNull PaperSimpleSenderMapper simpleSenderMapper() {
return new PaperSimpleSenderMapper();
}

ModernProvidedSenderMapper() {
PaperSimpleSenderMapper() {
}

@Override
public @NonNull Sender map(final @NonNull CommandSourceStack base) {
public @NonNull Source map(final @NonNull CommandSourceStack base) {
CommandSender commandSender = base.getSender();

if (commandSender instanceof ConsoleCommandSender) {
return new ConsoleSender(base);
return new ConsoleSource(base);
}

if (commandSender instanceof Player) {
return new PlayerSender(base);
return new PlayerSource(base);
}

return new GenericSender(base);
if (commandSender instanceof Entity) {
return new EntitySource(base);
}

return new GenericSource(base);
}

@Override
public @NonNull CommandSourceStack reverse(final @NonNull Sender mapped) {
return mapped.commandSourceStack();
public @NonNull CommandSourceStack reverse(final @NonNull Source mapped) {
return mapped.stack();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import org.checkerframework.checker.nullness.qual.NonNull;

@SuppressWarnings("UnstableApiUsage")
public final class PlayerSender extends GenericSender {
public final class PlayerSource extends GenericSource {

PlayerSender(final CommandSourceStack commandSourceStack) {
PlayerSource(final CommandSourceStack commandSourceStack) {
super(commandSourceStack);
}

@Override
public @NonNull Player sender() {
return (Player) super.sender();
public @NonNull Player source() {
return (Player) super.source();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,31 @@
package org.incendo.cloud.paper.sender;

import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.kyori.adventure.audience.Audience;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;

@SuppressWarnings("UnstableApiUsage")
public interface Sender {
public interface Source {

/**
* Gets the command source stack.
*
* @return the command source stack
*/
@NonNull CommandSourceStack commandSourceStack();
@NonNull CommandSourceStack stack();

/**
* Gets the underlying command sender from the command source stack.
*
* @return the sender.
*/
@NonNull CommandSender sender();
@NonNull CommandSender source();

/**
* Gets the audience of the source.
*
* @return the audience
*/
@NonNull Audience audience();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.paper.PaperCommandManager;
import org.incendo.cloud.paper.sender.Sender;
import org.incendo.cloud.paper.sender.Source;

@DefaultQualifier(NonNull.class)
public final class PaperPlugin extends JavaPlugin {
private final PaperCommandManager.Bootstrapped<Sender> commandManager;
private final PaperCommandManager.Bootstrapped<Source> commandManager;

public PaperPlugin(final PaperCommandManager.Bootstrapped<Sender> commandManager) {
public PaperPlugin(final PaperCommandManager.Bootstrapped<Source> commandManager) {
this.commandManager = commandManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.PaperCommandManager;
import org.incendo.cloud.paper.sender.ConsoleSender;
import org.incendo.cloud.paper.sender.ModernProvidedSenderMapper;
import org.incendo.cloud.paper.sender.PlayerSender;
import org.incendo.cloud.paper.sender.Sender;
import org.incendo.cloud.paper.sender.ConsoleSource;
import org.incendo.cloud.paper.sender.PaperSimpleSenderMapper;
import org.incendo.cloud.paper.sender.PlayerSource;
import org.incendo.cloud.paper.sender.Source;
import org.incendo.cloud.setting.ManagerSetting;

import static org.incendo.cloud.parser.standard.StringParser.stringParser;

@SuppressWarnings("UnstableApiUsage")
@DefaultQualifier(NonNull.class)
public final class PluginBootstrap implements io.papermc.paper.plugin.bootstrap.PluginBootstrap {
private PaperCommandManager.@MonotonicNonNull Bootstrapped<Sender> commandManager;
private PaperCommandManager.@MonotonicNonNull Bootstrapped<Source> commandManager;

@Override
public void bootstrap(final BootstrapContext context) {
final PaperCommandManager.Bootstrapped<Sender> mgr =
PaperCommandManager.builder(ModernProvidedSenderMapper.providedSenderMapper())
final PaperCommandManager.Bootstrapped<Source> mgr =
PaperCommandManager.builder(PaperSimpleSenderMapper.simpleSenderMapper())
.executionCoordinator(ExecutionCoordinator.simpleCoordinator())
.buildBootstrapped(context);

Expand All @@ -63,7 +63,7 @@ public void bootstrap(final BootstrapContext context) {
final String name = ctx.get("name");
mgr.command(
mgr.commandBuilder(name).handler(ctx1 -> {
ctx1.sender().sender().sendMessage("HI");
ctx1.sender().source().sendMessage("HI");
})
);
})
Expand All @@ -78,17 +78,17 @@ public void bootstrap(final BootstrapContext context) {
);
mgr.command(
mgr.commandBuilder("player_command")
.senderType(PlayerSender.class)
.senderType(PlayerSource.class)
.handler(ctx -> {
final Player player = ctx.sender().sender();
final Player player = ctx.sender().source();
player.sendMessage("hello player!");
})
);
mgr.command(
mgr.commandBuilder("console_command")
.senderType(ConsoleSender.class)
.senderType(ConsoleSource.class)
.handler(ctx -> {
final ConsoleCommandSender console = ctx.sender().sender();
final ConsoleCommandSender console = ctx.sender().source();
console.sendMessage("hello console!");
})
);
Expand Down

0 comments on commit b1ee8d1

Please sign in to comment.