-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
17 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
bungee/src/main/java/app/shopmc/plugin/bungee/BungeeResourceLoader.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,27 @@ | ||
package app.shopmc.plugin.bungee; | ||
|
||
import app.shopmc.plugin.resource.ResourceLoader; | ||
import app.shopmc.plugin.resource.ResourceLoaderException; | ||
import net.md_5.bungee.config.Configuration; | ||
import net.md_5.bungee.config.ConfigurationProvider; | ||
import net.md_5.bungee.config.YamlConfiguration; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
|
||
public class BungeeResourceLoader extends ResourceLoader<Configuration> { | ||
|
||
public BungeeResourceLoader(final Class<?> loadingClass, final File dataFolder) { | ||
super(loadingClass, dataFolder.toPath()); | ||
} | ||
|
||
@Override | ||
protected Configuration loadResource(final Path resourcePath) throws ResourceLoaderException { | ||
try { | ||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(resourcePath.toFile()); | ||
} catch (final Exception exception) { | ||
throw new ResourceLoaderException(ResourceLoaderException.Reason.FILE_NOT_LOADED, exception); | ||
} | ||
} | ||
|
||
} |
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
46 changes: 46 additions & 0 deletions
46
common/src/main/java/app/shopmc/plugin/resource/ResourceLoader.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,46 @@ | ||
package app.shopmc.plugin.resource; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
public abstract class ResourceLoader<T> { | ||
|
||
private final Class<?> loadingClass; | ||
private final Path dataDirectory; | ||
|
||
protected ResourceLoader(final Class<?> loadingClass, final Path dataDirectory) { | ||
this.loadingClass = loadingClass; | ||
this.dataDirectory = dataDirectory; | ||
} | ||
|
||
protected abstract T loadResource(final Path resourcePath) throws ResourceLoaderException; | ||
|
||
public boolean saveDefault(final String resourceName) throws ResourceLoaderException { | ||
if (!Files.exists(this.dataDirectory)) { | ||
try { | ||
Files.createDirectories(this.dataDirectory); | ||
} catch (final Exception exception) { | ||
throw new ResourceLoaderException(ResourceLoaderException.Reason.DIRECTORY_NOT_CREATED, exception); | ||
} | ||
} | ||
|
||
final Path resourcePath = this.dataDirectory.resolve(resourceName); | ||
if (!Files.exists(resourcePath)) { | ||
try (final InputStream in = this.loadingClass.getClassLoader().getResourceAsStream(resourceName)) { | ||
Files.copy(Objects.requireNonNull(in), resourcePath); | ||
return true; | ||
} catch (final Exception exception) { | ||
throw new ResourceLoaderException(ResourceLoaderException.Reason.DEFAULT_FILE_NOT_SAVED, exception); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public T load(final String resourceName) throws ResourceLoaderException { | ||
return this.loadResource(this.dataDirectory.resolve(resourceName)); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
common/src/main/java/app/shopmc/plugin/resource/ResourceLoaderException.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,32 @@ | ||
package app.shopmc.plugin.resource; | ||
|
||
public class ResourceLoaderException extends Exception { | ||
|
||
private final Reason reason; | ||
|
||
public ResourceLoaderException(final Reason reason, final Throwable cause) { | ||
super(cause); | ||
this.reason = reason; | ||
} | ||
|
||
public Reason getReason() { | ||
return this.reason; | ||
} | ||
|
||
public enum Reason { | ||
DIRECTORY_NOT_CREATED("Nie udało się utworzyć folderu pluginu"), | ||
DEFAULT_FILE_NOT_SAVED("Nie udało się zapisać domyślnego pliku %s"), | ||
FILE_NOT_LOADED("Nie udało się otworzyć pliku %s"); | ||
|
||
private final String message; | ||
|
||
Reason(final String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage(final String fileName) { | ||
return String.format(this.message, fileName); | ||
} | ||
} | ||
|
||
} |