Skip to content

Commit 0b87109

Browse files
committed
Add support for a NamespacedKey variant for the PotionEffectArgument
1 parent 07bc9c9 commit 0b87109

File tree

35 files changed

+262
-57
lines changed

35 files changed

+262
-57
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ This is the current roadmap for the CommandAPI (as of 11th May 2023):
430430
<li>https://github.com/JorelAli/CommandAPI/issues/524 Fixes <code>CommandAPIBukkit.get().getTags()</code> erroring in 1.20.4</li>
431431
<li>https://github.com/JorelAli/CommandAPI/issues/536, https://github.com/JorelAli/CommandAPI/pull/537 Fixes <code>MultiLiteralArgument</code> help displaying the node name instead of the literal text</li>
432432
<li>https://github.com/JorelAli/CommandAPI/pull/540 Add methods to "safe-cast" arguments to <code>CommandArguments</code></li>
433+
<li>https://github.com/JorelAli/CommandAPI/pull/541 Adds support for a <code>NamespacedKey</code> variant for the <code>PotionEffectArgument</code></li>
433434
</ul>
434435
<b>Minecraft Version Changes:</b>
435436
<ul>

commandapi-core/src/main/java/dev/jorel/commandapi/arguments/SuggestionProviders.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public enum SuggestionProviders {
3838
/**
3939
* A suggestion provider for the EntityTypeArgument
4040
*/
41-
ENTITIES;
41+
ENTITIES,
42+
43+
/**
44+
* A suggestion provider for the PotionEffectArgument
45+
*/
46+
POTION_EFFECTS;
4247
}

commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,25 @@ void argument_potion() {
818818
})
819819
.register();
820820
/* ANCHOR_END: argumentPotion1 */
821+
/* ANCHOR: argumentPotion2 */
822+
new CommandAPICommand("potion")
823+
.withArguments(new PlayerArgument("target"))
824+
.withArguments(new PotionEffectArgument.NamespacedKey("potion"))
825+
.withArguments(new TimeArgument("duration"))
826+
.withArguments(new IntegerArgument("strength"))
827+
.executes((sender, args) -> {
828+
Player target = (Player) args.get("target");
829+
NamespacedKey potionKey = (NamespacedKey) args.get("potion");
830+
int duration = (int) args.get("duration");
831+
int strength = (int) args.get("strength");
832+
833+
PotionEffectType potion = PotionEffectType.getByKey(potionKey);
834+
835+
// Add the potion effect to the target player
836+
target.addPotionEffect(new PotionEffect(potion, duration, strength));
837+
})
838+
.register();
839+
/* ANCHOR_END: argumentPotion2 */
821840
}
822841

823842
@SuppressWarnings("null")

commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,25 @@ CommandAPICommand("potion")
751751
})
752752
.register()
753753
/* ANCHOR_END: argumentPotion1 */
754+
/* ANCHOR: argumentPotion2 */
755+
CommandAPICommand("potion")
756+
.withArguments(PlayerArgument("target"))
757+
.withArguments(PotionEffectArgument.NamespacedKey("potion"))
758+
.withArguments(TimeArgument("duration"))
759+
.withArguments(IntegerArgument("strength"))
760+
.executes(CommandExecutor { _, args ->
761+
val target = args["target"] as Player
762+
val potionKey = args["potion"] as NamespacedKey
763+
val duration = args["duration"] as Int
764+
val strength = args["strength"] as Int
765+
766+
val potion = PotionEffectType.getByKey(potionKey)!!
767+
768+
// Add the potion effect to the target player
769+
target.addPotionEffect(PotionEffect(potion, duration, strength))
770+
})
771+
.register()
772+
/* ANCHOR_END: argumentPotion2 */
754773
}
755774

756775
fun argument_primitives() {

commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/ExamplesKotlinDSL.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,25 @@ commandAPICommand("potion") {
624624
}
625625
}
626626
/* ANCHOR_END: argumentPotion1 */
627+
/* ANCHOR: argumentPotion2 */
628+
commandAPICommand("potion") {
629+
playerArgument("target")
630+
potionEffectArgument("potion", true)
631+
timeArgument("duration")
632+
integerArgument("strength")
633+
anyExecutor { _, args ->
634+
val target = args["target"] as Player
635+
val potionKey = args["potion"] as NamespacedKey
636+
val duration = args["duration"] as Int
637+
val strength = args["strength"] as Int
638+
639+
val potion = PotionEffectType.getByKey(potionKey)!!
640+
641+
// Add the potion effect to the target player
642+
target.addPotionEffect(PotionEffect(potion, duration, strength))
643+
}
644+
}
645+
/* ANCHOR_END: argumentPotion2 */
627646
}
628647

629648
fun argument_primitives() {

commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandAPICommandDSL.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ inline fun CommandAPICommand.lootTableArgument(nodeName: String, optional: Boole
9393
inline fun CommandAPICommand.mathOperationArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandAPICommand = withArguments(MathOperationArgument(nodeName).setOptional(optional).apply(block))
9494
inline fun CommandAPICommand.namespacedKeyArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandAPICommand = withArguments(NamespacedKeyArgument(nodeName).setOptional(optional).apply(block))
9595
inline fun CommandAPICommand.particleArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandAPICommand = withArguments(ParticleArgument(nodeName).setOptional(optional).apply(block))
96-
inline fun CommandAPICommand.potionEffectArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandAPICommand = withArguments(PotionEffectArgument(nodeName).setOptional(optional).apply(block))
96+
inline fun CommandAPICommand.potionEffectArgument(nodeName: String, useNamespacedKey: Boolean = false, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandAPICommand =
97+
if (useNamespacedKey) withArguments(PotionEffectArgument.NamespacedKey(nodeName).setOptional(optional).apply(block)) else withArguments(PotionEffectArgument(nodeName).setOptional(optional).apply(block))
9798
inline fun CommandAPICommand.recipeArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandAPICommand = withArguments(RecipeArgument(nodeName).setOptional(optional).apply(block))
9899

99100
inline fun CommandAPICommand.soundArgument(nodeName: String, useNamespacedKey: Boolean = false, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandAPICommand =

commandapi-kotlin/commandapi-bukkit-kotlin/src/main/kotlin/dev/jorel/commandapi/kotlindsl/CommandTreeDSL.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ inline fun CommandTree.lootTableArgument(nodeName: String, optional: Boolean = f
8686
inline fun CommandTree.mathOperationArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandTree = then(MathOperationArgument(nodeName).setOptional(optional).apply(block))
8787
inline fun CommandTree.namespacedKeyArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandTree = then(NamespacedKeyArgument(nodeName).setOptional(optional).apply(block))
8888
inline fun CommandTree.particleArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandTree = then(ParticleArgument(nodeName).setOptional(optional).apply(block))
89-
inline fun CommandTree.potionEffectArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandTree = then(PotionEffectArgument(nodeName).setOptional(optional).apply(block))
89+
inline fun CommandTree.potionEffectArgument(nodeName: String, useNamespacedKey: Boolean = false, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandTree =
90+
if (useNamespacedKey) then(PotionEffectArgument.NamespacedKey(nodeName).setOptional(optional).apply(block)) else then(PotionEffectArgument(nodeName).setOptional(optional).apply(block))
9091
inline fun CommandTree.recipeArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandTree = then(RecipeArgument(nodeName).setOptional(optional).apply(block))
9192

9293
inline fun CommandTree.soundArgument(nodeName: String, useNamespacedKey: Boolean = false, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): CommandTree =
@@ -191,7 +192,8 @@ inline fun Argument<*>.lootTableArgument(nodeName: String, optional: Boolean = f
191192
inline fun Argument<*>.mathOperationArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): Argument<*> = then(MathOperationArgument(nodeName).setOptional(optional).apply(block))
192193
inline fun Argument<*>.namespacedKeyArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): Argument<*> = then(NamespacedKeyArgument(nodeName).setOptional(optional).apply(block))
193194
inline fun Argument<*>.particleArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): Argument<*> = then(ParticleArgument(nodeName).setOptional(optional).apply(block))
194-
inline fun Argument<*>.potionEffectArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): Argument<*> = then(PotionEffectArgument(nodeName).setOptional(optional).apply(block))
195+
inline fun Argument<*>.potionEffectArgument(nodeName: String, useNamespacedKey: Boolean = false, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): Argument<*> =
196+
if (useNamespacedKey) then(PotionEffectArgument.NamespacedKey(nodeName).setOptional(optional).apply(block)) else then(PotionEffectArgument(nodeName).setOptional(optional).apply(block))
195197
inline fun Argument<*>.recipeArgument(nodeName: String, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): Argument<*> = then(RecipeArgument(nodeName).setOptional(optional).apply(block))
196198

197199
inline fun Argument<*>.soundArgument(nodeName: String, useNamespacedKey: Boolean = false, optional: Boolean = false, block: Argument<*>.() -> Unit = {}): Argument<*> =

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/arguments/ArgumentSubType.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,15 @@ public enum ArgumentSubType {
7171
* Multiple scoreholders. Returns a
7272
* {@link Collection}{@code <}{@link String}{@code >}
7373
*/
74-
SCOREHOLDER_MULTIPLE;
74+
SCOREHOLDER_MULTIPLE,
75+
76+
/**
77+
* A PotionEffectType. Returns a {@link org.bukkit.potion.PotionEffectType}
78+
*/
79+
POTION_EFFECT_POTION_EFFECT,
80+
81+
/**
82+
* A PotionEffectType. Returns a {@link org.bukkit.NamespacedKey}
83+
*/
84+
POTION_EFFECT_NAMESPACEDKEY;
7585
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/arguments/PotionEffectArgument.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.mojang.brigadier.exceptions.CommandSyntaxException;
2525
import dev.jorel.commandapi.CommandAPIBukkit;
2626
import dev.jorel.commandapi.executors.CommandArguments;
27+
import org.bukkit.NamespacedKey;
2728
import org.bukkit.potion.PotionEffectType;
2829

2930
/**
@@ -53,6 +54,44 @@ public CommandAPIArgumentType getArgumentType() {
5354

5455
@Override
5556
public <CommandSourceStack> PotionEffectType parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
56-
return CommandAPIBukkit.<CommandSourceStack>get().getPotionEffect(cmdCtx, key);
57+
return (PotionEffectType) CommandAPIBukkit.<CommandSourceStack>get().getPotionEffect(cmdCtx, key, ArgumentSubType.POTION_EFFECT_POTION_EFFECT);
5758
}
59+
60+
/**
61+
* An argument that represents the Bukkit PotionEffectType object
62+
*
63+
* @apiNote Returns a {@link org.bukkit.NamespacedKey}
64+
*/
65+
public static class NamespacedKey extends SafeOverrideableArgument<org.bukkit.NamespacedKey, org.bukkit.NamespacedKey> implements CustomProvidedArgument {
66+
67+
/**
68+
* Constructs a PotionEffectArgument with the given node name. This returns a {@link org.bukkit.NamespacedKey}
69+
*
70+
* @param nodeName The name of the node for this argument
71+
*/
72+
public NamespacedKey(String nodeName) {
73+
super(nodeName, CommandAPIBukkit.get()._ArgumentMinecraftKeyRegistered(), org.bukkit.NamespacedKey::toString);
74+
}
75+
76+
@Override
77+
public SuggestionProviders getSuggestionProvider() {
78+
return SuggestionProviders.POTION_EFFECTS;
79+
}
80+
81+
@Override
82+
public Class<org.bukkit.NamespacedKey> getPrimitiveType() {
83+
return org.bukkit.NamespacedKey.class;
84+
}
85+
86+
@Override
87+
public CommandAPIArgumentType getArgumentType() {
88+
return CommandAPIArgumentType.POTION_EFFECT;
89+
}
90+
91+
@Override
92+
public <CommandSourceStack> org.bukkit.NamespacedKey parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
93+
return (org.bukkit.NamespacedKey) CommandAPIBukkit.<CommandSourceStack>get().getPotionEffect(cmdCtx, key, ArgumentSubType.POTION_EFFECT_NAMESPACEDKEY);
94+
}
95+
}
96+
5897
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ Objective getObjective(CommandContext<CommandListenerWrapper> cmdCtx, String key
363363
OfflinePlayer getOfflinePlayer(CommandContext<CommandListenerWrapper> cmdCtx, String key)
364364
throws CommandSyntaxException;
365365

366-
PotionEffectType getPotionEffect(CommandContext<CommandListenerWrapper> cmdCtx, String key)
366+
Object getPotionEffect(CommandContext<CommandListenerWrapper> cmdCtx, String key, ArgumentSubType subType)
367367
throws CommandSyntaxException;
368368

369369
Recipe getRecipe(CommandContext<CommandListenerWrapper> cmdCtx, String key) throws CommandSyntaxException;

0 commit comments

Comments
 (0)