-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
CopyBlockCommand.java
149 lines (122 loc) · 6.2 KB
/
CopyBlockCommand.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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.*;
import org.bukkit.inventory.InventoryHolder;
import java.util.ArrayList;
import java.util.List;
public class CopyBlockCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
// CopyBlock can move a single 'location' ...
if (arg.matchesArgumentType(dLocation.class)
&& !scriptEntry.hasObject("location")
&& !arg.matchesPrefix("t", "to")) {
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
// ... or and entire cuboid ...
else if (arg.matchesArgumentType(dCuboid.class)
&& !scriptEntry.hasObject("cuboid")) {
scriptEntry.addObject("cuboid", arg.asType(dCuboid.class));
}
// ... to a location.
else if (arg.matchesArgumentType(dLocation.class)
&& arg.matchesPrefix("t", "to")) {
scriptEntry.addObject("destination", arg.asType(dLocation.class));
}
else if (arg.matches("remove_original")) {
scriptEntry.addObject("remove", new Element(true));
}
else {
arg.reportUnhandled();
}
}
// Check required arguments
if (!scriptEntry.hasObject("location") && !scriptEntry.hasObject("cuboid")) {
throw new InvalidArgumentsException("Must specify a source location or cuboid.");
}
if (!scriptEntry.hasObject("destination")) {
throw new InvalidArgumentsException("Must specify a destination location.");
}
// Set defaults
scriptEntry.defaultObject("remove", new Element(false));
}
@Override
public void execute(ScriptEntry scriptEntry) {
dLocation copy_location = (dLocation) scriptEntry.getObject("location");
dLocation destination = (dLocation) scriptEntry.getObject("destination");
dCuboid copy_cuboid = (dCuboid) scriptEntry.getObject("cuboid");
Element remove_original = (Element) scriptEntry.getObject("remove");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), (copy_location != null ? copy_location.debug() : "")
+ (copy_cuboid != null ? copy_cuboid.debug() : "") + destination.debug() + remove_original.debug());
}
List<Location> locations = new ArrayList<>();
if (copy_location != null) {
locations.add(copy_location);
}
else if (copy_cuboid != null) {
locations.addAll(copy_cuboid.getBlockLocations()); // TODO: make this work?...
}
for (Location loc : locations) {
Block source = loc.getBlock();
BlockState sourceState = dLocation.getBlockStateFor(source);
Block update = destination.getBlock();
// TODO: 1.13 - confirm this works
BlockData blockData = NMSHandler.getInstance().getBlockHelper().getBlockData(source);
blockData.setBlock(update, false);
BlockState updateState = dLocation.getBlockStateFor(update);
// Note: only a BlockState, not a Block, is actually an instance
// of InventoryHolder
if (sourceState instanceof InventoryHolder) {
((InventoryHolder) updateState).getInventory()
.setContents(((InventoryHolder) sourceState).getInventory().getContents());
}
else if (sourceState instanceof Sign) {
int n = 0;
for (String line : ((Sign) sourceState).getLines()) {
((Sign) updateState).setLine(n, line);
n++;
}
}
else if (sourceState instanceof NoteBlock) {
((NoteBlock) updateState).setNote(((NoteBlock) sourceState).getNote());
}
else if (sourceState instanceof Skull) {
((Skull) updateState).setSkullType(((Skull) sourceState).getSkullType());
((Skull) updateState).setOwner(((Skull) sourceState).getOwner());
((Skull) updateState).setRotation(((Skull) sourceState).getRotation());
}
else if (sourceState instanceof Jukebox) {
((Jukebox) updateState).setPlaying(((Jukebox) sourceState).getPlaying());
}
else if (sourceState instanceof Banner) {
((Banner) updateState).setBaseColor(((Banner) sourceState).getBaseColor());
((Banner) updateState).setPatterns(((Banner) sourceState).getPatterns());
}
else if (sourceState instanceof CommandBlock) {
((CommandBlock) updateState).setName(((CommandBlock) sourceState).getName());
((CommandBlock) updateState).setCommand(((CommandBlock) sourceState).getCommand());
}
else if (sourceState instanceof CreatureSpawner) {
((CreatureSpawner) updateState).setSpawnedType(((CreatureSpawner) sourceState).getSpawnedType());
((CreatureSpawner) updateState).setDelay(((CreatureSpawner) sourceState).getDelay());
}
updateState.update();
if (remove_original.asBoolean()) {
loc.getBlock().setType(Material.AIR);
}
}
}
}