This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
GiveCommand.java
100 lines (91 loc) · 3.79 KB
/
GiveCommand.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
package com.denizenscript.denizen2sponge.commands.items;
import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import com.denizenscript.denizen2sponge.tags.objects.InventoryTag;
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
import com.denizenscript.denizen2sponge.tags.objects.PlayerTag;
import org.spongepowered.api.item.inventory.Carrier;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
public class GiveCommand extends AbstractCommand {
// <--[explanation]
// @Since 0.3.0
// @Name Item Types
// @Group Useful Lists
// @Description
// A list of all default item types can be found here:
// <@link url https://jd.spongepowered.org/7.0.0-SNAPSHOT/org/spongepowered/api/item/ItemTypes.html>item types list<@/link>
// These can be used with the give and equip commands.
// -->
// <--[command]
// @Since 0.3.0
// @Name give
// @Arguments <inventory/entity> <item>
// @Short gives an inventory an item.
// @Updated 2018/06/05
// @Group Items
// @Minimum 2
// @Maximum 2
// @Description
// Gives an inventory an item. Input an entity that carries an inventory (such as a player) to give to that entity's inventory.
// Related information: <@link explanation Item Types>item types<@/link>.
// TODO: Explain more!
// @Example
// # This example gives the player a stone.
// - give <player> stone
// -->
@Override
public String getName() {
return "give";
}
@Override
public String getArguments() {
return "<inventory/entity> <item>";
}
@Override
public int getMinimumArguments() {
return 2;
}
@Override
public int getMaximumArguments() {
return 2;
}
@Override
public void execute(CommandQueue queue, CommandEntry entry) {
ItemTag item = ItemTag.getFor(queue.error, entry.getArgumentObject(queue, 1), queue);
AbstractTagObject ato = entry.getArgumentObject(queue, 0);
InventoryTag inv;
if (ato instanceof InventoryTag) {
inv = (InventoryTag) ato;
}
else {
EntityTag et = EntityTag.getFor(queue.error, ato);
if (et instanceof Carrier) {
inv = new InventoryTag(((Carrier) et).getInventory());
}
else {
queue.error.run("Entity " + ColorSet.emphasis + et.debug() + ColorSet.warning + " does not carry an inventory!");
return;
}
}
if (queue.shouldShowGood()) {
queue.outGood("Giving " + ColorSet.emphasis + ato.debug() + ColorSet.good + ": " + ColorSet.emphasis + item.debug());
}
InventoryTransactionResult itr = inv.getInternal().offer(item.getInternal().copy());
queue.outGood("Give result: " + ColorSet.emphasis + itr.getType().name());
for (ItemStackSnapshot iss : itr.getReplacedItems()) {
if (queue.shouldShowGood()) {
queue.outGood("Successfully gave: " + ColorSet.emphasis + new ItemTag(iss.createStack()).debug());
}
}
for (ItemStackSnapshot iss : itr.getRejectedItems()) {
if (queue.shouldShowGood()) {
queue.outGood("Failed to give: " + ColorSet.emphasis + new ItemTag(iss.createStack()).debug());
}
}
}
}