Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c45bab5

Browse files
committed
Implement PR #478 in 1.21.4/5 NMS
1 parent a86575a commit c45bab5

File tree

4 files changed

+136
-14
lines changed

4 files changed

+136
-14
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R3.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.bukkit.craftbukkit.v1_21_R3.CraftParticle;
6666
import org.bukkit.craftbukkit.v1_21_R3.CraftServer;
6767
import org.bukkit.craftbukkit.v1_21_R3.CraftSound;
68+
import org.bukkit.craftbukkit.v1_21_R3.CraftWorld;
6869
import org.bukkit.craftbukkit.v1_21_R3.block.data.CraftBlockData;
6970
import org.bukkit.craftbukkit.v1_21_R3.command.BukkitCommandWrapper;
7071
import org.bukkit.craftbukkit.v1_21_R3.command.VanillaCommandWrapper;
@@ -838,8 +839,7 @@ public String getScoreHolderSingle(CommandContext<CommandSourceStack> cmdCtx, St
838839
}
839840

840841
@Override
841-
public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandContext<CommandSourceStack> cmdCtx,
842-
boolean isNative) {
842+
public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandContext<CommandSourceStack> cmdCtx, boolean isNative) {
843843
CommandSourceStack css = cmdCtx.getSource();
844844

845845
CommandSender sender = css.getBukkitSender();
@@ -851,10 +851,6 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
851851
// sender.
852852
sender = Bukkit.getConsoleSender();
853853
}
854-
Vec3 pos = css.getPosition();
855-
Vec2 rot = css.getRotation();
856-
World world = getWorldForCSS(css);
857-
Location location = new Location(world, pos.x(), pos.y(), pos.z(), rot.y, rot.x);
858854

859855
Entity proxyEntity = css.getEntity();
860856
CommandSender proxy = proxyEntity == null ? null : proxyEntity.getBukkitEntity();
@@ -863,12 +859,42 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
863859
proxy = sender;
864860
}
865861

866-
return new BukkitNativeProxyCommandSender(new NativeProxyCommandSender(sender, proxy, location, world));
862+
return new BukkitNativeProxyCommandSender(new NativeProxyCommandSender_1_21_R3(css, sender, proxy));
867863
} else {
868864
return wrapCommandSender(sender);
869865
}
870866
}
871867

868+
@Override
869+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
870+
if (callee == null) callee = caller;
871+
872+
// Most parameters default to what is defined by the caller
873+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
874+
875+
// Position and rotation may be overridden by the Location
876+
if (location != null) {
877+
css = css
878+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
879+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
880+
}
881+
882+
// ServerLevel may be overridden by the World
883+
if (world == null && location != null) {
884+
world = location.getWorld();
885+
}
886+
if (world != null) {
887+
css = css.withLevel(((CraftWorld) world).getHandle());
888+
}
889+
890+
// The proxied sender can only be an Entity in the CommandSourceStack
891+
if (callee instanceof org.bukkit.entity.Entity e) {
892+
css = css.withEntity(((CraftEntity) e).getHandle());
893+
}
894+
895+
return new NativeProxyCommandSender_1_21_R3(css, caller, callee);
896+
}
897+
872898
@Override
873899
public final SimpleCommandMap getSimpleCommandMap() {
874900
return ((CraftServer) Bukkit.getServer()).getCommandMap();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.jorel.commandapi.nms;
2+
3+
import dev.jorel.commandapi.CommandAPIBukkit;
4+
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
5+
import net.minecraft.commands.CommandSourceStack;
6+
import net.minecraft.world.phys.Vec2;
7+
import net.minecraft.world.phys.Vec3;
8+
import org.bukkit.Location;
9+
import org.bukkit.World;
10+
import org.bukkit.command.CommandSender;
11+
import org.bukkit.craftbukkit.v1_21_R3.command.ProxiedNativeCommandSender;
12+
13+
public class NativeProxyCommandSender_1_21_R3 extends ProxiedNativeCommandSender implements NativeProxyCommandSender {
14+
private final World world;
15+
private final Location location;
16+
17+
public NativeProxyCommandSender_1_21_R3(CommandSourceStack css, CommandSender caller, CommandSender callee) {
18+
super(css, caller, callee);
19+
20+
Vec3 pos = css.getPosition();
21+
Vec2 rot = css.getRotation();
22+
this.world = CommandAPIBukkit.get().getWorldForCSS(css);
23+
this.location = new Location(this.world, pos.x(), pos.y(), pos.z(), rot.y, rot.x);
24+
}
25+
26+
@Override
27+
public Location getLocation() {
28+
return location;
29+
}
30+
31+
@Override
32+
public World getWorld() {
33+
return world;
34+
}
35+
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R4.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.bukkit.craftbukkit.v1_21_R4.CraftParticle;
6666
import org.bukkit.craftbukkit.v1_21_R4.CraftServer;
6767
import org.bukkit.craftbukkit.v1_21_R4.CraftSound;
68+
import org.bukkit.craftbukkit.v1_21_R4.CraftWorld;
6869
import org.bukkit.craftbukkit.v1_21_R4.block.data.CraftBlockData;
6970
import org.bukkit.craftbukkit.v1_21_R4.command.BukkitCommandWrapper;
7071
import org.bukkit.craftbukkit.v1_21_R4.command.VanillaCommandWrapper;
@@ -838,8 +839,7 @@ public String getScoreHolderSingle(CommandContext<CommandSourceStack> cmdCtx, St
838839
}
839840

840841
@Override
841-
public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandContext<CommandSourceStack> cmdCtx,
842-
boolean isNative) {
842+
public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandContext<CommandSourceStack> cmdCtx, boolean isNative) {
843843
CommandSourceStack css = cmdCtx.getSource();
844844

845845
CommandSender sender = css.getBukkitSender();
@@ -851,10 +851,6 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
851851
// sender.
852852
sender = Bukkit.getConsoleSender();
853853
}
854-
Vec3 pos = css.getPosition();
855-
Vec2 rot = css.getRotation();
856-
World world = getWorldForCSS(css);
857-
Location location = new Location(world, pos.x(), pos.y(), pos.z(), rot.y, rot.x);
858854

859855
Entity proxyEntity = css.getEntity();
860856
CommandSender proxy = proxyEntity == null ? null : proxyEntity.getBukkitEntity();
@@ -863,12 +859,42 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
863859
proxy = sender;
864860
}
865861

866-
return new BukkitNativeProxyCommandSender(new NativeProxyCommandSender(sender, proxy, location, world));
862+
return new BukkitNativeProxyCommandSender(new NativeProxyCommandSender_1_21_R4(css, sender, proxy));
867863
} else {
868864
return wrapCommandSender(sender);
869865
}
870866
}
871867

868+
@Override
869+
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
870+
if (callee == null) callee = caller;
871+
872+
// Most parameters default to what is defined by the caller
873+
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));
874+
875+
// Position and rotation may be overridden by the Location
876+
if (location != null) {
877+
css = css
878+
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
879+
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
880+
}
881+
882+
// ServerLevel may be overridden by the World
883+
if (world == null && location != null) {
884+
world = location.getWorld();
885+
}
886+
if (world != null) {
887+
css = css.withLevel(((CraftWorld) world).getHandle());
888+
}
889+
890+
// The proxied sender can only be an Entity in the CommandSourceStack
891+
if (callee instanceof org.bukkit.entity.Entity e) {
892+
css = css.withEntity(((CraftEntity) e).getHandle());
893+
}
894+
895+
return new NativeProxyCommandSender_1_21_R4(css, caller, callee);
896+
}
897+
872898
@Override
873899
public final SimpleCommandMap getSimpleCommandMap() {
874900
return ((CraftServer) Bukkit.getServer()).getCommandMap();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.jorel.commandapi.nms;
2+
3+
import dev.jorel.commandapi.CommandAPIBukkit;
4+
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
5+
import net.minecraft.commands.CommandSourceStack;
6+
import net.minecraft.world.phys.Vec2;
7+
import net.minecraft.world.phys.Vec3;
8+
import org.bukkit.Location;
9+
import org.bukkit.World;
10+
import org.bukkit.command.CommandSender;
11+
import org.bukkit.craftbukkit.v1_21_R4.command.ProxiedNativeCommandSender;
12+
13+
public class NativeProxyCommandSender_1_21_R4 extends ProxiedNativeCommandSender implements NativeProxyCommandSender {
14+
private final World world;
15+
private final Location location;
16+
17+
public NativeProxyCommandSender_1_21_R4(CommandSourceStack css, CommandSender caller, CommandSender callee) {
18+
super(css, caller, callee);
19+
20+
Vec3 pos = css.getPosition();
21+
Vec2 rot = css.getRotation();
22+
this.world = CommandAPIBukkit.get().getWorldForCSS(css);
23+
this.location = new Location(this.world, pos.x(), pos.y(), pos.z(), rot.y, rot.x);
24+
}
25+
26+
@Override
27+
public Location getLocation() {
28+
return location;
29+
}
30+
31+
@Override
32+
public World getWorld() {
33+
return world;
34+
}
35+
}

0 commit comments

Comments
 (0)