This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split math expressions into Arithmetica
Add `cmd:arithmetica` brigadier command. Add `commander:arithmetica` loot number provider. Add `commander:print_arithmetica` event command. Narrow down selector context to just loot.
- Loading branch information
Showing
16 changed files
with
300 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/me/melontini/commander/builtin/brigadier/ArithmeticaCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package me.melontini.commander.builtin.brigadier; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.datafixers.util.Either; | ||
import me.melontini.commander.Commander; | ||
import me.melontini.commander.util.macro.PatternParser; | ||
import net.minecraft.loot.context.LootContext; | ||
import net.minecraft.loot.context.LootContextParameterSet; | ||
import net.minecraft.loot.context.LootContextParameters; | ||
import net.minecraft.loot.context.LootContextTypes; | ||
import net.minecraft.server.command.CommandManager; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.text.Text; | ||
|
||
public class ArithmeticaCommand { | ||
|
||
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { | ||
dispatcher.register(CommandManager.literal("cmd:arithmetica") | ||
.then(CommandManager.argument("expression", StringArgumentType.string()) | ||
.executes(context -> { | ||
try { | ||
String expression = StringArgumentType.getString(context, "expression"); | ||
|
||
var r = PatternParser.parseArithmetica(Either.right(expression)); | ||
if (r.error().isPresent()) { | ||
context.getSource().sendError(Text.literal(r.error().get().message())); | ||
return 0; | ||
} | ||
LootContext context1 = new LootContext.Builder(new LootContextParameterSet.Builder(context.getSource().getWorld()) | ||
.add(LootContextParameters.ORIGIN, context.getSource().getPosition()) | ||
.addOptional(LootContextParameters.THIS_ENTITY, context.getSource().getEntity()) | ||
.build(LootContextTypes.COMMAND)).build(null); | ||
|
||
context.getSource().sendMessage(Text.literal(String.valueOf(r.result().orElseThrow().asDouble(context1)))); | ||
return 1; | ||
} catch (Throwable t) { | ||
Commander.LOGGER.error(t); | ||
throw t; | ||
} | ||
}))); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/me/melontini/commander/builtin/commands/action/PrintArithmetica.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package me.melontini.commander.builtin.commands.action; | ||
|
||
import com.mojang.serialization.Codec; | ||
import me.melontini.commander.builtin.BuiltInCommands; | ||
import me.melontini.commander.command.Command; | ||
import me.melontini.commander.command.CommandType; | ||
import me.melontini.commander.event.EventContext; | ||
import me.melontini.commander.util.math.Arithmetica; | ||
|
||
public record PrintArithmetica(Arithmetica arithmetica) implements Command { | ||
|
||
public static final Codec<PrintArithmetica> CODEC = Arithmetica.CODEC.xmap(PrintArithmetica::new, PrintArithmetica::arithmetica).fieldOf("arithmetica").codec(); | ||
|
||
@Override | ||
public boolean execute(EventContext context) { | ||
System.out.println(arithmetica().apply(context.lootContext())); | ||
return true; | ||
} | ||
|
||
@Override | ||
public CommandType type() { | ||
return BuiltInCommands.PRINT_ARITHMETICA; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/me/melontini/commander/util/StdFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package me.melontini.commander.util; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import me.melontini.dark_matter.api.base.util.MathUtil; | ||
import net.minecraft.util.math.MathHelper; | ||
import net.objecthunter.exp4j.function.Function; | ||
|
||
import java.util.Set; | ||
|
||
public class StdFunctions { | ||
|
||
public static final Set<Function> FUNCTIONS = ImmutableSet.<Function>builder() | ||
.add(func("round", 1, args -> Math.round(args[0]))) | ||
.add(func("random", 2, args -> MathUtil.nextDouble(args[0], args[1]))) | ||
.add(func("clamp", 3, args -> MathHelper.clamp(args[0], args[1], args[2]))) | ||
.add(func("min", 2, args -> Math.min(args[0], args[1]))) | ||
.add(func("max", 2, args -> Math.max(args[0], args[1]))) | ||
.add(func("lerp", 3, args -> MathHelper.lerp(args[0], args[1], args[2]))) | ||
.build(); | ||
|
||
private static Function func(String name, int args, Calc calc) { | ||
return new Function(name, args) { | ||
@Override | ||
public double apply(double... args) { | ||
return calc.calc(args); | ||
} | ||
}; | ||
} | ||
|
||
private interface Calc { | ||
double calc(double... args); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
src/main/java/me/melontini/commander/util/macro/DynamicMacro.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package me.melontini.commander.util.macro; | ||
|
||
import me.melontini.commander.event.EventContext; | ||
import net.minecraft.loot.context.LootContext; | ||
|
||
import java.util.function.Function; | ||
|
||
public record DynamicMacro(String original, Function<EventContext, StringBuilder> start) implements BrigadierMacro { | ||
public record DynamicMacro(String original, Function<LootContext, StringBuilder> start) implements BrigadierMacro { | ||
|
||
public String build(EventContext context) { | ||
return start.apply(context).toString(); | ||
return start.apply(context.lootContext()).toString(); | ||
} | ||
} |
Oops, something went wrong.