-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomItemManager.java
More file actions
144 lines (127 loc) · 5.66 KB
/
CustomItemManager.java
File metadata and controls
144 lines (127 loc) · 5.66 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.github.pinont.singularitylib.api.manager;
import com.github.pinont.singularitylib.api.command.SimpleCommand;
import com.github.pinont.singularitylib.api.items.CustomItem;
import com.github.pinont.singularitylib.api.utils.Common;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import static com.github.pinont.singularitylib.plugin.CorePlugin.getInstance;
import static com.github.pinont.singularitylib.plugin.CorePlugin.sendConsoleMessage;
public class CustomItemManager implements SimpleCommand {
public List<CustomItem> customItems = new ArrayList<>();
public List<CustomItem> getCustomItems() {
return customItems;
}
public void register(List<CustomItem> item) {
if (item.isEmpty()) return;
sendConsoleMessage("Registering " + item.size() + " custom items");
for (CustomItem customItem : item) {
customItems.add(customItem);
customItem.register(); // call register to ensure the item is properly initialized
customItem.getItem(); // ensure the item is created and interaction is set up
if (customItem.getInteraction() != null) {
sendConsoleMessage("Registering interaction " + customItem.getInteraction().getName() + " for item " + customItem.getName());
}
}
}
@Override
public String getName() {
return "give";
}
@Override
public String description() {
return "Give item to player";
}
@Override
public void execute(CommandSourceStack commandSourceStack, String[] strings) {
if (strings.length < 2) {
commandSourceStack.getSender().sendMessage("Usage: /give <player> <item> [count]");
return;
}
String itemName = strings[1];
int count = strings.length > 2 ? Integer.parseInt(strings[2]) : 1;
// Find the item
ItemStack item = null;
if (itemName.startsWith("minecraft:")) {
itemName = itemName.replace("minecraft:", "");
item = new ItemStack(Objects.requireNonNull(Material.getMaterial(itemName.toUpperCase())), count);
} else if (itemName.startsWith(getInstance().getName().toLowerCase() + ":")) {
String finalItemName = itemName.replace(getInstance().getName().toLowerCase() + ":", "");
item = customItems.stream()
.filter(customItem -> customItem.getName().equalsIgnoreCase(finalItemName))
.findFirst()
.map(customItem -> customItem.register().setAmount(count).create())
.orElse(null);
}
if (item == null) {
commandSourceStack.getSender().sendMessage("Item not found");
return;
}
// Give the item to the player
if (strings[0].equalsIgnoreCase("@a")) {
for (Player player : Bukkit.getOnlinePlayers()) {
giveItemToPlayer(player, item);
}
} else if (strings[0].equalsIgnoreCase("@r")) {
List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
Player randomPlayer = players.get(new Random().nextInt(players.size()));
giveItemToPlayer(randomPlayer, item);
} else if (strings[0].equalsIgnoreCase("@s")) {
Player player = (Player) commandSourceStack.getSender();
giveItemToPlayer(player, item);
} else {
Player player = Bukkit.getPlayer(strings[0]);
if (player == null) {
commandSourceStack.getSender().sendMessage("Player not found");
return;
}
giveItemToPlayer(player, item);
}
}
private void giveItemToPlayer(Player player, ItemStack item) {
if (player != null) {
player.getInventory().addItem(item);
player.sendMessage("Gave " + item.getAmount() + " " + item.getItemMeta().getItemName() + ChatColor.RESET + " to " + player.getName());
} else {
player.sendMessage("Player not found");
}
}
@Override
public @NotNull Collection<String> suggest(CommandSourceStack commandSourceStack, String[] args) {
return switch (args.length) {
case 0, 1 -> {
List<String> players = new ArrayList<>();
players.addAll(Bukkit.getOnlinePlayers().stream().map(Player::getName).toList());
players.addAll(Arrays.asList("@a", "@s", "@r"));
yield players;
}
case 2 -> {
List<String> items = new ArrayList<>();
items.addAll(Arrays.stream(Common.getAllItemsMaterials())
.map(material -> "minecraft:" + material.name().toLowerCase())
.toList());
items.addAll(customItems.stream()
.map(item -> getInstance().getName().toLowerCase() + ":" + item.getName())
.toList());
if (!args[1].isEmpty()) {
yield items.stream()
.filter(item -> item.split(":")[1].toLowerCase().startsWith(args[1].toLowerCase()))
.toList();
}
yield items;
}
case 3 -> Collections.singletonList("<count>");
default -> Collections.emptyList();
};
}
@Override
public boolean canUse(CommandSender sender) {
return sender instanceof Player;
}
}