Skip to content

Commit 7ce2143

Browse files
committed
Ajout d'un système complet de commandes & d'un créateur d'items
1 parent 6bd85d9 commit 7ce2143

File tree

4 files changed

+203
-5
lines changed

4 files changed

+203
-5
lines changed

src/main/java/space/debian/WrapAPI/objects/commands/WCommand.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
import org.bukkit.command.Command;
44
import org.bukkit.command.CommandExecutor;
55
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Player;
67

78
import java.util.ArrayList;
89
import java.util.List;
10+
import java.util.Optional;
11+
import java.util.stream.Stream;
12+
13+
import static space.debian.WrapAPI.tools.Messages.errorPrefix;
14+
import static space.debian.WrapAPI.tools.Messages.noPermissionError;
915

1016
public abstract class WCommand implements CommandExecutor {
1117

12-
private String name;
13-
private WSubCommand command;
18+
private final String name;
19+
private boolean consoleOnly = false;
1420
private List<WSubCommand> subCommands = new ArrayList<>();
21+
private String permission = null;
1522

1623
/**
1724
* Create a new instance of a WCommand
@@ -37,18 +44,52 @@ public String getName() {
3744
@Override
3845
public boolean onCommand(CommandSender sender, Command command, String string, String[] args) {
3946

47+
if (args.length > 0) {
48+
49+
Optional<WSubCommand> subCommand = subCommands.stream().filter((singleSubCommand) -> singleSubCommand.getName().equalsIgnoreCase(args[0])).findFirst();
50+
51+
if (subCommand.isPresent())
52+
return subCommand.get().subCommandProcess(sender, command, string, args);
53+
}
54+
55+
if ((consoleOnly && sender instanceof Player) || (permission != null && !sender.hasPermission(permission))) {
56+
57+
sender.sendMessage(errorPrefix + noPermissionError);
58+
return true;
59+
}
60+
4061
return process(sender, command, string, args);
4162
}
4263

4364
/**
44-
* WrapAPI's command process method, override this method and build your command !
65+
* WrapAPI's command process method, override this method and build your command.
4566
* @param sender Command sender
4667
* @param command Command executed, a.k.a this
4768
* @param string Command name as executed, can be an aliase
4869
* @param args Command arguments
4970
* @return False if command is off, true otherwise
5071
*/
5172
public boolean process(CommandSender sender, Command command, String string, String[] args) {
73+
5274
return true;
5375
}
76+
77+
public List<WSubCommand> getSubCommands() {
78+
return subCommands;
79+
}
80+
81+
public WCommand addSubCommand(WSubCommand subCommand) {
82+
subCommands.add(subCommand);
83+
return this;
84+
}
85+
86+
public WCommand setConsoleOnly(boolean consoleOnly) {
87+
this.consoleOnly = consoleOnly;
88+
return this;
89+
}
90+
91+
public WCommand setPermission(String permission) {
92+
this.permission = permission;
93+
return this;
94+
}
5495
}

src/main/java/space/debian/WrapAPI/objects/commands/WSubCommand.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,75 @@
22

33
import org.bukkit.command.Command;
44
import org.bukkit.command.CommandSender;
5+
import org.bukkit.entity.Player;
6+
7+
import static space.debian.WrapAPI.tools.Messages.errorPrefix;
8+
import static space.debian.WrapAPI.tools.Messages.noPermissionError;
59

610
public class WSubCommand {
711

8-
private String name;
9-
private boolean consoleOnly;
12+
private final WCommand parent;
13+
private final String name;
14+
private boolean consoleOnly = false;
15+
private String permission = null;
16+
17+
/**
18+
* Create a new instance of a WSubCommand
19+
* @param parent WCommand parent of this subcommand
20+
* @param name Name of the subcommand, used when checking parent's args
21+
*/
22+
public WSubCommand(WCommand parent, String name) {
23+
this.parent = parent;
24+
this.name = name;
25+
}
26+
27+
/**
28+
* WrapAPI's subcommand processor method, only override this if you know what you're doing !
29+
* @param sender Command sender
30+
* @param command Command executed, a.k.a parent
31+
* @param string Command name as executed, can be an aliase
32+
* @param args Command arguments
33+
* @return False if command is off, true otherwise
34+
*/
35+
public boolean subCommandProcess(CommandSender sender, Command command, String string, String[] args) {
36+
37+
if ((consoleOnly && sender instanceof Player) || (permission != null && !sender.hasPermission(permission))) {
38+
39+
sender.sendMessage(errorPrefix + noPermissionError);
40+
return true;
41+
}
42+
43+
return execute(sender, command, string, args);
44+
}
1045

46+
/**
47+
* WrapAPI's subcommand process method, override this method and build your command.
48+
* @param sender Command sender
49+
* @param command Command executed, a.k.a parent
50+
* @param string Command name as executed, can be an aliase
51+
* @param args Command arguments
52+
* @return False if command is off, true otherwise
53+
*/
1154
public boolean execute(CommandSender sender, Command command, String string, String[] args) {
1255

1356
return true;
1457
}
58+
59+
public WCommand getParent() {
60+
return parent;
61+
}
62+
63+
public String getName() {
64+
return name;
65+
}
66+
67+
public WSubCommand setConsoleOnly(boolean consoleOnly) {
68+
this.consoleOnly = consoleOnly;
69+
return this;
70+
}
71+
72+
public WSubCommand setPermission(String permission) {
73+
this.permission = permission;
74+
return this;
75+
}
1576
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package space.debian.WrapAPI.tools;
2+
3+
public class Messages {
4+
5+
public static String errorPrefix = "§4 » §c";
6+
7+
public static String noPermissionError = "Vous n'avez pas la permission de faire ceci.";
8+
9+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package space.debian.WrapAPI.tools;
2+
3+
import org.bukkit.Material;
4+
import org.bukkit.enchantments.Enchantment;
5+
import org.bukkit.event.Event;
6+
import org.bukkit.inventory.ItemStack;
7+
import org.bukkit.inventory.meta.ItemMeta;
8+
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
13+
public abstract class SimpleItem {
14+
15+
private Material itemType;
16+
private String name;
17+
private List<String> lore;
18+
private short dataId = -1;
19+
private HashMap<Enchantment, Integer> enchantments = null;
20+
21+
public ItemStack asItemstack(int amount) {
22+
ItemStack result = (dataId == -1) ? new ItemStack(itemType, amount) : new ItemStack(itemType, amount, dataId);
23+
ItemMeta resultMeta = result.getItemMeta();
24+
resultMeta.setLore(lore);
25+
resultMeta.setDisplayName(name);
26+
if (enchantments != null)
27+
enchantments.forEach((k, v) -> resultMeta.addEnchant(k, v, true));
28+
result.setItemMeta(resultMeta);
29+
return (result);
30+
}
31+
32+
public ItemStack asItemstack() {
33+
return asItemstack(1);
34+
}
35+
36+
public Material getItemType() {
37+
return itemType;
38+
}
39+
40+
public SimpleItem setItemType(Material itemType) {
41+
this.itemType = itemType;
42+
return this;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public SimpleItem setName(String name) {
50+
this.name = name;
51+
return this;
52+
}
53+
54+
public List<String> getLore() {
55+
return lore;
56+
}
57+
58+
public SimpleItem addLoreLine(String loreLine) {
59+
lore.add(loreLine);
60+
return this;
61+
}
62+
63+
public SimpleItem setLore(List<String> lore) {
64+
this.lore = lore;
65+
return this;
66+
}
67+
68+
public short getDataId() {
69+
return dataId;
70+
}
71+
72+
public SimpleItem setDataId(short dataId) {
73+
this.dataId = dataId;
74+
return this;
75+
}
76+
77+
public void addEnchantment(Enchantment enchantment, Integer level) {
78+
if (enchantments == null)
79+
enchantments = new HashMap<>();
80+
enchantments.put(enchantment, level);
81+
}
82+
83+
public SimpleItem addEnchantment(Enchantment enchantment) {
84+
addEnchantment(enchantment, 1);
85+
return this;
86+
}
87+
}

0 commit comments

Comments
 (0)