Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose minecraft brigadier argument types #6695

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.papermc.paper.brigadier;

import com.mojang.brigadier.arguments.ArgumentType;
import io.papermc.paper.brigadier.arguments.SimpleArgument;
import io.papermc.paper.brigadier.types.GameProfileResult;
import io.papermc.paper.brigadier.types.Position;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public abstract class MinecraftArgumentType implements Keyed {

protected static final Map<NamespacedKey, MinecraftArgumentType> ARGUMENT_TYPE_MAP = new HashMap<>();

public static final SimpleArgument<GameProfileResult> GAME_PROFILES = SimpleArgument.wrapper("game_profile", GameProfileResult.class);

public static final SimpleArgument<Position> BLOCK_POSITION = SimpleArgument.wrapper("block_pos", Position.class);

public static final SimpleArgument<Position> VEC3 = SimpleArgument.wrapper("vec3", Position.class);

public static final SimpleArgument<Position> VEC2 = SimpleArgument.wrapper("vec2", Position.class);



public static final SimpleArgument<TextColor> COLOR = SimpleArgument.wrapper("color", TextColor.class);



public static final SimpleArgument<Component> COMPONENT = SimpleArgument.wrapper("component", Component.class);

public static final SimpleArgument<BinaryTagHolder> NBT_COMPOUND_TAG = SimpleArgument.wrapper("nbt_compound_tag", BinaryTagHolder.class);



public static final SimpleArgument<Integer> INVENTORY_SLOT = SimpleArgument.wrapper("item_slot", Integer.class);

public static final SimpleArgument<NamespacedKey> RESOURCE_LOCATION = SimpleArgument.wrapper("resource_location", NamespacedKey.class);

public static final SimpleArgument<PotionEffectType> MOB_EFFECT = SimpleArgument.wrapper("mob_effect", PotionEffectType.class);



public static final SimpleArgument<Enchantment> ENCHANTMENT = SimpleArgument.wrapper("item_enchantment", Enchantment.class);

public static final SimpleArgument<EntityType> ENTITY_TYPE = SimpleArgument.wrapper("entity_summon", EntityType.class);



public static final SimpleArgument<Position> ROTATION = SimpleArgument.wrapper("rotation", Position.class);


private final NamespacedKey key;

protected MinecraftArgumentType(@NotNull String id) {
this.key = NamespacedKey.minecraft(id);
}

@Override
public @NotNull NamespacedKey getKey() {
return this.key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.papermc.paper.brigadier.arguments;

import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.brigadier.MinecraftArgumentType;
import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.NotNull;

public abstract class SimpleArgument<P> extends MinecraftArgumentType {

private final Class<P> paperType;

protected SimpleArgument(String id, Class<P> paperType) {
super(id);
this.paperType = paperType;
}

public abstract ArgumentType<P> create();

public abstract P getFromContext(@NotNull CommandContext<BukkitBrigadierCommandSource> context, @NotNull String name);

public @NotNull Class<P> getPaperType() {
return paperType;
}

public static <P> Wrapper<P> wrapper(@NotNull String id, Class<P> paperType) {
return new Wrapper<>(id, paperType);
}

public static class Wrapper<P> extends SimpleArgument<P> {

private SimpleArgument<P> delegate;

private Wrapper(String id, Class<P> paperType) {
super(id, paperType);
}

@Override
public ArgumentType<P> create() {
checkDelegate();
return this.delegate.create();
}

@Override
public P getFromContext(@NotNull CommandContext<BukkitBrigadierCommandSource> context, @NotNull String name) {
checkDelegate();
return this.delegate.getFromContext(context, name);
}

private void checkDelegate() {
if (this.delegate == null) {
this.delegate = (SimpleArgument<P>) ARGUMENT_TYPE_MAP.get(this.getKey());
if (this.delegate == null) {
throw new IllegalStateException("delegate was null for " + this.getKey());
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.papermc.paper.brigadier.types;

import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

public interface GameProfileResult<C extends BukkitBrigadierCommandSource> {

@NotNull Collection<PlayerProfile> getProfiles(@NotNull C source) throws CommandSyntaxException;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.papermc.paper.brigadier.types;

import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

public interface Position<C extends BukkitBrigadierCommandSource> {

@NotNull Vector getPositionVector(@NotNull C source);

float getXRot(@NotNull C source);

float getYRot(@NotNull C source);

@NotNull Location getLocation(@NotNull C source);

boolean isXRelative();

boolean isYRelative();

boolean isZRelative();
}
5 changes: 5 additions & 0 deletions build-data/paper.at
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,8 @@ public-f com.mojang.brigadier.tree.CommandNode requirement
# Block Enderpearl Travel Exploit
public net.minecraft.world.entity.projectile.Projectile cachedOwner
public net.minecraft.world.entity.projectile.Projectile ownerUUID

# Expose more conversions
public org.bukkit.craftbukkit.scoreboard.CraftScoreboardTranslations
public org.bukkit.craftbukkit.scoreboard.CraftScoreboardTranslations toBukkitSlot(I)Lorg/bukkit/scoreboard/DisplaySlot;
public org.bukkit.craftbukkit.scoreboard.CraftScoreboardTranslations fromBukkitSlot(Lorg/bukkit/scoreboard/DisplaySlot;)I
Loading