-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
CopyBlockCommand.java
93 lines (67 loc) · 3.26 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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.inventory.InventoryHolder;
/**
* Modifies blocks based based of single block location.
* Possibility to do faux animations with blocks.
*
* @author Mason Adkins
*/
public class CopyBlockCommand extends AbstractCommand{
@Override
public void parseArgs(ScriptEntry scriptEntry)throws InvalidArgumentsException {
dLocation copy_location = null;
dLocation destination = null;
boolean remove_original = false;
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesLocation(arg))
copy_location = aH.getLocationFrom(arg);
else if (aH.matchesValueArg("to", arg, ArgumentType.Location))
destination = aH.getLocationFrom(arg);
else if (aH.matchesArg("and_remove", arg))
remove_original = true;
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
if (copy_location == null || destination == null)
throw new InvalidArgumentsException(Messages.ERROR_MISSING_LOCATION);
scriptEntry.addObject("copy_location", copy_location)
.addObject("destination", destination)
.addObject("remove_original", remove_original);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dLocation copy_location = (dLocation) scriptEntry.getObject("copy_location");
dLocation destination = (dLocation) scriptEntry.getObject("destination");
Boolean remove_original = (Boolean) scriptEntry.getObject("remove_original");
Block source = copy_location.getBlock();
BlockState sourceState = source.getState();
Block update = destination.getBlock();
update.setTypeIdAndData(source.getTypeId(), source.getData(), 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++;
}
updateState.update();
}
// TODO: Account for Noteblock, Skull, Jukebox, Sign
}
}