Skip to content

Commit f4bcca6

Browse files
Im-FranTheProgramSrc
authored andcommitted
4.2.10 Changelog:
+ Added Scroll Browser (Vertical Browser) + Added GUIUpdateEvent * Fixed issues with Browser Search Feature not working
1 parent e7f1a63 commit f4bcca6

File tree

7 files changed

+281
-70
lines changed

7 files changed

+281
-70
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## v4.2.10 Changelog:
2+
```
3+
+ Added Scroll Browser (Vertical Browser)
4+
+ Added GUIUpdateEvent
5+
* Fixed issues with Browser Search Feature not working
6+
```
7+
18
## v4.2.9 Changelog:
29
```
310
* Now SimpleItem#getDisplayName() and SimpleItem#getLore() doesn't return null to avoid errors

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.theprogramsrc</groupId>
88
<artifactId>SuperCoreAPI</artifactId>
9-
<version>4.2.10_BETA4</version>
9+
<version>4.2.10</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SuperCoreAPI</name>
Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package xyz.theprogramsrc.supercoreapi.spigot.guis;
22

3-
import org.bukkit.ChatColor;
43
import org.bukkit.entity.Player;
54
import xyz.theprogramsrc.supercoreapi.global.translations.Base;
65
import xyz.theprogramsrc.supercoreapi.spigot.dialog.Dialog;
76
import xyz.theprogramsrc.supercoreapi.spigot.guis.action.ClickAction;
7+
import xyz.theprogramsrc.supercoreapi.spigot.guis.events.GUIEvent;
8+
import xyz.theprogramsrc.supercoreapi.spigot.guis.events.GUIOpenEvent;
89
import xyz.theprogramsrc.supercoreapi.spigot.guis.objects.GUIRows;
910
import xyz.theprogramsrc.supercoreapi.spigot.items.SimpleItem;
1011

11-
import java.util.ArrayList;
12+
import java.util.Arrays;
1213
import java.util.List;
1314
import java.util.stream.Collectors;
1415

1516
public abstract class BrowserGUI<OBJ> extends GUI {
1617

1718
private String searchTerm = null;
1819
public int maxItemsPerPage = 36;
20+
public boolean backEnabled = false;
1921
private int page = 0;
2022

2123
/**
@@ -32,39 +34,33 @@ protected GUIRows getRows() {
3234
}
3335

3436
@Override
35-
public GUIButton[] getButtons() {
36-
OBJ[] objs = this.getObjects();
37-
List<OBJ> objectsFound = new ArrayList<>();
38-
for (OBJ obj : objs) {
39-
if(obj != null){
40-
if(this.searchTerm == null){
41-
objectsFound.add(obj);
42-
}else{
43-
GUIButton button = this.getButton(obj);
44-
String name = ChatColor.stripColor(new SimpleItem(button.getItemStack()).getDisplayName()).toLowerCase();
45-
if(name.contains(ChatColor.stripColor(this.searchTerm.toLowerCase()))){
46-
objectsFound.add(obj);
47-
}
37+
public void onEvent(GUIEvent event) {
38+
if(event instanceof GUIOpenEvent){
39+
this.clearButtons();
40+
OBJ[] objs = this.getObjects();
41+
List<OBJ> objectsFound = Arrays.stream(this.getObjects()).filter(obj->{
42+
if(this.searchTerm != null){
43+
String itemName = this.getSuperUtils().removeColor(new SimpleItem(this.getButton(obj).getItemStack()).getDisplayName()).toLowerCase();
44+
String search = this.getSuperUtils().removeColor(this.searchTerm).toLowerCase();
45+
return itemName.contains(search);
4846
}
47+
48+
return true;
49+
}).collect(Collectors.toList());
50+
int index0 = this.page * this.maxItemsPerPage;
51+
int index1 = Math.min(index0 + this.maxItemsPerPage, objectsFound.size());
52+
int maxPages = (int)Math.round(Math.ceil((double)objectsFound.size() / (double)maxItemsPerPage));
53+
List<GUIButton> buttons = objectsFound.subList(index0, index1).stream().map(this::getButton).collect(Collectors.toList());
54+
int i = 0;
55+
for(int o = 0; o < buttons.size(); ++o){
56+
buttons.set(o, buttons.get(o).setSlot(i));
57+
i++;
4958
}
50-
}
51-
int index0 = this.page * this.maxItemsPerPage;
52-
int index1 = Math.min(index0 + this.maxItemsPerPage, objectsFound.size());
53-
int maxPages = (int)Math.round(Math.ceil((double)objectsFound.size() / (double)maxItemsPerPage));
54-
List<GUIButton> buttons1 = new ArrayList<>();
55-
int slot = 0;
56-
for (GUIButton b : objectsFound.subList(index0, index1).stream().map(this::getButton).collect(Collectors.toList())) {
57-
b.setSlot(slot);
58-
buttons1.add(b);
59-
slot++;
60-
}
61-
List<GUIButton> buttons = new ArrayList<>(buttons1);
62-
buttons1.clear(); // Save memory
63-
objectsFound.clear(); // Save memory
6459

65-
buttons.add(new GUIButton(49, this.searchTerm == null ? this.getPreloadedItems().getSearchItem() : this.getPreloadedItems().getEndSearchItem()).setAction(a -> {
60+
this.addButtons(buttons);
61+
this.removeButtons(43, 44,45,46,47,48,49,50,51,52,53);
6662
if(this.searchTerm == null){
67-
new Dialog(this.getPlayer()){
63+
this.addButton(new GUIButton(49, this.getPreloadedItems().getSearchItem(), a-> new Dialog(this.getPlayer()){
6864

6965
@Override
7066
public String getTitle() {
@@ -88,32 +84,33 @@ public boolean onResult(String playerInput) {
8884
}
8985
}.setRecall(player ->{
9086
this.page = 0;
91-
this.open();
92-
});
87+
this.getSpigotTasks().runTask(this::open);
88+
})));
9389
}else{
94-
this.searchTerm = null;
95-
this.page = 0;
96-
this.open();
90+
this.addButton(new GUIButton(49, this.getPreloadedItems().getEndSearchItem(), a->{
91+
this.searchTerm = null;
92+
this.page = 0;
93+
this.open();
94+
}));
95+
}
96+
97+
if(this.page != 0){
98+
this.addButton(new GUIButton(52, this.getPreloadedItems().getPreviousItem()).setAction(a -> {
99+
this.page -= 1;
100+
this.open();
101+
}));
102+
}
103+
if(this.page+1 < maxPages){
104+
this.addButton(new GUIButton(53, this.getPreloadedItems().getNextItem()).setAction(a -> {
105+
this.page += 1;
106+
this.open();
107+
}));
97108
}
98-
}));
99-
buttons.add(new GUIButton(45, this.getPreloadedItems().getBackItem()).setAction(this::onBack));
100-
101-
if(this.page != 0){
102-
buttons.add(new GUIButton(52, this.getPreloadedItems().getPreviousItem()).setAction(a -> {
103-
this.page--;
104-
this.open();
105-
}));
106-
}
107-
if(this.page+1 < maxPages){
108-
buttons.add(new GUIButton(53, this.getPreloadedItems().getNextItem()).setAction(a -> {
109-
this.page++;
110-
this.open();
111-
}));
112-
}
113109

114-
GUIButton[] guiButtons = new GUIButton[buttons.size()];
115-
guiButtons = buttons.toArray(guiButtons);
116-
return guiButtons;
110+
if(this.backEnabled){
111+
this.addButton(new GUIButton(45, this.getPreloadedItems().getBackItem()).setAction(this::onBack));
112+
}
113+
}
117114
}
118115

119116
/**
@@ -133,5 +130,9 @@ public boolean onResult(String playerInput) {
133130
* Executed when the back button is clicked
134131
* @param clickAction The ClickAction
135132
*/
136-
public abstract void onBack(ClickAction clickAction);
133+
public void onBack(ClickAction clickAction){
134+
135+
}
136+
137+
137138
}

src/main/java/xyz/theprogramsrc/supercoreapi/spigot/guis/GUI.java

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import xyz.theprogramsrc.supercoreapi.spigot.guis.objects.GUIRows;
2323
import xyz.theprogramsrc.supercoreapi.spigot.utils.xseries.XMaterial;
2424

25+
import java.util.Collection;
2526
import java.util.LinkedHashMap;
27+
import java.util.Map;
2628
import java.util.UUID;
2729

2830
public abstract class GUI extends SpigotModule {
@@ -48,8 +50,8 @@ public GUI(Player player){
4850
* Opens the {@link GUI GUI}
4951
*/
5052
public void open(){
51-
HandlerList.unregisterAll(this);
52-
this.listener(this);
53+
if(this.inv == null)
54+
this.listener(this);
5355
this.inv = Bukkit.createInventory(null, this.getRows().getSize(), this.getSuperUtils().color(this.isTitleCentered() ? this.getCenteredTitle() : this.getTitle()));
5456
this.player.openInventory(this.inv);
5557
}
@@ -72,6 +74,28 @@ public void close(){
7274
});
7375
}
7476

77+
/**
78+
* Adds the specified buttons to the GUI
79+
* @param buttons the buttons to add
80+
*
81+
* @see #addButton(GUIButton)
82+
*/
83+
public void addButtons(Collection<GUIButton> buttons){
84+
for(GUIButton button : buttons)
85+
this.addButton(button);
86+
}
87+
88+
/**
89+
* Adds the specified buttons to the GUI
90+
* @param buttons the buttons to add
91+
*
92+
* @see #addButton(GUIButton)
93+
*/
94+
public void addButtons(GUIButton... buttons){
95+
for(GUIButton button : buttons)
96+
this.addButton(button);
97+
}
98+
7599
/**
76100
* Adds a button inside the {@link GUI GUI} using the last empty slot if there is no slot specified inside the {@link GUIButton button}
77101
* @param button the button to add inside the {@link GUI GUI}
@@ -86,15 +110,63 @@ public void addButton(GUIButton button){
86110
* @param button the button to add inside the {@link GUI GUI}
87111
*/
88112
public void setButton(int slot, GUIButton button){
89-
if(slot < 0 || slot > 53){
113+
if(slot < 0){
90114
while(this.buttons.containsKey(slot)){
91115
slot++;
92116
}
93117
button.setSlot(slot);
94118
}
119+
120+
if(this.buttons.containsKey(slot)){
121+
if(this.buttons.get(slot).equals(button)){
122+
return;
123+
}
124+
}
95125
this.buttons.put(slot, button);
96126
}
97127

128+
/**
129+
* Gets a button from the GUI
130+
* @param slot the slot
131+
* @return the button located in the slot or null if there is no button
132+
*/
133+
public GUIButton getButton(int slot){
134+
return this.buttons.get(slot);
135+
}
136+
137+
/**
138+
* Removes all the buttons in the specified slots
139+
* @param slots the slots of the buttons to remove
140+
*/
141+
public void removeButtons(int... slots){
142+
for(int slot : slots)
143+
this.removeButton(slot);
144+
}
145+
146+
/**
147+
* Removes a button from the GUI
148+
* @param slot the slot of the button to remove
149+
*/
150+
public void removeButton(int slot){
151+
this.buttons.remove(slot);
152+
}
153+
154+
/**
155+
* Removes all the buttons from the GUI
156+
*/
157+
public void clearButtons(){
158+
this.buttons.clear();
159+
}
160+
161+
/**
162+
* Checks if there is a button in the given slot
163+
* @param slot the slot to check
164+
* @return true if there is a button, otherwise false
165+
*/
166+
public boolean containsButton(int slot){
167+
return this.buttons.containsKey(slot);
168+
}
169+
98170
/**
99171
* Alternative method to add items into a {@link GUI GUI}
100172
* @return the buttons to add inside the {@link GUI GUI}
@@ -237,7 +309,7 @@ public void onQuit(PlayerQuitEvent event){
237309
public void syncItems(TimerEvent event){
238310
if(event.getTime() == Time.TICK){
239311
if(this.inv != null && this.player != null){
240-
this.inv.clear();
312+
this.onEvent(new GUIUpdateEvent(this));
241313
GUIButton[] buttonsArray = this.getButtons();
242314
if(buttonsArray != null){
243315
for (GUIButton button : buttonsArray) {
@@ -247,16 +319,13 @@ public void syncItems(TimerEvent event){
247319
}
248320
}
249321

250-
for (GUIButton button : this.buttons.values()) {
251-
int slot = button.getSlot();
252-
ItemStack item = button.getItemStack();
253-
if(item != null){
254-
if(slot <= this.getRows().getSize() && slot >= 0){
255-
this.inv.setItem(slot, item);
256-
}
257-
}
322+
this.inv.clear();
323+
for(Map.Entry<Integer, GUIButton> entry : new LinkedHashMap<>(this.buttons).entrySet()){
324+
int slot = entry.getKey();
325+
if(this.inv.getSize() < slot || slot > this.inv.getSize()) continue;
326+
GUIButton button = entry.getValue();
327+
this.inv.setItem(slot, button.getItemStack());
258328
}
259-
260329
this.player.updateInventory();
261330
}
262331
}

0 commit comments

Comments
 (0)