-
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.
- Loading branch information
Showing
12 changed files
with
178 additions
and
11 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
39 changes: 39 additions & 0 deletions
39
.../java/me/melontini/commander/impl/expression/extensions/convert/RegistryAccessStruct.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,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); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
...va/me/melontini/commander/impl/expression/functions/registry/DynamicRegistryFunction.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,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()); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...lontini/commander/impl/expression/functions/registry/DynamicRegistryRegistryFunction.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,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()); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...main/java/me/melontini/commander/impl/expression/functions/registry/RegistryFunction.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.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()); | ||
} | ||
} | ||
} |