forked from Exultant/Citadel
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAcid.java
136 lines (124 loc) · 4.63 KB
/
Acid.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
package vg.civcraft.mc.citadel.command.commands;
import java.util.*;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.BlockIterator;
import vg.civcraft.mc.citadel.Citadel;
import vg.civcraft.mc.citadel.CitadelConfigManager;
import vg.civcraft.mc.citadel.ReinforcementManager;
import vg.civcraft.mc.citadel.Utility;
import vg.civcraft.mc.citadel.events.AcidBlockEvent;
import vg.civcraft.mc.citadel.reinforcement.PlayerReinforcement;
import vg.civcraft.mc.citadel.reinforcement.Reinforcement;
import vg.civcraft.mc.citadel.reinforcementtypes.ReinforcementType;
import vg.civcraft.mc.namelayer.NameAPI;
public class Acid extends PlayerCommandMiddle {
private ReinforcementManager rm = Citadel.getReinforcementManager();
public Acid(String name) {
super(name);
setIdentifier("ctacid");
setDescription("Removes the block above it if using an acid block.");
setUsage("/ctacid");
setArguments(0, 0);
}
@Override
public boolean execute(CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("You must be a player to perform this command.");
return true;
}
Player p = (Player) sender;
Iterator<Block> itr = new BlockIterator(p, 40); // Within 2.5 chunks
Material acidBlock = CitadelConfigManager.getAcidBlock();
while (itr.hasNext()) {
Block block = itr.next();
if (block.getType() != acidBlock) {
continue;
}
Reinforcement rein = rm.getReinforcement(block);
if (rein == null) {
sendAndLog(p, ChatColor.RED, "That block is not reinforced.");
return true;
}
if (!(rein instanceof PlayerReinforcement)) { // Just in case.
// Most chance it is a PlayerReinforcement but otherwise.
sendAndLog(p, ChatColor.RED,
"An acid block cannot be a natural reinforcement.");
return true;
}
PlayerReinforcement pRein = (PlayerReinforcement) rein;
UUID uuid = NameAPI.getUUID(p.getName());
if (pRein.getGroup() == null) {
sendAndLog(p, ChatColor.RED, "No-one is on that group.");
return true;
}
if (!pRein.getGroup().isMember(uuid)) {
sendAndLog(p, ChatColor.RED, "You do not belong on that group.");
return true;
}
if (!pRein.canAcid(p)) {
sendAndLog(p, ChatColor.RED, "You do not have sufficient permission to use acid blocks on this group.");
return true;
}
int time = Utility.timeUntilAcidMature(pRein);
if (time != 0) {
sendAndLog(p, ChatColor.RED, "That acid block is not mature yet.");
return true;
}
Block topFace = block.getRelative(BlockFace.UP);
if (Material.AIR.equals(topFace.getType())) {
sendAndLog(p, ChatColor.RED, "There is no block above to acid block.");
return true;
}
Reinforcement topRein = rm.getReinforcement(topFace);
if (topRein == null) {
sendAndLog(p, ChatColor.RED, "That block doesn't have a reinforcement.");
return true;
}
if (!(topRein instanceof PlayerReinforcement)) {
continue;
}
PlayerReinforcement pTopRein = (PlayerReinforcement) topRein;
ReinforcementType acidBlockType = ReinforcementType
.getReinforcementType(pRein.getStackRepresentation());
ReinforcementType topReinType = ReinforcementType
.getReinforcementType(pTopRein.getStackRepresentation());
if (acidBlockType.getAcidTime() < topReinType.getAcidTime()) {
sendAndLog(p, ChatColor.RED, "This acid block is too weak for that reinforcement.");
return true;
}
AcidBlockEvent event = new AcidBlockEvent(p, pRein, pTopRein);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
if (CitadelConfigManager.shouldLogInternal()) {
Citadel.getInstance().getLogger().log(Level.INFO,
"Acid block event cancelled for acid at " + pRein.getLocation() );
}
return true;
}
if (CitadelConfigManager.shouldLogHostileBreaks()) {
Citadel.getInstance().getLogger().log(Level.INFO, "Acid at {0} broke {1} at {2}",
new Object[] {block.getLocation(), topFace.getType(), topFace.getLocation()});
}
topFace.setType(Material.AIR);
block.breakNaturally();
// Consider if should simply be an AcidBlockEvent listener. This will do for now.
Utility.reinforcementBroken(p, pRein);
rm.deleteReinforcement(pTopRein);
BlockState bs = block.getState();
bs.update(false, true);
}
return true;
}
@Override
public List<String> tabComplete(CommandSender sender, String[] args) {
return new ArrayList<String>();
}
}