-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add skin configuration files and utility classes
- Loading branch information
1 parent
54d32cd
commit e76f799
Showing
16 changed files
with
541 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
core/src/main/java/com/georgev22/skinoverlay/utilities/config/OverlayOptionsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.georgev22.skinoverlay.utilities.config; | ||
|
||
import com.georgev22.library.utilities.Color; | ||
import com.georgev22.library.yaml.file.FileConfiguration; | ||
import com.georgev22.skinoverlay.SkinOverlay; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public enum OverlayOptionsUtil { | ||
PARTS_OVERLAY_JACKET("overlay.jacket", false, Optional.empty()), | ||
|
||
PARTS_OVERLAY_LEFT_SLEEVE("overlay.left_sleeve", false, Optional.empty()), | ||
|
||
PARTS_OVERLAY_RIGHT_SLEEVE("overlay.right_sleeve", false, Optional.empty()), | ||
|
||
PARTS_OVERLAY_LEFT_PANTS("overlay.left_pants", false, Optional.empty()), | ||
|
||
PARTS_OVERLAY_RIGHT_PANTS("overlay.right_pants", false, Optional.empty()), | ||
|
||
PARTS_OVERLAY_HAT("overlay.hat", false, Optional.empty()), | ||
|
||
// PLAYER PARTS | ||
|
||
PARTS_PLAYER_JACKET("player.jacket", false, Optional.empty()), | ||
|
||
PARTS_PLAYER_LEFT_SLEEVE("player.left_sleeve", false, Optional.empty()), | ||
|
||
PARTS_PLAYER_RIGHT_SLEEVE("player.right_sleeve", false, Optional.empty()), | ||
|
||
PARTS_PLAYER_LEFT_PANTS("player.left_pants", false, Optional.empty()), | ||
|
||
PARTS_PLAYER_RIGHT_PANTS("player.right_pants", false, Optional.empty()), | ||
|
||
PARTS_PLAYER_HAT("player.hat", false, Optional.empty()), | ||
; | ||
|
||
private static final SkinOverlay mainPlugin = SkinOverlay.getInstance(); | ||
private final String pathName; | ||
private final Object value; | ||
private final Optional<String>[] oldPaths; | ||
|
||
@SafeVarargs | ||
@Contract(pure = true) | ||
OverlayOptionsUtil(final String pathName, final Object value, Optional<String>... oldPaths) { | ||
this.pathName = pathName; | ||
this.value = value; | ||
this.oldPaths = oldPaths; | ||
} | ||
|
||
public boolean getBooleanValue(FileConfiguration fileConfiguration) { | ||
return fileConfiguration.getBoolean(getPath(fileConfiguration), Boolean.parseBoolean(String.valueOf(getDefaultValue(fileConfiguration)))); | ||
} | ||
|
||
public Object getObjectValue(FileConfiguration fileConfiguration) { | ||
return fileConfiguration.get(getPath(fileConfiguration), getDefaultValue(fileConfiguration)); | ||
} | ||
|
||
public String getStringValue(FileConfiguration fileConfiguration) { | ||
return fileConfiguration.getString(getPath(fileConfiguration), String.valueOf(getDefaultValue(fileConfiguration))); | ||
} | ||
|
||
public @NotNull Long getLongValue(FileConfiguration fileConfiguration) { | ||
return fileConfiguration.getLong(getPath(fileConfiguration), Long.parseLong(String.valueOf(getDefaultValue(fileConfiguration)))); | ||
} | ||
|
||
public @NotNull Integer getIntValue(FileConfiguration fileConfiguration) { | ||
return fileConfiguration.getInt(getPath(fileConfiguration), Integer.parseInt(String.valueOf(getDefaultValue(fileConfiguration)))); | ||
} | ||
|
||
public @NotNull Double getDoubleValue(FileConfiguration fileConfiguration) { | ||
return fileConfiguration.getDouble(getPath(fileConfiguration), Double.parseDouble(String.valueOf(getDefaultValue(fileConfiguration)))); | ||
} | ||
|
||
public @NotNull List<String> getStringList(FileConfiguration fileConfiguration) { | ||
return fileConfiguration.getStringList(getPath(fileConfiguration)); | ||
} | ||
|
||
/** | ||
* Converts and return a String List of color codes to a List of Color classes that represent the colors. | ||
* | ||
* @return a List of Color classes that represent the colors. | ||
*/ | ||
public @NotNull List<Color> getColors(FileConfiguration fileConfiguration) { | ||
return getStringList(fileConfiguration).stream().map(Color::from).collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Returns the path. | ||
* | ||
* @return the path. | ||
*/ | ||
public @NotNull String getPath(FileConfiguration fileConfiguration) { | ||
if (fileConfiguration.get("Options." + getDefaultPath(fileConfiguration)) == null) { | ||
for (Optional<String> path : getOldPaths(fileConfiguration)) { | ||
if (path.isPresent()) { | ||
if (fileConfiguration.get("Options." + path.get()) != null) { | ||
return "Options." + path.get(); | ||
} | ||
} | ||
} | ||
} | ||
return "Options." + getDefaultPath(fileConfiguration); | ||
} | ||
|
||
/** | ||
* Returns the default path. | ||
* | ||
* @return the default path. | ||
*/ | ||
@Contract(pure = true) | ||
public @NotNull String getDefaultPath(FileConfiguration fileConfiguration) { | ||
return this.pathName; | ||
} | ||
|
||
/** | ||
* Returns the old path if it exists. | ||
* | ||
* @return the old path if it exists. | ||
*/ | ||
public Optional<String>[] getOldPaths(FileConfiguration fileConfiguration) { | ||
return oldPaths; | ||
} | ||
|
||
/** | ||
* Returns the default value if the path have no value. | ||
* | ||
* @return the default value if the path have no value. | ||
*/ | ||
public Object getDefaultValue(FileConfiguration fileConfiguration) { | ||
return value; | ||
} | ||
|
||
public Optional<String> getOptionalStringValue(FileConfiguration fileConfiguration) { | ||
return Optional.ofNullable(getStringValue(fileConfiguration)); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
core/src/main/java/com/georgev22/skinoverlay/utilities/config/SkinConfigurationFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.georgev22.skinoverlay.utilities.config; | ||
|
||
|
||
import com.georgev22.library.yaml.file.FileConfiguration; | ||
import com.georgev22.library.yaml.file.YamlConfiguration; | ||
import com.georgev22.skinoverlay.SkinOverlay; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
|
||
public class SkinConfigurationFile { | ||
|
||
private final String name; | ||
private FileConfiguration fileConfiguration; | ||
private File file; | ||
private boolean setup = false; | ||
|
||
public SkinConfigurationFile(final String name) { | ||
this.name = name; | ||
|
||
} | ||
|
||
public File getFile() { | ||
return file; | ||
} | ||
|
||
public void setupFile() { | ||
|
||
if (setup) { | ||
throw new RuntimeException( | ||
"The void file with the name: " + this.name + " can't be setup twice!\n" + | ||
"If you believe this is an issue, contact GeorgeV22."); | ||
} | ||
|
||
final SkinOverlay plugin = SkinOverlay.getInstance(); | ||
|
||
if (!plugin.getDataFolder().exists()) { | ||
plugin.getDataFolder().mkdirs(); | ||
} | ||
|
||
this.file = new File(plugin.getDataFolder(), "skins-cfg" + File.separator + this.name + ".yml"); | ||
|
||
if (!this.file.exists()) { | ||
try { | ||
this.file.createNewFile(); | ||
} catch (final IOException e) { | ||
plugin.getLogger().log(Level.SEVERE, "Error while creating a new file:", e); | ||
} | ||
} | ||
this.reloadFile(); | ||
setup = true; | ||
} | ||
|
||
public void reloadFile() { | ||
this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file); | ||
} | ||
|
||
@NotNull | ||
public FileConfiguration getFileConfiguration() { | ||
return this.fileConfiguration; | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
core/src/main/java/com/georgev22/skinoverlay/utilities/config/SkinFileCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.georgev22.skinoverlay.utilities.config; | ||
|
||
import com.georgev22.library.maps.HashObjectMap; | ||
import com.georgev22.library.maps.ObjectMap; | ||
import com.georgev22.library.utilities.Utils; | ||
import com.georgev22.skinoverlay.SkinOverlay; | ||
import com.georgev22.skinoverlay.storage.data.Skin; | ||
import com.georgev22.skinoverlay.utilities.SerializableBufferedImage; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Map.Entry; | ||
import java.util.Objects; | ||
import java.util.logging.Level; | ||
|
||
public class SkinFileCache { | ||
private final ObjectMap<String, SkinConfigurationFile> skinConfigurationFiles = new HashObjectMap<>(); | ||
|
||
private final ObjectMap<String, SerializableBufferedImage> skinImages = new HashObjectMap<>(); | ||
|
||
private final SkinOverlay skinOverlay = SkinOverlay.getInstance(); | ||
|
||
@NotNull | ||
public ObjectMap<String, SkinConfigurationFile> getSkinConfigurationFiles() { | ||
return skinConfigurationFiles; | ||
} | ||
|
||
public ObjectMap<String, SerializableBufferedImage> getSkinImages() { | ||
return skinImages; | ||
} | ||
|
||
public void cache() { | ||
this.skinOverlay.getLogger().info("Attempting to load skins and its configuration files in memory."); | ||
this.skinConfigurationFiles.clear(); | ||
this.skinImages.clear(); | ||
|
||
final File folder = new File(skinOverlay.getDataFolder(), "skins-cfg"); | ||
if (!folder.exists()) { | ||
if (folder.mkdirs()) { | ||
this.skinOverlay.getLogger().log(Level.INFO, "skins-cfg folder was successfully created."); | ||
} | ||
this.copyFilesToFolder(false); | ||
this.skinOverlay.getLogger().info("skins-cfg folder didn't exist, creating one and copy the default skins configuration files for it."); | ||
} | ||
|
||
if (!this.skinOverlay.getSkinsDataFolder().exists()) { | ||
if (this.skinOverlay.getSkinsDataFolder().mkdirs()) { | ||
this.skinOverlay.getLogger().log(Level.INFO, "skins data folder was successfully created!!"); | ||
} | ||
this.copyFilesToFolder(true); | ||
this.skinOverlay.getLogger().info("skins folder didn't exist, creating one and copy the default skins for it."); | ||
} | ||
|
||
for (File file : Objects.requireNonNull(this.skinOverlay.getSkinsDataFolder().listFiles((dir, name) -> name.endsWith(".yml")))) { | ||
final String skinName = file.getName().substring(0, file.getName().length() - 4); | ||
|
||
if (skinName.isEmpty()) { | ||
this.skinOverlay.getLogger().info("Invalid skin file name: " + skinName); | ||
continue; | ||
} | ||
|
||
try { | ||
SerializableBufferedImage serializableBufferedImage = new SerializableBufferedImage(ImageIO.read(file)); | ||
this.skinImages.put(skinName, serializableBufferedImage); | ||
this.skinOverlay.getLogger().info("Skin image file: " + skinName + " successfully loaded in memory."); | ||
} catch (IOException e) { | ||
this.skinOverlay.getLogger().log(Level.WARNING, "Cannot load skin image file: " + skinName, e); | ||
} | ||
} | ||
|
||
for (File file : Objects.requireNonNull(folder.listFiles((dir, name) -> name.endsWith(".yml")))) { | ||
final String skinName = file.getName().substring(0, file.getName().length() - 4); | ||
|
||
if (skinName.isEmpty()) { | ||
this.skinOverlay.getLogger().info("Invalid skin configuration file name: " + skinName); | ||
continue; | ||
} | ||
|
||
final SkinConfigurationFile skinFile = new SkinConfigurationFile(skinName); | ||
skinFile.setupFile(); | ||
this.skinConfigurationFiles.put(skinName, skinFile); | ||
this.skinOverlay.getLogger().info("Skin configuration file: " + skinName + " successfully loaded in memory."); | ||
} | ||
|
||
} | ||
|
||
private void copyFilesToFolder(boolean b) { | ||
String[] resources = new String[]{"alley", "frog_pijama", "bubbo_transparent", "fire_demon", "flame", "glare", "hoodie", "migrator", "pirate", "smoking", "policeman", "mustache"}; | ||
if (b) { | ||
for (String resource : resources) { | ||
if (new File(this.skinOverlay.getSkinsDataFolder(), resource + ".png").exists()) continue; | ||
try { | ||
Utils.saveResource("skins/" + resource + ".png", false, this.skinOverlay.getDataFolder(), this.getClass()); | ||
} catch (Exception e) { | ||
this.skinOverlay.getLogger().log(Level.WARNING, "Cannot save default skins: ", e.getCause()); | ||
} | ||
} | ||
} else { | ||
for (String resource : resources) { | ||
if (new File(this.skinOverlay.getDataFolder(), "skins-cfg/" + resource + ".yml").exists()) continue; | ||
try { | ||
Utils.saveResource("skins-cfg/" + resource + ".yml", false, this.skinOverlay.getDataFolder(), this.skinOverlay.getClass()); | ||
} catch (Exception e) { | ||
this.skinOverlay.getLogger().log(Level.WARNING, "Cannot save default skin configuration files: ", e.getCause()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Returns a cached or attempts to cache a possible SkinFile object from | ||
* a specific key. | ||
*/ | ||
@Nullable | ||
public SkinConfigurationFile getCacheSkinConfig(final String name) { | ||
SkinConfigurationFile cache = this.skinConfigurationFiles.get(name); | ||
if (cache != null) { | ||
return cache; | ||
} | ||
for (Entry<String, SkinConfigurationFile> entry : this.skinConfigurationFiles.entrySet()) { | ||
if (entry.getKey().equalsIgnoreCase(name)) { | ||
return entry.getValue(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
public SkinConfigurationFile getCacheSkinConfig(final @NotNull Skin skin) { | ||
return this.getCacheSkinConfig(skin.skinName()); | ||
} | ||
|
||
@Nullable | ||
public SerializableBufferedImage getSkinImage(final String skinName) { | ||
SerializableBufferedImage cache = this.skinImages.get(skinName); | ||
if (cache != null) { | ||
return cache; | ||
} | ||
for (Entry<String, SerializableBufferedImage> entry : this.skinImages.entrySet()) { | ||
if (entry.getKey().equalsIgnoreCase(skinName)) { | ||
return entry.getValue(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
public SerializableBufferedImage getSkinImage(final @NotNull Skin skin) { | ||
return this.getSkinImage(skin.skinName()); | ||
} | ||
} |
Oops, something went wrong.