|
1 | 1 | package ch.njol.skript.effects; |
2 | 2 |
|
3 | | -import org.bukkit.Location; |
4 | | -import org.bukkit.Material; |
5 | | -import org.bukkit.block.Block; |
6 | | -import org.bukkit.block.data.BlockData; |
7 | | -import org.bukkit.entity.Player; |
8 | | -import org.bukkit.event.Event; |
9 | | -import org.jetbrains.annotations.Nullable; |
10 | | - |
11 | 3 | import ch.njol.skript.Skript; |
12 | 4 | import ch.njol.skript.aliases.ItemType; |
13 | 5 | import ch.njol.skript.doc.Description; |
14 | | -import ch.njol.skript.doc.Examples; |
| 6 | +import ch.njol.skript.doc.Example; |
15 | 7 | import ch.njol.skript.doc.Name; |
16 | 8 | import ch.njol.skript.doc.Since; |
17 | 9 | import ch.njol.skript.lang.Effect; |
18 | 10 | import ch.njol.skript.lang.Expression; |
19 | | -import ch.njol.skript.lang.SkriptParser; |
| 11 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 12 | +import ch.njol.skript.lang.SyntaxStringBuilder; |
20 | 13 | import ch.njol.util.Kleenean; |
| 14 | +import org.bukkit.Location; |
| 15 | +import org.bukkit.block.data.BlockData; |
| 16 | +import org.bukkit.entity.Player; |
| 17 | +import org.bukkit.event.Event; |
| 18 | +import org.jetbrains.annotations.Nullable; |
21 | 19 |
|
22 | 20 | @Name("Send Block Change") |
23 | | -@Description("Makes a player see a block as something it really isn't. BlockData support is only for MC 1.13+") |
24 | | -@Examples({"make player see block at player as dirt", |
25 | | - "make player see target block as campfire[facing=south]"}) |
26 | | -@Since("2.2-dev37c, 2.5.1 (block data support)") |
| 21 | +@Description("Makes a player see a block as something else or as the original.") |
| 22 | +@Example("make player see block at player as dirt") |
| 23 | +@Example("make player see player's target block as campfire[facing=south]") |
| 24 | +@Example(""" |
| 25 | + make all players see (blocks in radius 5 of location(0, 0, 0)) as bedrock |
| 26 | + make all players see (blocks in radius 5 of location(0, 0, 0)) as original |
| 27 | + """) |
| 28 | +@Since("2.2-dev37c, 2.5.1 (block data support), INSERT VERSION (as original)") |
27 | 29 | public class EffSendBlockChange extends Effect { |
28 | 30 |
|
29 | | - private static final boolean BLOCK_DATA_SUPPORT = Skript.classExists("org.bukkit.block.data.BlockData"); |
30 | | - private static final boolean SUPPORTED = |
31 | | - Skript.methodExists( |
32 | | - Player.class, |
33 | | - "sendBlockChange", |
34 | | - Location.class, |
35 | | - Material.class, |
36 | | - byte.class |
37 | | - ); |
38 | | - |
39 | 31 | static { |
40 | 32 | Skript.registerEffect(EffSendBlockChange.class, |
41 | | - BLOCK_DATA_SUPPORT ? "make %players% see %blocks% as %itemtype/blockdata%" : "make %players% see %blocks% as %itemtype%" |
| 33 | + "make %players% see %locations% as %itemtype/blockdata%", |
| 34 | + "make %players% see %locations% as [the|its] (original|normal|actual) [block]" |
42 | 35 | ); |
43 | 36 | } |
44 | 37 |
|
45 | | - @SuppressWarnings("null") |
46 | 38 | private Expression<Player> players; |
47 | | - |
48 | | - @SuppressWarnings("null") |
49 | | - private Expression<Block> blocks; |
50 | | - |
51 | | - @SuppressWarnings("null") |
52 | | - private Expression<Object> as; |
| 39 | + private Expression<Location> locations; |
| 40 | + private @Nullable Expression<Object> type; |
| 41 | + private boolean asOriginal; |
53 | 42 |
|
54 | 43 | @Override |
55 | 44 | @SuppressWarnings("unchecked") |
56 | | - public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { |
57 | | - if (!SUPPORTED) { |
58 | | - Skript.error("The send block change effect is not supported on this version. " + |
59 | | - "If Spigot has added a replacement method without magic values " + |
60 | | - "please open an issue at https://github.com/SkriptLang/Skript/issues " + |
61 | | - "and support will be added for it."); |
62 | | - return false; |
63 | | - } |
| 45 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
64 | 46 | players = (Expression<Player>) exprs[0]; |
65 | | - blocks = (Expression<Block>) exprs[1]; |
66 | | - as = (Expression<Object>) exprs[2]; |
| 47 | + locations = (Expression<Location>) exprs[1]; |
| 48 | + asOriginal = matchedPattern == 1; |
| 49 | + if (!asOriginal) |
| 50 | + type = (Expression<Object>) exprs[2]; |
67 | 51 | return true; |
68 | 52 | } |
69 | 53 |
|
70 | 54 | @Override |
71 | | - protected void execute(Event e) { |
72 | | - Object object = this.as.getSingle(e); |
73 | | - if (object instanceof ItemType) { |
74 | | - ItemType itemType = (ItemType) object; |
75 | | - for (Player player : players.getArray(e)) { |
76 | | - for (Block block : blocks.getArray(e)) { |
77 | | - itemType.sendBlockChange(player, block.getLocation()); |
78 | | - } |
| 55 | + protected void execute(Event event) { |
| 56 | + if (asOriginal) { |
| 57 | + Player[] players = this.players.getArray(event); |
| 58 | + for (Location location : locations.getArray(event)) { |
| 59 | + for (Player player : players) |
| 60 | + player.sendBlockChange(location, location.getBlock().getBlockData()); |
79 | 61 | } |
80 | | - } else if (BLOCK_DATA_SUPPORT && object instanceof BlockData) { |
81 | | - BlockData blockData = (BlockData) object; |
82 | | - for (Player player : players.getArray(e)) { |
83 | | - for (Block block : blocks.getArray(e)) { |
84 | | - player.sendBlockChange(block.getLocation(), blockData); |
85 | | - } |
| 62 | + return; |
| 63 | + } |
| 64 | + assert type != null; |
| 65 | + Object type = this.type.getSingle(event); |
| 66 | + if (type == null) |
| 67 | + return; |
| 68 | + Player[] players = this.players.getArray(event); |
| 69 | + if (type instanceof ItemType itemType) { |
| 70 | + for (Location location : locations.getArray(event)) { |
| 71 | + for (Player player : players) |
| 72 | + itemType.sendBlockChange(player, location); |
| 73 | + } |
| 74 | + } else if (type instanceof BlockData blockData) { |
| 75 | + for (Location location : locations.getArray(event)) { |
| 76 | + for (Player player : players) |
| 77 | + player.sendBlockChange(location, blockData); |
86 | 78 | } |
87 | 79 | } |
88 | 80 | } |
89 | 81 |
|
90 | 82 | @Override |
91 | | - public String toString(@Nullable Event e, boolean debug) { |
92 | | - return String.format( |
93 | | - "make %s see %s as %s", |
94 | | - players.toString(e, debug), |
95 | | - blocks.toString(e, debug), |
96 | | - as.toString(e, debug) |
97 | | - ); |
| 83 | + public String toString(@Nullable Event event, boolean debug) { |
| 84 | + SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); |
| 85 | + builder.append("make", players, "see", locations, "as"); |
| 86 | + if (asOriginal) { |
| 87 | + builder.append("original"); |
| 88 | + } else { |
| 89 | + assert type != null; |
| 90 | + builder.append(type); |
| 91 | + } |
| 92 | + return builder.toString(); |
98 | 93 | } |
99 | 94 |
|
100 | 95 | } |
0 commit comments