Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 121d7c3

Browse files
KamicjuszJakubk15
andauthored
GH-43 add close button to main gui & change ConfigItem system (#43)
* add close button to main gui change previous ConfigItem class name to ConfigItemImpl add ConfigItem interface add example item to reduce description lines in configuration file * Update src/main/java/com/eternalcode/friends/config/implementation/CloseItem.java Co-authored-by: Jakubk15 <[email protected]> * Update src/main/java/com/eternalcode/friends/config/implementation/CloseItem.java Co-authored-by: Jakubk15 <[email protected]> --------- Co-authored-by: Jakubk15 <[email protected]>
1 parent 7fb4535 commit 121d7c3

File tree

8 files changed

+218
-57
lines changed

8 files changed

+218
-57
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ tasks.withType<ShadowJar> {
122122

123123
tasks {
124124
runServer {
125-
minecraftVersion("1.19.3")
125+
minecraftVersion("1.19.4")
126126
}
127127
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.eternalcode.friends.config.implementation;
2+
3+
import com.eternalcode.friends.util.AdventureUtil;
4+
import com.eternalcode.friends.util.legacy.LegacyColorProcessor;
5+
import dev.triumphteam.gui.builder.item.ItemBuilder;
6+
import dev.triumphteam.gui.guis.GuiItem;
7+
import net.dzikoysk.cdn.entity.Contextual;
8+
import net.dzikoysk.cdn.entity.Description;
9+
import net.dzikoysk.cdn.entity.Exclude;
10+
import net.kyori.adventure.text.minimessage.MiniMessage;
11+
import org.bukkit.Material;
12+
import org.bukkit.inventory.ItemFlag;
13+
14+
import java.util.List;
15+
16+
@Contextual
17+
public class CloseItem implements ConfigItem<CloseItem> {
18+
19+
@Exclude
20+
private final MiniMessage miniMessage = MiniMessage.builder()
21+
.postProcessor(new LegacyColorProcessor())
22+
.build();
23+
24+
public Material type = Material.STONE;
25+
26+
public String name = "&cClose";
27+
28+
public List<String> lore = List.of("&fFirst line of lore");
29+
30+
@Description("# List of commands triggered on click")
31+
@Description("# Available placeholders: {player}")
32+
public List<String> commandOnClick = List.of("give {player} stone 1", "tell {player} you received stone!");
33+
34+
@Override
35+
public GuiItem toGuiItem() {
36+
return ItemBuilder.from(this.type)
37+
.name(AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(this.name)))
38+
.lore(this.lore.stream().map(line -> AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(line))).toList())
39+
.flags(ItemFlag.HIDE_ATTRIBUTES)
40+
.flags(ItemFlag.HIDE_ENCHANTS)
41+
.asGuiItem();
42+
}
43+
44+
@Override
45+
public CloseItem setType(Material type) {
46+
this.type = type;
47+
return this;
48+
}
49+
50+
@Override
51+
public CloseItem setName(String name) {
52+
this.name = name;
53+
return this;
54+
}
55+
56+
@Override
57+
public CloseItem setLore(List<String> lore) {
58+
this.lore = lore;
59+
return this;
60+
}
61+
}
Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,16 @@
11
package com.eternalcode.friends.config.implementation;
22

3-
import com.eternalcode.friends.util.AdventureUtil;
4-
import com.eternalcode.friends.util.legacy.LegacyColorProcessor;
5-
import dev.triumphteam.gui.builder.item.ItemBuilder;
63
import dev.triumphteam.gui.guis.GuiItem;
7-
import net.dzikoysk.cdn.entity.Contextual;
8-
import net.dzikoysk.cdn.entity.Description;
9-
import net.dzikoysk.cdn.entity.Exclude;
10-
import net.kyori.adventure.text.minimessage.MiniMessage;
114
import org.bukkit.Material;
12-
import org.bukkit.inventory.ItemFlag;
13-
import java.util.List;
14-
15-
@Contextual
16-
public class ConfigItem {
17-
18-
@Exclude
19-
private final MiniMessage miniMessage = MiniMessage.builder()
20-
.postProcessor(new LegacyColorProcessor())
21-
.build();
225

23-
@Description("# Material from https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html")
24-
public Material type = Material.STONE;
25-
26-
public String name = "&fItem name";
27-
28-
@Description("# Description of item")
29-
public List<String> lore = List.of("&fFirst line of lore", "&9Second line of lore");
6+
import java.util.List;
307

31-
public GuiItem toGuiItem() {
32-
return ItemBuilder.from(this.type)
33-
.name(AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(this.name)))
34-
.lore(this.lore.stream().map(line -> AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(line))).toList())
35-
.flags(ItemFlag.HIDE_ATTRIBUTES)
36-
.flags(ItemFlag.HIDE_ENCHANTS)
37-
.asGuiItem();
38-
}
8+
public interface ConfigItem<T> {
9+
T setType(Material type);
3910

40-
public ConfigItem setType(Material type) {
41-
this.type = type;
42-
return this;
43-
}
11+
T setName(String name);
4412

45-
public ConfigItem setName(String name) {
46-
this.name = name;
47-
return this;
48-
}
13+
T setLore(List<String> lore);
4914

50-
public ConfigItem setLore(List<String> lore) {
51-
this.lore = lore;
52-
return this;
53-
}
15+
GuiItem toGuiItem();
5416
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.eternalcode.friends.config.implementation;
2+
3+
import com.eternalcode.friends.util.AdventureUtil;
4+
import com.eternalcode.friends.util.legacy.LegacyColorProcessor;
5+
import dev.triumphteam.gui.builder.item.ItemBuilder;
6+
import dev.triumphteam.gui.guis.GuiItem;
7+
import net.dzikoysk.cdn.entity.Contextual;
8+
import net.dzikoysk.cdn.entity.Exclude;
9+
import net.kyori.adventure.text.minimessage.MiniMessage;
10+
import org.bukkit.Material;
11+
import org.bukkit.inventory.ItemFlag;
12+
import java.util.List;
13+
14+
@Contextual
15+
public class ConfigItemImpl implements ConfigItem<ConfigItemImpl> {
16+
17+
@Exclude
18+
private final MiniMessage miniMessage = MiniMessage.builder()
19+
.postProcessor(new LegacyColorProcessor())
20+
.build();
21+
22+
public Material type = Material.STONE;
23+
24+
public String name = "&fItem name";
25+
26+
public List<String> lore = List.of("&fFirst line of lore", "&9Second line of lore");
27+
28+
public GuiItem toGuiItem() {
29+
return ItemBuilder.from(this.type)
30+
.name(AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(this.name)))
31+
.lore(this.lore.stream().map(line -> AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(line))).toList())
32+
.flags(ItemFlag.HIDE_ATTRIBUTES)
33+
.flags(ItemFlag.HIDE_ENCHANTS)
34+
.asGuiItem();
35+
}
36+
37+
@Override
38+
public ConfigItemImpl setType(Material type) {
39+
this.type = type;
40+
return this;
41+
}
42+
43+
@Override
44+
public ConfigItemImpl setName(String name) {
45+
this.name = name;
46+
return this;
47+
}
48+
49+
@Override
50+
public ConfigItemImpl setLore(List<String> lore) {
51+
this.lore = lore;
52+
return this;
53+
}
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.eternalcode.friends.config.implementation;
2+
3+
import com.eternalcode.friends.util.AdventureUtil;
4+
import com.eternalcode.friends.util.legacy.LegacyColorProcessor;
5+
import dev.triumphteam.gui.builder.item.ItemBuilder;
6+
import dev.triumphteam.gui.guis.GuiItem;
7+
import net.dzikoysk.cdn.entity.Contextual;
8+
import net.dzikoysk.cdn.entity.Description;
9+
import net.dzikoysk.cdn.entity.Exclude;
10+
import net.kyori.adventure.text.minimessage.MiniMessage;
11+
import org.bukkit.Material;
12+
import org.bukkit.inventory.ItemFlag;
13+
14+
import java.util.List;
15+
16+
@Contextual
17+
public class ExampleConfigItem implements ConfigItem<ExampleConfigItem> {
18+
19+
@Exclude
20+
private final MiniMessage miniMessage = MiniMessage.builder()
21+
.postProcessor(new LegacyColorProcessor())
22+
.build();
23+
24+
@Description("# Material from https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html")
25+
public Material type = Material.STONE;
26+
27+
@Description("# Name of item")
28+
public String name = "&fItem name";
29+
30+
@Description("# Description of item")
31+
public List<String> lore = List.of("&fFirst line of lore", "&9Second line of lore", "&cThird line of lore");
32+
33+
public GuiItem toGuiItem() {
34+
return ItemBuilder.from(this.type)
35+
.name(AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(this.name)))
36+
.lore(this.lore.stream().map(line -> AdventureUtil.RESET_ITEM.append(miniMessage.deserialize(line))).toList())
37+
.flags(ItemFlag.HIDE_ATTRIBUTES)
38+
.flags(ItemFlag.HIDE_ENCHANTS)
39+
.asGuiItem();
40+
}
41+
42+
@Override
43+
public ExampleConfigItem setType(Material type) {
44+
this.type = type;
45+
return this;
46+
}
47+
48+
@Override
49+
public ExampleConfigItem setName(String name) {
50+
this.name = name;
51+
return this;
52+
}
53+
54+
@Override
55+
public ExampleConfigItem setLore(List<String> lore) {
56+
this.lore = lore;
57+
return this;
58+
}
59+
}

src/main/java/com/eternalcode/friends/config/implementation/GuiConfig.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class MenuItems {
3030
public FriendHead inviteListfriendHead = new FriendHead()
3131
.setLore(List.of("", "&aClick LMB to accept invitation", "&cClick RMB to decline invitation", ""));
3232

33-
public ConfigItem nextPageItem = new ConfigItem()
33+
public ExampleConfigItem nextPageItem = new ExampleConfigItem()
3434
.setName("&aNext page")
3535
.setType(Material.PAPER)
3636
.setLore(List.of(
@@ -39,7 +39,7 @@ public static class MenuItems {
3939
""
4040
));
4141

42-
public ConfigItem previousPageItem = new ConfigItem()
42+
public ConfigItemImpl previousPageItem = new ConfigItemImpl()
4343
.setName("&cPrevious page")
4444
.setType(Material.PAPER)
4545
.setLore(List.of(
@@ -48,7 +48,7 @@ public static class MenuItems {
4848
""
4949
));
5050

51-
public ConfigItem confirmItem = new ConfigItem()
51+
public ConfigItemImpl confirmItem = new ConfigItemImpl()
5252
.setName("&aYes")
5353
.setType(Material.LIME_STAINED_GLASS_PANE)
5454
.setLore(List.of(
@@ -57,7 +57,7 @@ public static class MenuItems {
5757
""
5858
));
5959

60-
public ConfigItem denyItem = new ConfigItem()
60+
public ConfigItemImpl denyItem = new ConfigItemImpl()
6161
.setName("&cNo")
6262
.setType(Material.RED_STAINED_GLASS_PANE)
6363
.setLore(List.of(
@@ -66,34 +66,42 @@ public static class MenuItems {
6666
""
6767
));
6868

69-
public ConfigItem receivedInvitesItem = new ConfigItem()
69+
public ConfigItemImpl receivedInvitesItem = new ConfigItemImpl()
7070
.setName("&aReceived invitations")
7171
.setType(Material.BOOK)
7272
.setLore(List.of(
7373
"",
7474
"&7Click to open",
7575
""
7676
));
77-
public ConfigItem sendInvitesItem = new ConfigItem()
77+
78+
public ConfigItemImpl sendInvitesItem = new ConfigItemImpl()
7879
.setName("&aSending invitations")
7980
.setType(Material.WRITABLE_BOOK)
8081
.setLore(List.of(
8182
"",
8283
"&7Click to open",
8384
""
8485
));
85-
public ConfigItem settingItem = new ConfigItem()
86+
87+
public ConfigItemImpl settingItem = new ConfigItemImpl()
8688
.setName("&aSettings")
8789
.setType(Material.REPEATER)
8890
.setLore(List.of(
8991
"",
9092
"&7Click to open",
9193
""
9294
));
93-
public ConfigItem backToMainMenuItem = new ConfigItem()
95+
96+
public ConfigItemImpl backToMainMenuItem = new ConfigItemImpl()
9497
.setName("&7Back to main menu")
9598
.setType(Material.ARROW)
9699
.setLore(List.of());
100+
101+
public CloseItem closeItem = new CloseItem()
102+
.setName("&cClose menu")
103+
.setType(Material.BARRIER)
104+
.setLore(List.of());
97105
}
98106

99107
@Contextual

src/main/java/com/eternalcode/friends/gui/MainGui.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.eternalcode.friends.gui;
22

33
import com.eternalcode.friends.NotificationAnnouncer;
4+
import com.eternalcode.friends.config.implementation.CloseItem;
45
import com.eternalcode.friends.config.implementation.GuiConfig;
56
import com.eternalcode.friends.config.implementation.MessagesConfig;
67
import com.eternalcode.friends.friend.FriendManager;
@@ -15,6 +16,7 @@
1516
import net.kyori.adventure.text.minimessage.MiniMessage;
1617
import org.bukkit.Material;
1718
import org.bukkit.Server;
19+
import org.bukkit.entity.HumanEntity;
1820
import org.bukkit.entity.Player;
1921
import org.bukkit.plugin.Plugin;
2022

@@ -38,6 +40,7 @@ public class MainGui {
3840
private static final int NEXT_PAGE_ITEM_SLOT = 53;
3941
private static final int BACK_PAGE_ITEM_SLOT = 45;
4042
private static final int RECEIVED_ITEM_SLOT = 48;
43+
private static final int CLOSE_ITEM_SLOT = 49;
4144
private static final int SEND_ITEM_SLOT = 50;
4245

4346

@@ -99,13 +102,26 @@ public void openMainGui(Player player) {
99102
});
100103
});
101104

105+
CloseItem closeItemConfig = menuItems.closeItem;
106+
GuiItem closeButton = menuItems.closeItem.toGuiItem();
107+
closeButton.setAction(event -> {
108+
HumanEntity whoClicked = event.getWhoClicked();
109+
whoClicked.closeInventory();
110+
111+
closeItemConfig.commandOnClick.forEach(command -> {
112+
this.server.dispatchCommand(this.server.getConsoleSender(), command
113+
.replace("{player}", whoClicked.getName()));
114+
});
115+
});
116+
102117
generateFriendsHeads(player, gui);
103118

104119
gui.getFiller().fillBottom(ItemBuilder.from(Material.BLACK_STAINED_GLASS_PANE).name(Component.text(" ")).asGuiItem());
105120
gui.setItem(RECEIVED_ITEM_SLOT, receivedInvitesItem);
106121
gui.setItem(SEND_ITEM_SLOT, sendInvitesItem);
107122
gui.setItem(NEXT_PAGE_ITEM_SLOT, nextPageButton);
108123
gui.setItem(BACK_PAGE_ITEM_SLOT, backPageButton);
124+
gui.setItem(CLOSE_ITEM_SLOT, closeButton);
109125

110126
gui.open(player);
111127
}

0 commit comments

Comments
 (0)