Skip to content

Commit

Permalink
Expression Registry access.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed Jul 14, 2024
1 parent 054b056 commit 44fb814
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 11 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@

User Changes:

* Added `arrayFindAny` and `arrayFindFirst` functions to expressions. As the name suggests, those functions will try to find an element of an array or return `null` if the array is empty.
* Added `arrayFindAny` and `arrayFindFirst` functions to expressions. As the name suggests, those functions will try to find an element of an array or return `null` if the array is empty.
* Java `Iterable`s can now be converted to arrays.
* Added registry access for expressions! Now you can access the game's static and dynamic content registries by using new `Registry` and `DynamicRegistry` functions.
* Why is this useful? For example, this allows you to compare item stacks based on their item type:
```
this_entity.getHandItems[0].getItem == Registry('item').chest
```
* For item and block registries there is a shortcut: `Item`, `Block`.
```
this_entity.getHandItems[0].getItem == Item('chest')
```
* `Biome` and `DimensionType` are available dynamic registry shortcuts.
10 changes: 10 additions & 0 deletions src/main/java/me/melontini/commander/impl/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.JsonParser;
import lombok.Cleanup;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import lombok.extern.log4j.Log4j2;
Expand All @@ -26,6 +27,7 @@
import me.melontini.dark_matter.api.data.loading.ServerReloadersEvent;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.minecraft.loot.condition.LootConditionType;
Expand All @@ -34,8 +36,10 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -63,6 +67,8 @@ public class Commander {

@Getter
private AmbiguousRemapper mappingKeeper;
@Getter @Setter
private @Nullable MinecraftServer currentServer;

public static Identifier id(String path) {
return new Identifier("commander", path);
Expand Down Expand Up @@ -115,6 +121,10 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
}

ServerReloadersEvent.EVENT.register(context -> context.register(new DynamicEventManager()));

ServerLifecycleEvents.SERVER_STARTING.register(server -> this.currentServer = server);
ServerLifecycleEvents.SERVER_STOPPING.register(server -> this.currentServer = null);

EvalUtils.init();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
import me.melontini.commander.impl.expression.functions.math.ClampFunction;
import me.melontini.commander.impl.expression.functions.math.LerpFunction;
import me.melontini.commander.impl.expression.functions.math.RangedRandomFunction;
import me.melontini.commander.impl.expression.functions.registry.DynamicRegistryFunction;
import me.melontini.commander.impl.expression.functions.registry.DynamicRegistryRegistryFunction;
import me.melontini.commander.impl.expression.functions.registry.RegistryFunction;
import me.melontini.commander.impl.mixin.evalex.EvaluationValueAccessor;
import me.melontini.commander.impl.mixin.evalex.ExpressionAccessor;
import me.melontini.commander.impl.mixin.evalex.ExpressionConfigurationAccessor;
import me.melontini.commander.impl.mixin.evalex.MapBasedFunctionDictionaryAccessor;
import net.minecraft.loot.context.LootContext;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -81,6 +86,14 @@ public class EvalUtils {

functions.put("structContainsKey", new StructContainsKeyFunction());
functions.put("hasContext", new HasContextFunction());

functions.put("Registry", new RegistryFunction(Registries.REGISTRIES));
functions.put("Item", new RegistryFunction(Registries.ITEM));
functions.put("Block", new RegistryFunction(Registries.BLOCK));

functions.put("DynamicRegistry", new DynamicRegistryRegistryFunction());
functions.put("Biome", new DynamicRegistryFunction(RegistryKeys.BIOME));
functions.put("DimensionType", new DynamicRegistryFunction(RegistryKeys.DIMENSION_TYPE));
builder.functionDictionary(SimpleFunctionDictionary.ofFunctions(functions));

CONFIGURATION = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public EvaluationValue get(Object key) {

@Override
public String toString() {
return "ReflectiveMapStructure{object=" + this.object + ";}";
return String.valueOf(this.object);
}

static final class Struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.data.conversion.*;
import me.melontini.commander.impl.expression.extensions.convert.LazyArrayConverter;
import me.melontini.commander.impl.expression.extensions.convert.RegistryAccessStruct;
import me.melontini.commander.impl.expression.extensions.convert.nbt.NbtConverter;
import net.minecraft.registry.Registry;

import java.util.Arrays;
import java.util.List;
Expand All @@ -31,6 +33,8 @@ public EvaluationValue convertObject(Object object, ExpressionConfiguration conf
if (converter.canConvert(object)) return converter.convert(object, configuration);
}

if (object instanceof Registry<?> reg) return EvaluationValue.structureValue(new RegistryAccessStruct(reg));

return EvaluationValue.structureValue(new ReflectiveMapStructure(object));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.melontini.commander.impl.expression.extensions.convert;

import com.ezylang.evalex.data.EvaluationValue;
import lombok.EqualsAndHashCode;
import me.melontini.commander.impl.expression.extensions.ProxyMap;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

@EqualsAndHashCode(callSuper = false)
public class RegistryAccessStruct extends ProxyMap {

private final Registry<?> registry;

public RegistryAccessStruct(Registry<?> registry) {
this.registry = registry;
}

@Override
public boolean containsKey(Object key) {
if (!(key instanceof String s)) return false;
return registry.containsId(new Identifier(s));
}

@Override
public EvaluationValue get(Object key) {
if (!(key instanceof String s)) return EvaluationValue.NULL_VALUE;
return convert(registry.get(new Identifier(s)));
}

@Override
public int size() {
return registry.size();
}

@Override
public String toString() {
return String.valueOf(registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public EvaluationValue get(Object key) {

@Override
public String toString() {
return "EntityAttributesStruct{" +
"container=" + container.toNbt() +
'}';
return String.valueOf(container.toNbt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public int size() {

@Override
public String toString() {
return "NbtCompoundStruct{" +
"compound=" + compound +
'}';
return String.valueOf(compound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public int size() {

@Override
public String toString() {
return "StateStruct{" +
"state=" + state.getEntries() +
'}';
return String.valueOf(state.getEntries());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.melontini.commander.impl.expression.functions.registry;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;
import me.melontini.commander.impl.Commander;
import me.melontini.commander.impl.expression.extensions.ProxyMap;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.util.Identifier;

import java.util.NoSuchElementException;

@FunctionParameter(name = "identifier")
public class DynamicRegistryFunction extends AbstractFunction {

protected final RegistryKey<? extends Registry<?>> registry;

public DynamicRegistryFunction(RegistryKey<? extends Registry<?>> registry) {
this.registry = registry;
}

@Override
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... par) throws EvaluationException {
try {
var id = new Identifier(par[0].getStringValue());
return ProxyMap.convert(Commander.get().currentServer().getRegistryManager().get(registry).getOrEmpty(id).orElseThrow(() -> new NoSuchElementException("No such %s: %s".formatted(registry.getValue(), id))));
} catch (Exception e) {
throw new EvaluationException(functionToken, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.melontini.commander.impl.expression.functions.registry;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;
import me.melontini.commander.impl.Commander;
import me.melontini.commander.impl.expression.extensions.ProxyMap;
import net.minecraft.registry.RegistryKey;
import net.minecraft.util.Identifier;

import java.util.NoSuchElementException;

@FunctionParameter(name = "identifier")
public class DynamicRegistryRegistryFunction extends AbstractFunction {

@Override
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... par) throws EvaluationException {
try {
var id = new Identifier(par[0].getStringValue());
return ProxyMap.convert(Commander.get().currentServer().getRegistryManager().getOptional(RegistryKey.ofRegistry(id)).orElseThrow(() -> new NoSuchElementException("No such %s: %s".formatted("minecraft:root", id))));
} catch (Exception e) {
throw new EvaluationException(functionToken, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.melontini.commander.impl.expression.functions.registry;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;
import me.melontini.commander.impl.expression.extensions.ProxyMap;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

import java.util.NoSuchElementException;

@FunctionParameter(name = "identifier")
public class RegistryFunction extends AbstractFunction {

private final Registry<?> registry;

public RegistryFunction(Registry<?> registry) {
this.registry = registry;
}

@Override
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... par) throws EvaluationException {
try {
var id = new Identifier(par[0].getStringValue());
return ProxyMap.convert(this.registry.getOrEmpty(id).orElseThrow(() -> new NoSuchElementException("No such %s: %s".formatted(registry.getKey().getValue(), id))));
} catch (Exception e) {
throw new EvaluationException(functionToken, e.getMessage());
}
}
}

0 comments on commit 44fb814

Please sign in to comment.