Skip to content

Commit

Permalink
Clean up api package.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed Apr 17, 2024
1 parent 0a212a1 commit eaaccbc
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import me.melontini.commander.api.event.EventContext;
import me.melontini.commander.api.event.EventType;
import me.melontini.commander.impl.command.ConditionedCommand;
import net.minecraft.util.Identifier;


/**
* Base command interface to be implemented on your command classes.
* <p>Commands along with their {@link Codec} must be registered with {@link CommandType#register(Identifier, Codec)}.</p>
*/
public interface Command {

Codec<Conditioned> CODEC = (Codec<Conditioned>) ConditionedCommand.CODEC;
Expand All @@ -18,6 +22,9 @@ default DataResult<Void> validate(EventType type) {
return DataResult.success(null);
}

/**
* Executable command proxy. This interface is to be used when you nest additional commands in your base command.
*/
interface Conditioned {
boolean execute(EventContext context);
DataResult<Void> validate(EventType type);
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/me/melontini/commander/api/command/Selector.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.function.Function;

public interface Selector {
/**
* Base selector function. Selectors transform input {@link LootContext} parameters into {@link ServerCommandSource}.
* <p>Selectors must be registered with {@link Selector#register(Identifier, Selector)}</p>
*/
public interface Selector extends Function<LootContext, ServerCommandSource> {

Codec<Conditioned> CODEC = (Codec<Conditioned>) ConditionedSelector.CODEC;

static Selector register(Identifier identifier, Selector selector) {
return SelectorTypes.register(identifier, selector);
}

@Override
default ServerCommandSource apply(LootContext context) {
return this.select(context);
}

@Nullable ServerCommandSource select(LootContext context);

/**
* Executable selector proxy.
*/
interface Conditioned {
Optional<ServerCommandSource> select(EventContext context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import me.melontini.commander.impl.event.EventContextImpl;
import net.minecraft.loot.context.LootContext;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

public interface EventContext {

static EventContext.Builder builder(EventType type) {
@Contract("_ -> new")
static EventContext.@NotNull Builder builder(EventType type) {
return new EventContextImpl.Builder(type);
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/me/melontini/commander/api/event/EventKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import me.melontini.commander.impl.Commander;
import net.minecraft.loot.context.LootContext;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -20,7 +22,8 @@ public final class EventKey<T> {

private final Identifier id;

public static <T> EventKey<T> create(Identifier id) {
@Contract("_ -> new")
public static <T> @NotNull EventKey<T> create(Identifier id) {
return new EventKey<>(id);
}
}
9 changes: 9 additions & 0 deletions src/main/java/me/melontini/commander/api/event/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@

import static me.melontini.commander.impl.Commander.id;

/**
* Event types define basic information about events, like the return type, additional parameters and the finalizer.
*
* <p>- {@link EventType#EXTENSION} allows you to pass additional parameters to the subscription and {@link EventType#FINALIZER} allows you to process those parameters.</p>
* <p>- {@link EventType#CANCEL_TERM} defines if the {@code commander:cancel} command is supported by this event.</p>
*/
public interface EventType extends Context {

/**
* Dummy event type. Can be used to execute commands outside of events, or to replace subscriptions in JSON.
*/
EventType NULL = EventType.builder().extension(null, subscriptions -> null).build(id("none"));

Context.Key<Codec<?>> EXTENSION = Context.key("extension");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,42 @@

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import me.melontini.commander.api.util.functions.ToDoubleFunction;
import me.melontini.commander.impl.util.eval.EvalUtils;
import me.melontini.dark_matter.api.data.codecs.ExtraCodecs;
import net.minecraft.loot.context.LootContext;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.function.ToDoubleFunction;

/**
* A simple {@code context -> double} functions, which is encoded as either a double or an expression.
* <p>Can be used as a substitute for {@link Codec#DOUBLE} if {@link LootContext} is available</p>
*/
public interface Arithmetica extends ToDoubleFunction<LootContext> {

Codec<Arithmetica> CODEC = ExtraCodecs.either(Codec.DOUBLE, Codec.STRING).comapFlatMap(EvalUtils::parseEither, Arithmetica::toSource);

default long asLong(LootContext context) {
return (long) this.apply(context);
return (long) this.applyAsDouble(context);
}

default int asInt(LootContext context) {
return (int) this.apply(context);
return (int) this.applyAsDouble(context);
}

default float asFloat(LootContext context) {
return (float) this.apply(context);
return (float) this.applyAsDouble(context);
}

default double asDouble(LootContext context) {
return this.apply(context);
return this.applyAsDouble(context);
}

Either<Double, String> toSource();

static Arithmetica constant(double d) {
@Contract("_ -> new")
static @NotNull Arithmetica constant(double d) {
Either<Double, String> either = Either.left(d);
return new Arithmetica() {
@Override
Expand All @@ -38,13 +46,14 @@ public Either<Double, String> toSource() {
}

@Override
public double apply(LootContext context) {
public double applyAsDouble(LootContext context) {
return d;
}
};
}

static Arithmetica of(ToDoubleFunction<LootContext> function, String expression) {
@Contract("_, _ -> new")
static @NotNull Arithmetica of(ToDoubleFunction<LootContext> function, String expression) {
Either<Double, String> either = Either.right(expression);
return new Arithmetica() {
@Override
Expand All @@ -53,8 +62,8 @@ public Either<Double, String> toSource() {
}

@Override
public double apply(LootContext context) {
return function.apply(context);
public double applyAsDouble(LootContext context) {
return function.applyAsDouble(context);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@
import me.melontini.commander.impl.util.macro.PatternParser;
import net.minecraft.loot.context.LootContext;

public interface BrigadierMacro {
import java.util.function.Function;

/**
* A special type of string function with support for {@code ${{}}} macros.
* <p>The main purpose is to enable command macros in {@code commander:commands}, but can be used anywhere else.</p>
*/
public interface BrigadierMacro extends Function<LootContext, String> {

Codec<BrigadierMacro> CODEC = Codec.STRING.comapFlatMap(BrigadierMacro::parse, BrigadierMacro::original);

static DataResult<BrigadierMacro> parse(String input) {
return PatternParser.parse(input);
}

@Override
default String apply(LootContext context) {
return this.build(context);
}

String build(LootContext context);
String original();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import me.melontini.commander.impl.event.data.types.ExtractionTypes;
import net.minecraft.loot.context.LootContextParameter;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* A registry for {@link LootContextParameter}. Parameters already come with an identifier, so specifying it separately is unnecessary.
*/
@ApiStatus.Experimental
public class LootContextParameterRegistry {

public static void register(LootContextParameter<?>... parameters) {
public static void register(LootContextParameter<?> @NotNull ... parameters) {
for (LootContextParameter<?> parameter : parameters) {
ExtractionTypes.register(parameter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import net.minecraft.loot.context.LootContext;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Supplier;

@UtilityClass
public class EventExecutors {
public static void runVoid(EventType type, World world, Supplier<LootContext> supplier) {
public static void runVoid(EventType type, @NotNull World world, Supplier<LootContext> supplier) {
if (world.isClient()) return;

List<Command.Conditioned> subscribers = Subscription.getData(MakeSure.notNull(world.getServer()), type);
Expand All @@ -28,7 +29,7 @@ public static void runVoid(EventType type, World world, Supplier<LootContext> su
for (Command.Conditioned subscriber : subscribers) subscriber.execute(context);
}

public static boolean runBoolean(EventType type, boolean def, World world, Supplier<LootContext> supplier) {
public static boolean runBoolean(EventType type, boolean def, @NotNull World world, Supplier<LootContext> supplier) {
if (world.isClient()) return def;

List<Command.Conditioned> subscribers = Subscription.getData(MakeSure.notNull(world.getServer()), type);
Expand All @@ -49,7 +50,7 @@ public static boolean runBoolean(EventType type, World world, Supplier<LootConte
return runBoolean(type, true, world, supplier);
}

public static <T extends Enum<T>> T runEnum(EventType type, T def, World world, Supplier<LootContext> supplier) {
public static <T extends Enum<T>> T runEnum(EventType type, T def, @NotNull World world, Supplier<LootContext> supplier) {
if (world.isClient()) return def;

List<Command.Conditioned> subscribers = Subscription.getData(MakeSure.notNull(world.getServer()), type);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit eaaccbc

Please sign in to comment.