format, fixes #254
This commit is contained in:
1 parent
0b72f85474
commit
7577c475f6
6 files changed
+177
-6
No files matched your search
@@ -23,6 +23,7 @@ import carpet.script.exception.ExpressionException;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.value.BlockValue;
|
||||
import carpet.script.value.EntityValue;
|
||||
import carpet.script.value.FormattedTextValue;
|
||||
import carpet.script.value.FunctionValue;
|
||||
import carpet.script.value.LazyListValue;
|
||||
import carpet.script.value.ListValue;
|
||||
@@ -2677,17 +2678,33 @@ public class CarpetExpression
|
||||
{
|
||||
ServerCommandSource s = ((CarpetContext)c).s;
|
||||
Value res = lv.get(0).evalValue(c);
|
||||
if (s.getEntity() instanceof PlayerEntity)
|
||||
if (res instanceof FormattedTextValue)
|
||||
{
|
||||
Messenger.m((PlayerEntity) s.getEntity(), "w "+ res.getString());
|
||||
s.sendFeedback(((FormattedTextValue) res).getText(), false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Messenger.m(s, "w "+ res.getString());
|
||||
if (s.getEntity() instanceof PlayerEntity)
|
||||
{
|
||||
Messenger.m((PlayerEntity) s.getEntity(), "w " + res.getString());
|
||||
}
|
||||
else
|
||||
{
|
||||
Messenger.m(s, "w " + res.getString());
|
||||
}
|
||||
}
|
||||
return (_c, _t) -> res; // pass through for variables
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("format", -1, (c, t, lv) -> {
|
||||
if (lv.size() == 0 ) throw new InternalExpressionException("'format' requires at least one component");
|
||||
List<Value> values = lv.stream().map(lazy -> lazy.evalValue(c)).collect(Collectors.toList());
|
||||
if (values.get(0) instanceof ListValue && values.size()==1)
|
||||
values = ((ListValue) values.get(0)).getItems();
|
||||
Value ret = new FormattedTextValue(Messenger.c(values.stream().map(Value::getString).toArray()));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
//"overidden" native call to cancel if on main thread
|
||||
this.expr.addLazyFunction("task_join", 1, (c, t, lv) -> {
|
||||
if (((CarpetContext)c).s.getMinecraftServer().isOnThread())
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import net.minecraft.text.BaseText;
|
||||
|
||||
public class FormattedTextValue extends StringValue
|
||||
{
|
||||
BaseText text;
|
||||
public FormattedTextValue(BaseText text)
|
||||
{
|
||||
super(null);
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString() {
|
||||
return text.getString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean() {
|
||||
return text.getSiblings().size() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value clone()
|
||||
{
|
||||
return new FormattedTextValue(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeString()
|
||||
{
|
||||
return "text";
|
||||
}
|
||||
|
||||
public BaseText getText()
|
||||
{
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -116,10 +116,14 @@ public class Messenger
|
||||
{
|
||||
message = "w"+message;
|
||||
}
|
||||
String[] parts = message.split("\\s", 2);
|
||||
String desc = parts[0];
|
||||
int limit = message.indexOf(' ');
|
||||
String desc = message;
|
||||
String str = "";
|
||||
if (parts.length > 1) str = parts[1];
|
||||
if (limit >= 0)
|
||||
{
|
||||
desc = message.substring(0, limit);
|
||||
str = message.substring(limit+1);
|
||||
}
|
||||
if (desc.charAt(0) == '/') // deprecated
|
||||
{
|
||||
if (previous_message != null)
|
||||
|
||||
Reference in new issue
Block a user