Skip to content

Commit 2625bfe

Browse files
committed
Improve item creation in the GUI class
1 parent 217fd75 commit 2625bfe

File tree

1 file changed

+109
-71
lines changed
  • src/main/java/net/onelitefeather/bettergopaint/utils

1 file changed

+109
-71
lines changed

src/main/java/net/onelitefeather/bettergopaint/utils/GUI.java

Lines changed: 109 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,27 @@
3636
import org.bukkit.Bukkit;
3737
import org.bukkit.Material;
3838
import org.bukkit.inventory.Inventory;
39+
import org.bukkit.inventory.ItemStack;
3940
import org.bukkit.plugin.java.JavaPlugin;
4041
import org.jetbrains.annotations.NotNull;
4142

42-
public class GUI {
43+
import java.util.List;
4344

45+
public final class GUI {
46+
47+
private static final String INCREASE_DECREASE_LORE = "\n§7Left click to increase\n§7Right click to decrease";
48+
private static final ItemStack WHITE_DECORATION = Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", "");
49+
private static final ItemStack LIME_DECORATION = Items.create(Material.LIME_STAINED_GLASS_PANE, 1, "§7", "");
50+
private static final ItemStack YELLOW_DECORATION = Items.create(Material.YELLOW_STAINED_GLASS_PANE, 1, "§7", "");
51+
private static final ItemStack RED_DECORATION = Items.create(Material.RED_STAINED_GLASS_PANE, 1, "§7", "");
52+
private static final ItemStack ORANGE_DECORATION = Items.create(Material.ORANGE_STAINED_GLASS_PANE, 1, "§7", "");
53+
private static final ItemStack EMPTY_SLOT = Items.create(Material.BARRIER, 1, "§cEmpty Slot", "\n§7Click with a block to set");
4454
private static final BetterGoPaint plugin = JavaPlugin.getPlugin(BetterGoPaint.class);
4555

56+
private GUI() {
57+
throw new UnsupportedOperationException("This class cannot be instantiated");
58+
}
59+
4660
public static @NotNull Inventory create(PlayerBrush pb) {
4761
Inventory inv = Bukkit.createInventory(null, 54, Component.text("goPaint Menu", NamedTextColor.DARK_BLUE));
4862
update(inv, pb);
@@ -64,7 +78,7 @@ public class GUI {
6478

6579
private static void formatDefault(@NotNull Inventory inventory) {
6680
for (int slot = 0; slot < inventory.getSize(); slot++) {
67-
inventory.setItem(slot, Items.create(Material.GRAY_STAINED_GLASS_PANE, 1, "§7", ""));
81+
inventory.setItem(slot, LIME_DECORATION);
6882
}
6983
}
7084

@@ -73,145 +87,129 @@ public static void update(@NotNull Inventory inventory, @NotNull PlayerBrush pla
7387

7488
// FILLER
7589
formatDefault(inventory);
76-
7790
// goPaint toggle
78-
if (playerBrush.enabled()) {
79-
inventory.setItem(1, Items.create(Material.LIME_STAINED_GLASS_PANE, 1, "§7", ""));
80-
inventory.setItem(10, Items.create(Settings.settings().generic.DEFAULT_BRUSH, 1, "§6goPaint Brush",
81-
"§a§lEnabled\n\n§7Left click with item to export\n§7Right click to toggle"
82-
));
83-
inventory.setItem(19, Items.create(Material.LIME_STAINED_GLASS_PANE, 1, "§7", ""));
84-
} else {
85-
inventory.setItem(1, Items.create(Material.RED_STAINED_GLASS_PANE, 1, "§7", ""));
86-
inventory.setItem(10, Items.create(Settings.settings().generic.DEFAULT_BRUSH, 1, "§6goPaint Brush",
87-
"§c§lDisabled\n\n§7Left click with item to export\n§7Right click to toggle"
88-
));
89-
inventory.setItem(19, Items.create(Material.RED_STAINED_GLASS_PANE, 1, "§7", ""));
90-
}
91-
91+
setPaintToggle(inventory, playerBrush);
9292
// Brushes + Chance
93-
inventory.setItem(2, Items.create(Material.ORANGE_STAINED_GLASS_PANE, 1, "§7", ""));
94-
93+
inventory.setItem(2, ORANGE_DECORATION);
9594

9695
String clicks = "\n§7Shift click to select\n§7Click to cycle brush\n\n";
9796

9897
inventory.setItem(11, Items.createHead(brush.getHead(), 1, "§6Selected Brush type",
9998
clicks + plugin.getBrushManager().getBrushLore(brush)
10099
));
101-
inventory.setItem(20, Items.create(Material.ORANGE_STAINED_GLASS_PANE, 1, "§7", ""));
100+
inventory.setItem(20, ORANGE_DECORATION);
102101

103102
// chance
104103
if (brush instanceof SprayBrush) {
105-
inventory.setItem(3, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
104+
inventory.setItem(3, WHITE_DECORATION);
106105
inventory.setItem(12, Items.create(Material.GOLD_NUGGET, 1,
107106
"§6Place chance: §e" + playerBrush.chance() + "%",
108-
"\n§7Left click to increase\n§7Right click to decrease"
107+
INCREASE_DECREASE_LORE
109108
));
110-
inventory.setItem(21, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
109+
inventory.setItem(21, WHITE_DECORATION);
111110
}
112111

113112
// axis
114113
if (brush instanceof DiscBrush) {
115-
inventory.setItem(3, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
114+
inventory.setItem(3, WHITE_DECORATION);
116115
inventory.setItem(12, Items.create(Material.COMPASS, 1,
117116
"§6Axis: §e" + playerBrush.axis(), "\n§7Click to change"
118117
));
119-
inventory.setItem(21, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
118+
inventory.setItem(21, WHITE_DECORATION);
120119
}
121120

122121

123122
// thickness
124123
if (brush instanceof OverlayBrush || brush instanceof UnderlayBrush) {
125-
inventory.setItem(3, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
124+
inventory.setItem(3, WHITE_DECORATION);
126125
inventory.setItem(12, Items.create(Material.BOOK, 1,
127126
"§6Layer Thickness: §e" + playerBrush.thickness(),
128-
"\n§7Left click to increase\n§7Right click to decrease"
127+
INCREASE_DECREASE_LORE
129128
));
130-
inventory.setItem(21, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
129+
inventory.setItem(21, WHITE_DECORATION);
131130
}
132131

133132
// angle settings
134133
if (brush instanceof AngleBrush) {
135-
inventory.setItem(3, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
134+
inventory.setItem(3, WHITE_DECORATION);
136135
inventory.setItem(12, Items.create(Material.DAYLIGHT_DETECTOR, 1,
137136
"§6Angle Check Distance: §e" + playerBrush.angleDistance(),
138-
"\n§7Left click to increase\n§7Right click to decrease"
137+
INCREASE_DECREASE_LORE
139138
));
140-
inventory.setItem(21, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
141-
142-
inventory.setItem(4, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
139+
inventory.setItem(21, WHITE_DECORATION);
140+
inventory.setItem(4, WHITE_DECORATION);
143141
inventory.setItem(13, Items.create(Material.BLAZE_ROD, 1,
144142
"§6Maximum Angle: §e" + playerBrush.angleHeightDifference() + "°",
145143
"\n§7Left click to increase\n§7Right click to decrease\n§7Shift click to change by 15"
146144
));
147-
inventory.setItem(22, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
145+
inventory.setItem(22, WHITE_DECORATION);
148146
}
149147

150148
// fracture settings
151149
if (brush instanceof FractureBrush) {
152-
inventory.setItem(3, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
150+
inventory.setItem(3, WHITE_DECORATION);
153151
inventory.setItem(12, Items.create(Material.DAYLIGHT_DETECTOR, 1,
154152
"§6Fracture Check Distance: §e" + playerBrush.fractureDistance(),
155-
"\n§7Left click to increase\n§7Right click to decrease"
153+
INCREASE_DECREASE_LORE
156154
));
157-
inventory.setItem(21, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
155+
inventory.setItem(21, WHITE_DECORATION);
158156
}
159157

160158
// angle settings
161159
if (brush instanceof GradientBrush) {
162-
inventory.setItem(4, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
160+
inventory.setItem(4, WHITE_DECORATION);
163161
inventory.setItem(13, Items.create(Material.MAGMA_CREAM, 1,
164162
"§6Mixing Strength: §e" + playerBrush.mixingStrength() + "%",
165-
"\n§7Left click to increase\n§7Right click to decrease"
163+
INCREASE_DECREASE_LORE
166164
));
167-
inventory.setItem(22, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
165+
inventory.setItem(22, WHITE_DECORATION);
168166
}
169167

170168
if (brush instanceof SplatterBrush || brush instanceof PaintBrush || brush instanceof GradientBrush) {
171-
inventory.setItem(3, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
169+
inventory.setItem(3, WHITE_DECORATION);
172170
inventory.setItem(12, Items.create(Material.BLAZE_POWDER, 1,
173171
"§6Falloff Strength: §e" + playerBrush.falloffStrength() + "%",
174-
"\n§7Left click to increase\n§7Right click to decrease"
172+
INCREASE_DECREASE_LORE
175173
));
176-
inventory.setItem(21, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
174+
inventory.setItem(21, WHITE_DECORATION);
177175
}
178176

179177

180178
// Size
181-
inventory.setItem(5, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
179+
inventory.setItem(5, WHITE_DECORATION);
182180
inventory.setItem(14, Items.create(Material.BROWN_MUSHROOM, 1,
183181
"§6Brush Size: §e" + playerBrush.size(),
184182
"\n§7Left click to increase\n§7Right click to decrease\n§7Shift click to change by 10"
185183
));
186-
inventory.setItem(23, Items.create(Material.WHITE_STAINED_GLASS_PANE, 1, "§7", ""));
184+
inventory.setItem(23, WHITE_DECORATION);
187185

188186
// Mask toggle
189-
if (playerBrush.maskEnabled()) {
190-
inventory.setItem(6, Items.create(Material.LIME_STAINED_GLASS_PANE, 1, "§7", ""));
191-
inventory.setItem(15, Items.create(Material.JACK_O_LANTERN, 1,
192-
"§6Mask",
193-
"§a§lEnabled\n\n§7Click to toggle"
194-
));
195-
inventory.setItem(24, Items.create(Material.LIME_STAINED_GLASS_PANE, 1, "§7", ""));
196-
} else {
197-
inventory.setItem(6, Items.create(Material.RED_STAINED_GLASS_PANE, 1, "§7", ""));
198-
inventory.setItem(15, Items.create(Material.CARVED_PUMPKIN, 1, "§6Mask", "§c§lDisabled\n\n§7Click to toggle"));
199-
inventory.setItem(24, Items.create(Material.RED_STAINED_GLASS_PANE, 1, "§7", ""));
200-
}
187+
setMaskItems(inventory, playerBrush);
201188

202189
// Surface Mode toggle
203190
addSurfaceModeSwitch(inventory, playerBrush);
204191

205192
// Place Block
206193
for (int x = 37; x <= 41; x++) {
207-
inventory.setItem(x, Items.create(Material.YELLOW_STAINED_GLASS_PANE, 1, "§7", ""));
194+
inventory.setItem(x, YELLOW_DECORATION);
208195
}
209196
for (int x = 46; x <= 50; x++) {
210-
inventory.setItem(x, Items.create(Material.BARRIER, 1, "§cEmpty Slot", "\n§7Click with a block to set"));
197+
inventory.setItem(x, EMPTY_SLOT);
211198
}
199+
200+
// Block Change
201+
setBlockChangeItems(inventory, playerBrush);
202+
203+
// Mask Block
204+
inventory.setItem(43, YELLOW_DECORATION);
205+
inventory.setItem(52, Items.create(playerBrush.mask(), 1, "§6Current Mask", "\n§7Left click with a block to change"));
206+
}
207+
208+
private static void setBlockChangeItems(@NotNull Inventory inventory, @NotNull PlayerBrush playerBrush) {
209+
final List<Material> blocks = playerBrush.blocks();
210+
if (blocks.isEmpty()) return;
212211
int x = 46;
213-
int size = playerBrush.blocks().size();
214-
int chance = size == 0 ? 0 : 100 / size;
212+
int chance = blocks.isEmpty() ? 0 : 100 / blocks.size();
215213
for (Material material : playerBrush.blocks()) {
216214
if (chance > 64) {
217215
inventory.setItem(x, Items.create(material, 1,
@@ -226,30 +224,70 @@ public static void update(@NotNull Inventory inventory, @NotNull PlayerBrush pla
226224
}
227225
x++;
228226
}
227+
}
229228

230-
// Mask Block
231-
inventory.setItem(43, Items.create(Material.YELLOW_STAINED_GLASS_PANE, 1, "§7", ""));
232-
inventory.setItem(52, Items.create(playerBrush.mask(), 1, "§6Current Mask", "\n§7Left click with a block to change"));
229+
/**
230+
* Set the relevant {@link ItemStack}'s into an {@link Inventory} to represent the goPaint toggle
231+
*
232+
* @param inventory the inventory to set the items in
233+
* @param playerBrush the {@link PlayerBrush} to get the goPaint status from
234+
*/
235+
private static void setPaintToggle(@NotNull Inventory inventory, @NotNull PlayerBrush playerBrush) {
236+
final String lore = "§a§lEnabled\n\n§7Left click with item to export\n§7Right click to toggle";
237+
final String displayName = "§6goPaint Brush";
238+
final boolean hasBrushEnabled = playerBrush.enabled();
239+
inventory.setItem(1, hasBrushEnabled ? LIME_DECORATION : RED_DECORATION);
240+
inventory.setItem(10, Items.create(Settings.settings().generic.DEFAULT_BRUSH, 1, displayName, lore));
241+
inventory.setItem(19, hasBrushEnabled ? LIME_DECORATION : RED_DECORATION);
242+
}
243+
244+
/**
245+
* Set the relevant {@link ItemStack}'s into an {@link Inventory} to represent the mask toggle
246+
*
247+
* @param inventory the inventory to set the items in
248+
* @param playerBrush the {@link PlayerBrush} to get the mask status from
249+
*/
250+
private static void setMaskItems(@NotNull Inventory inventory, @NotNull PlayerBrush playerBrush) {
251+
if (playerBrush.maskEnabled()) {
252+
inventory.setItem(6, LIME_DECORATION);
253+
inventory.setItem(15, Items.create(Material.JACK_O_LANTERN, 1,
254+
"§6Mask",
255+
"§a§lEnabled\n\n§7Click to toggle"
256+
));
257+
inventory.setItem(24, LIME_DECORATION);
258+
return;
259+
}
260+
final ItemStack mask = Items.create(Material.CARVED_PUMPKIN, 1, "§6Mask", "§c§lDisabled\n\n§7Click to toggle");
261+
inventory.setItem(6, RED_DECORATION);
262+
inventory.setItem(15, mask);
263+
inventory.setItem(24, RED_DECORATION);
233264
}
234265

235-
private static void addSurfaceModeSwitch(Inventory inv, PlayerBrush playerBrush) {
236-
Material pane = switch (playerBrush.surfaceMode()) {
237-
case DIRECT -> Material.LIME_STAINED_GLASS_PANE;
238-
case DISABLED -> Material.RED_STAINED_GLASS_PANE;
239-
case RELATIVE -> Material.ORANGE_STAINED_GLASS_PANE;
266+
/**
267+
* Add the surface mode switch to the {@link Inventory}.
268+
*
269+
* @param inv the inventory to add the switch to
270+
* @param playerBrush the {@link PlayerBrush} to get the surface mode from
271+
*/
272+
private static void addSurfaceModeSwitch(@NotNull Inventory inv, @NotNull PlayerBrush playerBrush) {
273+
// Reuses the constant for the pane to reduce object creation
274+
ItemStack pane = switch (playerBrush.surfaceMode()) {
275+
case DIRECT -> LIME_DECORATION;
276+
case DISABLED -> RED_DECORATION;
277+
case RELATIVE -> ORANGE_DECORATION;
240278
};
241279
String color = switch (playerBrush.surfaceMode()) {
242280
case DIRECT -> "§a";
243281
case DISABLED -> "§c";
244282
case RELATIVE -> "§6";
245283
};
246284

247-
inv.setItem(7, Items.create(pane, 1, "§7", ""));
285+
inv.setItem(7, pane);
248286
inv.setItem(16, Items.create(Material.LIGHT_WEIGHTED_PRESSURE_PLATE, 1,
249287
"§6Surface Mode",
250288
color + "§l" + playerBrush.surfaceMode().getName() + "\n\n§7Click to toggle"
251289
));
252-
inv.setItem(25, Items.create(pane, 1, "§7", ""));
290+
inv.setItem(25, pane);
253291
}
254292

255293
}

0 commit comments

Comments
 (0)