/
RedstoneFire.java
122 lines (102 loc) · 4.21 KB
/
RedstoneFire.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* CraftBook Copyright (C) EngineHub and Contributors <https://enginehub.org/>
*
* This program 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.
*
* This program 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 this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package org.enginehub.craftbook.mechanics;
import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.enginehub.craftbook.AbstractCraftBookMechanic;
import org.enginehub.craftbook.util.EventUtil;
import org.enginehub.craftbook.util.events.SourcedBlockRedstoneEvent;
import javax.annotation.Nonnull;
/**
* This mechanism allow players to toggle the fire on top of Netherrack or Soul Soil.
*/
public class RedstoneFire extends AbstractCraftBookMechanic {
@EventHandler(priority = EventPriority.HIGH)
public void onBlockRedstoneChange(SourcedBlockRedstoneEvent event) {
if (!EventUtil.passesFilter(event)) {
return;
}
if (event.isMinor()) {
return;
}
Material type = event.getBlock().getType();
if (!doesAffectBlock(event.getBlock().getType())) {
return;
}
Block above = event.getBlock().getRelative(BlockFace.UP);
Material aboveType = above.getType();
if (event.isOn() && canReplaceWithFire(aboveType)) {
above.setType(getFireForBlock(type));
} else if (!event.isOn() && (aboveType == Material.FIRE || aboveType == Material.SOUL_FIRE)) {
above.setType(Material.AIR);
}
}
@EventHandler(priority = EventPriority.HIGH)
public void onLeftClick(PlayerInteractEvent event) {
if (!EventUtil.passesFilter(event)) {
return;
}
if (event.getAction() != Action.LEFT_CLICK_BLOCK || !doesAffectBlock(event.getClickedBlock().getType())) {
return;
}
if (event.getBlockFace() == BlockFace.UP) {
Block fire = event.getClickedBlock().getRelative(event.getBlockFace());
Material fireMaterial = fire.getType();
if ((fireMaterial == Material.FIRE || fireMaterial == Material.SOUL_FIRE) && fire.getRelative(BlockFace.DOWN).isBlockPowered()) {
event.setCancelled(true);
}
}
}
private boolean doesAffectBlock(Material type) {
return (enableNetherrack && type == Material.NETHERRACK) || (enableSoulSoil && type == Material.SOUL_SOIL);
}
@Nonnull
private Material getFireForBlock(Material type) {
switch (type) {
case NETHERRACK:
return Material.FIRE;
case SOUL_SOIL:
return Material.SOUL_FIRE;
}
throw new RuntimeException("Tried to place fire on an unsupported block. Please report this error to CraftBook");
}
private static boolean canReplaceWithFire(Material type) {
switch (type) {
case SNOW:
case GRASS:
case VINE:
case DEAD_BUSH:
case AIR:
return true;
default:
return false;
}
}
private boolean enableNetherrack;
private boolean enableSoulSoil;
@Override
public void loadFromConfiguration(YAMLProcessor config) {
config.setComment("enable-netherrack", "Whether the mechanic should affect Netherrack.");
enableNetherrack = config.getBoolean("enable-netherrack", true);
config.setComment("enable-soul-soil", "Whether the mechanic should affect Soul Soil.");
enableSoulSoil = config.getBoolean("enable-soul-soil", true);
}
}