Skip to content

Commit afafce5

Browse files
authored
Merge pull request #33 from K0bus/develop
Add ItemsAdder support and general code improvements
2 parents a96d9a3 + 8648a02 commit afafce5

File tree

5 files changed

+135
-39
lines changed

5 files changed

+135
-39
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@
124124
<id>vault-repo</id>
125125
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
126126
</repository>
127+
<repository>
128+
<id>matteodev</id>
129+
<url>https://maven.devs.beer/</url>
130+
</repository>
127131
</repositories>
128132

129133
<!-- Your addon must contain Spigot and BentoBox APIs dependencies. -->
@@ -147,6 +151,13 @@
147151
<artifactId>annotations</artifactId>
148152
<version>18.0.0</version>
149153
</dependency>
154+
155+
<dependency>
156+
<groupId>dev.lone</groupId>
157+
<artifactId>api-itemsadder</artifactId>
158+
<version>4.0.10</version>
159+
<scope>provided</scope>
160+
</dependency>
150161
</dependencies>
151162

152163
<!-- Build contains information for maven. It allows to create correct

src/main/java/world/bentobox/controlpanel/managers/ControlPanelManager.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import world.bentobox.controlpanel.database.objects.ControlPanelObject.ControlPanelButton;
3434
import world.bentobox.controlpanel.panels.GuiUtils;
3535
import world.bentobox.controlpanel.utils.Constants;
36+
import world.bentobox.controlpanel.utils.ItemsAdderParse;
3637
import world.bentobox.controlpanel.utils.Utils;
3738

3839

@@ -247,7 +248,6 @@ public void importControlPanels(@Nullable User user, @NotNull GameModeAddon addo
247248
* @param user - user
248249
* @param gameModeName - gamemode name where ControlPanels must be imported.
249250
* @param fileName Specifies from which file control panel will be loaded
250-
* @return true if successful
251251
*/
252252
private void importControlPanels(@Nullable User user, String gameModeName, @NotNull String fileName)
253253
{
@@ -356,48 +356,53 @@ private void readControlPanel(YamlConfiguration config, @Nullable User user, fin
356356
if (buttonListSection != null)
357357
{
358358
buttonListSection.getKeys(false).forEach(slotReference -> {
359-
ControlPanelButton button = new ControlPanelButton();
360-
button.setSlot(Integer.parseInt(slotReference));
359+
for (int slotNum : Utils.readIntArray(List.of(slotReference))) {
360+
ControlPanelButton button = new ControlPanelButton();
361+
button.setSlot(slotNum);
361362

362-
ConfigurationSection buttonSection =
363-
buttonListSection.getConfigurationSection(slotReference);
363+
ConfigurationSection buttonSection =
364+
buttonListSection.getConfigurationSection(slotReference);
364365

365-
if (buttonSection != null)
366-
{
367-
button.setName(buttonSection.getString("name"));
368-
button.setCommand(buttonSection.getString("command", "[user_command]"));
369-
370-
// Create empty list
371-
button.setDescriptionLines(new ArrayList<>());
372-
373-
if (buttonSection.isList("description"))
374-
{
375-
// Read description by each line
376-
buttonSection.getStringList("description").forEach(line ->
377-
button.getDescriptionLines().add(
378-
line.replace("[gamemode]", gameMode.toLowerCase())));
379-
}
380-
else if (buttonSection.isString("description"))
366+
if (buttonSection != null)
381367
{
382-
// Check if description is not defined as simple string
383-
String input = buttonSection.getString("description", "");
368+
button.setName(buttonSection.getString("name"));
369+
button.setCommand(buttonSection.getString("command", "[user_command]"));
384370

385-
if (input != null && !input.isEmpty())
371+
// Create empty list
372+
button.setDescriptionLines(new ArrayList<>());
373+
374+
if (buttonSection.isList("description"))
386375
{
387-
button.getDescriptionLines().add(
388-
input.replace("[gamemode]", gameMode.toLowerCase()));
376+
// Read description by each line
377+
buttonSection.getStringList("description").forEach(line ->
378+
button.getDescriptionLines().add(
379+
line.replace("[gamemode]", gameMode.toLowerCase())));
380+
}
381+
else if (buttonSection.isString("description"))
382+
{
383+
// Check if description is not defined as simple string
384+
String input = buttonSection.getString("description", "");
385+
386+
if (!input.isEmpty())
387+
{
388+
button.getDescriptionLines().add(
389+
input.replace("[gamemode]", gameMode.toLowerCase()));
390+
}
391+
}
392+
else
393+
{
394+
this.addon.logWarning("Description for button "
395+
+ button.getSlot() + " could not be read.");
389396
}
390-
}
391-
else
392-
{
393-
this.addon.logWarning("Description for button " +
394-
+ button.getSlot() + " could not be read.");
395-
}
396397

397-
button.setMaterial(Material.matchMaterial(buttonSection.getString("material", "GRASS")));
398-
button.setIcon(ItemParser.parse("icon", new ItemStack(Material.PAPER)));
398+
button.setMaterial(Material.matchMaterial(buttonSection.getString("material", "GRASS")));
399+
if(buttonSection.getString("icon") != null)
400+
button.setIcon(ItemParser.parse(buttonSection.getString("icon"), new ItemStack(Material.PAPER)));
401+
if(buttonSection.getString("itemsadder") != null)
402+
button.setIcon(ItemsAdderParse.parse(buttonSection.getString("itemsadder")));
399403

400-
buttonList.add(button);
404+
buttonList.add(button);
405+
}
401406
}
402407
});
403408
}
@@ -501,15 +506,15 @@ public ControlPanelObject getUserControlPanel(User user, World world, String per
501506
/**
502507
* Control Panel Addon instance.
503508
*/
504-
private ControlPanelAddon addon;
509+
private final ControlPanelAddon addon;
505510

506511
/**
507512
* This database allows to access to all stored control panels.
508513
*/
509-
private Database<ControlPanelObject> controlPanelDatabase;
514+
private final Database<ControlPanelObject> controlPanelDatabase;
510515

511516
/**
512517
* This map contains all control panel object linked to their reference game mode.
513518
*/
514-
private Map<String, ControlPanelObject> controlPanelCache;
519+
private final Map<String, ControlPanelObject> controlPanelCache;
515520
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package world.bentobox.controlpanel.utils;
2+
3+
import dev.lone.itemsadder.api.CustomStack;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.Material;
6+
import org.bukkit.inventory.ItemStack;
7+
8+
public class ItemsAdderParse {
9+
public static ItemStack parse(String string, ItemStack itemStack)
10+
{
11+
if(!Bukkit.getServer().getPluginManager().isPluginEnabled("ItemsAdder")) return itemStack;
12+
CustomStack stack = CustomStack.getInstance(string);
13+
if(stack != null) return stack.getItemStack();
14+
return itemStack;
15+
}
16+
public static ItemStack parse(String string)
17+
{
18+
return parse(string, new ItemStack(Material.PAPER));
19+
}
20+
}

src/main/java/world/bentobox/controlpanel/utils/Utils.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
import org.bukkit.World;
1111
import org.bukkit.permissions.PermissionAttachmentInfo;
12+
13+
import java.util.ArrayList;
1214
import java.util.List;
1315
import java.util.stream.Collectors;
16+
import java.util.stream.IntStream;
1417

1518
import world.bentobox.bentobox.BentoBox;
1619
import world.bentobox.bentobox.api.addons.GameModeAddon;
@@ -143,4 +146,61 @@ public static <T> T getPreviousValue(T[] values, T currentValue)
143146

144147
return currentValue;
145148
}
149+
150+
/**
151+
* Reads a list of objects and converts it into an array of integers.
152+
* <p>
153+
* The method processes each element in the list:
154+
* <ul>
155+
* <li>If the element is an Integer, it adds it directly to the result array.</li>
156+
* <li>If the element is a String in the format "start-end", it adds all integers
157+
* from "start" to "end" (inclusive) to the result array.</li>
158+
* </ul>
159+
*
160+
* <b>Example:</b>
161+
* If the input list is `["1-3", 5, "7-9"]`, the method will return an array
162+
* containing `[1, 2, 3, 5, 7, 8, 9]`.
163+
*
164+
* @param objectList A list of objects, each of which can either be an Integer or a String
165+
* in the format "start-end" (inclusive range).
166+
* @return An array of integers containing all the integers parsed from the input list.
167+
* The integers from ranges are added to the result array in order.
168+
* @throws NumberFormatException If the string representation of a number is not valid.
169+
*/
170+
public static int[] readIntArray(List<?> objectList) {
171+
List<Integer> values = new ArrayList<>();
172+
173+
// Process each item in the objectList
174+
for (Object o : objectList) {
175+
// If the object is an Integer, add it directly to values
176+
if (o instanceof Integer) {
177+
values.add((int) o);
178+
}
179+
// If the object is a String in the form "start-end"
180+
else if (o instanceof String) {
181+
try {
182+
int n = Integer.parseInt((String) o);
183+
values.add(n);
184+
}catch (NumberFormatException ignored) {}
185+
String[] args = ((String) o).split("-");
186+
if (args.length >= 2) {
187+
try {
188+
int n0 = Integer.parseInt(args[0]);
189+
int n1 = Integer.parseInt(args[1]) + 1; // Add 1 to include the upper bound
190+
// Add all integers in the range [n0, n1)
191+
for (int n : IntStream.range(n0, n1).toArray()) {
192+
values.add(n);
193+
}
194+
continue;
195+
} catch (NumberFormatException e) {
196+
// Handle invalid number format if necessary
197+
throw new NumberFormatException("Invalid number format in range string: " + o);
198+
}
199+
}
200+
}
201+
}
202+
203+
// Convert List<Integer> to a primitive int array and return it
204+
return values.stream().mapToInt(Integer::intValue).toArray();
205+
}
146206
}

src/main/resources/addon.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ authors:
1919
- BONNe
2020

2121
# Soft dependencies of current addon.
22-
softdepend: AcidIsland, BSkyBlock, CaveBlock, SkyGrid, AOneBlock
22+
softdepend: AcidIsland, BSkyBlock, CaveBlock, SkyGrid, AOneBlock, ItemsAdder
2323

2424
# List of addon permissions
2525
permissions:

0 commit comments

Comments
 (0)