Merge branch 'master' into 1.17

This commit is contained in:
gnembon committed 2021-04-26 23:29:27 -03:00
commit 988357317a
13 files changed
+111 -8

No files matched your search

+4
View File
@@ -422,6 +422,10 @@ Numbers related to player's xp. `xp` is the overall xp player has, `xp_level` is
Number indicating remaining entity health, or `null` if not applicable. Number indicating remaining entity health, or `null` if not applicable.
### `query(e, 'language')`
Returns `null` for any non-player entity, if not returns the player's language as a string.
### `query(e, 'holds', slot?)` ### `query(e, 'holds', slot?)`
Returns triple of short name, stack count, and NBT of item held in `slot`, or `null` if nothing or not applicable. Available options for `slot` are: Returns triple of short name, stack count, and NBT of item held in `slot`, or `null` if nothing or not applicable. Available options for `slot` are:
@@ -267,6 +267,7 @@ but like everywhere else, doing that sounds like a bad idea.
- `io_exception`: This is the parent for any exception that occurs due to an error handling external data. - `io_exception`: This is the parent for any exception that occurs due to an error handling external data.
- `nbt_error`: Incorrect input/output NBT file. - `nbt_error`: Incorrect input/output NBT file.
- `json_error`: Incorrect input/output JSON data. - `json_error`: Incorrect input/output JSON data.
- `b64_error`: Incorrect input/output b64 (base 64) string
- `user_exception`: Exception thrown by default with `throw` function. - `user_exception`: Exception thrown by default with `throw` function.
Synopsis: Synopsis:
@@ -146,3 +146,4 @@ it will be used from now on as a new value for the accumulator.
reduce([1,2,3,4],_a+_,0) => 10 reduce([1,2,3,4],_a+_,0) => 10
reduce([1,2,3,4],_a*_,1) => 24 reduce([1,2,3,4],_a*_,1) => 24
</pre> </pre>
+25 -7
View File
@@ -243,16 +243,34 @@ identical with `fun(1, 2, 3)`. For maps, it unpacks them to a list of key-value
In function signatures it identifies a vararg parameter. In function signatures it identifies a vararg parameter.
`fun(a, b, ... rest) -> [a, b, rest]; fun(1, 2, 3, 4)` => `[1, 2, [3, 4]]` <pre>
fun(a, b, ... rest) -> [a, b, rest]; fun(1, 2, 3, 4) => [1, 2, [3, 4]]
</pre>
Effects of `...` can be surprisingly lasting. It is kept through the use of variables and function calls. Effects of `...` can be surprisingly lasting. It is kept through the use of variables and function calls.
`fun(a, b, ... rest) -> [a, b, ... rest]; fun(1, 2, 3, 4)` => `[1, 2, 3, 4]` <pre>
`args() -> ... [1, 2, 3]; sum(a, b, c) -> a+b+c; sum(args())` => `6` fun(a, b, ... rest) -> [a, b, ... rest]; fun(1, 2, 3, 4) => [1, 2, 3, 4]
`a = ... [1, 2, 3]; sum(a, b, c) -> a+b+c; sum(a)` => `6` args() -> ... [1, 2, 3]; sum(a, b, c) -> a+b+c; sum(args()) => 6
a = ... [1, 2, 3]; sum(a, b, c) -> a+b+c; sum(a) => 6
</pre>
Unpacking mechanics can be used for list and map constriction, not just for function calls. Unpacking mechanics can be used for list and map constriction, not just for function calls.
`[...range(5), pi, ...range(5,-1,-1)]` => `[0, 1, 2, 3, 4, 3.14159265359, 5, 4, 3, 2, 1, 0]` <pre>
`{ ... map(range(5), _ -> _*_ )}` => `{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}` [...range(5), pi, ...range(5,-1,-1)] => [0, 1, 2, 3, 4, 3.14159265359, 5, 4, 3, 2, 1, 0]
`{...{1 -> 2, 3 -> 4}, ...{5 -> 6, 7 -> 8}}` => `{1: 2, 3: 4, 5: 6, 7: 8}` { ... map(range(5), _ -> _*_ )} => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
{...{1 -> 2, 3 -> 4}, ...{5 -> 6, 7 -> 8}} => {1: 2, 3: 4, 5: 6, 7: 8}
</pre>
Fine print: unpacking of argument lists happens just before functions are evaluated.
This means that in some situations, for instance
when an expression is expected (`map(list, expr)`), or a function should not evaluate some (most!) of its arguments (`if(...)`),
unpacking cannot be used, and will be ignored, leaving `... list` identical to `list`.
Functions that don't honor unpacking mechanics, should have no use for it at the first place
(i.e. have one, or very well-defined, and very specific parameters),
so some caution (prior testing) is advised. Some of these multi-argument built-in functions are
`if`, `try`, `sort_key`, `system_variable_get`, `synchronize`, `sleep`, `in_dimension`,
all container functions (`get`, `has`, `put`, `delete`),
and all loop functions (`while`, `loop`, `map`, `filter`, `first`, `all`, `c_for`, `for` and`reduce`).
+9
View File
@@ -350,6 +350,15 @@ This will give you a date:
It is currently `hrs`:`mins` and `secs` seconds on the `date`th of `month`, `year` It is currently `hrs`:`mins` and `secs` seconds on the `date`th of `month`, `year`
### `encode_b64(string)`, `decode_b64(string)`
Encode or decode a string from b64, throwing a `b64_error` exception if it's invalid
### `encode_json(value)`, `decode_json(string)`
Encodes a value as a json string, and decodes a json string as a valid value, throwing a `json_error` exception if it
doesn't parse properly
### `profile_expr(expression)` ### `profile_expr(expression)`
Returns number of times given expression can be run in 50ms time. Useful to profile and optimize your code. Returns number of times given expression can be run in 50ms time. Useful to profile and optimize your code.
@@ -0,0 +1,5 @@
package carpet.fakes;
public interface ClientSettingsC2SPacketInterface {
public String getLanguage();
}
@@ -7,5 +7,5 @@ public interface ServerPlayerEntityInterface
EntityPlayerActionPack getActionPack(); EntityPlayerActionPack getActionPack();
void invalidateEntityObjectReference(); void invalidateEntityObjectReference();
boolean isInvalidEntityObject(); boolean isInvalidEntityObject();
String getLanguage();
} }
@@ -0,0 +1,18 @@
package carpet.mixins;
import carpet.fakes.ClientSettingsC2SPacketInterface;
import net.minecraft.network.packet.c2s.play.ClientSettingsC2SPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(ClientSettingsC2SPacket.class)
public class ClientSettingsC2SPacket_scarpetMixin implements ClientSettingsC2SPacketInterface {
@Shadow
private String language;
@Override
public String getLanguage(){
return this.language;
}
}
@@ -1,5 +1,6 @@
package carpet.mixins; package carpet.mixins;
import carpet.fakes.ClientSettingsC2SPacketInterface;
import carpet.fakes.EntityInterface; import carpet.fakes.EntityInterface;
import carpet.fakes.ServerPlayerEntityInterface; import carpet.fakes.ServerPlayerEntityInterface;
import carpet.script.EntityEventsGroup; import carpet.script.EntityEventsGroup;
@@ -7,6 +8,7 @@ import com.mojang.authlib.GameProfile;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.packet.c2s.play.ClientSettingsC2SPacket;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.stat.Stat; import net.minecraft.stat.Stat;
@@ -126,4 +128,16 @@ public abstract class ServerPlayerEntity_scarpetEventMixin extends PlayerEntity
{ {
return isInvalidReference; return isInvalidReference;
} }
//getting player language
private String language;
public String getLanguage(){
return this.language;
}
@Inject(method = "setClientSettings", at = @At("HEAD"))
public void setClientSettings(ClientSettingsC2SPacket packet, CallbackInfo ci){
this.language = ((ClientSettingsC2SPacketInterface) packet).getLanguage();
}
} }
@@ -27,6 +27,7 @@ public class Throwables {
public static final Throwables IO_EXCEPTION = register("io_exception", THROWN_EXCEPTION_TYPE); public static final Throwables IO_EXCEPTION = register("io_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables NBT_ERROR = register("nbt_error", IO_EXCEPTION); public static final Throwables NBT_ERROR = register("nbt_error", IO_EXCEPTION);
public static final Throwables JSON_ERROR = register("json_error", IO_EXCEPTION); public static final Throwables JSON_ERROR = register("json_error", IO_EXCEPTION);
public static final Throwables B64_ERROR = register("b64_error", IO_EXCEPTION);
public static final Throwables USER_DEFINED = register("user_exception", THROWN_EXCEPTION_TYPE); public static final Throwables USER_DEFINED = register("user_exception", THROWN_EXCEPTION_TYPE);
/** /**
@@ -4,6 +4,9 @@ import carpet.script.Context;
import carpet.script.Expression; import carpet.script.Expression;
import carpet.script.LazyValue; import carpet.script.LazyValue;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.script.utils.ScarpetJsonDeserializer;
import carpet.script.value.ContainerValueInterface; import carpet.script.value.ContainerValueInterface;
import carpet.script.value.LContainerValue; import carpet.script.value.LContainerValue;
import carpet.script.value.LazyListValue; import carpet.script.value.LazyListValue;
@@ -12,8 +15,12 @@ import carpet.script.value.MapValue;
import carpet.script.value.NumericValue; import carpet.script.value.NumericValue;
import carpet.script.value.StringValue; import carpet.script.value.StringValue;
import carpet.script.value.Value; import carpet.script.value.Value;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -320,5 +327,23 @@ public class DataStructures {
Value ret = new NumericValue(((ContainerValueInterface) container).delete(lv.get(lv.size()-1).evalValue(c))); Value ret = new NumericValue(((ContainerValueInterface) container).delete(lv.get(lv.size()-1).evalValue(c)));
return (cc, tt) -> ret; return (cc, tt) -> ret;
}); });
expression.addUnaryFunction("encode_b64", v -> StringValue.of(Base64.getEncoder().encodeToString(v.getString().getBytes())));
expression.addUnaryFunction("decode_b64", v -> {
try {
return StringValue.of(new String(Base64.getDecoder().decode(v.getString()), StandardCharsets.ISO_8859_1));//using this charset cos it's the one used in decoding function
} catch (IllegalArgumentException iae){
throw new ThrowStatement("Invalid b64 string: " + v.getString(), Throwables.B64_ERROR);
}
});
expression.addUnaryFunction("encode_json", v -> StringValue.of(v.toJson().toString()));
expression.addUnaryFunction("decode_json", v -> {
try {
return new ScarpetJsonDeserializer().deserialize(new JsonParser().parse(v.getString()), null, null);
} catch (JsonParseException jpe){
throw new ThrowStatement("Invalid json string: " + v.getString(), Throwables.JSON_ERROR);
}
});
} }
} }
@@ -551,6 +551,12 @@ public class EntityValue extends Value
}); });
put("air", (e, a) -> new NumericValue(e.getAir())); put("air", (e, a) -> new NumericValue(e.getAir()));
put("language", (e, a)->{
if(!(e instanceof ServerPlayerEntity))
return NULL;
String lang = ((ServerPlayerEntityInterface) e).getLanguage();
return StringValue.of(lang);
});
put("persistence", (e, a) -> { put("persistence", (e, a) -> {
if (e instanceof MobEntity) return new NumericValue(((MobEntity) e).isPersistent()); if (e instanceof MobEntity) return new NumericValue(((MobEntity) e).isPersistent());
return Value.NULL; return Value.NULL;
+1
View File
@@ -75,6 +75,7 @@
"ExperienceOrbEntityMixin", "ExperienceOrbEntityMixin",
"PlayerActionC2SPacketMixin", "PlayerActionC2SPacketMixin",
"ServerPlayerEntityMixin", "ServerPlayerEntityMixin",
"ClientSettingsC2SPacket_scarpetMixin",
"PlayerManager_coreMixin", "PlayerManager_coreMixin",
"PlayerManager_fakePlayersMixin", "PlayerManager_fakePlayersMixin",
"RedstoneWireBlockMixin", "RedstoneWireBlockMixin",