code suggestions
This commit is contained in:
1 parent
238257f057
commit
e6158c1aab
5 files changed
+85
-17
No files matched your search
@@ -101,6 +101,7 @@ public class CarpetExpression
|
||||
private ServerCommandSource source;
|
||||
private BlockPos origin;
|
||||
private Expression expr;
|
||||
Expression getExpr() {return expr;}
|
||||
private static long tickStart = 0L;
|
||||
|
||||
private static boolean stopAll = false;
|
||||
@@ -766,6 +767,8 @@ public class CarpetExpression
|
||||
|
||||
this.expr.addLazyFunction("harvest", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size()<2)
|
||||
throw new InternalExpressionException("harvest takes at least 2 parameters: entity and block, or position, to harvest");
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
World world = cc.s.getWorld();
|
||||
Value entityValue = lv.get(0).evalValue(cc);
|
||||
@@ -791,14 +794,12 @@ public class CarpetExpression
|
||||
|
||||
this.expr.addLazyFunction("place_item", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size()<2)
|
||||
throw new InternalExpressionException("place_item takes at least 2 parameters: item and block, or position, to place onto");
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
String itemString = lv.get(0).evalValue(c).getString();
|
||||
BlockValue.VectorLocator locator = BlockValue.locateVec(cc, lv, 1);
|
||||
ItemStackArgument stackArg = NBTSerializableValue.parseItem(itemString);
|
||||
if (stackArg == null)
|
||||
{
|
||||
throw new InternalExpressionException("unknown item: "+itemString);
|
||||
}
|
||||
BlockPos where = new BlockPos(locator.vec);
|
||||
String facing = "up";
|
||||
if (lv.size() > locator.offset)
|
||||
@@ -876,6 +877,8 @@ public class CarpetExpression
|
||||
this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null)
|
||||
return (_c, _t) -> Value.NULL;
|
||||
if (lv.size() == inventoryLocator.offset)
|
||||
{
|
||||
List<Value> fullInventory = new ArrayList<>();
|
||||
@@ -897,6 +900,8 @@ public class CarpetExpression
|
||||
this.expr.addLazyFunction("inventory_drop", -1, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null)
|
||||
return (_c, _t) -> Value.NULL;
|
||||
if (lv.size() == inventoryLocator.offset)
|
||||
throw new InternalExpressionException("slot number is required for inventory_drop");
|
||||
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset).evalValue(c)).getLong();
|
||||
@@ -947,16 +952,14 @@ public class CarpetExpression
|
||||
this.expr.addLazyFunction("inventory_find", -1, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null)
|
||||
return (_c, _t) -> Value.NULL;
|
||||
ItemStackArgument itemArg = null;
|
||||
if (lv.size() > inventoryLocator.offset)
|
||||
{
|
||||
Value secondArg = lv.get(inventoryLocator.offset+0).evalValue(c);
|
||||
if (!(secondArg instanceof NullValue))
|
||||
{
|
||||
itemArg = NBTSerializableValue.parseItem(secondArg.getString());
|
||||
if (itemArg == null)
|
||||
throw new InternalExpressionException("Incorrect item format: "+secondArg.getString());
|
||||
}
|
||||
}
|
||||
int startIndex = 0;
|
||||
if (lv.size() > inventoryLocator.offset+1)
|
||||
@@ -980,7 +983,8 @@ public class CarpetExpression
|
||||
this.expr.addLazyFunction("inventory_set", -1, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
|
||||
if (inventoryLocator == null)
|
||||
return (_c, _t) -> Value.NULL;
|
||||
if (lv.size() < inventoryLocator.offset+2)
|
||||
throw new InternalExpressionException("inventory_set requires at least slot number and new stack size, and optional new item");
|
||||
int slot = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+0).evalValue(c)).getLong();
|
||||
@@ -1019,7 +1023,8 @@ public class CarpetExpression
|
||||
this.expr.addLazyFunction("inventory_remove", -1, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
|
||||
if (inventoryLocator == null)
|
||||
return (_c, _t) -> Value.NULL;
|
||||
if (lv.size() <= inventoryLocator.offset)
|
||||
throw new InternalExpressionException("inventory_set requires at least an item to be removed");
|
||||
ItemStackArgument searchItem = NBTSerializableValue.parseItem(lv.get(inventoryLocator.offset).evalValue(c).getString());
|
||||
|
||||
@@ -32,6 +32,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
@@ -350,6 +351,7 @@ public class Expression implements Cloneable
|
||||
boolean isAnOperator(String opname) { return operators.containsKey(opname) || operators.containsKey(opname+"u");}
|
||||
|
||||
private Map<String, ILazyFunction> functions = new HashMap<>();
|
||||
Set<String> getFunctionNames() {return functions.keySet();}
|
||||
|
||||
//static final Map<String, UserDefinedFunction> globalFunctions = new HashMap<>();
|
||||
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
package carpet.script;
|
||||
|
||||
import carpet.CarpetServer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* sole purpose of this package is to provide public access to package-private methods of Expression and CarpetExpression
|
||||
@@ -17,4 +24,29 @@ public class ExpressionInspector
|
||||
{
|
||||
return e.getName();
|
||||
}
|
||||
|
||||
private static TreeSet<String> scarpetFunctions;
|
||||
private static TreeSet<String> APIFunctions;
|
||||
static
|
||||
{
|
||||
Set<String> allFunctions = (new CarpetExpression("null", null, null)).getExpr().getFunctionNames();
|
||||
scarpetFunctions = new TreeSet<>(new Expression("null").getFunctionNames());
|
||||
APIFunctions = new TreeSet<>(allFunctions.stream().filter(s -> !scarpetFunctions.contains(s)).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
public static List<String> suggestFunctions(String previous, String prefix)
|
||||
{
|
||||
previous = previous.replace("\\'", "");
|
||||
int quoteCount = StringUtils.countMatches(previous,'\'');
|
||||
if (quoteCount % 2 == 1)
|
||||
return Collections.emptyList();
|
||||
int maxLen = prefix.length()<3 ? (prefix.length()*2+1) : 1234;
|
||||
List<String> scarpetMatches = scarpetFunctions.stream().
|
||||
filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList());
|
||||
scarpetMatches.addAll(APIFunctions.stream().
|
||||
filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList()));
|
||||
scarpetMatches.addAll(CarpetServer.scriptServer.globalHost.globalVariables.keySet().stream().
|
||||
filter(s -> s.startsWith(prefix)).collect(Collectors.toList()));
|
||||
return scarpetMatches;
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public class NBTSerializableValue extends Value
|
||||
else if (e instanceof VillagerEntity) inv = ((VillagerEntity) e).getInventory();
|
||||
|
||||
if (inv == null)
|
||||
throw new InternalExpressionException("Entity " + e + " has no inventory");
|
||||
return null;
|
||||
|
||||
return new InventoryLocator(e, e.getBlockPos(), inv, offset + 1);
|
||||
}
|
||||
@@ -59,10 +59,10 @@ public class NBTSerializableValue extends Value
|
||||
{
|
||||
BlockPos pos = ((BlockValue) v1).getPos();
|
||||
if (pos == null)
|
||||
throw new InternalExpressionException("Block to acess inventory needs to be positioned in the world");
|
||||
throw new InternalExpressionException("Block to access inventory needs to be positioned in the world");
|
||||
inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos);
|
||||
if (inv == null)
|
||||
throw new InternalExpressionException("Block at " + pos + " has no inventory");
|
||||
return null;
|
||||
return new InventoryLocator(pos, pos, inv, offset + 1);
|
||||
}
|
||||
else if (v1 instanceof ListValue)
|
||||
@@ -74,7 +74,7 @@ public class NBTSerializableValue extends Value
|
||||
NumericValue.asNumber(args.get(2)).getDouble());
|
||||
inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos);
|
||||
if (inv == null)
|
||||
throw new InternalExpressionException("Block at " + pos + " has no inventory");
|
||||
return null;
|
||||
return new InventoryLocator(pos, pos, inv, offset + 1);
|
||||
}
|
||||
BlockPos pos = new BlockPos(
|
||||
@@ -83,7 +83,7 @@ public class NBTSerializableValue extends Value
|
||||
NumericValue.asNumber(params.get(2 + offset).evalValue(c)).getDouble());
|
||||
inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos);
|
||||
if (inv == null)
|
||||
throw new InternalExpressionException("Block at " + pos + " has no inventory");
|
||||
return null;
|
||||
return new InventoryLocator(pos, pos, inv, offset + 3);
|
||||
}
|
||||
catch (IndexOutOfBoundsException e)
|
||||
@@ -109,7 +109,7 @@ public class NBTSerializableValue extends Value
|
||||
}
|
||||
catch (CommandSyntaxException e)
|
||||
{
|
||||
return null;
|
||||
throw new InternalExpressionException("Incorrect item: "+itemString);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user