|
1 | 1 | package ch.njol.skript.effects; |
2 | 2 |
|
3 | | -import java.lang.invoke.MethodHandle; |
4 | | -import java.lang.invoke.MethodHandles; |
5 | | -import java.lang.invoke.MethodType; |
| 3 | +import java.util.function.Function; |
6 | 4 |
|
7 | 5 | import org.bukkit.block.Block; |
8 | | -import org.bukkit.block.BlockFace; |
9 | 6 | import org.bukkit.block.data.BlockData; |
10 | 7 | import org.bukkit.block.data.Openable; |
11 | 8 | import org.bukkit.block.data.Powerable; |
12 | 9 | import org.bukkit.event.Event; |
| 10 | +import org.jetbrains.annotations.NotNull; |
13 | 11 | import org.jetbrains.annotations.Nullable; |
14 | 12 |
|
15 | 13 | import ch.njol.skript.Skript; |
| 14 | +import ch.njol.skript.classes.Changer.ChangeMode; |
| 15 | +import ch.njol.skript.classes.Changer.ChangerUtils; |
16 | 16 | import ch.njol.skript.doc.Description; |
17 | 17 | import ch.njol.skript.doc.Examples; |
18 | 18 | import ch.njol.skript.doc.Name; |
19 | 19 | import ch.njol.skript.doc.Since; |
20 | 20 | import ch.njol.skript.lang.Effect; |
21 | 21 | import ch.njol.skript.lang.Expression; |
22 | 22 | import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 23 | +import ch.njol.skript.util.Patterns; |
23 | 24 | import ch.njol.util.Kleenean; |
24 | 25 |
|
25 | | -/** |
26 | | - * @author Peter Güttinger |
27 | | - */ |
28 | | -@SuppressWarnings("deprecation") |
29 | 26 | @Name("Toggle") |
30 | | -@Description("Toggle the state of a block.") |
| 27 | +@Description("Toggle the state of a block or boolean.") |
31 | 28 | @Examples({"# use arrows to toggle switches, doors, etc.", |
32 | 29 | "on projectile hit:", |
33 | 30 | "\tprojectile is arrow", |
34 | | - "\ttoggle the block at the arrow"}) |
35 | | -@Since("1.4") |
| 31 | + "\ttoggle the block at the arrow", |
| 32 | + "", |
| 33 | + "# With booleans", |
| 34 | + "toggle gravity of player" |
| 35 | +}) |
| 36 | +@Since("1.4, INSERT VERSION (booleans)") |
36 | 37 | public class EffToggle extends Effect { |
37 | | - |
| 38 | + |
| 39 | + private enum Action { |
| 40 | + ACTIVATE, DEACTIVATE, TOGGLE; |
| 41 | + |
| 42 | + public boolean apply(boolean current) { |
| 43 | + return switch(this) { |
| 44 | + case ACTIVATE -> true; |
| 45 | + case DEACTIVATE -> false; |
| 46 | + case TOGGLE -> !current; |
| 47 | + }; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private enum Type { |
| 52 | + BLOCKS, BOOLEANS, MIXED; |
| 53 | + |
| 54 | + /** |
| 55 | + * Determines the appropriate Type based on the return type of an expression. |
| 56 | + * @param returnType The class representing the return type |
| 57 | + * @return The corresponding Type |
| 58 | + */ |
| 59 | + public static Type fromClass(Class<?> returnType) { |
| 60 | + boolean isBlockType = Block.class.isAssignableFrom(returnType); |
| 61 | + boolean isBooleanType = Boolean.class.isAssignableFrom(returnType); |
| 62 | + |
| 63 | + if (isBlockType && !isBooleanType) { |
| 64 | + return BLOCKS; |
| 65 | + } else if (isBooleanType && !isBlockType) { |
| 66 | + return BOOLEANS; |
| 67 | + } else { |
| 68 | + return MIXED; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static final Patterns<Action> patterns = new Patterns<>(new Object[][]{ |
| 74 | + {"(open|turn on|activate) %blocks%", Action.ACTIVATE}, |
| 75 | + {"(close|turn off|de[-]activate) %blocks%", Action.DEACTIVATE}, |
| 76 | + {"(toggle|switch) [[the] state of] %blocks/booleans%", Action.TOGGLE} |
| 77 | + }); |
| 78 | + |
38 | 79 | static { |
39 | | - Skript.registerEffect(EffToggle.class, "(close|turn off|de[-]activate) %blocks%", "(toggle|switch) [[the] state of] %blocks%", "(open|turn on|activate) %blocks%"); |
| 80 | + Skript.registerEffect(EffToggle.class, patterns.getPatterns()); |
40 | 81 | } |
41 | 82 |
|
42 | | - @SuppressWarnings("null") |
43 | | - private Expression<Block> blocks; |
44 | | - private int toggle; |
45 | | - |
46 | | - @SuppressWarnings({"unchecked", "null"}) |
| 83 | + private Expression<?> togglables; |
| 84 | + private Action action; |
| 85 | + private Type type; |
| 86 | + |
47 | 87 | @Override |
48 | | - public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { |
49 | | - blocks = (Expression<Block>) vars[0]; |
50 | | - toggle = matchedPattern - 1; |
| 88 | + public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
| 89 | + togglables = expressions[0]; |
| 90 | + action = patterns.getInfo(matchedPattern); |
| 91 | + |
| 92 | + // Determine expression type using the enum method |
| 93 | + type = Type.fromClass(togglables.getReturnType()); |
| 94 | + |
| 95 | + // Validate based on type |
| 96 | + if (type == Type.BOOLEANS && |
| 97 | + !ChangerUtils.acceptsChange(togglables, ChangeMode.SET, Boolean.class)) { |
| 98 | + Skript.error("Cannot toggle '" + togglables + "' as it cannot be set to booleans."); |
| 99 | + return false; |
| 100 | + } else if (type == Type.MIXED && !ChangerUtils.acceptsChange(togglables, ChangeMode.SET, Block.class, Boolean.class)) { |
| 101 | + Skript.error("Cannot toggle '" + togglables + "' as it cannot be set to both blocks and booleans."); |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
51 | 105 | return true; |
52 | 106 | } |
53 | 107 |
|
54 | 108 | @Override |
55 | | - protected void execute(final Event e) { |
56 | | - for (Block b : blocks.getArray(e)) { |
57 | | - BlockData data = b.getBlockData(); |
58 | | - if (toggle == -1) { |
59 | | - if (data instanceof Openable) |
60 | | - ((Openable) data).setOpen(false); |
61 | | - else if (data instanceof Powerable) |
62 | | - ((Powerable) data).setPowered(false); |
63 | | - } else if (toggle == 1) { |
64 | | - if (data instanceof Openable) |
65 | | - ((Openable) data).setOpen(true); |
66 | | - else if (data instanceof Powerable) |
67 | | - ((Powerable) data).setPowered(true); |
68 | | - } else { |
69 | | - if (data instanceof Openable) // open = NOT was open |
70 | | - ((Openable) data).setOpen(!((Openable) data).isOpen()); |
71 | | - else if (data instanceof Powerable) // power = NOT power |
72 | | - ((Powerable) data).setPowered(!((Powerable) data).isPowered()); |
| 109 | + protected void execute(Event event) { |
| 110 | + switch (type) { |
| 111 | + case BOOLEANS -> toggleBooleans(event); |
| 112 | + case BLOCKS -> toggleBlocks(event); |
| 113 | + case MIXED -> toggleMixed(event); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Toggles blocks by opening/closing or powering/unpowering them. |
| 119 | + * @param event the event used for evaluation |
| 120 | + */ |
| 121 | + private void toggleBlocks(Event event) { |
| 122 | + for (Object obj : togglables.getArray(event)) { |
| 123 | + if (obj instanceof Block block) { |
| 124 | + toggleSingleBlock(block); |
73 | 125 | } |
74 | | - |
75 | | - b.setBlockData(data); |
76 | 126 | } |
77 | 127 | } |
78 | 128 |
|
| 129 | + /** |
| 130 | + * Toggles a single block, either by opening/closing or powering/unpowering it. |
| 131 | + * @param block The block to toggle |
| 132 | + */ |
| 133 | + private void toggleSingleBlock(@NotNull Block block) { |
| 134 | + BlockData data = block.getBlockData(); |
| 135 | + if (data instanceof Openable openable) { |
| 136 | + openable.setOpen(action.apply(openable.isOpen())); |
| 137 | + } else if (data instanceof Powerable powerable) { |
| 138 | + powerable.setPowered(action.apply(powerable.isPowered())); |
| 139 | + } |
| 140 | + block.setBlockData(data); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Uses {@link Expression#changeInPlace(Event, Function)} to toggle booleans. |
| 145 | + * @param event the event used for evaluation |
| 146 | + */ |
| 147 | + private void toggleBooleans(Event event) { |
| 148 | + togglables.changeInPlace(event, (obj) -> { |
| 149 | + if (!(obj instanceof Boolean bool)) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + return action.apply(bool); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Uses {@link Expression#changeInPlace(Event, Function)} to toggle both blocks and booleans. |
| 158 | + * @param event the event used for evaluation |
| 159 | + */ |
| 160 | + private void toggleMixed(Event event) { |
| 161 | + togglables.changeInPlace(event, (obj) -> { |
| 162 | + if (obj instanceof Block block) { |
| 163 | + toggleSingleBlock(block); |
| 164 | + return block; |
| 165 | + } else if (obj instanceof Boolean bool) { |
| 166 | + return action.apply(bool); |
| 167 | + } |
| 168 | + return obj; |
| 169 | + }); |
| 170 | + } |
| 171 | + |
79 | 172 | @Override |
80 | | - public String toString(final @Nullable Event e, final boolean debug) { |
81 | | - return "toggle " + blocks.toString(e, debug); |
| 173 | + public String toString(@Nullable Event event, boolean debug) { |
| 174 | + String actionText = switch (action) { |
| 175 | + case ACTIVATE -> "activate"; |
| 176 | + case DEACTIVATE -> "deactivate"; |
| 177 | + case TOGGLE -> "toggle"; |
| 178 | + }; |
| 179 | + return actionText + " " + togglables.toString(event, debug); |
82 | 180 | } |
83 | | - |
84 | 181 | } |
0 commit comments