Skip to content

Commit 717a22e

Browse files
committed
v1.0.0, added lots of utils
1 parent 7f1af37 commit 717a22e

File tree

9 files changed

+481
-2
lines changed

9 files changed

+481
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group 'me.dkim19375'
6-
version '0.0.2'
6+
version '1.0.0'
77

88
repositories {
99
mavenCentral()
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package me.dkim19375.dkim19375core;
2+
3+
import org.bukkit.configuration.file.FileConfiguration;
4+
import org.bukkit.configuration.file.YamlConfiguration;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.util.Map;
10+
11+
public class ConfigFile {
12+
13+
private final File configFile;
14+
private FileConfiguration config;
15+
private final File pluginDataFolder;
16+
private final String fileName;
17+
18+
/**
19+
* @since 1.0.0
20+
* @param pluginDataFolder The plugin's data directory, accessible with JavaPlugin#getDataFolder();
21+
* @param fileName The name of the config file excluding file extensions.
22+
*
23+
* Please notice that the constructor does not yet create the YAML-configuration file. To create the file on the disk, use {@link ConfigFile#createConfig()}.
24+
*/
25+
public ConfigFile(File pluginDataFolder, String fileName) {
26+
this.fileName = fileName;
27+
configFile = new File(pluginDataFolder, fileName);
28+
this.pluginDataFolder = pluginDataFolder;
29+
config = YamlConfiguration.loadConfiguration(configFile);
30+
}
31+
32+
/**
33+
* @since 1.0.0
34+
* @param javaPlugin A JavaPlugin instance of the plugin using this config
35+
* @param fileName The name of the config file excluding file extensions.
36+
*
37+
* Please notice that the constructor does not yet create the YAML-configuration file. To create the file on the disk, use {@link ConfigFile#createConfig()}.
38+
*/
39+
public ConfigFile(JavaPlugin javaPlugin, String fileName) {
40+
this.fileName = fileName;
41+
configFile = new File(javaPlugin.getDataFolder(), fileName);
42+
this.pluginDataFolder = javaPlugin.getDataFolder();
43+
config = YamlConfiguration.loadConfiguration(configFile);
44+
}
45+
46+
/**
47+
* @since 1.0.0
48+
* This creates the configuration file. If the data folder is invalid, it will be created along with the config file.
49+
*/
50+
public void createConfig() {
51+
if (! configFile.exists()) {
52+
if (! this.pluginDataFolder.exists()) {
53+
this.pluginDataFolder.mkdir();
54+
}
55+
try {
56+
configFile.createNewFile();
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
}
62+
63+
/**
64+
* @since 1.0.0
65+
* @return The configuration file's directory. To get its name, use {@link ConfigFile#getName()} instead.
66+
*/
67+
public File getDirectory() {
68+
return pluginDataFolder;
69+
}
70+
71+
/**
72+
* @since 1.0.0
73+
* @return The name of the configuration file, including file extensions.
74+
* This returns the name of the configuration file with the .yml extension. To get the file's directory, use {@link ConfigFile#getDirectory()}.
75+
*/
76+
public String getName() {
77+
return fileName;
78+
}
79+
80+
/**
81+
* @since 1.0.0
82+
* @return The config file.
83+
* This returns the actual File object of the config file.
84+
*/
85+
public File getFile() {
86+
return configFile;
87+
}
88+
89+
/**
90+
* @since 1.0.0
91+
* @return The FileConfiguration object.
92+
* This returns the actual FileConfiguration object of the config file.
93+
*/
94+
public FileConfiguration getConfig() {
95+
return config;
96+
}
97+
98+
/**
99+
* @since 1.0.0
100+
* @param key The config key, including the path.
101+
* @param value the config value.
102+
* Set a default configuration value: if the entered key already exists (and it holds another value), it will do nothing. If it doesn't, it will create the key with the wanted value.
103+
*/
104+
public void addDefault(String key, String value) {
105+
if (config.getString(key) == null) {
106+
config.set(key, value);
107+
try {
108+
config.save(configFile);
109+
} catch (IOException e) {
110+
e.printStackTrace();
111+
}
112+
113+
}
114+
}
115+
116+
/**
117+
* @since 1.0.0
118+
* @param defaults A map containing the default configuration keys and values.
119+
* This sets the default configuration values as the ones contained in the map.
120+
*/
121+
public void addDefaults(Map<String,Object> defaults) {
122+
config.addDefaults(defaults);
123+
}
124+
125+
/**
126+
* @since 1.0.0
127+
* This saves the configuration file. Saving is required every time you write to it.
128+
*/
129+
public void save() {
130+
try {
131+
config.save(configFile);
132+
} catch(IOException e) {
133+
e.printStackTrace();
134+
}
135+
}
136+
137+
/**
138+
* @since 1.0.0
139+
* This reloads the configuration file, making Java acknowledge and load the new config and its values.
140+
*/
141+
public void reload() {
142+
config = YamlConfiguration.loadConfiguration(configFile);
143+
}
144+
145+
/**
146+
* @since 1.0.0
147+
* @return true if and only if the file or directory is
148+
* successfully deleted; otherwise
149+
* This deletes the config file.
150+
*/
151+
public boolean deleteFile() {
152+
return configFile.delete();
153+
}
154+
155+
/**
156+
* @since 1.0.0
157+
* This deletes the config file's directory and all it's contents.
158+
*/
159+
public boolean deleteDir() {
160+
return getDirectory().delete();
161+
}
162+
163+
/**
164+
* @since 1.0.0
165+
* This deletes and recreates the file, wiping all its contents.
166+
*/
167+
public void reset() {
168+
this.deleteFile();
169+
try {
170+
configFile.createNewFile();
171+
} catch (IOException e) {
172+
e.printStackTrace();
173+
}
174+
}
175+
176+
/**
177+
* @since 1.0.0
178+
* Wipe the config file's directory, including the file itself.
179+
*/
180+
public void wipeDirectory() {
181+
this.getDirectory().delete();
182+
this.pluginDataFolder.mkdir();
183+
}
184+
185+
/**
186+
* @since 1.0.0
187+
* @param name The sub directory's name.
188+
* @return true if and only if the file or directory is
189+
* successfully deleted; otherwise
190+
* @throws IOException If the entered string has a file extension or already exists.
191+
* This will create a sub-directory in the plugin's data folder, which can be accessed with {@link ConfigFile#getDirectory()}.
192+
* If the entered name is not a valid name for a directory or the sub-directory already exists or the data folder does not exist, an IOException will be thrown.
193+
*/
194+
public boolean createSubDirectory(String name) throws IOException {
195+
if (!pluginDataFolder.exists()) {
196+
throw new IOException("Data folder not found.");
197+
}
198+
File subDir = new File(pluginDataFolder, name);
199+
if (subDir.exists()) {
200+
throw new IOException("Sub directory already existing.");
201+
}
202+
if (!subDir.isDirectory()) {
203+
throw new IOException("The first argument is not a directory.");
204+
}
205+
return subDir.mkdir();
206+
}
207+
208+
/**
209+
* @since 1.0.0
210+
* @param value - Check if it contains the string
211+
* @return true or false
212+
* This returns true if the config contains the given value.
213+
*/
214+
public boolean contains(String value) {
215+
return config.contains(value);
216+
}
217+
218+
public boolean contains(String value, boolean b) {
219+
return config.contains(value, b);
220+
}
221+
222+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package me.dkim19375.dkim19375core;
2+
3+
import org.bukkit.plugin.java.JavaPlugin;
4+
5+
public abstract class CoreJavaPlugin extends JavaPlugin {
6+
public void printToConsole(String msg) {
7+
getServer().getConsoleSender().sendMessage("[" + getDescription().getName() + "] " + msg);
8+
}
9+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package me.dkim19375.dkim19375core;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.entity.Player;
5+
6+
import java.util.UUID;
7+
8+
public class PlayerUtils {
9+
private PlayerUtils() {
10+
11+
}
12+
public enum InputTypes {
13+
VALID_USERNAME, VALID_UUID, INVALID_USERNAME, INVALID_UUID, INVALID
14+
}
15+
public static Player getFromAll(final String uuidOrPlayer) {
16+
if (uuidOrPlayer.matches("^\\w{3,16}$")) {
17+
18+
return Bukkit.getPlayer(uuidOrPlayer);
19+
}
20+
if (uuidOrPlayer.matches("[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}")) {
21+
UUID uuid = UUID.fromString(uuidOrPlayer);
22+
return Bukkit.getPlayer(uuid);
23+
}
24+
return null;
25+
}
26+
27+
public static InputTypes getInputType(final String uuidOrPlayer) {
28+
if (uuidOrPlayer.matches("^\\w{3,16}$")) {
29+
Player player = Bukkit.getPlayer(uuidOrPlayer);
30+
if (player != null) {
31+
return InputTypes.VALID_USERNAME;
32+
}
33+
return InputTypes.INVALID_USERNAME;
34+
}
35+
if (uuidOrPlayer.matches("[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}")) {
36+
UUID uuid = UUID.fromString(uuidOrPlayer);
37+
Player player = Bukkit.getPlayer(uuid);
38+
if (player != null) {
39+
return InputTypes.VALID_UUID;
40+
}
41+
}
42+
if (uuidOrPlayer.matches("[0-9a-fA-F]{32}")) {
43+
return InputTypes.INVALID_UUID;
44+
}
45+
return InputTypes.INVALID;
46+
}
47+
48+
public static Player getPlayerFromUsername(final String username) {
49+
if (username.matches("^\\w{3,16}$")) {
50+
return Bukkit.getPlayer(username);
51+
}
52+
return null;
53+
}
54+
55+
public static Player getPlayerFromUUID(final String StringUUID) {
56+
UUID uuid = UUID.fromString(StringUUID);
57+
return Bukkit.getPlayer(uuid);
58+
}
59+
}

src/main/java/me/dkim19375/dkim19375core/TestClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ public static void testPrint() {
99
public static void testPrint(CommandSender sender) {
1010
sender.sendMessage("TESTING");
1111
}
12-
}
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.dkim19375.dkim19375core.external;
2+
3+
import me.clip.placeholderapi.PlaceholderAPI;
4+
import org.bukkit.ChatColor;
5+
import org.bukkit.entity.Player;
6+
7+
public class FormattingUtils {
8+
private FormattingUtils() {
9+
10+
}
11+
public static String formatWithPAPIAndColors(final Player player, final String string) {
12+
String parsed;
13+
parsed = PlaceholderAPI.setPlaceholders(player, string);
14+
parsed = ChatColor.translateAlternateColorCodes('&', parsed);
15+
return parsed;
16+
}
17+
18+
public static String formatWithPAPIAndColors(final Player player, final String string, final char altColorChar) {
19+
String parsed;
20+
parsed = PlaceholderAPI.setPlaceholders(player, string);
21+
parsed = ChatColor.translateAlternateColorCodes(altColorChar, parsed);
22+
return parsed;
23+
}
24+
25+
public static String formatWithColors(final String string) {
26+
return ChatColor.translateAlternateColorCodes('&', string);
27+
}
28+
29+
public static String formatWithColors(final String string, final char altColorChar) {
30+
return ChatColor.translateAlternateColorCodes(altColorChar, string);
31+
}
32+
}

0 commit comments

Comments
 (0)