diff --git a/docs/scarpet/Full.md b/docs/scarpet/Full.md index 41c83e2e..c0bde734 100644 --- a/docs/scarpet/Full.md +++ b/docs/scarpet/Full.md @@ -2186,22 +2186,32 @@ pos_offset(block(0,5,0), 'up', 2) => l(0,7,0) pos_offset(l(0,5,0), 'up', -2 ) => l(0,3,0) -### `block_properties(pos)` +### `(Deprecated) block_properties(pos)` -Returns a list of available block properties for a particular block. If a block has no properties, returns an empty list. +Deprecated by `keys(block_state(pos))`. -### `property(pos, name)` +### `(Deprecated) property(pos, name)` -Returns property of block at `pos`, or specified by `block` argument. If a block doesn't have that property, `null` -value is returned. Returned values are always strings. It is expected from the user to know what to expect and convert -values to numbers using `number()` function or booleans using `bool()` function. +Deprecated by `block_state(pos, name)` + +### `block_state(block)`, `block_state(block, property)` + +If used with a `block` argument only, it returns a map of block properties and their values. If a block has no properties, returns an +empty map. + +If `property` is specified, returns value of that property, or `null` if property is not applicable. + +Returned values or properties are always strings. It is expected from the user to know what to expect and convert +values to numbers using `number()` function or booleans using `bool()` function. Returned string values can be directly used +back in state definition in various applications where block properties are required.
-set(x,y,z,'iron_trapdoor','half','top'); property(x,y,z,'half')  => top
-set(x,y,z,'air'); property(x,y,z,'half')  => null
-property(block('iron_trapdoor[half=top]'),'half')  => top
-property(block('iron_trapdoor[half=top]'),'powered')  => false
-bool(property(block('iron_trapdoor[half=top]'),'powered'))  => 0
+set(x,y,z,'iron_trapdoor','half','top'); block_state(x,y,z)  => {waterlogged: false, half: top, open: false, ...}
+set(x,y,z,'iron_trapdoor','half','top'); block_state(x,y,z,'half')  => top
+set(x,y,z,'air'); block_state(x,y,z,'half')  => null
+block_state(block('iron_trapdoor[half=top]'),'half')  => top
+block_state(block('iron_trapdoor[half=top]'),'powered')  => false
+bool(block_state(block('iron_trapdoor[half=top]'),'powered'))  => 0
 
### `block_data(pos)` diff --git a/docs/scarpet/api/BlocksAndWorldAccess.md b/docs/scarpet/api/BlocksAndWorldAccess.md index 3d1ff567..bf6ae4b2 100644 --- a/docs/scarpet/api/BlocksAndWorldAccess.md +++ b/docs/scarpet/api/BlocksAndWorldAccess.md @@ -235,22 +235,32 @@ pos_offset(block(0,5,0), 'up', 2) => l(0,7,0) pos_offset(l(0,5,0), 'up', -2 ) => l(0,3,0) -### `block_properties(pos)` +### `(Deprecated) block_properties(pos)` -Returns a list of available block properties for a particular block. If a block has no properties, returns an empty list. +Deprecated by `keys(block_state(pos))`. -### `property(pos, name)` +### `(Deprecated) property(pos, name)` -Returns property of block at `pos`, or specified by `block` argument. If a block doesn't have that property, `null` -value is returned. Returned values are always strings. It is expected from the user to know what to expect and convert -values to numbers using `number()` function or booleans using `bool()` function. +Deprecated by `block_state(pos, name)` + +### `block_state(block)`, `block_state(block, property)` + +If used with a `block` argument only, it returns a map of block properties and their values. If a block has no properties, returns an +empty map. + +If `property` is specified, returns value of that property, or `null` if property is not applicable. + +Returned values or properties are always strings. It is expected from the user to know what to expect and convert +values to numbers using `number()` function or booleans using `bool()` function. Returned string values can be directly used +back in state definition in various applications where block properties are required.
-set(x,y,z,'iron_trapdoor','half','top'); property(x,y,z,'half')  => top
-set(x,y,z,'air'); property(x,y,z,'half')  => null
-property(block('iron_trapdoor[half=top]'),'half')  => top
-property(block('iron_trapdoor[half=top]'),'powered')  => false
-bool(property(block('iron_trapdoor[half=top]'),'powered'))  => 0
+set(x,y,z,'iron_trapdoor','half','top'); block_state(x,y,z)  => {waterlogged: false, half: top, open: false, ...}
+set(x,y,z,'iron_trapdoor','half','top'); block_state(x,y,z,'half')  => top
+set(x,y,z,'air'); block_state(x,y,z,'half')  => null
+block_state(block('iron_trapdoor[half=top]'),'half')  => top
+block_state(block('iron_trapdoor[half=top]'),'powered')  => false
+bool(block_state(block('iron_trapdoor[half=top]'),'powered'))  => 0
 
### `block_data(pos)` diff --git a/docs/scarpet/resources/editors/idea/2.txt b/docs/scarpet/resources/editors/idea/2.txt index db277164..012ad76f 100644 --- a/docs/scarpet/resources/editors/idea/2.txt +++ b/docs/scarpet/resources/editors/idea/2.txt @@ -5,8 +5,8 @@ blast_resistance block block_data block_light -block_properties block_sound +block_state block_tick blocks_daylight blocks_movement @@ -69,7 +69,6 @@ poi pos pos_offset power -property query random_tick recipe_data diff --git a/docs/scarpet/resources/editors/npp/scarpet.xml b/docs/scarpet/resources/editors/npp/scarpet.xml index 2a04dbef..cad62780 100644 --- a/docs/scarpet/resources/editors/npp/scarpet.xml +++ b/docs/scarpet/resources/editors/npp/scarpet.xml @@ -53,7 +53,7 @@ transparent opacity blocks_daylight emitted_light light block_light sky_light see_sky brightness hardness blast_resistance top loaded suffocates power ticks_randomly update block_tick random_tick in_slime_chunk set without_updates - blocks_movement block_sound material map_colour property block_properties block_data poi set_poi + blocks_movement block_sound material map_colour block_state block_data poi set_poi place_item set_biome biome loaded_status generation_status inhabited_time spawn_potential chunk_tickets structure_eligibility structures structure_references set_structure reset_chunk reload_chunk custom_dimension add_chunk_ticket diff --git a/src/main/java/carpet/commands/PlayerCommand.java b/src/main/java/carpet/commands/PlayerCommand.java index ff8fd1df..d1e1bd77 100644 --- a/src/main/java/carpet/commands/PlayerCommand.java +++ b/src/main/java/carpet/commands/PlayerCommand.java @@ -186,12 +186,18 @@ public class PlayerCommand return true; } GameProfile profile = server.getUserCache().findByName(playerName); - if (manager.getUserBanList().contains(profile)) + if (profile == null) { - Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is banned"); + Messenger.m(context.getSource(), "r Player "+playerName+" is either banned by Mojang, or auth servers are down. " + + "Banned players can only be summoned in Singleplayer and in servers in off-line mode."); return true; } - if (manager.isWhitelistEnabled() && profile != null && manager.isWhitelisted(profile) && !context.getSource().hasPermissionLevel(2)) + if (manager.getUserBanList().contains(profile)) + { + Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is banned on this server"); + return true; + } + if (manager.isWhitelistEnabled() && manager.isWhitelisted(profile) && !context.getSource().hasPermissionLevel(2)) { Messenger.m(context.getSource(), "r Whitelisted players can only be spawned by operators"); return true; diff --git a/src/main/java/carpet/script/api/WorldAccess.java b/src/main/java/carpet/script/api/WorldAccess.java index d9cd3406..efb81c31 100644 --- a/src/main/java/carpet/script/api/WorldAccess.java +++ b/src/main/java/carpet/script/api/WorldAccess.java @@ -958,6 +958,8 @@ public class WorldAccess { stateStringQuery(c, "map_colour", lv, (s, p) -> BlockInfo.mapColourName.get(s.getMapColor(((CarpetContext)c).s.getWorld(), p)))); + + // Deprecated for block_state() expression.addLazyFunction("property", -1, (c, t, lv) -> { BlockArgument locator = BlockArgument.findIn((CarpetContext) c, lv, 0); @@ -973,6 +975,7 @@ public class WorldAccess { return (_c, _t) -> retval; }); + // Deprecated for block_state() expression.addLazyFunction("block_properties", -1, (c, t, lv) -> { BlockArgument locator = BlockArgument.findIn((CarpetContext) c, lv, 0); @@ -984,6 +987,31 @@ public class WorldAccess { return (_c, _t) -> res; }); + // block_state(block) + // block_state(block, property) + expression.addLazyFunction("block_state", -1, (c, t, lv) -> + { + BlockArgument locator = BlockArgument.findIn((CarpetContext) c, lv, 0); + BlockState state = locator.block.getBlockState(); + StateManager states = state.getBlock().getStateManager(); + if (locator.offset == lv.size()) + { + Map properties = new HashMap<>(); + for(Property p : states.getProperties()) + { + properties.put(StringValue.of(p.getName()), ValueConversions.fromProperty(state, p));// ValueConversions.fromObject(state.get(p), false)); + } + Value ret = MapValue.wrap(properties); + return (_c, _t) -> ret; + } + String tag = lv.get(locator.offset).evalValue(c).getString(); + Property property = states.getProperty(tag); + if (property == null) + return LazyValue.NULL; + Value retval = ValueConversions.fromProperty(state, property); + return (_c, _t) -> retval; + }); + expression.addLazyFunction("biome", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; BlockArgument locator = BlockArgument.findIn(cc, lv, 0); diff --git a/src/main/java/carpet/script/language/Functions.java b/src/main/java/carpet/script/language/Functions.java index 12bca880..e45d6a21 100644 --- a/src/main/java/carpet/script/language/Functions.java +++ b/src/main/java/carpet/script/language/Functions.java @@ -85,14 +85,14 @@ public class Functions { expression.addLazyFunction("outer", 1, (c, t, lv) -> { if (t != Context.LOCALIZATION) - throw new InternalExpressionException("Outer scoping of variables is only possible in function signatures"); + throw new InternalExpressionException("Outer scoping of variables is only possible in function signatures."); return (cc, tt) -> new FunctionAnnotationValue(lv.get(0).evalValue(c), 0); }); expression.addLazyUnaryOperator("...", Operators.precedence.get("def->..."), false, (c, t, lv) -> { if (t != Context.LOCALIZATION) - throw new InternalExpressionException("Outer scoping of variables is only possible in function signatures"); + throw new InternalExpressionException("That functionality has not been implemented yet."); return (cc, tt) -> new FunctionAnnotationValue(lv.evalValue(c), 1); }); diff --git a/src/main/java/carpet/script/value/ValueConversions.java b/src/main/java/carpet/script/value/ValueConversions.java index 9d03a33e..0a229680 100644 --- a/src/main/java/carpet/script/value/ValueConversions.java +++ b/src/main/java/carpet/script/value/ValueConversions.java @@ -2,6 +2,7 @@ package carpet.script.value; import carpet.script.exception.InternalExpressionException; import carpet.utils.BlockInfo; +import net.minecraft.block.BlockState; import net.minecraft.block.MapColor; import net.minecraft.entity.Entity; import net.minecraft.entity.ai.brain.LookTarget; @@ -17,10 +18,12 @@ import net.minecraft.scoreboard.ScoreboardObjective; import net.minecraft.server.MinecraftServer; import net.minecraft.server.world.ServerWorld; +import net.minecraft.state.property.Property; import net.minecraft.structure.StructurePiece; import net.minecraft.structure.StructureStart; import net.minecraft.util.Identifier; +import net.minecraft.util.StringIdentifiable; import net.minecraft.util.dynamic.GlobalPos; import net.minecraft.util.math.BlockBox; import net.minecraft.util.math.BlockPos; @@ -316,4 +319,15 @@ public class ValueConversions ret.put(new StringValue("pieces"), ListValue.wrap(pieces)); return MapValue.wrap(ret); } + + public static Value fromProperty(BlockState state, Property p) + { + Comparable object = state.get(p); + if (object instanceof Boolean || object instanceof Number) return StringValue.of(object.toString()); + if (object instanceof StringIdentifiable) + { + return StringValue.of(((StringIdentifiable) object).asString()); + } + throw new InternalExpressionException("Unknown property type: "+p.getName()); + } }