This commit is contained in:
gnembon committed 2019-06-29 20:39:41 -03:00
1 parent 83a8f35094
commit 5bb813be76
7 files changed
+41 -37

No files matched your search

@@ -44,27 +44,25 @@ import static net.minecraft.server.command.CommandSource.suggestMatching;
public class ScriptCommand public class ScriptCommand
{ {
private static CompletableFuture<Suggestions> suggestCode(SuggestionsBuilder suggestionsBuilder) { private static CompletableFuture<Suggestions> suggestCode(
CommandContext<ServerCommandSource> context,
SuggestionsBuilder suggestionsBuilder
)
{
ScriptHost currentHost = getHost(context);
String previous = suggestionsBuilder.getRemaining().toLowerCase(Locale.ROOT); String previous = suggestionsBuilder.getRemaining().toLowerCase(Locale.ROOT);
int strlen = previous.length(); int strlen = previous.length();
StringBuilder lastToken = new StringBuilder(); StringBuilder lastToken = new StringBuilder();
for (int idx = strlen-1; idx >=0; idx--) for (int idx = strlen-1; idx >=0; idx--)
{ {
char ch = previous.charAt(idx); char ch = previous.charAt(idx);
if (Character.isLetterOrDigit(ch) || ch == '_') if (Character.isLetterOrDigit(ch) || ch == '_') lastToken.append(ch); else break;
{
lastToken.append(ch);
}
else
{
break;
}
} }
if (lastToken.length() == 0) if (lastToken.length() == 0)
return suggestionsBuilder.buildFuture(); return suggestionsBuilder.buildFuture();
String prefix = lastToken.reverse().toString(); String prefix = lastToken.reverse().toString();
String previousString = previous.substring(0,previous.length()-prefix.length()) ; String previousString = previous.substring(0,previous.length()-prefix.length()) ;
ExpressionInspector.suggestFunctions(previousString, prefix).forEach(text -> suggestionsBuilder.suggest(previousString+text)); ExpressionInspector.suggestFunctions(currentHost, previousString, prefix).forEach(text -> suggestionsBuilder.suggest(previousString+text));
return suggestionsBuilder.buildFuture(); return suggestionsBuilder.buildFuture();
} }
@@ -78,7 +76,7 @@ public class ScriptCommand
executes( (cc) -> { CarpetServer.scriptServer.stopAll = false; return 1;}); executes( (cc) -> { CarpetServer.scriptServer.stopAll = false; return 1;});
LiteralArgumentBuilder<ServerCommandSource> l = literal("run"). LiteralArgumentBuilder<ServerCommandSource> l = literal("run").
requires((player) -> player.hasPermissionLevel(2)). requires((player) -> player.hasPermissionLevel(2)).
then(argument("expr", StringArgumentType.greedyString()).suggests((cc, bb) -> suggestCode(bb)). then(argument("expr", StringArgumentType.greedyString()).suggests((cc, bb) -> suggestCode(cc, bb)).
executes((cc) -> compute( executes((cc) -> compute(
cc, cc,
StringArgumentType.getString(cc, "expr")))); StringArgumentType.getString(cc, "expr"))));
@@ -141,7 +139,7 @@ public class ScriptCommand
then(argument("from", BlockPosArgumentType.create()). then(argument("from", BlockPosArgumentType.create()).
then(argument("to", BlockPosArgumentType.create()). then(argument("to", BlockPosArgumentType.create()).
then(argument("expr", StringArgumentType.greedyString()). then(argument("expr", StringArgumentType.greedyString()).
suggests((cc, bb) -> suggestCode(bb)). suggests((cc, bb) -> suggestCode(cc, bb)).
executes( (cc) -> scriptScan( executes( (cc) -> scriptScan(
cc, cc,
BlockPosArgumentType.getBlockPos(cc, "origin"), BlockPosArgumentType.getBlockPos(cc, "origin"),
@@ -866,7 +866,6 @@ public class CarpetExpression
BlockSoundGroup blockSoundGroup = placementState.getSoundGroup(); BlockSoundGroup blockSoundGroup = placementState.getSoundGroup();
cc.s.getWorld().playSound(null, where, blockSoundGroup.getPlaceSound(), SoundCategory.BLOCKS, (blockSoundGroup.getVolume() + 1.0F) / 2.0F, blockSoundGroup.getPitch() * 0.8F); cc.s.getWorld().playSound(null, where, blockSoundGroup.getPlaceSound(), SoundCategory.BLOCKS, (blockSoundGroup.getVolume() + 1.0F) / 2.0F, blockSoundGroup.getPitch() * 0.8F);
return (_c, _t) -> Value.TRUE; return (_c, _t) -> Value.TRUE;
} }
} }
} }
@@ -907,13 +906,15 @@ public class CarpetExpression
public void API_InventoryManipulation() public void API_InventoryManipulation()
{ {
this.expr.addLazyFunction("stack_limit", 1, (c, t, lv) -> { this.expr.addLazyFunction("stack_limit", 1, (c, t, lv) ->
{
ItemStackArgument item = NBTSerializableValue.parseItem(lv.get(0).evalValue(c).getString()); ItemStackArgument item = NBTSerializableValue.parseItem(lv.get(0).evalValue(c).getString());
Value res = new NumericValue(item.getItem().getMaxCount()); Value res = new NumericValue(item.getItem().getMaxCount());
return (_c, _t) -> res; return (_c, _t) -> res;
}); });
this.expr.addLazyFunction("inventory_size", -1, (c, t, lv) -> { this.expr.addLazyFunction("inventory_size", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
if (inventoryLocator == null) if (inventoryLocator == null)
@@ -923,7 +924,8 @@ public class CarpetExpression
}); });
//inventory_get(<b, e>, <n>) -> item_triple //inventory_get(<b, e>, <n>) -> item_triple
this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) -> { this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
if (inventoryLocator == null) if (inventoryLocator == null)
@@ -947,7 +949,8 @@ public class CarpetExpression
}); });
//inventory_drop(<b, e>, <n>, <amount=1, 0-whatever's there>) -> entity_item (and sets slot) or null if cannot //inventory_drop(<b, e>, <n>, <amount=1, 0-whatever's there>) -> entity_item (and sets slot) or null if cannot
this.expr.addLazyFunction("inventory_drop", -1, (c, t, lv) -> { this.expr.addLazyFunction("inventory_drop", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
if (inventoryLocator == null) if (inventoryLocator == null)
@@ -1000,7 +1003,8 @@ public class CarpetExpression
}); });
//inventory_find(<b, e>, <item> or null (first empty slot), <start_from=0> ) -> <N> or null //inventory_find(<b, e>, <item> or null (first empty slot), <start_from=0> ) -> <N> or null
this.expr.addLazyFunction("inventory_find", -1, (c, t, lv) -> { this.expr.addLazyFunction("inventory_find", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
if (inventoryLocator == null) if (inventoryLocator == null)
@@ -1031,7 +1035,8 @@ public class CarpetExpression
}); });
//inventory_set(<b,e>, <n>, <count>, <item>, <nbt>) //inventory_set(<b,e>, <n>, <count>, <item>, <nbt>)
this.expr.addLazyFunction("inventory_set", -1, (c, t, lv) -> { this.expr.addLazyFunction("inventory_set", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
if (inventoryLocator == null) if (inventoryLocator == null)
+10 -11
View File
@@ -1840,11 +1840,10 @@ public class Expression implements Cloneable
return LazyListValue.range(from, to, step); return LazyListValue.range(from, to, step);
}); });
addBinaryFunction("get", (v1, v2) -> { addBinaryFunction("get", (v1, v2) ->
{
if (v1 instanceof LazyListValue || !(v1 instanceof ListValue)) if (v1 instanceof LazyListValue || !(v1 instanceof ListValue))
{
throw new InternalExpressionException("First argument of element should be a list"); throw new InternalExpressionException("First argument of element should be a list");
}
List<Value> items = ((ListValue)v1).getItems(); List<Value> items = ((ListValue)v1).getItems();
long index = NumericValue.asNumber(v2).getLong(); long index = NumericValue.asNumber(v2).getLong();
int numitems = items.size(); int numitems = items.size();
@@ -1854,12 +1853,11 @@ public class Expression implements Cloneable
return items.get((int)index); return items.get((int)index);
}); });
//Deprecated //Deprecated, use "get" instead
addBinaryFunction("element", (v1, v2) -> { addBinaryFunction("element", (v1, v2) ->
{
if (v1 instanceof LazyListValue || !(v1 instanceof ListValue)) if (v1 instanceof LazyListValue || !(v1 instanceof ListValue))
{
throw new InternalExpressionException("First argument of element should be a list"); throw new InternalExpressionException("First argument of element should be a list");
}
List<Value> items = ((ListValue)v1).getItems(); List<Value> items = ((ListValue)v1).getItems();
long index = NumericValue.asNumber(v2).getLong(); long index = NumericValue.asNumber(v2).getLong();
int numitems = items.size(); int numitems = items.size();
@@ -1868,6 +1866,7 @@ public class Expression implements Cloneable
index = index % numitems; index = index % numitems;
return items.get((int)index); return items.get((int)index);
}); });
addFunction("put", (lv) -> addFunction("put", (lv) ->
{ {
if(lv.size()<3) if(lv.size()<3)
@@ -2777,10 +2776,10 @@ public class Expression implements Cloneable
{ {
return exit.retval; return exit.retval;
} }
//catch (StackOverflowError ignored) catch (StackOverflowError ignored)
//{ {
// throw new ExpressionException("Your thoughts are too deep"); throw new ExpressionException("Your thoughts are too deep");
//} }
catch (InternalExpressionException exc) catch (InternalExpressionException exc)
{ {
throw new ExpressionException("Your expression result is incorrect:"+exc.getMessage()); throw new ExpressionException("Your expression result is incorrect:"+exc.getMessage());
@@ -34,7 +34,7 @@ public class ExpressionInspector
APIFunctions = new TreeSet<>(allFunctions.stream().filter(s -> !scarpetFunctions.contains(s)).collect(Collectors.toSet())); APIFunctions = new TreeSet<>(allFunctions.stream().filter(s -> !scarpetFunctions.contains(s)).collect(Collectors.toSet()));
} }
public static List<String> suggestFunctions(String previous, String prefix) public static List<String> suggestFunctions(ScriptHost host, String previous, String prefix)
{ {
previous = previous.replace("\\'", ""); previous = previous.replace("\\'", "");
int quoteCount = StringUtils.countMatches(previous,'\''); int quoteCount = StringUtils.countMatches(previous,'\'');
@@ -45,7 +45,12 @@ public class ExpressionInspector
filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList()); filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList());
scarpetMatches.addAll(APIFunctions.stream(). scarpetMatches.addAll(APIFunctions.stream().
filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList())); filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> s+"(").collect(Collectors.toList()));
scarpetMatches.addAll(CarpetServer.scriptServer.globalHost.globalVariables.keySet().stream(). // not that useful in commandline, more so in external scripts, so skipping here
//scarpetMatches.addAll(CarpetServer.scriptServer.events.eventHandlers.keySet().stream().
// filter(s -> s.startsWith(prefix) && s.length() <= maxLen).map(s -> "__"+s+"(").collect(Collectors.toList()));
scarpetMatches.addAll(host.globalFunctions.keySet().stream().
filter(s -> s.startsWith(prefix)).map(s -> s+"(").collect(Collectors.toList()));
scarpetMatches.addAll(host.globalVariables.keySet().stream().
filter(s -> s.startsWith(prefix)).collect(Collectors.toList())); filter(s -> s.startsWith(prefix)).collect(Collectors.toList()));
return scarpetMatches; return scarpetMatches;
} }
@@ -163,13 +163,10 @@ public class NBTSerializableValue extends Value
public boolean equals(final Value o) public boolean equals(final Value o)
{ {
if (o instanceof NBTSerializableValue) if (o instanceof NBTSerializableValue)
{
return getTag().equals(((NBTSerializableValue) o).getTag()); return getTag().equals(((NBTSerializableValue) o).getTag());
}
return super.equals(o); return super.equals(o);
} }
@Override @Override
public String getString() public String getString()
{ {
@@ -204,7 +204,7 @@ public class CarpetSettings
@Rule(desc = "Enables /script command", extra = "An in-game scripting API for Scarpet programming language", category = COMMAND) @Rule(desc = "Enables /script command", extra = "An in-game scripting API for Scarpet programming language", category = COMMAND)
public static boolean commandScript = true; public static boolean commandScript = true;
@Rule(desc = "Enables /player command to control/spawn players", category = COMMAND) @Rule(desc = "Enables /player command to control/spawn players", category = COMMAND, validate = Validator.WIP.class)
public static boolean commandPlayer = true; public static boolean commandPlayer = true;
@Rule(desc = "Placing carpets may issue carpet commands for non-op players", category = SURVIVAL) @Rule(desc = "Placing carpets may issue carpet commands for non-op players", category = SURVIVAL)
+1 -1
View File
@@ -47,7 +47,7 @@ public abstract class Validator<T>
if (source != null) if (source != null)
try try
{ {
Messenger.m(source, "r Work in progress - limited or no functionality available for "+currentRule.name); Messenger.m(source, "r "+currentRule.name+" not yet fully implemented - we are still working on it.");
} }
catch (NullPointerException ignored) { } catch (NullPointerException ignored) { }
return newValue; return newValue;