Skip to content

Commit df1d56d

Browse files
author
Francisco Solis
authored
Fixes, New Gui System & New Utils (#13)
* New Gui System (`xyz.thepogramsrc.superocreapi.spigot.gui.Gui`) * Added new util to retrieve time seconds from string & word * Code cleanup & Fixes
1 parent f385646 commit df1d56d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1295
-62
lines changed

dependency-reduced-pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>xyz.theprogramsrc</groupId>
55
<artifactId>SuperCoreAPI</artifactId>
66
<name>SuperCoreAPI</name>
7-
<version>5.1.0</version>
7+
<version>5.2.0-SNAPSHOT</version>
88
<build>
99
<sourceDirectory>src/main/java</sourceDirectory>
1010
<testSourceDirectory>src/test/java</testSourceDirectory>
@@ -184,7 +184,7 @@
184184
<dependency>
185185
<groupId>org.junit.jupiter</groupId>
186186
<artifactId>junit-jupiter</artifactId>
187-
<version>RELEASE</version>
187+
<version>5.7.2</version>
188188
<scope>test</scope>
189189
<exclusions>
190190
<exclusion>

pom.xml

Lines changed: 13 additions & 8 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>5.1.0</version>
9+
<version>5.2.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SuperCoreAPI</name>
@@ -95,6 +95,17 @@
9595
<shadedPattern>xyz.theprogramsrc.supercoreapi.libs.zip4j</shadedPattern>
9696
</relocation>
9797
</relocations>
98+
<filters>
99+
<filter>
100+
<artifact>*:*</artifact>
101+
<excludes>
102+
<exclude>module-info.class</exclude>
103+
<exclude>META-INF/*.SF</exclude>
104+
<exclude>META-INF/*.DSA</exclude>
105+
<exclude>META-INF/*.RSA</exclude>
106+
</excludes>
107+
</filter>
108+
</filters>
98109
</configuration>
99110
</execution>
100111
</executions>
@@ -119,12 +130,6 @@
119130
<version>2.22.1</version>
120131
</plugin>
121132
</plugins>
122-
<resources>
123-
<resource>
124-
<directory>src/main/resources</directory>
125-
<filtering>true</filtering>
126-
</resource>
127-
</resources>
128133
</build>
129134

130135
<distributionManagement>
@@ -283,7 +288,7 @@
283288
<dependency>
284289
<groupId>org.junit.jupiter</groupId>
285290
<artifactId>junit-jupiter</artifactId>
286-
<version>RELEASE</version>
291+
<version>5.7.2</version>
287292
<scope>test</scope>
288293
</dependency>
289294
</dependencies>

src/main/java/xyz/theprogramsrc/supercoreapi/global/files/yml/YMLConfig.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package xyz.theprogramsrc.supercoreapi.global.files.yml;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.List;
6+
import java.util.Set;
7+
38
import org.simpleyaml.configuration.ConfigurationSection;
49
import org.simpleyaml.configuration.comments.CommentType;
510
import org.simpleyaml.configuration.file.FileConfiguration;
611
import org.simpleyaml.configuration.file.YamlFile;
712
import org.simpleyaml.exceptions.InvalidConfigurationException;
8-
import xyz.theprogramsrc.supercoreapi.global.utils.Utils;
913

10-
import java.io.File;
11-
import java.io.IOException;
12-
import java.util.List;
13-
import java.util.Set;
14+
import xyz.theprogramsrc.supercoreapi.global.utils.Utils;
1415

1516
public class YMLConfig {
1617

@@ -127,6 +128,15 @@ public void add(String path, Object value){
127128
if(!this.contains(path)) this.set(path, value);
128129
}
129130

131+
/**
132+
* Removes a path from the file
133+
* @param path the path to remove
134+
*/
135+
public void remove(String path){
136+
this.config.remove(path);
137+
this.save();
138+
}
139+
130140
/**
131141
* Used to check if the configuration contains a specific Path
132142
* @param path Path to check

src/main/java/xyz/theprogramsrc/supercoreapi/global/utils/Utils.java

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package xyz.theprogramsrc.supercoreapi.global.utils;
22

3-
import com.google.common.base.Preconditions;
4-
import com.google.gson.JsonObject;
5-
import com.google.gson.JsonParser;
6-
import org.apache.commons.io.FileUtils;
7-
import xyz.theprogramsrc.supercoreapi.global.translations.Base;
8-
9-
import java.io.*;
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
import java.io.OutputStream;
1011
import java.net.HttpURLConnection;
1112
import java.net.URL;
1213
import java.net.URLConnection;
@@ -16,9 +17,25 @@
1617
import java.security.NoSuchAlgorithmException;
1718
import java.time.LocalDateTime;
1819
import java.time.format.DateTimeFormatter;
19-
import java.util.*;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Base64;
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.LinkedHashMap;
26+
import java.util.List;
27+
import java.util.Objects;
28+
import java.util.concurrent.TimeUnit;
2029
import java.util.stream.Collectors;
2130

31+
import com.google.common.base.Preconditions;
32+
import com.google.gson.JsonObject;
33+
import com.google.gson.JsonParser;
34+
35+
import org.apache.commons.io.FileUtils;
36+
37+
import xyz.theprogramsrc.supercoreapi.global.translations.Base;
38+
2239
public class Utils {
2340

2441
private static long lastInternetCheck = 0L;
@@ -282,6 +299,47 @@ public static boolean isNow(String time, String format){
282299
return now.equals(time);
283300
}
284301

302+
/**
303+
* Gets the specified time string in seconds
304+
* Format:
305+
* - 1h 30s
306+
* - 1d 4h 30s
307+
* - 5m 37s
308+
*
309+
* Example: getTimeSecondsFromString("1h 30s") will return 3630 seconds
310+
* @param timeString Time string to convert
311+
* @return Time in seconds
312+
* @since 5.2.0
313+
*/
314+
public static long getTimeSecondsFromString(String timeString) {
315+
return Arrays.stream(timeString.split(" ")).mapToLong(s -> getTimeSecondsFromWord(s)).sum();
316+
}
317+
318+
/**
319+
* Gets the specified time word in seconds.
320+
* Format:
321+
* - 1h
322+
* - 30m
323+
* - 1d
324+
* - 37s
325+
*
326+
* Example: getTimeSecondsFromWord("1h") will return 3600 seconds
327+
* @param word Word to get the seconds from
328+
* @return The amount in seconds
329+
* @since 5.2.0
330+
*/
331+
public static long getTimeSecondsFromWord(String word) {
332+
if (word.length() < 2) return 0L;
333+
String timeUnitString = word.toCharArray()[word.length() - 1] + "";
334+
TimeUnit timeUnit = Arrays.stream(new TimeUnit[] { TimeUnit.DAYS, TimeUnit.HOURS, TimeUnit.MINUTES, TimeUnit.SECONDS }).filter((t) -> t.toString().toLowerCase().startsWith(timeUnitString)).findFirst().orElse(null);
335+
if(timeUnit == null) return 0L;
336+
try{
337+
return timeUnit == null ? 0L : timeUnit.toSeconds((long) Integer.parseInt(word.substring(0, word.length() - 1)));
338+
}catch(NumberFormatException e){
339+
return 0L;
340+
}
341+
}
342+
285343
/* Internet */
286344

287345
/**
@@ -404,7 +462,6 @@ public static int random(int min, int max) {
404462
* @return The hypotenuse length.
405463
*
406464
* @since 4.12.1
407-
* @author Larskrs
408465
*/
409466
public static double parseHypotenuse(double... values) {
410467
double result = 0.0;
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package xyz.theprogramsrc.supercoreapi.spigot.gui;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
import java.util.stream.Collectors;
6+
7+
import org.bukkit.entity.Player;
8+
9+
import xyz.theprogramsrc.supercoreapi.global.translations.Base;
10+
import xyz.theprogramsrc.supercoreapi.spigot.dialog.Dialog;
11+
import xyz.theprogramsrc.supercoreapi.spigot.gui.objets.GuiAction;
12+
import xyz.theprogramsrc.supercoreapi.spigot.gui.objets.GuiEntry;
13+
import xyz.theprogramsrc.supercoreapi.spigot.gui.objets.GuiModel;
14+
import xyz.theprogramsrc.supercoreapi.spigot.gui.objets.GuiRows;
15+
16+
/**
17+
* Representation of a Browser GUI
18+
* @since 5.2.0
19+
*/
20+
public abstract class BrowserGui<OBJ> extends Gui {
21+
22+
public boolean backEnabled = false;
23+
public String searchTerm = null;
24+
public int maxItemsPerPage = 36;
25+
public int page = 0;
26+
27+
/**
28+
* Creates a new Browser GUI
29+
* @param player The player
30+
* @param automaticallyOpen If the GUI should be opened automatically
31+
*/
32+
public BrowserGui(Player player, boolean automaticallyOpen) {
33+
super(player, automaticallyOpen);
34+
}
35+
36+
37+
/**
38+
* Creates a new Browser GUI but automatically being opened. See {@link BrowserGui#BrowserGui(Player, boolean)}
39+
* @param player The player
40+
*/
41+
public BrowserGui(Player player){
42+
this(player, true);
43+
}
44+
45+
@Override
46+
public GuiRows getRows() {
47+
return GuiRows.SIX;
48+
}
49+
50+
@Override
51+
public void onBuild(GuiModel model) {
52+
LinkedList<OBJ> objects = new LinkedList<>(Arrays.stream(this.getObjects()).filter(obj -> {
53+
String[] tags = this.getSearchTags(obj);
54+
if(tags == null || this.searchTerm == null) return true;
55+
if(tags.length == 0) return true;
56+
return Arrays.stream(tags).anyMatch(tag -> this.getSuperUtils().removeColor(this.searchTerm).contains(this.getSuperUtils().removeColor(tag)));
57+
}).collect(Collectors.toList()));
58+
int offset = this.page * this.maxItemsPerPage;
59+
int itemsToTake = Math.min(offset + this.maxItemsPerPage, objects.size());
60+
int maxPages = (int)Math.round(Math.ceil((double)objects.size() / (double)maxItemsPerPage));
61+
LinkedList<GuiEntry> entries = objects.subList(offset, itemsToTake).stream().map(this::getEntry).collect(Collectors.toCollection(LinkedList::new));
62+
int slot = 0;
63+
for(int o = 0; o < entries.size(); o++) {
64+
model.setButton(slot, entries.get(o));
65+
slot++;
66+
}
67+
68+
GuiEntry searchEntry;
69+
if(this.searchTerm == null){
70+
searchEntry = new GuiEntry(this.getPreloadedItems().getSearchItem().build(), a-> new Dialog(this.player){
71+
@Override
72+
public String getTitle() {
73+
return Base.DIALOG_SEARCH_TITLE.toString();
74+
}
75+
76+
@Override
77+
public String getSubtitle() {
78+
return Base.DIALOG_SEARCH_SUBTITLE.toString();
79+
}
80+
81+
@Override
82+
public String getActionbar() {
83+
return Base.DIALOG_SEARCH_ACTIONBAR.toString();
84+
}
85+
86+
@Override
87+
public boolean onResult(String input){
88+
BrowserGui.this.searchTerm = input;
89+
return true;
90+
}
91+
}.setRecall(player -> {
92+
this.page = 0;
93+
this.getSpigotTasks().runTask(this::open);
94+
}));
95+
}else{
96+
searchEntry = new GuiEntry(this.getPreloadedItems().getEndSearchItem().build(), a -> {
97+
this.searchTerm = null;
98+
this.page = 0;
99+
this.open();
100+
});
101+
}
102+
model.setButton(49, searchEntry);
103+
104+
if(this.page != 0){
105+
model.setButton(52, new GuiEntry(this.getPreloadedItems().getPreviousItem().build(), a -> {
106+
this.page -= 1;
107+
this.open();
108+
}));
109+
}
110+
111+
if(this.page+1 < maxPages){
112+
model.setButton(53, new GuiEntry(this.getPreloadedItems().getNextItem().build(), a -> {
113+
this.page += 1;
114+
this.open();
115+
}));
116+
}
117+
118+
if(this.backEnabled){
119+
model.setButton(45, new GuiEntry(this.getPreloadedItems().getBackItem().build(), this::onBack));
120+
}
121+
}
122+
123+
/**
124+
* Gets a button from an object
125+
* @param obj The Object
126+
* @return The button for the Object
127+
*/
128+
public abstract GuiEntry getEntry(OBJ obj);
129+
130+
/**
131+
* Gets the search tags from an object
132+
* @param obj The Object
133+
* @return The search tags for the Object
134+
*/
135+
public abstract String[] getSearchTags(OBJ obj);
136+
137+
/**
138+
* Gets all the objects to show
139+
* @return The objects
140+
*/
141+
public abstract OBJ[] getObjects();
142+
143+
/**
144+
* Executed when the back button is clicked
145+
* @param action The GuiAction
146+
*/
147+
public void onBack(GuiAction action){}
148+
}

0 commit comments

Comments
 (0)