Skip to content

Commit bfb7e37

Browse files
committed
allow both position and slot settings
1 parent 49ffac0 commit bfb7e37

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

src/main/java/me/hsgamer/bettergui/menu/SimpleMenu.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.hsgamer.bettergui.menu;
22

3+
import io.github.projectunified.craftux.common.Button;
34
import io.github.projectunified.craftux.simple.SimpleButtonMask;
45
import io.github.projectunified.craftux.spigot.SpigotInventoryUtil;
56
import me.hsgamer.bettergui.BetterGUI;
@@ -10,6 +11,7 @@
1011
import me.hsgamer.hscore.config.Config;
1112

1213
import java.util.Map;
14+
import java.util.Optional;
1315
import java.util.UUID;
1416

1517
public class SimpleMenu extends BaseInventoryMenu<SimpleButtonMask> {
@@ -27,9 +29,13 @@ protected SimpleButtonMask createMask(Map<String, Object> sectionMap) {
2729
continue;
2830
}
2931
Map<String, Object> values = MapUtils.createLowercaseStringObjectMap((Map<?, ?>) value);
30-
BetterGUI.getInstance().get(ButtonBuilder.class)
31-
.build(new ButtonBuilder.Input(this, "button_" + key, values))
32-
.ifPresent(button -> SlotUtil.getSlots(values).forEach(slot -> mask.setButton(SpigotInventoryUtil.toPosition(slot, getInventoryType()), button)));
32+
Optional<WrappedButton> optionalButton = BetterGUI.getInstance().get(ButtonBuilder.class).build(new ButtonBuilder.Input(this, "button_" + key, values));
33+
if (!optionalButton.isPresent()) {
34+
continue;
35+
}
36+
Button button = optionalButton.get();
37+
SlotUtil.getPosition(values).ifPresent(position -> mask.setButton(position, button));
38+
SlotUtil.getSlot(values).forEach(slot -> mask.setButton(SpigotInventoryUtil.toPosition(slot, getInventoryType()), button));
3339
}
3440
return mask;
3541
}

src/main/java/me/hsgamer/bettergui/util/SlotUtil.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.hsgamer.bettergui.util;
22

3+
import io.github.projectunified.craftux.common.Position;
34
import me.hsgamer.hscore.common.Validate;
45

56
import java.math.BigDecimal;
@@ -21,28 +22,35 @@ private SlotUtil() {
2122
}
2223

2324
/**
24-
* Get the slots
25+
* Get the position from the settings
2526
*
26-
* @param map the value map
27+
* @param map the settings
2728
*
28-
* @return the slots
29+
* @return the position
2930
*/
30-
public static IntStream getSlots(Map<String, Object> map) {
31-
IntStream slots = IntStream.empty();
32-
31+
public static Optional<Position> getPosition(Map<String, Object> map) {
3332
if (map.containsKey(POS_X) || map.containsKey(POS_Y)) {
3433
Optional<Integer> x = Validate.getNumber(String.valueOf(map.get(POS_X))).map(BigDecimal::intValue);
3534
Optional<Integer> y = Validate.getNumber(String.valueOf(map.get(POS_Y))).map(BigDecimal::intValue);
3635
if (x.isPresent() && y.isPresent()) {
37-
slots = IntStream.of((y.get() - 1) * 9 + x.get() - 1);
36+
return Optional.of(Position.of(x.get(), y.get()));
3837
}
3938
}
39+
return Optional.empty();
40+
}
4041

42+
/**
43+
* Get the slots from the settings
44+
*
45+
* @param map the settings
46+
*
47+
* @return the slots
48+
*/
49+
public static IntStream getSlot(Map<String, Object> map) {
4150
if (map.containsKey(POS_SLOT)) {
42-
slots = IntStream.concat(slots, generateSlots(String.valueOf(map.get(POS_SLOT))));
51+
return generateSlots(String.valueOf(map.get(POS_SLOT)));
4352
}
44-
45-
return slots;
53+
return IntStream.empty();
4654
}
4755

4856
/**

0 commit comments

Comments
 (0)