22
33import com .eternalcode .annotations .scan .command .DescriptionDocs ;
44import com .eternalcode .core .configuration .implementation .PluginConfiguration ;
5- import com .eternalcode .core .feature .essentials .item .enchant .EnchantArgument ;
65import com .eternalcode .core .injector .annotations .Inject ;
76import com .eternalcode .core .notice .NoticeService ;
87import com .eternalcode .core .util .MaterialUtil ;
9- import com .eternalcode .core .viewer .Viewer ;
108import dev .rollczi .litecommands .annotations .argument .Arg ;
119import dev .rollczi .litecommands .annotations .context .Context ;
1210import dev .rollczi .litecommands .annotations .execute .Execute ;
1311import dev .rollczi .litecommands .annotations .permission .Permission ;
1412import dev .rollczi .litecommands .annotations .command .Command ;
15- import dev .triumphteam .gui .builder .item .ItemBuilder ;
1613import org .bukkit .Material ;
17- import org .bukkit .enchantments . Enchantment ;
14+ import org .bukkit .command . CommandSender ;
1815import org .bukkit .entity .Player ;
19- import org .bukkit .inventory .ItemStack ;
2016
2117@ Command (name = "give" , aliases = { "i" , "item" })
2218@ Permission ("eternalcore.give" )
2319class GiveCommand {
2420
2521 private final NoticeService noticeService ;
26- private final PluginConfiguration pluginConfig ;
22+ private final GiveService giveService ;
23+ private final PluginConfiguration config ;
2724
2825 @ Inject
29- GiveCommand (NoticeService noticeService , PluginConfiguration pluginConfig ) {
26+ GiveCommand (NoticeService noticeService , GiveService giveService , PluginConfiguration config ) {
3027 this .noticeService = noticeService ;
31- this .pluginConfig = pluginConfig ;
28+ this .giveService = giveService ;
29+ this .config = config ;
3230 }
3331
3432 @ Execute
3533 @ DescriptionDocs (description = "Gives you an item" , arguments = "<item>" )
3634 void execute (@ Context Player player , @ Arg Material material ) {
37- String formattedMaterial = MaterialUtil .format (material );
38-
39- this .giveItem (player , material );
40-
41- this .noticeService .create ()
42- .placeholder ("{ITEM}" , formattedMaterial )
43- .notice (translation -> translation .item ().giveReceived ())
44- .player (player .getUniqueId ())
45- .send ();
35+ this .execute (player , material , this .config .items .defaultGiveAmount );
4636 }
4737
4838 @ Execute
4939 @ DescriptionDocs (description = "Gives an item to another player" , arguments = "<item> <player>" )
50- void execute (@ Context Viewer viewer , @ Arg Material material , @ Arg Player target ) {
51- String formattedMaterial = MaterialUtil .format (material );
52-
53- this .giveItem (target , material );
54-
55- this .noticeService .create ()
56- .placeholder ("{ITEM}" , formattedMaterial )
57- .notice (translation -> translation .item ().giveReceived ())
58- .player (target .getUniqueId ())
59- .send ();
60-
61- this .noticeService .create ()
62- .placeholder ("{ITEM}" , formattedMaterial )
63- .placeholder ("{PLAYER}" , target .getName ())
64- .notice (translation -> translation .item ().giveGiven ())
65- .viewer (viewer )
66- .send ();
40+ void execute (@ Context CommandSender sender , @ Arg Material material , @ Arg Player target ) {
41+ this .execute (sender , material , this .config .items .defaultGiveAmount , target );
6742 }
6843
6944 @ Execute
7045 @ DescriptionDocs (description = "Gives you an item with a custom amount" , arguments = "<item> <amount>" )
7146 void execute (@ Context Player player , @ Arg Material material , @ Arg (GiveArgument .KEY ) int amount ) {
72- String formattedMaterial = MaterialUtil .format (material );
73-
74- this .giveItem (player , material , amount );
47+ boolean isSuccess = this .giveService .giveItem (player , player , material , amount );
7548
76- this .noticeService .create ()
77- .placeholder ("{ITEM}" , formattedMaterial )
78- .notice (translation -> translation .item ().giveReceived ())
79- .player (player .getUniqueId ())
80- .send ();
49+ if (isSuccess ) {
50+ this .noticeService .create ()
51+ .placeholder ("{ITEM}" , MaterialUtil .format (material ))
52+ .notice (translation -> translation .item ().giveReceived ())
53+ .player (player .getUniqueId ())
54+ .send ();
55+ }
8156 }
8257
8358 @ Execute
8459 @ DescriptionDocs (description = "Gives an item with a custom amount to another player" , arguments = "<item> <amount> <player>" )
85- void execute (@ Context Viewer viewer , @ Arg Material material , @ Arg (GiveArgument .KEY ) int amount , @ Arg Player target ) {
86- String formattedMaterial = MaterialUtil . format ( material );
60+ void execute (@ Context CommandSender sender , @ Arg Material material , @ Arg (GiveArgument .KEY ) int amount , @ Arg Player target ) {
61+ boolean isSuccess = this . giveService . giveItem ( sender , target , material , amount );
8762
88- this .giveItem (target , material , amount );
63+ if (!isSuccess ) {
64+ return ;
65+ }
8966
67+ String formattedMaterial = MaterialUtil .format (material );
9068 this .noticeService .create ()
9169 .placeholder ("{ITEM}" , formattedMaterial )
9270 .notice (translation -> translation .item ().giveReceived ())
@@ -97,68 +75,8 @@ void execute(@Context Viewer viewer, @Arg Material material, @Arg(GiveArgument.K
9775 .placeholder ("{ITEM}" , formattedMaterial )
9876 .placeholder ("{PLAYER}" , target .getName ())
9977 .notice (translation -> translation .item ().giveGiven ())
100- .viewer (viewer )
101- .send ();
102- }
103-
104- @ Execute
105- @ DescriptionDocs (description = "Gives an item with a custom amount to another player" , arguments = "<item> <amount> <enchantment> <level> <player>" )
106- void execute (@ Context Viewer viewer , @ Arg Material material , @ Arg (GiveArgument .KEY ) int amount , @ Arg Enchantment enchantment , @ Arg (EnchantArgument .KEY ) int level , @ Arg Player target ) {
107- String formattedMaterial = MaterialUtil .format (material );
108-
109- this .giveItem (target , material , amount , enchantment , level );
110-
111- this .noticeService .create ()
112- .placeholder ("{ITEM}" , formattedMaterial )
113- .placeholder ("{ENCHANTMENT}" , enchantment .getKey ().getKey ())
114- .placeholder ("{ENCHANTMENT_LEVEL}" , String .valueOf (level ))
115- .notice (translation -> translation .item ().giveReceivedEnchantment ())
116- .player (target .getUniqueId ())
117- .send ();
118-
119- this .noticeService .create ()
120- .placeholder ("{ITEM}" , formattedMaterial )
121- .placeholder ("{PLAYER}" , target .getName ())
122- .placeholder ("{ENCHANTMENT}" , enchantment .getKey ().getKey ())
123- .placeholder ("{ENCHANTMENT_LEVEL}" , String .valueOf (level ))
124- .notice (translation -> translation .item ().giveGivenEnchantment ())
125- .viewer (viewer )
78+ .sender (sender )
12679 .send ();
12780 }
12881
129- private void giveItem (Player player , Material material ) {
130- int amount = this .pluginConfig .items .defaultGiveAmount ;
131-
132- if (!material .isItem ()) {
133- this .noticeService .create ()
134- .notice (translation -> translation .item ().giveNotItem ())
135- .player (player .getUniqueId ())
136- .send ();
137- return ;
138- }
139-
140- ItemStack item = ItemBuilder .from (material )
141- .amount (amount )
142- .build ();
143-
144- player .getInventory ().addItem (item );
145- }
146-
147- private void giveItem (Player player , Material material , int amount ) {
148- ItemStack item = ItemBuilder .from (material )
149- .amount (amount )
150- .build ();
151-
152- player .getInventory ().addItem (item );
153- }
154-
155- private void giveItem (Player player , Material material , int amount , Enchantment enchantment , int level ) {
156- ItemStack item = ItemBuilder .from (material )
157- .amount (amount )
158- .enchant (enchantment , level )
159- .build ();
160-
161- player .getInventory ().addItem (item );
162- }
163-
16482}
0 commit comments