Skip to content

Commit 8b05856

Browse files
committed
Add NativeProxyCommandSender#from
1 parent 4a10caa commit 8b05856

File tree

25 files changed

+486
-1
lines changed
  • commandapi-platforms/commandapi-bukkit
    • commandapi-bukkit-core/src/main/java/dev/jorel/commandapi
    • commandapi-bukkit-nms
    • commandapi-bukkit-test
      • commandapi-bukkit-test-impl-1.16.5/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.17/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.18/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.19.2/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.19.4/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.20.2/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.20.3/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.20.5/src/main/java/dev/jorel/commandapi/test
      • commandapi-bukkit-test-impl-1.20/src/main/java/dev/jorel/commandapi/test

25 files changed

+486
-1
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/nms/NMS.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.bukkit.advancement.Advancement;
4040
import org.bukkit.block.Block;
4141
import org.bukkit.block.data.BlockData;
42+
import org.bukkit.command.CommandSender;
4243
import org.bukkit.command.SimpleCommandMap;
4344
import org.bukkit.enchantments.Enchantment;
4445
import org.bukkit.entity.EntityType;
@@ -64,6 +65,7 @@
6465
import dev.jorel.commandapi.wrappers.IntegerRange;
6566
import dev.jorel.commandapi.wrappers.Location2D;
6667
import dev.jorel.commandapi.wrappers.MathOperation;
68+
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
6769
import dev.jorel.commandapi.wrappers.ParticleData;
6870
import dev.jorel.commandapi.wrappers.Rotation;
6971
import dev.jorel.commandapi.wrappers.ScoreboardSlot;
@@ -427,4 +429,6 @@ String getScoreHolderSingle(CommandContext<CommandListenerWrapper> cmdCtx, Strin
427429
Message generateMessageFromJson(String json);
428430

429431
CommandRegistrationStrategy<CommandListenerWrapper> createCommandRegistrationStrategy();
432+
433+
NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world);
430434
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/wrappers/NativeProxyCommandSender.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,39 @@
2020
*******************************************************************************/
2121
package dev.jorel.commandapi.wrappers;
2222

23+
import dev.jorel.commandapi.CommandAPIBukkit;
2324
import org.bukkit.Location;
2425
import org.bukkit.World;
26+
import org.bukkit.command.CommandSender;
2527
import org.bukkit.command.ProxiedCommandSender;
2628

2729
/**
2830
* A simple representation of Minecraft's CommandListenerWrapper, in the form of
2931
* Bukkit's ProxiedCommandSender
3032
*/
3133
public interface NativeProxyCommandSender extends ProxiedCommandSender {
34+
/**
35+
* Constructs a NativeProxyCommandSender, which is basically Minecraft's CommandListenerWrapper
36+
*
37+
* @param caller the command sender that actually sent the command
38+
* @param callee the command sender that will be executing the command
39+
* @param location the proxied location that the command will be run at
40+
* @param world the proxied world that the command will be run in
41+
*/
42+
static NativeProxyCommandSender from(CommandSender caller, CommandSender callee, Location location, World world) {
43+
return CommandAPIBukkit.get().createNativeProxyCommandSender(caller, callee, location, world);
44+
}
45+
3246
/**
3347
* Returns the location that this native command sender represents
48+
*
3449
* @return the location that this native command sender represents
3550
*/
3651
Location getLocation();
3752

3853
/**
3954
* Returns the world that this native command sender represents
55+
*
4056
* @return the world that this native command sender represents
4157
*/
4258
World getWorld();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.16.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_16_R3.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.bukkit.craftbukkit.v1_16_R3.CraftParticle;
6262
import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
6363
import org.bukkit.craftbukkit.v1_16_R3.CraftSound;
64+
import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
6465
import org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData;
6566
import org.bukkit.craftbukkit.v1_16_R3.command.BukkitCommandWrapper;
6667
import org.bukkit.craftbukkit.v1_16_R3.command.VanillaCommandWrapper;
@@ -849,6 +850,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
849850
}
850851
}
851852

853+
@Override
854+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
855+
if (callee == null) callee = caller;
856+
857+
// Most parameters default to what is defined by the caller
858+
CommandListenerWrapper clw = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
859+
860+
// Position and rotation may be overridden by the Location
861+
if (location != null) {
862+
clw = clw
863+
.a(new Vec3D(location.getX(), location.getY(), location.getZ()))
864+
.a(new Vec2F(location.getPitch(), location.getYaw()));
865+
}
866+
867+
// WorldServer may be overridden by the World
868+
if (world == null && location != null) {
869+
world = location.getWorld();
870+
}
871+
if (world != null) {
872+
clw = clw.a(((CraftWorld) world).getHandle());
873+
}
874+
875+
// The proxied sender can only be an Entity
876+
if (callee instanceof org.bukkit.entity.Entity e) {
877+
clw = clw.a(((CraftEntity) e).getHandle());
878+
}
879+
880+
return new NativeProxyCommandSender_1_16_R3(clw, caller, callee);
881+
}
882+
852883
@Override
853884
public SimpleCommandMap getSimpleCommandMap() {
854885
return ((CraftServer) Bukkit.getServer()).getCommandMap();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.17-common/src/main/java/dev/jorel/commandapi/nms/NMS_1_17_Common.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.bukkit.craftbukkit.v1_17_R1.CraftParticle;
6161
import org.bukkit.craftbukkit.v1_17_R1.CraftServer;
6262
import org.bukkit.craftbukkit.v1_17_R1.CraftSound;
63+
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
6364
import org.bukkit.craftbukkit.v1_17_R1.block.data.CraftBlockData;
6465
import org.bukkit.craftbukkit.v1_17_R1.command.BukkitCommandWrapper;
6566
import org.bukkit.craftbukkit.v1_17_R1.command.VanillaCommandWrapper;
@@ -615,7 +616,37 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
615616
return wrapCommandSender(sender);
616617
}
617618
}
618-
619+
620+
@Override
621+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
622+
if (callee == null) callee = caller;
623+
624+
// Most parameters default to what is defined by the caller
625+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
626+
627+
// Position and rotation may be overridden by the Location
628+
if (location != null) {
629+
css = css
630+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
631+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
632+
}
633+
634+
// ServerLevel may be overridden by the World
635+
if (world == null && location != null) {
636+
world = location.getWorld();
637+
}
638+
if (world != null) {
639+
css = css.withLevel(((CraftWorld) world).getHandle());
640+
}
641+
642+
// The proxied sender can only be an Entity in the CommandSourceStack
643+
if (callee instanceof org.bukkit.entity.Entity e) {
644+
css = css.withEntity(((CraftEntity) e).getHandle());
645+
}
646+
647+
return new NativeProxyCommandSender_1_17_Common(css, caller, callee);
648+
}
649+
619650
@Override
620651
public SimpleCommandMap getSimpleCommandMap() {
621652
return ((CraftServer) Bukkit.getServer()).getCommandMap();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.18.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_18_R2.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.bukkit.craftbukkit.v1_18_R2.CraftParticle;
6464
import org.bukkit.craftbukkit.v1_18_R2.CraftServer;
6565
import org.bukkit.craftbukkit.v1_18_R2.CraftSound;
66+
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
6667
import org.bukkit.craftbukkit.v1_18_R2.block.data.CraftBlockData;
6768
import org.bukkit.craftbukkit.v1_18_R2.command.BukkitCommandWrapper;
6869
import org.bukkit.craftbukkit.v1_18_R2.command.VanillaCommandWrapper;
@@ -666,6 +667,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
666667
}
667668
}
668669

670+
@Override
671+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
672+
if (callee == null) callee = caller;
673+
674+
// Most parameters default to what is defined by the caller
675+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
676+
677+
// Position and rotation may be overridden by the Location
678+
if (location != null) {
679+
css = css
680+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
681+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
682+
}
683+
684+
// ServerLevel may be overridden by the World
685+
if (world == null && location != null) {
686+
world = location.getWorld();
687+
}
688+
if (world != null) {
689+
css = css.withLevel(((CraftWorld) world).getHandle());
690+
}
691+
692+
// The proxied sender can only be an Entity in the CommandSourceStack
693+
if (callee instanceof org.bukkit.entity.Entity e) {
694+
css = css.withEntity(((CraftEntity) e).getHandle());
695+
}
696+
697+
return new NativeProxyCommandSender_1_18_R2(css, caller, callee);
698+
}
699+
669700
@Override
670701
public SimpleCommandMap getSimpleCommandMap() {
671702
return ((CraftServer) Bukkit.getServer()).getCommandMap();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.18/src/main/java/dev/jorel/commandapi/nms/NMS_1_18_R1.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.bukkit.craftbukkit.v1_18_R1.CraftParticle;
6363
import org.bukkit.craftbukkit.v1_18_R1.CraftServer;
6464
import org.bukkit.craftbukkit.v1_18_R1.CraftSound;
65+
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
6566
import org.bukkit.craftbukkit.v1_18_R1.block.data.CraftBlockData;
6667
import org.bukkit.craftbukkit.v1_18_R1.command.BukkitCommandWrapper;
6768
import org.bukkit.craftbukkit.v1_18_R1.command.VanillaCommandWrapper;
@@ -616,6 +617,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
616617
}
617618
}
618619

620+
@Override
621+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
622+
if (callee == null) callee = caller;
623+
624+
// Most parameters default to what is defined by the caller
625+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
626+
627+
// Position and rotation may be overridden by the Location
628+
if (location != null) {
629+
css = css
630+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
631+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
632+
}
633+
634+
// ServerLevel may be overridden by the World
635+
if (world == null && location != null) {
636+
world = location.getWorld();
637+
}
638+
if (world != null) {
639+
css = css.withLevel(((CraftWorld) world).getHandle());
640+
}
641+
642+
// The proxied sender can only be an Entity in the CommandSourceStack
643+
if (callee instanceof org.bukkit.entity.Entity e) {
644+
css = css.withEntity(((CraftEntity) e).getHandle());
645+
}
646+
647+
return new NativeProxyCommandSender_1_18_R1(css, caller, callee);
648+
}
649+
619650
@Override
620651
public SimpleCommandMap getSimpleCommandMap() {
621652
return ((CraftServer) Bukkit.getServer()).getCommandMap();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.19-common/src/main/java/dev/jorel/commandapi/nms/NMS_1_19_Common.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
import org.bukkit.craftbukkit.v1_19_R1.CraftParticle;
113113
import org.bukkit.craftbukkit.v1_19_R1.CraftServer;
114114
import org.bukkit.craftbukkit.v1_19_R1.CraftSound;
115+
import org.bukkit.craftbukkit.v1_19_R1.CraftWorld;
115116
import org.bukkit.craftbukkit.v1_19_R1.block.data.CraftBlockData;
116117
import org.bukkit.craftbukkit.v1_19_R1.command.BukkitCommandWrapper;
117118
import org.bukkit.craftbukkit.v1_19_R1.command.VanillaCommandWrapper;
@@ -721,6 +722,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
721722
}
722723
}
723724

725+
@Override
726+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
727+
if (callee == null) callee = caller;
728+
729+
// Most parameters default to what is defined by the caller
730+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
731+
732+
// Position and rotation may be overridden by the Location
733+
if (location != null) {
734+
css = css
735+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
736+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
737+
}
738+
739+
// ServerLevel may be overridden by the World
740+
if (world == null && location != null) {
741+
world = location.getWorld();
742+
}
743+
if (world != null) {
744+
css = css.withLevel(((CraftWorld) world).getHandle());
745+
}
746+
747+
// The proxied sender can only be an Entity in the CommandSourceStack
748+
if (callee instanceof org.bukkit.entity.Entity e) {
749+
css = css.withEntity(((CraftEntity) e).getHandle());
750+
}
751+
752+
return new NativeProxyCommandSender_1_19_Common(css, caller, callee);
753+
}
754+
724755
@Override
725756
public final SimpleCommandMap getSimpleCommandMap() {
726757
return ((CraftServer) Bukkit.getServer()).getCommandMap();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.19.3/src/main/java/dev/jorel/commandapi/nms/NMS_1_19_3_R2.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import org.bukkit.craftbukkit.v1_19_R2.CraftParticle;
111111
import org.bukkit.craftbukkit.v1_19_R2.CraftServer;
112112
import org.bukkit.craftbukkit.v1_19_R2.CraftSound;
113+
import org.bukkit.craftbukkit.v1_19_R2.CraftWorld;
113114
import org.bukkit.craftbukkit.v1_19_R2.block.data.CraftBlockData;
114115
import org.bukkit.craftbukkit.v1_19_R2.command.BukkitCommandWrapper;
115116
import org.bukkit.craftbukkit.v1_19_R2.command.VanillaCommandWrapper;
@@ -600,6 +601,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
600601
}
601602
}
602603

604+
@Override
605+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
606+
if (callee == null) callee = caller;
607+
608+
// Most parameters default to what is defined by the caller
609+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
610+
611+
// Position and rotation may be overridden by the Location
612+
if (location != null) {
613+
css = css
614+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
615+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
616+
}
617+
618+
// ServerLevel may be overridden by the World
619+
if (world == null && location != null) {
620+
world = location.getWorld();
621+
}
622+
if (world != null) {
623+
css = css.withLevel(((CraftWorld) world).getHandle());
624+
}
625+
626+
// The proxied sender can only be an Entity in the CommandSourceStack
627+
if (callee instanceof org.bukkit.entity.Entity e) {
628+
css = css.withEntity(((CraftEntity) e).getHandle());
629+
}
630+
631+
return new NativeProxyCommandSender_1_19_3_R2(css, caller, callee);
632+
}
633+
603634
@Override
604635
public final SimpleCommandMap getSimpleCommandMap() {
605636
return ((CraftServer) Bukkit.getServer()).getCommandMap();

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.19.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_19_4_R3.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import org.bukkit.craftbukkit.v1_19_R3.CraftParticle;
111111
import org.bukkit.craftbukkit.v1_19_R3.CraftServer;
112112
import org.bukkit.craftbukkit.v1_19_R3.CraftSound;
113+
import org.bukkit.craftbukkit.v1_19_R3.CraftWorld;
113114
import org.bukkit.craftbukkit.v1_19_R3.block.data.CraftBlockData;
114115
import org.bukkit.craftbukkit.v1_19_R3.command.BukkitCommandWrapper;
115116
import org.bukkit.craftbukkit.v1_19_R3.command.VanillaCommandWrapper;
@@ -594,6 +595,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
594595
}
595596
}
596597

598+
@Override
599+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
600+
if (callee == null) callee = caller;
601+
602+
// Most parameters default to what is defined by the caller
603+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
604+
605+
// Position and rotation may be overridden by the Location
606+
if (location != null) {
607+
css = css
608+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
609+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
610+
}
611+
612+
// ServerLevel may be overridden by the World
613+
if (world == null && location != null) {
614+
world = location.getWorld();
615+
}
616+
if (world != null) {
617+
css = css.withLevel(((CraftWorld) world).getHandle());
618+
}
619+
620+
// The proxied sender can only be an Entity in the CommandSourceStack
621+
if (callee instanceof org.bukkit.entity.Entity e) {
622+
css = css.withEntity(((CraftEntity) e).getHandle());
623+
}
624+
625+
return new NativeProxyCommandSender_1_19_4_R3(css, caller, callee);
626+
}
627+
597628
@Override
598629
public final SimpleCommandMap getSimpleCommandMap() {
599630
return ((CraftServer) Bukkit.getServer()).getCommandMap();

0 commit comments

Comments
 (0)