Skip to content

Commit

Permalink
Improve cmd:arithmetica error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed Apr 27, 2024
1 parent e044994 commit 69a2556
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
### What's New:

* Added `hasContext` function to expressions. Allows checking if loot context has the required parameter: `if(hasContext("tool"), "Hi", "Bye")`
* Slightly improved error messages returned by `cmd:arithmetica`.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import me.melontini.dark_matter.api.data.codecs.ExtraCodecs;
import net.minecraft.loot.context.LootContextParameter;
import net.minecraft.util.Identifier;

import java.util.Objects;
import org.jetbrains.annotations.Nullable;

public class ExtractionTypes {

private static final BiMap<Identifier, LootContextParameter<?>> KNOWN_PARAMETERS = HashBiMap.create();
public static final Codec<LootContextParameter<?>> CODEC = ExtraCodecs.mapLookup(Identifier.CODEC, KNOWN_PARAMETERS);

public static LootContextParameter<?> getParameter(Identifier identifier) {
return Objects.requireNonNull(KNOWN_PARAMETERS.get(identifier), () -> "No such loot context parameter %s!".formatted(identifier));
public static @Nullable LootContextParameter<?> getParameter(Identifier identifier) {
return KNOWN_PARAMETERS.get(identifier);
}

public static void register(LootContextParameter<?> parameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,19 @@ public static class MapBasedDataAccessor implements DataAccessorIfc {

@Override
public @Nullable EvaluationValue getData(String variable) {
var id = new Identifier(variable.replace("__idcl__", ":"));
var r = Identifier.validate(variable.replace("__idcl__", ":"));
if (r.error().isPresent()) {
throw new CmdEvalException("%s - %s".formatted(variable.replace("__idcl__", ":"), r.error().orElseThrow().message()));
}

var id = r.result().orElseThrow();
var func = overrides.get(id);
if (func != null) return CONFIGURATION.getEvaluationValueConverter().convertObject(func.apply(LOCAL.get()), CONFIGURATION);

var object = LOCAL.get().get(ExtractionTypes.getParameter(id));
var param = ExtractionTypes.getParameter(id);
if (param == null) throw new CmdEvalException("%s is not a registered loot context parameter!".formatted(id));

var object = LOCAL.get().get(param);
if (object == null) return null;
return CONFIGURATION.getEvaluationValueConverter().convertObject(object, CONFIGURATION);
}
Expand Down

0 comments on commit 69a2556

Please sign in to comment.