/
Ammeter.java
153 lines (133 loc) · 5.76 KB
/
Ammeter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* 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 com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.world.item.ItemTypes;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.AnaloguePowerable;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Lightable;
import org.bukkit.block.data.Powerable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.enginehub.craftbook.AbstractCraftBookMechanic;
import org.enginehub.craftbook.CraftBook;
import org.enginehub.craftbook.CraftBookPlayer;
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
import org.enginehub.craftbook.util.EventUtil;
import org.enginehub.craftbook.util.ItemSyntax;
import org.enginehub.craftbook.util.ProtectionUtil;
// TODO Potentially turn into a WE tool
/**
* This allows users to Right-click to check the power level of redstone.
*/
public class Ammeter extends AbstractCraftBookMechanic {
@EventHandler(priority = EventPriority.HIGH)
public void onRightClick(PlayerInteractEvent event) {
Block block = event.getClickedBlock();
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getHand() == null || block == null) {
return;
}
if (!EventUtil.passesFilter(event)) {
return;
}
if (!event.getPlayer().getInventory().getItem(event.getHand()).isSimilar(item)) {
return;
}
CraftBookPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
if (!player.hasPermission("craftbook.ammeter.use")) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages) {
player.printError("mech.use-permission");
}
return;
}
if (!ProtectionUtil.canUse(event.getPlayer(), block.getLocation(), event.getBlockFace(), event.getAction())) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages) {
player.printError("area.use-permissions");
}
return;
}
int data = getSpecialData(block);
if (data >= 0) {
Component component = TranslatableComponent
.of("craftbook.ammeter.line", getCurrentLine(data), TextComponent.of(data, TextColor.YELLOW))
.color(TextColor.YELLOW);
if (actionBar) {
player.printActionBar(component);
} else {
player.print(component);
}
event.setCancelled(true);
}
}
private int getSpecialData(Block block) {
Material type = block.getType();
BlockData blockData = block.getBlockData();
int current = -1;
if (blockData instanceof Powerable) {
current = ((Powerable) blockData).isPowered() ? 15 : 0;
} else if (blockData instanceof AnaloguePowerable) {
current = ((AnaloguePowerable) blockData).getPower();
} else if ((type == Material.REDSTONE_TORCH || type == Material.REDSTONE_WALL_TORCH) && blockData instanceof Lightable) {
current = ((Lightable) blockData).isLit() ? 15 : 0;
} else if (block.getType() == Material.REDSTONE_BLOCK) {
current = 15;
}
return current;
}
private Component getCurrentLine(int data) {
TextComponent.Builder line = TextComponent.builder();
line.append("[", TextColor.YELLOW);
TextComponent.Builder colorBuilder = TextComponent.builder();
if (data > 10) {
line.color(TextColor.DARK_GREEN);
} else if (data > 5) {
line.color(TextColor.GOLD);
} else if (data > 0) {
line.color(TextColor.DARK_RED);
}
for (int i = 0; i < data; i++) {
colorBuilder.append("|");
}
line.append(colorBuilder.build());
if (data < 15) {
TextComponent.Builder blackBuilder = TextComponent.builder();
blackBuilder.color(TextColor.BLACK);
for (int i = data; i < 15; i++) {
blackBuilder.append("|");
}
line.append(blackBuilder.build());
}
line.append("]", TextColor.YELLOW);
return line.build();
}
private ItemStack item;
private boolean actionBar;
@Override
public void loadFromConfiguration(YAMLProcessor config) {
config.setComment("item", "The item for the ammeter tool.");
item = ItemSyntax.getItem(config.getString("item", ItemTypes.COAL.getId()));
config.setComment("use-action-bar", "Whether to use the action bar or the player's chat.");
actionBar = config.getBoolean("use-action-bar", true);
}
}