Skip to content

Commit 35ac251

Browse files
committed
Fix toggling 1.8 doors (#114)
1 parent 8ce56b2 commit 35ac251

File tree

1 file changed

+132
-108
lines changed

1 file changed

+132
-108
lines changed
Lines changed: 132 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,132 @@
1-
/*
2-
* This file is part of Skript.
3-
*
4-
* Skript is free software: you can redistribute it and/or modify
5-
* it under the terms of the GNU General Public License as published by
6-
* the Free Software Foundation, either version 3 of the License, or
7-
* (at your option) any later version.
8-
*
9-
* Skript is distributed in the hope that it will be useful,
10-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
* GNU General Public License for more details.
13-
*
14-
* You should have received a copy of the GNU General Public License
15-
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
16-
*
17-
*
18-
* Copyright 2011-2014 Peter Güttinger
19-
*
20-
*/
21-
22-
package ch.njol.skript.effects;
23-
24-
import org.bukkit.Material;
25-
import org.bukkit.block.Block;
26-
import org.bukkit.block.BlockFace;
27-
import org.bukkit.event.Event;
28-
import org.eclipse.jdt.annotation.Nullable;
29-
30-
import ch.njol.skript.Skript;
31-
import ch.njol.skript.doc.Description;
32-
import ch.njol.skript.doc.Examples;
33-
import ch.njol.skript.doc.Name;
34-
import ch.njol.skript.doc.Since;
35-
import ch.njol.skript.lang.Effect;
36-
import ch.njol.skript.lang.Expression;
37-
import ch.njol.skript.lang.SkriptParser.ParseResult;
38-
import ch.njol.util.Kleenean;
39-
40-
/**
41-
* @author Peter Güttinger
42-
*/
43-
@SuppressWarnings("deprecation")
44-
@Name("Toggle")
45-
@Description("Toggle the state of a block.")
46-
@Examples({"# use arrows to toggle switches, doors, etc.",
47-
"on projectile hit:",
48-
" projectile is arrow",
49-
" toggle the block at the arrow"})
50-
@Since("1.4")
51-
public class EffToggle extends Effect {
52-
static {
53-
Skript.registerEffect(EffToggle.class, "(close|turn off|de[-]activate) %blocks%", "(toggle|switch) [[the] state of] %blocks%", "(open|turn on|activate) %blocks%");
54-
}
55-
56-
@SuppressWarnings("null")
57-
private Expression<Block> blocks;
58-
private int toggle;
59-
60-
@SuppressWarnings({"unchecked", "null"})
61-
@Override
62-
public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
63-
blocks = (Expression<Block>) vars[0];
64-
toggle = matchedPattern - 1;
65-
return true;
66-
}
67-
68-
// TODO !Update with every version [blocks]
69-
private final static byte[] bitFlags = new byte[Skript.MAXBLOCKID + 1];
70-
static {
71-
bitFlags[Material.DETECTOR_RAIL.getId()] = 0x8;
72-
bitFlags[Material.WOODEN_DOOR.getId()] = 0x4;
73-
bitFlags[Material.IRON_DOOR_BLOCK.getId()] = 0x4;
74-
bitFlags[Material.LEVER.getId()] = 0x8;
75-
bitFlags[Material.STONE_PLATE.getId()] = 0x1;
76-
bitFlags[Material.WOOD_PLATE.getId()] = 0x1;
77-
bitFlags[Material.STONE_BUTTON.getId()] = 0x8;
78-
bitFlags[Material.TRAP_DOOR.getId()] = 0x4;
79-
bitFlags[Material.FENCE_GATE.getId()] = 0x4;
80-
}
81-
82-
@Override
83-
protected void execute(final Event e) {
84-
for (Block b : blocks.getArray(e)) {
85-
int type = b.getTypeId();
86-
byte data = b.getData();
87-
if ((type == Material.WOODEN_DOOR.getId() || type == Material.IRON_DOOR_BLOCK.getId()) && (data & 0x8) == 0x8) {
88-
b = b.getRelative(BlockFace.DOWN);
89-
type = b.getTypeId();
90-
if (type != Material.WOODEN_DOOR.getId() && type != Material.IRON_DOOR_BLOCK.getId())
91-
continue;
92-
data = b.getData();
93-
}
94-
if (toggle == -1)
95-
b.setData((byte) (data & ~bitFlags[type]));
96-
else if (toggle == 0)
97-
b.setData((byte) (data ^ bitFlags[type]));
98-
else
99-
b.setData((byte) (data | bitFlags[type]));
100-
}
101-
}
102-
103-
@Override
104-
public String toString(final @Nullable Event e, final boolean debug) {
105-
return "toggle " + blocks.toString(e, debug);
106-
}
107-
108-
}
1+
/*
2+
* This file is part of Skript.
3+
*
4+
* Skript is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Skript is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*
18+
* Copyright 2011-2014 Peter Güttinger
19+
*
20+
*/
21+
22+
package ch.njol.skript.effects;
23+
24+
import org.bukkit.Material;
25+
import org.bukkit.block.Block;
26+
import org.bukkit.block.BlockFace;
27+
import org.bukkit.event.Event;
28+
import org.eclipse.jdt.annotation.Nullable;
29+
30+
import ch.njol.skript.Skript;
31+
import ch.njol.skript.doc.Description;
32+
import ch.njol.skript.doc.Examples;
33+
import ch.njol.skript.doc.Name;
34+
import ch.njol.skript.doc.Since;
35+
import ch.njol.skript.lang.Effect;
36+
import ch.njol.skript.lang.Expression;
37+
import ch.njol.skript.lang.SkriptParser.ParseResult;
38+
import ch.njol.util.Kleenean;
39+
40+
/**
41+
* @author Peter Güttinger
42+
*/
43+
@SuppressWarnings("deprecation")
44+
@Name("Toggle")
45+
@Description("Toggle the state of a block.")
46+
@Examples({"# use arrows to toggle switches, doors, etc.",
47+
"on projectile hit:",
48+
" projectile is arrow",
49+
" toggle the block at the arrow"})
50+
@Since("1.4")
51+
public class EffToggle extends Effect {
52+
static {
53+
Skript.registerEffect(EffToggle.class, "(close|turn off|de[-]activate) %blocks%", "(toggle|switch) [[the] state of] %blocks%", "(open|turn on|activate) %blocks%");
54+
}
55+
56+
@SuppressWarnings("null")
57+
private Expression<Block> blocks;
58+
private int toggle;
59+
60+
@SuppressWarnings({"unchecked", "null"})
61+
@Override
62+
public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
63+
blocks = (Expression<Block>) vars[0];
64+
toggle = matchedPattern - 1;
65+
return true;
66+
}
67+
68+
// TODO !Update with every version [blocks]
69+
private final static byte[] bitFlags = new byte[Skript.MAXBLOCKID + 1];
70+
private final static boolean[] doors = new boolean[Skript.MAXBLOCKID + 1]; // Update also array length
71+
static {
72+
bitFlags[Material.DETECTOR_RAIL.getId()] = 0x8;
73+
// Doors
74+
bitFlags[Material.WOODEN_DOOR.getId()] = 0x4;
75+
bitFlags[Material.SPRUCE_DOOR.getId()] = 0x4;
76+
bitFlags[Material.BIRCH_DOOR.getId()] = 0x4;
77+
bitFlags[Material.JUNGLE_DOOR.getId()] = 0x4;
78+
bitFlags[Material.ACACIA_DOOR.getId()] = 0x4;
79+
bitFlags[Material.DARK_OAK_DOOR.getId()] = 0x4;
80+
bitFlags[Material.IRON_DOOR_BLOCK.getId()] = 0x4;
81+
// Redstone stuff
82+
bitFlags[Material.LEVER.getId()] = 0x8;
83+
bitFlags[Material.STONE_PLATE.getId()] = 0x1;
84+
bitFlags[Material.WOOD_PLATE.getId()] = 0x1;
85+
bitFlags[Material.STONE_BUTTON.getId()] = 0x8;
86+
// Trapdoors
87+
bitFlags[Material.TRAP_DOOR.getId()] = 0x4;
88+
bitFlags[Material.IRON_TRAPDOOR.getId()] = 0x4;
89+
// Fence gates
90+
bitFlags[Material.FENCE_GATE.getId()] = 0x4;
91+
bitFlags[Material.SPRUCE_FENCE_GATE.getId()] = 0x4;
92+
bitFlags[Material.BIRCH_FENCE_GATE.getId()] = 0x4;
93+
bitFlags[Material.JUNGLE_FENCE_GATE.getId()] = 0x4;
94+
bitFlags[Material.DARK_OAK_FENCE_GATE.getId()] = 0x4;
95+
bitFlags[Material.ACACIA_FENCE_GATE.getId()] = 0x4;
96+
97+
doors[Material.WOODEN_DOOR.getId()] = true;
98+
doors[Material.SPRUCE_DOOR.getId()] = true;
99+
doors[Material.BIRCH_DOOR.getId()] = true;
100+
doors[Material.JUNGLE_DOOR.getId()] = true;
101+
doors[Material.ACACIA_DOOR.getId()] = true;
102+
doors[Material.DARK_OAK_DOOR.getId()] = true;
103+
doors[Material.IRON_DOOR_BLOCK.getId()] = true;
104+
}
105+
106+
@Override
107+
protected void execute(final Event e) {
108+
for (Block b : blocks.getArray(e)) {
109+
int type = b.getTypeId();
110+
byte data = b.getData();
111+
if (doors[type] == true && (data & 0x8) == 0x8) {
112+
b = b.getRelative(BlockFace.DOWN);
113+
type = b.getTypeId();
114+
if (doors[type] != true)
115+
continue;
116+
data = b.getData();
117+
}
118+
if (toggle == -1)
119+
b.setData((byte) (data & ~bitFlags[type]));
120+
else if (toggle == 0)
121+
b.setData((byte) (data ^ bitFlags[type]));
122+
else
123+
b.setData((byte) (data | bitFlags[type]));
124+
}
125+
}
126+
127+
@Override
128+
public String toString(final @Nullable Event e, final boolean debug) {
129+
return "toggle " + blocks.toString(e, debug);
130+
}
131+
132+
}

0 commit comments

Comments
 (0)