/
CopyBlockCommand.java
156 lines (130 loc) · 6.25 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
150
151
152
153
154
155
156
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.BlockData;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.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 {
// <--[command]
// @Name CopyBlock
// @Syntax copyblock [<location>] [to:<location>] (remove_original)
// @Required 1
// @Short Copies a block to another location, keeping metadata when possible.
// @Group world
//
// @Description
// Copies a block to another location.
// You may also use the 'remove_original' argument to delete the original block.
// This effectively moves the block to the target location.
//
// @Tags
// <LocationTag.material>
//
// @Usage
// Use to copy the block the player is looking at to their current location
// - copyblock <player.cursor_on> to:<player.location>
//
// @Usage
// Use to move the block the player is looking at to their current location (removing it from its original location)
// - copyblock <player.cursor_on> to:<player.location> remove_original
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesArgumentType(LocationTag.class)
&& !scriptEntry.hasObject("location")
&& !arg.matchesPrefix("t", "to")) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (arg.matchesArgumentType(LocationTag.class)
&& arg.matchesPrefix("t", "to")) {
scriptEntry.addObject("destination", arg.asType(LocationTag.class));
}
else if (arg.matches("remove_original")) {
scriptEntry.addObject("remove", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
// Check required arguments
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a source location.");
}
if (!scriptEntry.hasObject("destination")) {
throw new InvalidArgumentsException("Must specify a destination location.");
}
// Set defaults
scriptEntry.defaultObject("remove", new ElementTag(false));
}
@Override
public void execute(ScriptEntry scriptEntry) {
LocationTag copy_location = (LocationTag) scriptEntry.getObject("location");
LocationTag destination = (LocationTag) scriptEntry.getObject("destination");
ElementTag remove_original = (ElementTag) scriptEntry.getObject("remove");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (copy_location != null ? copy_location.debug() : "")
+ destination.debug() + remove_original.debug());
}
List<Location> locations = new ArrayList<>();
if (copy_location != null) {
locations.add(copy_location);
}
for (Location loc : locations) {
Block source = loc.getBlock();
BlockState sourceState = source.getState();
Block update = destination.getBlock();
// TODO: 1.13 - confirm this works
BlockData blockData = NMSHandler.getBlockHelper().getBlockData(source);
blockData.setBlock(update, false);
BlockState updateState = update.getState();
// 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 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);
}
}
}
}