From 05be906686d0f742a3eb6570da565c935a8b4a37 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Mon, 3 Aug 2020 01:11:23 -0700 Subject: [PATCH 1/3] Introducing block state modification --- .../skript/classes/data/SkriptClasses.java | 71 ++++ .../expressions/ExprBlockDataState.java | 122 +++++++ .../ch/njol/skript/util/BlockDataUtils.java | 330 ++++++++++++++++++ src/main/resources/lang/english.lang | 2 + .../expressions/ExprBlockDataState.sk | 22 ++ 5 files changed, 547 insertions(+) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java create mode 100644 src/main/java/ch/njol/skript/util/BlockDataUtils.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk diff --git a/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java b/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java index 23eacb74fa4..7674de8223e 100644 --- a/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java @@ -47,6 +47,9 @@ import ch.njol.skript.localization.Noun; import ch.njol.skript.localization.RegexMessage; import ch.njol.skript.registrations.Classes; +import ch.njol.skript.util.BlockDataUtils; +import ch.njol.skript.util.BlockDataUtils.StateType; +import ch.njol.skript.util.BlockDataUtils.StateValues; import ch.njol.skript.util.Color; import ch.njol.skript.util.Date; import ch.njol.skript.util.Direction; @@ -853,6 +856,74 @@ public String getVariableNamePattern() { .since("2.5") .serializer(new YggdrasilSerializer()) ); + + if (Skript.isRunningMinecraft(1, 13)) { + Classes.registerClass(new ClassInfo<>(StateType.class, "blockstatetype") + .user("block ?state ?types?") + .name("Block State Type") + .description("Represents the different states of a block's BlockData.") + .usage(BlockDataUtils.getStateTypeNames()) + .examples("set lit state of target block to false", + "set waterlogged state of target block to true") + .since("INSERT VERSION") + .requiredPlugins("Minecraft 1.13+") + .parser(new Parser() { + @Nullable + @Override + public StateType parse(String s, ParseContext context) { + return BlockDataUtils.getByName(s.replace("_", " ")); + } + + @Override + public String toString(StateType o, int flags) { + return o.getName(); + } + + @Override + public String toVariableNameString(StateType o) { + return "blockstatetype: " + o.toString(); + } + + @Override + public String getVariableNamePattern() { + return "blockstatetype:+."; + } + }) + .serializer(new YggdrasilSerializer<>())); + + Classes.registerClass(new ClassInfo<>(StateValues.class, "blockstatevalue") + .user("block ?state ?values?") + .name("Block State Value") + .description("Represents different block state values which currently do not match a Skript type.") + .usage(StateValues.getNames()) + .examples("set axis state of target block to x", + "set half state of target block to upper", + "set orientation state of block at player to up north") + .since("INSERT VERSION") + .requiredPlugins("Minecraft 1.13+") + .parser(new Parser() { + @Nullable + @Override + public StateValues parse(String s, ParseContext context) { + return StateValues.getByName(s.replace("_", " ")); + } + + @Override + public String toString(StateValues o, int flags) { + return o.toString(); + } + + @Override + public String toVariableNameString(StateValues o) { + return o.toString(); + } + + @Override + public String getVariableNamePattern() { + return ".*"; + } + })); + } } } diff --git a/src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java b/src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java new file mode 100644 index 00000000000..a54296fa7cb --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java @@ -0,0 +1,122 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.expressions; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.block.Block; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.util.BlockDataUtils; +import ch.njol.skript.util.BlockDataUtils.StateType; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; + +@Name("Block Data State") +@Description("Get or modify different states of a block.") +@Examples({"set lit state of target block to true", + "set facing state of target block to north", + "set {_s} to orientation state of target block"}) +@Since("INSERT VERSION") +@RequiredPlugins("Minecraft 1.13+") +public class ExprBlockDataState extends SimpleExpression { + + + static { + if (Skript.classExists("org.bukkit.block.data.BlockData")) + Skript.registerExpression(ExprBlockDataState.class, Object.class, ExpressionType.COMBINED, + "%blockstatetype% [block] state of %blocks%"); + } + + @SuppressWarnings("null") + private Expression stateType; + @SuppressWarnings("null") + private Expression blocks; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + stateType = (Expression) exprs[0]; + blocks = (Expression) exprs[1]; + return true; + } + + @Nullable + @Override + protected Object[] get(Event e) { + List objects = new ArrayList<>(); + StateType stateType = this.stateType.getSingle(e); + assert stateType != null; + for (Block block : blocks.getArray(e)) { + Object o = BlockDataUtils.getBlockStateType(block, stateType); + if (o != null) + objects.add(o); + } + return objects.toArray(); + } + + @Nullable + @Override + public Class[] acceptChange(ChangeMode mode) { + if (mode == ChangeMode.SET) + return CollectionUtils.array(Object.class); + return null; + } + + @Override + public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { + if (delta == null) + return; + + StateType stateType = this.stateType.getSingle(e); + assert stateType != null; + for (Block block : blocks.getArray(e)) { + BlockDataUtils.setBlockState(block, stateType, delta[0]); + } + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Object.class; + } + + @Override + public String toString(@Nullable Event e, boolean d) { + return stateType.toString(e, d) + " state of " + blocks.toString(e, d); + } + +} diff --git a/src/main/java/ch/njol/skript/util/BlockDataUtils.java b/src/main/java/ch/njol/skript/util/BlockDataUtils.java new file mode 100644 index 00000000000..01c06938401 --- /dev/null +++ b/src/main/java/ch/njol/skript/util/BlockDataUtils.java @@ -0,0 +1,330 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.util; + +import java.io.NotSerializableException; +import java.io.StreamCorruptedException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang.math.NumberUtils; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.BlockData; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.util.StringUtils; +import ch.njol.yggdrasil.Fields; +import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable; + +/** + * Utility class for modifying {@link BlockData} of {@link Block Blocks} + */ +public class BlockDataUtils { + + private static final Map stateTypes = new HashMap<>(); + + static { + // Load all variations of block state types + if (Skript.isRunningMinecraft(1, 13)) { + for (Material material : Material.values()) { + // If this material is not a block, let's continue. + if (!material.isBlock()) + continue; + + BlockData blockData = material.createBlockData(); + String blockDataString = blockData.getAsString(); + + // If this block data does not have states, let's continue. + if (!blockDataString.contains("[")) + continue; + + String stateString = blockDataString.replaceAll(".+\\[|]", ""); + String[] splits = stateString.split(","); + for (String s : splits) { + String[] split = s.split("="); + String state = split[0]; + String name = state.replace("_", " "); + + if (!stateTypes.containsKey(name)) + stateTypes.put(name, new StateType(name, state)); + + } + } + } + } + + /** + * Get all state types + * + * @return Collection of all state types + */ + public static Collection getStateTypes() { + return stateTypes.values(); + } + + /** + * Get a string of all state type names + * Used when registering class info + * + * @return String of all state type names + */ + public static String getStateTypeNames() { + return StringUtils.join(stateTypes.keySet(), ", "); + } + + /** + * Get a state type by name + * + * @param name Name of state type + * @return State type by name + */ + public static StateType getByName(String name) { + return stateTypes.get(name); + } + + /** + * Change the state of a block + * + * @param block Block to change + * @param stateType StateType to change + * @param value New value + */ + public static void setBlockState(Block block, StateType stateType, Object value) { + BlockData blockData = block.getBlockData(); + String key = block.getType().getKey().toString(); + + try { + BlockData newData = Bukkit.createBlockData(key + "[" + stateType.type + "=" + value.toString().toLowerCase() + "]"); + blockData = blockData.merge(newData); + block.setBlockData(blockData); + } catch (IllegalArgumentException ignore) { + } + } + + /** + * Get a block state from a block + * + * @param block Block to grab state from + * @param stateType StateType to grab + * @return Value of state type from block + */ + @Nullable + public static Object getBlockStateType(Block block, StateType stateType) { + BlockData blockData = block.getBlockData(); + String blockDataString = blockData.getAsString(); + + if (!blockDataString.contains("[")) + return null; + + String stateString = blockDataString.replaceAll(".+\\[|]", ""); + String[] splits = stateString.split(","); + for (String s : splits) { + String[] split = s.split("="); + String state = split[0]; + String value = split[1]; + if (!state.equalsIgnoreCase(stateType.name)) + continue; + + if (NumberUtils.isNumber(value)) + return NumberUtils.createNumber(value); + if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) + return value.equalsIgnoreCase("true"); + + @Nullable + StateValues stateValues = StateValues.getByName(value); + if (stateValues != null) + return stateValues; + + @Nullable + Direction direction = getDirection(value); + if (direction != null) + return direction; + return value; + } + return null; + } + + @Nullable + private static Direction getDirection(String dir) { + switch (dir) { + case "north": + return new Direction(BlockFace.NORTH, 1); + case "south": + return new Direction(BlockFace.SOUTH, 1); + case "east": + return new Direction(BlockFace.EAST, 1); + case "west": + return new Direction(BlockFace.WEST, 1); + case "up": + return new Direction(BlockFace.UP, 1); + case "down": + return new Direction(BlockFace.DOWN, 1); + } + return null; + } + + /** + * Represents state types a block's {@link BlockData} can have + */ + public static class StateType implements YggdrasilExtendedSerializable { + + private String name = ""; + private String type = ""; + + public StateType() {} + + public StateType(String name, String type) { + this.name = name; + this.type = type; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return type; + } + + @Override + public Fields serialize() throws NotSerializableException { + return new Fields(this); + } + + @Override + public void deserialize(@NonNull Fields fields) throws StreamCorruptedException, NotSerializableException { + fields.setFields(this); + } + } + + /** + * Represents special block state values which are currently not handled by Skript + */ + public enum StateValues { + + // AXIS + X("x"), + Y("y"), + Z("z"), + + // ORIENTATION (used in jigsaws) + DOWN_EAST("down east"), + DOWN_NORTH("down north"), + DOWN_SOUTH("down south"), + DOWN_WEST("down west"), + EAST_UP("east up"), + NORTH_UP("north up"), + SOUTH_UP("south up"), + UP_EAST("up east"), + UP_NORTH("up north"), + UP_SOUTH("up south"), + UP_WEST("up west"), + WEST_UP("west up"), + + // ATTACHMENT / FACE / HINGE + CEILING("ceiling"), + DOUBLE_WALL("double wall"), + FLOOR("floor"), + SINGLE_WALL("single wall"), + WALL("wall"), + LEFT("left"), + RIGHT("right"), + + // HALF / PART + LOWER("lower"), + UPPER("upper"), + BOTTOM("bottom"), + TOP("top"), + FOOT("foot"), + HEAD("head"), + + // BLOCK SIDES + NONE("none"), + SIDE("side"), + + // SIZE + SMALL("small"), + LARGE("large"), + + // REDSTONE MODE + COMPARE("compare"), + SUBTRACT("subtract"), + CORNER("corner"), + DATA("data"), + LOAD("load"), + SAVE("save"), + + // SHAPE (used for rails) + ASCENDING_EAST("ascending east"), + ASCENDING_NORTH("ascending north"), + ASCENDING_SOUTH("ascending south"), + ASCENDING_WEST("ascending west"), + INNER_LEFT("inner left"), + INNER_RIGHT("inner right"), + OUTER_LEFT("outer left"), + OUTER_RIGHT("outer right"), + STRAIGHT("straight"), + EAST_WEST("east west"), + NORTH_SOUTH("north south"), + + // PISTON TYPE + NORMAL("normal"), + STICKY("sticky"), + SINGLE("single"), + DOUBLE("double"); + + private final String name; + + StateValues(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + private static final Map names = new HashMap<>(); + + static { + for (StateValues value : StateValues.values()) { + names.put(value.name, value); + } + } + + public static String getNames() { + return StringUtils.join(names.keySet(), ", "); + } + + @Nullable + public static StateValues getByName(String name) { + if (names.containsKey(name)) + return names.get(name); + return null; + } + } + +} diff --git a/src/main/resources/lang/english.lang b/src/main/resources/lang/english.lang index 67e29001a69..e639d3019f1 100644 --- a/src/main/resources/lang/english.lang +++ b/src/main/resources/lang/english.lang @@ -1826,6 +1826,8 @@ types: resourcepackstate: resource pack state¦s @a gene: panda gene¦s @a gamerulevalue: gamerule value¦s @a + blockstatetype: block state type¦s @a + blockstatevalue: block state value¦s @a # Hooks money: money diff --git a/src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk b/src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk new file mode 100644 index 00000000000..3ac8dc0604c --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk @@ -0,0 +1,22 @@ +test "block data state" when minecraft version is "1.16": + set {_b} to block at spawn of world "world" + + set block at {_b} to a campfire + #By default it is already true, but lets test just in case Minecraft decides to shake things up + set lit state of block at {_b} to true + assert lit state of block at {_b} = true with "block state 'lit' of block should have been true but was %lit state of block at {_b}%" + set lit state of block at {_b} to false + assert lit state of block at {_b} = false with "block state 'lit' of block should have been false but was %lit state of block at {_b}%" + + set block at {_b} to grass + assert snowy state of block at {_b} = false with "block state 'snowy' of block should have been false" + set snowy state of block at {_b} to true + assert snowy state of block at {_b} = true with "block state 'snowy' of block should have been true" + + set block at {_b} to north facing oak stairs + assert facing state of block at {_b} = north with "block state 'facing' of block should have been north" + assert waterlogged state of block at {_b} = false with "block state 'waterlogged' of block should have been false" + set facing state of block at {_b} to west + assert facing state of block at {_b} = west with "block state 'facing' of block should have been west" + set waterlogged state of block at {_b} to true + assert waterlogged state of block at {_b} = true with "block state 'waterlogged' of block should have been true" From 2ea350b47773ff6ebaf48bf6a3c44833778e7838 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Mon, 3 Aug 2020 01:24:46 -0700 Subject: [PATCH 2/3] Revert "Introducing block state modification" This reverts commit 05be9066 --- .../skript/classes/data/SkriptClasses.java | 71 ---- .../expressions/ExprBlockDataState.java | 122 ------- .../ch/njol/skript/util/BlockDataUtils.java | 330 ------------------ src/main/resources/lang/english.lang | 2 - .../expressions/ExprBlockDataState.sk | 22 -- 5 files changed, 547 deletions(-) delete mode 100644 src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java delete mode 100644 src/main/java/ch/njol/skript/util/BlockDataUtils.java delete mode 100644 src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk diff --git a/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java b/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java index 7674de8223e..23eacb74fa4 100644 --- a/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/SkriptClasses.java @@ -47,9 +47,6 @@ import ch.njol.skript.localization.Noun; import ch.njol.skript.localization.RegexMessage; import ch.njol.skript.registrations.Classes; -import ch.njol.skript.util.BlockDataUtils; -import ch.njol.skript.util.BlockDataUtils.StateType; -import ch.njol.skript.util.BlockDataUtils.StateValues; import ch.njol.skript.util.Color; import ch.njol.skript.util.Date; import ch.njol.skript.util.Direction; @@ -856,74 +853,6 @@ public String getVariableNamePattern() { .since("2.5") .serializer(new YggdrasilSerializer()) ); - - if (Skript.isRunningMinecraft(1, 13)) { - Classes.registerClass(new ClassInfo<>(StateType.class, "blockstatetype") - .user("block ?state ?types?") - .name("Block State Type") - .description("Represents the different states of a block's BlockData.") - .usage(BlockDataUtils.getStateTypeNames()) - .examples("set lit state of target block to false", - "set waterlogged state of target block to true") - .since("INSERT VERSION") - .requiredPlugins("Minecraft 1.13+") - .parser(new Parser() { - @Nullable - @Override - public StateType parse(String s, ParseContext context) { - return BlockDataUtils.getByName(s.replace("_", " ")); - } - - @Override - public String toString(StateType o, int flags) { - return o.getName(); - } - - @Override - public String toVariableNameString(StateType o) { - return "blockstatetype: " + o.toString(); - } - - @Override - public String getVariableNamePattern() { - return "blockstatetype:+."; - } - }) - .serializer(new YggdrasilSerializer<>())); - - Classes.registerClass(new ClassInfo<>(StateValues.class, "blockstatevalue") - .user("block ?state ?values?") - .name("Block State Value") - .description("Represents different block state values which currently do not match a Skript type.") - .usage(StateValues.getNames()) - .examples("set axis state of target block to x", - "set half state of target block to upper", - "set orientation state of block at player to up north") - .since("INSERT VERSION") - .requiredPlugins("Minecraft 1.13+") - .parser(new Parser() { - @Nullable - @Override - public StateValues parse(String s, ParseContext context) { - return StateValues.getByName(s.replace("_", " ")); - } - - @Override - public String toString(StateValues o, int flags) { - return o.toString(); - } - - @Override - public String toVariableNameString(StateValues o) { - return o.toString(); - } - - @Override - public String getVariableNamePattern() { - return ".*"; - } - })); - } } } diff --git a/src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java b/src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java deleted file mode 100644 index a54296fa7cb..00000000000 --- a/src/main/java/ch/njol/skript/expressions/ExprBlockDataState.java +++ /dev/null @@ -1,122 +0,0 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * - * Copyright 2011-2017 Peter Güttinger and contributors - */ -package ch.njol.skript.expressions; - -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.block.Block; -import org.bukkit.event.Event; -import org.eclipse.jdt.annotation.Nullable; - -import ch.njol.skript.Skript; -import ch.njol.skript.classes.Changer.ChangeMode; -import ch.njol.skript.doc.Description; -import ch.njol.skript.doc.Examples; -import ch.njol.skript.doc.Name; -import ch.njol.skript.doc.RequiredPlugins; -import ch.njol.skript.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.util.BlockDataUtils; -import ch.njol.skript.util.BlockDataUtils.StateType; -import ch.njol.util.Kleenean; -import ch.njol.util.coll.CollectionUtils; - -@Name("Block Data State") -@Description("Get or modify different states of a block.") -@Examples({"set lit state of target block to true", - "set facing state of target block to north", - "set {_s} to orientation state of target block"}) -@Since("INSERT VERSION") -@RequiredPlugins("Minecraft 1.13+") -public class ExprBlockDataState extends SimpleExpression { - - - static { - if (Skript.classExists("org.bukkit.block.data.BlockData")) - Skript.registerExpression(ExprBlockDataState.class, Object.class, ExpressionType.COMBINED, - "%blockstatetype% [block] state of %blocks%"); - } - - @SuppressWarnings("null") - private Expression stateType; - @SuppressWarnings("null") - private Expression blocks; - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - stateType = (Expression) exprs[0]; - blocks = (Expression) exprs[1]; - return true; - } - - @Nullable - @Override - protected Object[] get(Event e) { - List objects = new ArrayList<>(); - StateType stateType = this.stateType.getSingle(e); - assert stateType != null; - for (Block block : blocks.getArray(e)) { - Object o = BlockDataUtils.getBlockStateType(block, stateType); - if (o != null) - objects.add(o); - } - return objects.toArray(); - } - - @Nullable - @Override - public Class[] acceptChange(ChangeMode mode) { - if (mode == ChangeMode.SET) - return CollectionUtils.array(Object.class); - return null; - } - - @Override - public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { - if (delta == null) - return; - - StateType stateType = this.stateType.getSingle(e); - assert stateType != null; - for (Block block : blocks.getArray(e)) { - BlockDataUtils.setBlockState(block, stateType, delta[0]); - } - } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return Object.class; - } - - @Override - public String toString(@Nullable Event e, boolean d) { - return stateType.toString(e, d) + " state of " + blocks.toString(e, d); - } - -} diff --git a/src/main/java/ch/njol/skript/util/BlockDataUtils.java b/src/main/java/ch/njol/skript/util/BlockDataUtils.java deleted file mode 100644 index 01c06938401..00000000000 --- a/src/main/java/ch/njol/skript/util/BlockDataUtils.java +++ /dev/null @@ -1,330 +0,0 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * - * Copyright 2011-2017 Peter Güttinger and contributors - */ -package ch.njol.skript.util; - -import java.io.NotSerializableException; -import java.io.StreamCorruptedException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.lang.math.NumberUtils; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.block.data.BlockData; -import org.eclipse.jdt.annotation.NonNull; -import org.eclipse.jdt.annotation.Nullable; - -import ch.njol.skript.Skript; -import ch.njol.util.StringUtils; -import ch.njol.yggdrasil.Fields; -import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable; - -/** - * Utility class for modifying {@link BlockData} of {@link Block Blocks} - */ -public class BlockDataUtils { - - private static final Map stateTypes = new HashMap<>(); - - static { - // Load all variations of block state types - if (Skript.isRunningMinecraft(1, 13)) { - for (Material material : Material.values()) { - // If this material is not a block, let's continue. - if (!material.isBlock()) - continue; - - BlockData blockData = material.createBlockData(); - String blockDataString = blockData.getAsString(); - - // If this block data does not have states, let's continue. - if (!blockDataString.contains("[")) - continue; - - String stateString = blockDataString.replaceAll(".+\\[|]", ""); - String[] splits = stateString.split(","); - for (String s : splits) { - String[] split = s.split("="); - String state = split[0]; - String name = state.replace("_", " "); - - if (!stateTypes.containsKey(name)) - stateTypes.put(name, new StateType(name, state)); - - } - } - } - } - - /** - * Get all state types - * - * @return Collection of all state types - */ - public static Collection getStateTypes() { - return stateTypes.values(); - } - - /** - * Get a string of all state type names - * Used when registering class info - * - * @return String of all state type names - */ - public static String getStateTypeNames() { - return StringUtils.join(stateTypes.keySet(), ", "); - } - - /** - * Get a state type by name - * - * @param name Name of state type - * @return State type by name - */ - public static StateType getByName(String name) { - return stateTypes.get(name); - } - - /** - * Change the state of a block - * - * @param block Block to change - * @param stateType StateType to change - * @param value New value - */ - public static void setBlockState(Block block, StateType stateType, Object value) { - BlockData blockData = block.getBlockData(); - String key = block.getType().getKey().toString(); - - try { - BlockData newData = Bukkit.createBlockData(key + "[" + stateType.type + "=" + value.toString().toLowerCase() + "]"); - blockData = blockData.merge(newData); - block.setBlockData(blockData); - } catch (IllegalArgumentException ignore) { - } - } - - /** - * Get a block state from a block - * - * @param block Block to grab state from - * @param stateType StateType to grab - * @return Value of state type from block - */ - @Nullable - public static Object getBlockStateType(Block block, StateType stateType) { - BlockData blockData = block.getBlockData(); - String blockDataString = blockData.getAsString(); - - if (!blockDataString.contains("[")) - return null; - - String stateString = blockDataString.replaceAll(".+\\[|]", ""); - String[] splits = stateString.split(","); - for (String s : splits) { - String[] split = s.split("="); - String state = split[0]; - String value = split[1]; - if (!state.equalsIgnoreCase(stateType.name)) - continue; - - if (NumberUtils.isNumber(value)) - return NumberUtils.createNumber(value); - if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) - return value.equalsIgnoreCase("true"); - - @Nullable - StateValues stateValues = StateValues.getByName(value); - if (stateValues != null) - return stateValues; - - @Nullable - Direction direction = getDirection(value); - if (direction != null) - return direction; - return value; - } - return null; - } - - @Nullable - private static Direction getDirection(String dir) { - switch (dir) { - case "north": - return new Direction(BlockFace.NORTH, 1); - case "south": - return new Direction(BlockFace.SOUTH, 1); - case "east": - return new Direction(BlockFace.EAST, 1); - case "west": - return new Direction(BlockFace.WEST, 1); - case "up": - return new Direction(BlockFace.UP, 1); - case "down": - return new Direction(BlockFace.DOWN, 1); - } - return null; - } - - /** - * Represents state types a block's {@link BlockData} can have - */ - public static class StateType implements YggdrasilExtendedSerializable { - - private String name = ""; - private String type = ""; - - public StateType() {} - - public StateType(String name, String type) { - this.name = name; - this.type = type; - } - - public String getName() { - return name; - } - - @Override - public String toString() { - return type; - } - - @Override - public Fields serialize() throws NotSerializableException { - return new Fields(this); - } - - @Override - public void deserialize(@NonNull Fields fields) throws StreamCorruptedException, NotSerializableException { - fields.setFields(this); - } - } - - /** - * Represents special block state values which are currently not handled by Skript - */ - public enum StateValues { - - // AXIS - X("x"), - Y("y"), - Z("z"), - - // ORIENTATION (used in jigsaws) - DOWN_EAST("down east"), - DOWN_NORTH("down north"), - DOWN_SOUTH("down south"), - DOWN_WEST("down west"), - EAST_UP("east up"), - NORTH_UP("north up"), - SOUTH_UP("south up"), - UP_EAST("up east"), - UP_NORTH("up north"), - UP_SOUTH("up south"), - UP_WEST("up west"), - WEST_UP("west up"), - - // ATTACHMENT / FACE / HINGE - CEILING("ceiling"), - DOUBLE_WALL("double wall"), - FLOOR("floor"), - SINGLE_WALL("single wall"), - WALL("wall"), - LEFT("left"), - RIGHT("right"), - - // HALF / PART - LOWER("lower"), - UPPER("upper"), - BOTTOM("bottom"), - TOP("top"), - FOOT("foot"), - HEAD("head"), - - // BLOCK SIDES - NONE("none"), - SIDE("side"), - - // SIZE - SMALL("small"), - LARGE("large"), - - // REDSTONE MODE - COMPARE("compare"), - SUBTRACT("subtract"), - CORNER("corner"), - DATA("data"), - LOAD("load"), - SAVE("save"), - - // SHAPE (used for rails) - ASCENDING_EAST("ascending east"), - ASCENDING_NORTH("ascending north"), - ASCENDING_SOUTH("ascending south"), - ASCENDING_WEST("ascending west"), - INNER_LEFT("inner left"), - INNER_RIGHT("inner right"), - OUTER_LEFT("outer left"), - OUTER_RIGHT("outer right"), - STRAIGHT("straight"), - EAST_WEST("east west"), - NORTH_SOUTH("north south"), - - // PISTON TYPE - NORMAL("normal"), - STICKY("sticky"), - SINGLE("single"), - DOUBLE("double"); - - private final String name; - - StateValues(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - private static final Map names = new HashMap<>(); - - static { - for (StateValues value : StateValues.values()) { - names.put(value.name, value); - } - } - - public static String getNames() { - return StringUtils.join(names.keySet(), ", "); - } - - @Nullable - public static StateValues getByName(String name) { - if (names.containsKey(name)) - return names.get(name); - return null; - } - } - -} diff --git a/src/main/resources/lang/english.lang b/src/main/resources/lang/english.lang index e639d3019f1..67e29001a69 100644 --- a/src/main/resources/lang/english.lang +++ b/src/main/resources/lang/english.lang @@ -1826,8 +1826,6 @@ types: resourcepackstate: resource pack state¦s @a gene: panda gene¦s @a gamerulevalue: gamerule value¦s @a - blockstatetype: block state type¦s @a - blockstatevalue: block state value¦s @a # Hooks money: money diff --git a/src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk b/src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk deleted file mode 100644 index 3ac8dc0604c..00000000000 --- a/src/test/skript/tests/syntaxes/expressions/ExprBlockDataState.sk +++ /dev/null @@ -1,22 +0,0 @@ -test "block data state" when minecraft version is "1.16": - set {_b} to block at spawn of world "world" - - set block at {_b} to a campfire - #By default it is already true, but lets test just in case Minecraft decides to shake things up - set lit state of block at {_b} to true - assert lit state of block at {_b} = true with "block state 'lit' of block should have been true but was %lit state of block at {_b}%" - set lit state of block at {_b} to false - assert lit state of block at {_b} = false with "block state 'lit' of block should have been false but was %lit state of block at {_b}%" - - set block at {_b} to grass - assert snowy state of block at {_b} = false with "block state 'snowy' of block should have been false" - set snowy state of block at {_b} to true - assert snowy state of block at {_b} = true with "block state 'snowy' of block should have been true" - - set block at {_b} to north facing oak stairs - assert facing state of block at {_b} = north with "block state 'facing' of block should have been north" - assert waterlogged state of block at {_b} = false with "block state 'waterlogged' of block should have been false" - set facing state of block at {_b} to west - assert facing state of block at {_b} = west with "block state 'facing' of block should have been west" - set waterlogged state of block at {_b} to true - assert waterlogged state of block at {_b} = true with "block state 'waterlogged' of block should have been true" From d61a6a4a72e3a4faffd4f4a481a08c5ee130fd8f Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Thu, 6 Aug 2020 01:14:24 -0700 Subject: [PATCH 3/3] Update aliases --- skript-aliases | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skript-aliases b/skript-aliases index bf3e348ea26..ab21762093a 160000 --- a/skript-aliases +++ b/skript-aliases @@ -1 +1 @@ -Subproject commit bf3e348ea26842de4510b0b35ae515194bec6781 +Subproject commit ab21762093ad5a354e7c39be7f17cf4a326f69f4