-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ModifyBlockCommand.java
176 lines (143 loc) · 6.77 KB
/
ModifyBlockCommand.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
/**
* Modifies blocks based based of single block location.
* Possibility to do faux animations with blocks.
*
* @author Mason Adkins
*/
public class ModifyBlockCommand extends AbstractCommand{
@Override
public void onEnable() {
//nothing to do here
}
/* MODIFYBLOCK [LOCATION:x,y,z,world] [MATERIAL:DATA VALUE] (RADIUS:##) (HEIGHT:##) (DEPTH:##) */
/*
* Arguments: [] - Required, () - Optional
* [LOCATION:x,y,z,world] Block location
* [MATERIAL|M] Material/ID to change block(s) to
* (RADIUS|R) Radius of the selection, default is zero (only changes the one block)
* (DEPTH|D) Depth of the selection, default is zero
* (HEIGHT|H) Height of the selection, default is zero
*
* Example Usage:
* MODIFYBLOCK LOCATION:??? MATERIAL:GRASS RADIUS:2 DEPTH:1 HEIGHT:1
* MODIFYBLOCK LOCATION:??? M:STONE R:2 D:3 H:2
*
*/
@Override
public void parseArgs(ScriptEntry scriptEntry)throws InvalidArgumentsException {
Material material = null;
int data = 0;
dLocation location = null;
int radius = 0;
int height = 0;
int depth = 0;
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesLocation(arg)){
location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to: " + location);
}
else if (aH.matchesValueArg("MATERIAL, M", arg, ArgumentType.Custom)) {
String value = aH.getStringFrom(arg).toUpperCase();
if (value.split(":", 2).length > 1) {
data = aH.getIntegerFrom(value.split(":", 2)[1]);
}
value = value.split(":", 2)[0];
if (aH.matchesInteger(value)) {
material = Material.getMaterial(aH.getIntegerFrom(value));
}
else {
material = Material.getMaterial(value);
}
if (material != null) dB.echoDebug("...material set to " + material);
else dB.echoDebug("...material not valid.");
}
else if (aH.matchesValueArg("RADIUS, R", arg, ArgumentType.Integer)) {
radius = aH.getIntegerFrom(arg);
dB.echoDebug("...radius set to " + radius);
}
else if (aH.matchesValueArg("HEIGHT, H", arg, ArgumentType.Integer)) {
height = aH.getIntegerFrom(arg);
dB.echoDebug("...height set to " + height);
}
else if (aH.matchesValueArg("DEPTH, D", arg, ArgumentType.Integer)) {
depth = aH.getIntegerFrom(arg);
dB.echoDebug("...depth set to " + depth);
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
// Store objects in ScriptEntry for use in execute()
scriptEntry.addObject("material", material);
scriptEntry.addObject("data", data);
scriptEntry.addObject("location", location);
scriptEntry.addObject("radius", radius);
scriptEntry.addObject("height", height);
scriptEntry.addObject("depth", depth);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
final Material material = (Material) scriptEntry.getObject("material");
final int data = (Integer) scriptEntry.getObject("data");
final dLocation location = (dLocation) scriptEntry.getObject("location");
final int radius = (Integer) scriptEntry.getObject("radius");
final int height = (Integer) scriptEntry.getObject("height");
final int depth = (Integer) scriptEntry.getObject("depth");
if (location == null || material == null){
dB.echoDebug("...can not exectue");
return;
}
World world = scriptEntry.getPlayer().getPlayerEntity().getWorld();
Block startBlock = location.getBlock();
Block currentBlock;
startBlock.setType(material);
startBlock.setData((byte) data);
if (radius != 0){
for (int x = 0; x < 2*radius+1; x++){
for (int z = 0; z < 2*radius+1; z++){
currentBlock = world.getBlockAt(startBlock.getX() + x - radius, startBlock.getY(), startBlock.getZ() + z - radius);
if (currentBlock.getType() != material){
currentBlock.setType(material);
currentBlock.setData((byte) data);
}
}
}
}
if (height != 0){
for (int x = 0; x < 2*radius+1; x++){
for (int z = 0; z < 2*radius+1; z++){
for (int y = 1; y < height + 1; y++){
currentBlock = world.getBlockAt(startBlock.getX() + x - radius, startBlock.getY() + y, startBlock.getZ() + z - radius);
if (currentBlock.getType() != material){
currentBlock.setType(material);
currentBlock.setData((byte) data);
}
}
}
}
}
if (depth != 0){
for (int x = 0; x < 2*radius+1; x++){
for (int z = 0; z < 2*radius+1; z++){
for (int y = 1; y < depth + 1; y++){
currentBlock = world.getBlockAt(startBlock.getX() + x - radius, startBlock.getY() - y, startBlock.getZ() + z - radius);
if (currentBlock.getType() != material){
currentBlock.setType(material);
currentBlock.setData((byte) data);
}
}
}
}
}
}
}