Skip to content

Commit

Permalink
Add NativeProxyCommandSender#from
Browse files Browse the repository at this point in the history
  • Loading branch information
willkroboth committed Aug 12, 2023
1 parent 3134c50 commit 423df48
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
Expand All @@ -67,6 +68,7 @@
import dev.jorel.commandapi.wrappers.IntegerRange;
import dev.jorel.commandapi.wrappers.Location2D;
import dev.jorel.commandapi.wrappers.MathOperation;
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
import dev.jorel.commandapi.wrappers.ParticleData;
import dev.jorel.commandapi.wrappers.Rotation;
import dev.jorel.commandapi.wrappers.ScoreboardSlot;
Expand Down Expand Up @@ -457,4 +459,5 @@ String getScoreHolderSingle(CommandContext<CommandListenerWrapper> cmdCtx, Strin

Message generateMessageFromJson(String json);

NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,39 @@
*******************************************************************************/
package dev.jorel.commandapi.wrappers;

import dev.jorel.commandapi.CommandAPIBukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ProxiedCommandSender;

/**
* A simple representation of Minecraft's CommandListenerWrapper, in the form of
* Bukkit's ProxiedCommandSender
*/
public interface NativeProxyCommandSender extends ProxiedCommandSender {
/**
* Constructs a NativeProxyCommandSender, which is basically Minecraft's CommandListenerWrapper
*
* @param caller the command sender that actually sent the command
* @param callee the command sender that will be executing the command
* @param location the proxied location that the command will be run at
* @param world the proxied world that the command will be run in
*/
static NativeProxyCommandSender from(CommandSender caller, CommandSender callee, Location location, World world) {
return CommandAPIBukkit.get().createNativeProxyCommandSender(caller, callee, location, world);
}

/**
* Returns the location that this native command sender represents
*
* @return the location that this native command sender represents
*/
Location getLocation();

/**
* Returns the world that this native command sender represents
*
* @return the world that this native command sender represents
*/
World getWorld();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import dev.jorel.commandapi.wrappers.IntegerRange;
import dev.jorel.commandapi.wrappers.Location2D;
import dev.jorel.commandapi.wrappers.MathOperation;
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
import dev.jorel.commandapi.wrappers.ParticleData;
import dev.jorel.commandapi.wrappers.Rotation;
import dev.jorel.commandapi.wrappers.ScoreboardSlot;
Expand Down Expand Up @@ -852,6 +853,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
}
}

@Override
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
if (callee == null) callee = caller;

// Most parameters default to what is defined by the caller
CommandListenerWrapper clw = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));

// Position and rotation may be overridden by the Location
if (location != null) {
clw = clw
.a(new Vec3D(location.getX(), location.getY(), location.getZ()))
.a(new Vec2F(location.getPitch(), location.getYaw()));
}

// WorldServer may be overridden by the World
if (world == null && location != null) {
world = location.getWorld();
}
if (world != null) {
clw = clw.a(((CraftWorld) world).getHandle());
}

// The proxied sender can only be an Entity
if (callee instanceof org.bukkit.entity.Entity e) {
clw = clw.a(((CraftEntity) e).getHandle());
}

return new NativeProxyCommandSender_1_15(clw, caller, callee);
}

@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import dev.jorel.commandapi.wrappers.IntegerRange;
import dev.jorel.commandapi.wrappers.Location2D;
import dev.jorel.commandapi.wrappers.MathOperation;
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
import dev.jorel.commandapi.wrappers.ParticleData;
import dev.jorel.commandapi.wrappers.Rotation;
import dev.jorel.commandapi.wrappers.ScoreboardSlot;
Expand Down Expand Up @@ -875,6 +876,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
}
}

@Override
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
if (callee == null) callee = caller;

// Most parameters default to what is defined by the caller
CommandListenerWrapper clw = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));

// Position and rotation may be overridden by the Location
if (location != null) {
clw = clw
.a(new Vec3D(location.getX(), location.getY(), location.getZ()))
.a(new Vec2F(location.getPitch(), location.getYaw()));
}

// WorldServer may be overridden by the World
if (world == null && location != null) {
world = location.getWorld();
}
if (world != null) {
clw = clw.a(((CraftWorld) world).getHandle());
}

// The proxied sender can only be an Entity
if (callee instanceof org.bukkit.entity.Entity e) {
clw = clw.a(((CraftEntity) e).getHandle());
}

return new NativeProxyCommandSender_1_16_R1(clw, caller, callee);
}

@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.bukkit.craftbukkit.v1_16_R2.CraftParticle;
import org.bukkit.craftbukkit.v1_16_R2.CraftServer;
import org.bukkit.craftbukkit.v1_16_R2.CraftSound;
import org.bukkit.craftbukkit.v1_16_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R2.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_16_R2.command.BukkitCommandWrapper;
import org.bukkit.craftbukkit.v1_16_R2.command.VanillaCommandWrapper;
Expand Down Expand Up @@ -91,6 +92,7 @@
import dev.jorel.commandapi.wrappers.IntegerRange;
import dev.jorel.commandapi.wrappers.Location2D;
import dev.jorel.commandapi.wrappers.MathOperation;
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
import dev.jorel.commandapi.wrappers.ParticleData;
import dev.jorel.commandapi.wrappers.Rotation;
import dev.jorel.commandapi.wrappers.ScoreboardSlot;
Expand Down Expand Up @@ -871,6 +873,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
}
}

@Override
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
if (callee == null) callee = caller;

// Most parameters default to what is defined by the caller
CommandListenerWrapper clw = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));

// Position and rotation may be overridden by the Location
if (location != null) {
clw = clw
.a(new Vec3D(location.getX(), location.getY(), location.getZ()))
.a(new Vec2F(location.getPitch(), location.getYaw()));
}

// WorldServer may be overridden by the World
if (world == null && location != null) {
world = location.getWorld();
}
if (world != null) {
clw = clw.a(((CraftWorld) world).getHandle());
}

// The proxied sender can only be an Entity
if (callee instanceof org.bukkit.entity.Entity e) {
clw = clw.a(((CraftEntity) e).getHandle());
}

return new NativeProxyCommandSender_1_16_R2(clw, caller, callee);
}

@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.bukkit.craftbukkit.v1_16_R3.CraftParticle;
import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
import org.bukkit.craftbukkit.v1_16_R3.CraftSound;
import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_16_R3.command.BukkitCommandWrapper;
import org.bukkit.craftbukkit.v1_16_R3.command.VanillaCommandWrapper;
Expand Down Expand Up @@ -111,6 +112,7 @@
import dev.jorel.commandapi.wrappers.IntegerRange;
import dev.jorel.commandapi.wrappers.Location2D;
import dev.jorel.commandapi.wrappers.MathOperation;
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
import dev.jorel.commandapi.wrappers.ParticleData;
import dev.jorel.commandapi.wrappers.Rotation;
import dev.jorel.commandapi.wrappers.ScoreboardSlot;
Expand Down Expand Up @@ -825,6 +827,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
}
}

@Override
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
if (callee == null) callee = caller;

// Most parameters default to what is defined by the caller
CommandListenerWrapper clw = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));

// Position and rotation may be overridden by the Location
if (location != null) {
clw = clw
.a(new Vec3D(location.getX(), location.getY(), location.getZ()))
.a(new Vec2F(location.getPitch(), location.getYaw()));
}

// WorldServer may be overridden by the World
if (world == null && location != null) {
world = location.getWorld();
}
if (world != null) {
clw = clw.a(((CraftWorld) world).getHandle());
}

// The proxied sender can only be an Entity
if (callee instanceof org.bukkit.entity.Entity e) {
clw = clw.a(((CraftEntity) e).getHandle());
}

return new NativeProxyCommandSender_1_16_4_R3(clw, caller, callee);
}

@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.bukkit.craftbukkit.v1_17_R1.CraftParticle;
import org.bukkit.craftbukkit.v1_17_R1.CraftServer;
import org.bukkit.craftbukkit.v1_17_R1.CraftSound;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_17_R1.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_17_R1.command.BukkitCommandWrapper;
import org.bukkit.craftbukkit.v1_17_R1.command.VanillaCommandWrapper;
Expand Down Expand Up @@ -97,6 +98,7 @@
import dev.jorel.commandapi.wrappers.ComplexRecipeImpl;
import dev.jorel.commandapi.wrappers.FunctionWrapper;
import dev.jorel.commandapi.wrappers.Location2D;
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
import dev.jorel.commandapi.wrappers.ParticleData;
import dev.jorel.commandapi.wrappers.SimpleFunctionWrapper;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -587,6 +589,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
return wrapCommandSender(sender);
}
}

@Override
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
if (callee == null) callee = caller;

// Most parameters default to what is defined by the caller
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));

// Position and rotation may be overridden by the Location
if (location != null) {
css = css
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
}

// ServerLevel may be overridden by the World
if (world == null && location != null) {
world = location.getWorld();
}
if (world != null) {
css = css.withLevel(((CraftWorld) world).getHandle());
}

// The proxied sender can only be an Entity in the CommandSourceStack
if (callee instanceof org.bukkit.entity.Entity e) {
css = css.withEntity(((CraftEntity) e).getHandle());
}

return new NativeProxyCommandSender_1_17_Common(css, caller, callee);
}

@Override
public SimpleCommandMap getSimpleCommandMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.bukkit.craftbukkit.v1_18_R2.CraftParticle;
import org.bukkit.craftbukkit.v1_18_R2.CraftServer;
import org.bukkit.craftbukkit.v1_18_R2.CraftSound;
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R2.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_18_R2.command.BukkitCommandWrapper;
import org.bukkit.craftbukkit.v1_18_R2.command.VanillaCommandWrapper;
Expand Down Expand Up @@ -102,6 +103,7 @@
import dev.jorel.commandapi.wrappers.ComplexRecipeImpl;
import dev.jorel.commandapi.wrappers.FunctionWrapper;
import dev.jorel.commandapi.wrappers.Location2D;
import dev.jorel.commandapi.wrappers.NativeProxyCommandSender;
import dev.jorel.commandapi.wrappers.ParticleData;
import dev.jorel.commandapi.wrappers.SimpleFunctionWrapper;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -641,6 +643,36 @@ public BukkitCommandSender<? extends CommandSender> getSenderForCommand(CommandC
}
}

@Override
public NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world) {
if (callee == null) callee = caller;

// Most parameters default to what is defined by the caller
CommandSourceStack css = getBrigadierSourceFromCommandSender(wrapCommandSender(caller));

// Position and rotation may be overridden by the Location
if (location != null) {
css = css
.withPosition(new Vec3(location.getX(), location.getY(), location.getZ()))
.withRotation(new Vec2(location.getPitch(), location.getYaw()));
}

// ServerLevel may be overridden by the World
if (world == null && location != null) {
world = location.getWorld();
}
if (world != null) {
css = css.withLevel(((CraftWorld) world).getHandle());
}

// The proxied sender can only be an Entity in the CommandSourceStack
if (callee instanceof org.bukkit.entity.Entity e) {
css = css.withEntity(((CraftEntity) e).getHandle());
}

return new NativeProxyCommandSender_1_18_R2(css, caller, callee);
}

@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
Expand Down
Loading

0 comments on commit 423df48

Please sign in to comment.