Skip to content

Commit bef2f86

Browse files
committed
Added support for multiple effects
1 parent 90386f1 commit bef2f86

File tree

4 files changed

+72
-63
lines changed

4 files changed

+72
-63
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.nik2143</groupId>
88
<artifactId>CustomGapple</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>CustomGapple</name>

src/main/java/io/github/nik2143/customgapple/commands/CustomGappleCommand.java

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.github.nik2143.customgapple.CustomGapple;
55
import org.apache.commons.lang.StringUtils;
66
import org.apache.commons.lang.math.NumberUtils;
7-
import org.bukkit.Bukkit;
87
import org.bukkit.Material;
98
import org.bukkit.command.Command;
109
import org.bukkit.command.CommandExecutor;
@@ -20,80 +19,84 @@
2019

2120
public 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
}

src/main/java/io/github/nik2143/customgapple/listeners/EatListener.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.nik2143.customgapple.listeners;
22

33
import de.tr7zw.changeme.nbtapi.NBTItem;
4+
import org.bukkit.Bukkit;
45
import org.bukkit.Material;
56
import org.bukkit.event.EventHandler;
67
import org.bukkit.event.Listener;
@@ -16,11 +17,13 @@ private void GappleEatEvent(PlayerItemConsumeEvent e){
1617
if (e.getItem().getType().equals(Material.GOLDEN_APPLE)){
1718
ItemStack item = e.getItem();
1819
NBTItem nbti = new NBTItem(item);
19-
if (nbti.getCompound("GappleEffect") != null){
20+
if (nbti.getCompound("GappleEffects") != null){
2021
e.setCancelled(true);
21-
e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getByName(nbti.getCompound("GappleEffect").getString("Effect")),
22-
nbti.getCompound("GappleEffect").getInteger("Duration"),
23-
nbti.getCompound("GappleEffect").getInteger("Level")));
22+
for(String key : nbti.getCompound("GappleEffects").getKeys()){
23+
e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getByName(nbti.getCompound("GappleEffects").getCompound(key).getString("Effect")),
24+
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Duration"),
25+
nbti.getCompound("GappleEffects").getCompound(key).getInteger("Level")),true);
26+
}
2427
item.setAmount(item.getAmount() - 1);
2528
e.getPlayer().getInventory().setItemInHand(item);
2629
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ authors: [nik2143]
66
description: Allow to create and use custom golden apples with custom effects
77
commands:
88
customgapple:
9-
description: Command to give yourself a golden apple with custom effect
9+
description: Command to give yourself a golden apple with custom effects

0 commit comments

Comments
 (0)