44import io .github .nik2143 .customgapple .CustomGapple ;
55import org .apache .commons .lang .StringUtils ;
66import org .apache .commons .lang .math .NumberUtils ;
7- import org .bukkit .Bukkit ;
87import org .bukkit .Material ;
98import org .bukkit .command .Command ;
109import org .bukkit .command .CommandExecutor ;
2019
2120public class CustomGappleCommand implements CommandExecutor , TabCompleter {
2221
23- private CustomGapple plugin ;
22+ private final CustomGapple plugin ;
2423
2524 public CustomGappleCommand (CustomGapple plugin ){
2625 this .plugin = plugin ;
2726 }
2827
2928 @ Override
3029 public boolean onCommand (CommandSender sender , Command command , String label , String [] args ) {
31- if (command .getName ().equalsIgnoreCase ("customgapple" )){
30+ if (command .getName ().equalsIgnoreCase ("customgapple" )) {
3231 if (!(sender instanceof Player )) {
3332 sender .sendMessage ("[CustomGapple] You can't use this command from console" );
3433 return true ;
3534 }
36- if (!sender .isOp () || !sender .hasPermission ("customgapple.use" )){
35+ if (!sender .isOp () || !sender .hasPermission ("customgapple.use" )) {
3736 sender .sendMessage ("[CustomGapple] You haven't permissions to use this command" );
3837 return true ;
3938 }
4039 Player player = (Player ) sender ;
41- switch (args .length ){
40+ switch (args .length ) {
4241 case 0 :
43- sender .sendMessage ("Wrong sintax. Use /customgapple <Effect> [Duration ] [Level ] [Amount]" );
42+ sender .sendMessage ("Wrong sintax. Use /customgapple <EffectsNumbers> <Effects> [Level ] [Duration ] [Amount]" );
4443 return true ;
4544 case 1 :
46- if (PotionEffectType .getByName (args [0 ])==null ){
47- sender .sendMessage ("Unknown potion effect" );
48- return true ;
49- }
50- player .getInventory ().addItem (CreateGapple (PotionEffectType .getByName (args [0 ]),plugin .getConfig ().getInt ("Default-Duration" ),1 , plugin .getConfig ().getInt ("Default-Amount" )));
51- break ;
52- case 2 :
53- if (PotionEffectType .getByName (args [0 ])==null ){
54- sender .sendMessage ("Unknown potion effect" );
55- return true ;
56- }
57- if (!NumberUtils .isNumber (args [1 ])){
58- sender .sendMessage (args [1 ] + " isn't a number" );
59- return true ;
60- }
61- player .getInventory ().addItem (CreateGapple (PotionEffectType .getByName (args [0 ]), Integer .parseInt (args [1 ]),1 , plugin .getConfig ().getInt ("Default-Amount" )));
62- break ;
63- case 3 :
64- if (PotionEffectType .getByName (args [0 ])==null ){
65- sender .sendMessage ("Unknown potion effect" );
66- return true ;
67- }
68- if (!NumberUtils .isNumber (args [1 ])){
69- sender .sendMessage (args [1 ] + " isn't a number" );
70- return true ;
71- }
72- if (!NumberUtils .isNumber (args [2 ])){
73- sender .sendMessage (args [2 ] + " isn't a number" );
45+ sender .sendMessage ("Wrong sintax. Use /customgapple <EffectsNumber> <Effects> [Level] [Duration] [Amount]" );
46+ return true ;
47+ default :
48+ if (!NumberUtils .isNumber (args [0 ])){
49+ sender .sendMessage (args [0 ] + " isn't a number" );
7450 return true ;
7551 }
76- player . getInventory (). addItem ( CreateGapple ( PotionEffectType . getByName ( args [ 0 ]), Integer . parseInt ( args [ 1 ]), Integer . parseInt ( args [ 2 ]), plugin . getConfig (). getInt ( "Default-Amount" )) );
77- break ;
78- case 4 :
79- if (PotionEffectType . getByName ( args [ 0 ])== null ) {
80- sender .sendMessage ("Unknown potion effect " );
52+ List < PotionEffectType > effects = new ArrayList <>( );
53+ List < Integer > levels = new ArrayList <>() ;
54+ int effectsnumber = Integer . parseInt ( args [ 0 ]);
55+ if (args . length < effectsnumber + 1 ) {
56+ sender .sendMessage ("The number of effects is wrong " );
8157 return true ;
8258 }
83- if (!NumberUtils .isNumber (args [1 ])){
84- sender .sendMessage (args [1 ] + " isn't a number" );
85- return true ;
59+ for (int i = 0 ; i < effectsnumber ; i ++){
60+ if (PotionEffectType .getByName (args [i +1 ])!=null ){
61+ effects .add (PotionEffectType .getByName (args [i +1 ]));
62+ } else {
63+ sender .sendMessage ("Effect " + args [i +1 ] + " doesn't exist" );
64+ return true ;
65+ }
8666 }
87- if (!NumberUtils .isNumber (args [2 ])){
88- sender .sendMessage (args [2 ] + " isn't a number" );
89- return true ;
67+ if (args .length >= effectsnumber * 2 + 1 ) {
68+ for (int i = 0 ; i < effectsnumber ; i ++){
69+ if (NumberUtils .isNumber (args [i +effectsnumber +1 ])){
70+ levels .add (Integer .valueOf (args [i +effectsnumber +1 ]));
71+ } else {
72+ sender .sendMessage (args [i +effectsnumber +1 ] + " isn't a number" );
73+ return true ;
74+ }
75+ }
76+ }else {
77+ Collections .addAll (levels , 1 , 1 , 1 );
9078 }
91- if (!NumberUtils .isNumber (args [3 ])){
92- sender .sendMessage (args [3 ] + " isn't a number" );
93- return true ;
79+ if (args .length >= effectsnumber * 2 + 3 ){
80+ if (!NumberUtils .isNumber (args [effectsnumber * 2 + 2 ])){
81+ sender .sendMessage (args [effectsnumber * 2 + 2 ] + " isn't a number" );
82+ return true ;
83+ }
84+ if (NumberUtils .isNumber (args [effectsnumber * 2 + 1 ])){
85+ player .getInventory ().addItem (CreateGapple (effectsnumber , effects , levels , Integer .parseInt (args [effectsnumber * 2 + 1 ]), Integer .parseInt (args [effectsnumber * 2 + 2 ])));
86+ } else {
87+ sender .sendMessage (args [effectsnumber * 2 + 1 ] + " isn't a number" );
88+ return true ;
89+ }
90+ } else if (args .length >= effectsnumber * 2 + 2 ){
91+ if (NumberUtils .isNumber (args [effectsnumber * 2 + 1 ])){
92+ player .getInventory ().addItem (CreateGapple (effectsnumber , effects , levels , Integer .parseInt (args [effectsnumber * 2 + 1 ]), plugin .getConfig ().getInt ("Default-Amount" )));
93+ } else {
94+ sender .sendMessage (args [effectsnumber * 2 + 1 ] + " isn't a number" );
95+ return true ;
96+ }
97+ } else {
98+ player .getInventory ().addItem (CreateGapple (effectsnumber , effects , levels , plugin .getConfig ().getInt ("Default-Duration" ), plugin .getConfig ().getInt ("Default-Amount" )));
9499 }
95- player .getInventory ().addItem (CreateGapple (PotionEffectType .getByName (args [0 ]), Integer .parseInt (args [1 ]),Integer .parseInt (args [2 ]),Integer .parseInt (args [3 ])));
96- break ;
97100 }
98101 return true ;
99102 }
@@ -103,8 +106,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
103106 @ Override
104107 public List <String > onTabComplete (CommandSender sender , Command command , String alias , String [] args ) {
105108 if (command .getName ().equalsIgnoreCase ("customgapple" )){
106- List <String > autocomplete = new ArrayList <String >();
107- if (args .length == 1 ){
109+ List <String > autocomplete = new ArrayList <>();
110+ if (args .length > 1 && NumberUtils . isNumber ( args [ 0 ]) && args . length < Integer . parseInt ( args [ 0 ]) + 2 ){
108111 for (PotionEffectType effect : PotionEffectType .values ()){
109112 if (effect == null ) {
110113 continue ;
@@ -118,12 +121,15 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
118121 return Collections .emptyList ();
119122 }
120123
121- private ItemStack CreateGapple (PotionEffectType effect , int duration , int level , int amount ){
124+ private ItemStack CreateGapple (int effectsnumber , List < PotionEffectType > effects , List < Integer > level , int duration , int amount ){
122125 NBTItem nbti = new NBTItem (new ItemStack (Material .GOLDEN_APPLE , amount ));
123- nbti .addCompound ("GappleEffect" );
124- nbti .getCompound ("GappleEffect" ).setString ("Effect" , effect .getName ());
125- nbti .getCompound ("GappleEffect" ).setInteger ("Duration" ,duration * 20 );
126- nbti .getCompound ("GappleEffect" ).setInteger ("Level" ,level - 1 );
126+ nbti .addCompound ("GappleEffects" );
127+ for (int i = 0 ;i <effectsnumber ;i ++){
128+ nbti .getCompound ("GappleEffects" ).addCompound ("Effect" +i );
129+ nbti .getCompound ("GappleEffects" ).getCompound ("Effect" +i ).setString ("Effect" , effects .get (i ).getName ());
130+ nbti .getCompound ("GappleEffects" ).getCompound ("Effect" +i ).setInteger ("Duration" ,duration * 20 );
131+ nbti .getCompound ("GappleEffects" ).getCompound ("Effect" +i ).setInteger ("Level" ,level .get (i ) - 1 );
132+ }
127133 return nbti .getItem ();
128134 }
129135}
0 commit comments