added string ops and biome getter

This commit is contained in:
gnembon committed 2019-09-30 10:39:25 -03:00
1 parent a066c02469
commit 99af49661b
2 files changed
+28

No files matched your search

@@ -400,6 +400,8 @@ public class CarpetExpression
* place_item('piston,x,y,z,'down') // places a piston facing down
* place_item('carrot',x,y,z) // attempts to plant a carrot plant. Returns true if could place carrots at that position.
* </pre>
* <h3><code>biome(pos)</code></h3>
* <p>returns biome at that block position.</p>
* <h3><code>set_biome(pos, biome_name)</code></h3>
* <p>changes biome at that block position.</p>
* <h3><code>update(pos)</code></h3>
@@ -997,6 +999,16 @@ public class CarpetExpression
return (_c, _t) -> res;
});
this.expr.addLazyFunction("biome", -1, (c, t, lv) -> {
CarpetContext cc = (CarpetContext)c;
BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0);
ServerWorld world = cc.s.getWorld();
BlockPos pos = locator.block.getPos();
Biome biome = world.getBiome(pos);
Value res = new StringValue(biome.getTranslationKey().replaceFirst("^biome\\.minecraft\\.", ""));
return (_c, _t) -> res;
});
this.expr.addLazyFunction("set_biome", -1, (c, t, lv) -> {
CarpetContext cc = (CarpetContext)c;
BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0);
@@ -20,6 +20,7 @@ import carpet.script.value.ListValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import org.apache.commons.lang3.text.WordUtils;
import java.math.BigInteger;
import java.util.ArrayList;
@@ -2288,6 +2289,14 @@ public class Expression implements Cloneable
* <hr>
* <h2>Auxiliary functions</h2>
*
* <h3><code>lower(expr), uppper(expr), title(expr)</code></h3>
* <p>Returns lowercase, uppercase or titlecase representation of a string representation of the passed expression</p>
* <pre>
* lower('aBc') =&gt; 'abc'
* upper('aBc') =&gt; 'ABC'
* title('aBc') =&gt; 'Abc'
* </pre>
*
* <h3><code>length(expr)</code></h3>
* <p>Returns length of the expression, the length of the string,
* the length of the integer part of the number, or length of the list</p>
@@ -2483,6 +2492,13 @@ public class Expression implements Cloneable
}
});
addUnaryFunction("lower", v -> new StringValue(v.getString().toLowerCase(Locale.ROOT)));
addUnaryFunction("upper", v -> new StringValue(v.getString().toUpperCase(Locale.ROOT)));
addUnaryFunction("title", v -> new StringValue(WordUtils.capitalizeFully(v.getString())));
addUnaryFunction("type", v -> new StringValue(v.getTypeString()));
addUnaryFunction("length", v -> new NumericValue(v.length()));