Skip to content

Commit c1f3d55

Browse files
author
TheProgramSrc
committed
Change Log:
• Added GUICloseEvent and GUIOpenEvent • Improvements • Bug Fixes NOTE: THIS IS A BETA, IS NOT RECOMMENDED TO USE IN PRODUCTION OR RELEASES
1 parent fcb0dcf commit c1f3d55

File tree

6 files changed

+131
-41
lines changed

6 files changed

+131
-41
lines changed

SuperCoreAPI.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
4848
<orderEntry type="library" scope="PROVIDED" name="Maven: com.zaxxer:HikariCP:3.3.1" level="project" />
4949
<orderEntry type="library" scope="PROVIDED" name="Maven: org.xerial:sqlite-jdbc:3.25.2" level="project" />
50-
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-core:2.5" level="project" />
51-
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-api:2.5" level="project" />
50+
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-core:2.13.2" level="project" />
51+
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-api:2.13.2" level="project" />
5252
</component>
5353
</module>

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.theprogramsrc</groupId>
88
<artifactId>SuperCoreAPI</artifactId>
9-
<version>3.5.0</version>
9+
<version>3.6.0_BETA1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SuperCoreAPI</name>
@@ -162,7 +162,7 @@
162162
<dependency>
163163
<groupId>org.apache.logging.log4j</groupId>
164164
<artifactId>log4j-core</artifactId>
165-
<version>2.5</version>
165+
<version>2.13.2</version>
166166
<scope>provided</scope>
167167
</dependency>
168168
</dependencies>

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

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
import xyz.theprogramsrc.supercoreapi.spigot.events.timer.TimerEvent;
1818
import xyz.theprogramsrc.supercoreapi.spigot.guis.action.ClickAction;
1919
import xyz.theprogramsrc.supercoreapi.spigot.guis.action.ClickType;
20-
import xyz.theprogramsrc.supercoreapi.spigot.guis.events.GUIClickEvent;
21-
import xyz.theprogramsrc.supercoreapi.spigot.guis.events.GUIEmptyClickEvent;
22-
import xyz.theprogramsrc.supercoreapi.spigot.guis.events.GUIEvent;
23-
import xyz.theprogramsrc.supercoreapi.spigot.guis.events.GUIOutsideClickEvent;
20+
import xyz.theprogramsrc.supercoreapi.spigot.guis.events.*;
2421
import xyz.theprogramsrc.supercoreapi.spigot.guis.objects.GUIRows;
2522
import xyz.theprogramsrc.supercoreapi.spigot.utils.xseries.XMaterial;
2623

@@ -57,6 +54,8 @@ public void open(){
5754
((SpigotPlugin)this.plugin).listener(this);
5855
}
5956
this.inventory = Bukkit.createInventory(null, this.getRows().getSize(), this.plugin.getSuperUtils().color(this.applyPlaceholders(this.centerTitle() ? this.getCenteredTitle() : this.getTitle())));
57+
this.loadGUIItemsAndSyncItems();
58+
this.onEvent(new GUIOpenEvent(this, this.player));
6059
this.player.openInventory(this.inventory);
6160
});
6261
}
@@ -66,9 +65,13 @@ public void open(){
6665
*/
6766
public void close(){
6867
this.getSpigotTasks().runTask(()->{
69-
HandlerList.unregisterAll(this);
70-
this.inventory = null;
71-
this.player.closeInventory();
68+
GUICloseEvent event = new GUICloseEvent(this, this.player);
69+
this.onEvent(event);
70+
if(!event.isCancelled()){
71+
HandlerList.unregisterAll(this);
72+
this.inventory = null;
73+
this.player.closeInventory();
74+
}
7275
});
7376
}
7477

@@ -97,6 +100,35 @@ public void clear(){
97100
this.inventory.clear();
98101
}
99102

103+
/**
104+
* Adds a new GUIButton to the GUI
105+
* @param guiButton the button
106+
*/
107+
public void addButton(GUIButton guiButton){
108+
if(guiButton.getSlot() == -1){
109+
if(this.inventory != null){
110+
this.buttons.put(this.inventory.firstEmpty(), guiButton);
111+
}else{
112+
for(int i = 0; i < this.getRows().getSize(); ++i){
113+
if(!this.buttons.containsKey(i)){
114+
this.buttons.put(i, guiButton);
115+
return;
116+
}
117+
}
118+
}
119+
}else{
120+
this.buttons.put(guiButton.getSlot(), guiButton);
121+
}
122+
}
123+
124+
/**
125+
* Removes a button from the GUI
126+
* @param slot the slot
127+
*/
128+
public void remButton(int slot){
129+
this.buttons.remove(slot);
130+
}
131+
100132
/**
101133
* Used to know if the GUI title should be centered
102134
* @return if the GUI title should be centered
@@ -163,36 +195,7 @@ protected String applyPlaceholders(String text){
163195
@EventHandler
164196
public void syncItems(TimerEvent event){
165197
if(event.getTime() == Time.TICK){
166-
if(this.inventory != null){
167-
this.buttons = new HashMap<>();
168-
GUIButton[] buttons = this.getButtons();
169-
if(buttons == null) return;
170-
for (GUIButton b : buttons) {
171-
int slot = b.getSlot();
172-
if (slot == -1) {
173-
for(slot = 0; this.buttons.containsKey(slot);){
174-
slot++;
175-
}
176-
}
177-
178-
this.buttons.put(slot, b);
179-
}
180-
181-
this.inventory.clear();
182-
183-
for(GUIButton b : this.buttons.values()){
184-
int slot = b.getSlot();
185-
ItemStack item = b.getItemStack();
186-
if(item != null){
187-
if(slot <= this.getRows().getSize() && slot >= 0){
188-
this.inventory.setItem(slot, item);
189-
}
190-
}
191-
}
192-
193-
194-
this.player.updateInventory();
195-
}
198+
this.loadGUIItemsAndSyncItems();
196199
}
197200
}
198201

@@ -305,4 +308,37 @@ protected void onEvent(GUIEvent event){}
305308
*/
306309
protected abstract GUIButton[] getButtons();
307310

311+
private void loadGUIItemsAndSyncItems(){
312+
if(this.inventory != null){
313+
this.buttons = new HashMap<>();
314+
GUIButton[] buttons = this.getButtons();
315+
if(buttons == null) return;
316+
for (GUIButton b : buttons) {
317+
int slot = b.getSlot();
318+
if (slot == -1) {
319+
for(slot = 0; this.buttons.containsKey(slot);){
320+
slot++;
321+
}
322+
}
323+
324+
this.buttons.put(slot, b);
325+
}
326+
327+
this.inventory.clear();
328+
329+
for(GUIButton b : this.buttons.values()){
330+
int slot = b.getSlot();
331+
ItemStack item = b.getItemStack();
332+
if(item != null){
333+
if(slot <= this.getRows().getSize() && slot >= 0){
334+
this.inventory.setItem(slot, item);
335+
}
336+
}
337+
}
338+
339+
340+
this.player.updateInventory();
341+
}
342+
}
343+
308344
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package xyz.theprogramsrc.supercoreapi.spigot.guis.events;
2+
3+
import org.bukkit.entity.Player;
4+
import xyz.theprogramsrc.supercoreapi.spigot.guis.GUI;
5+
6+
public class GUICloseEvent extends GUIEvent{
7+
8+
private boolean cancelled;
9+
10+
public GUICloseEvent(GUI gui, Player player) {
11+
super(gui, player);
12+
this.cancelled = false;
13+
}
14+
15+
public boolean isCancelled() {
16+
return cancelled;
17+
}
18+
19+
public void setCancelled(boolean cancelled) {
20+
this.cancelled = cancelled;
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package xyz.theprogramsrc.supercoreapi.spigot.guis.events;
2+
3+
import org.bukkit.entity.Player;
4+
import xyz.theprogramsrc.supercoreapi.spigot.guis.GUI;
5+
6+
public class GUIOpenEvent extends GUIEvent{
7+
8+
public GUIOpenEvent(GUI gui, Player player) {
9+
super(gui, player);
10+
}
11+
12+
}

src/main/java/xyz/theprogramsrc/supercoreapi/spigot/items/SimpleItem.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,26 @@ public SimpleItem setShowEnchantments(boolean showEnchantments){
306306
return this;
307307
}
308308

309+
/**
310+
* Sets if the item glows or not (Using an enchantment)
311+
*
312+
* NOTE: This will add and remove the enchantment 'Durability' with Level '1'
313+
* If you need that enchantment is not recommended to use <pre>setGlowing(false)</pre>
314+
*
315+
* @param glowing either show the item glowing or not
316+
*/
317+
public SimpleItem setGlowing(boolean glowing){
318+
ItemFlag flag = ItemFlag.HIDE_ENCHANTS;
319+
if(glowing){
320+
this.addFlag(flag);
321+
this.addEnchantment(Enchantment.DURABILITY);
322+
}else{
323+
this.removeFlag(flag);
324+
this.removeEnchantment(Enchantment.DURABILITY);
325+
}
326+
return this;
327+
}
328+
309329
/**
310330
* Sets if the attributes should be shown
311331
* @param showAttributes true to show the attributes, false to hide them

0 commit comments

Comments
 (0)