Skip to content

Commit

Permalink
[FIX|UPDATE] Fixed multiple configurations problems. Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
nl1x committed Feb 11, 2024
1 parent fb3d848 commit 40f23a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ Cette librairie est faîte pour manipuler des fichiers de configuration ainsi qu

### Prototype
```java
HashConfig(String resourcePath, String outputPath, boolean withDotEnv);
HashConfig(Class<?> plugin, String resourcePath, String outputPath, boolean withDotEnv);
```

### Description
Classe principale permettant de charger et manipuler un fichier de configuration facilement.

### Paramètres
`Class<?> plugin`: La classe principale du plugin.
`String resourcePath`: Le chemin du fichier de configuration se trouvant dans votre `.jar`. *(Appelé ressource)*
`String outputPath`: Le chemin vers la sauvegarde locale du fichier de configuration.
`boolean withDotEnv`: Si il faut charger le fichier d'environnement ou non.
Expand Down Expand Up @@ -68,7 +69,8 @@ TOKEN=YOUR_TOKEN

**Modifier / Accéder aux valeurs du fichier de configuration:**
```java
HashConfig config = new HashConfig("configuration_file/config.yml", "plugins/TonPlugin/config.yml", false);
// `this` doit être l'instance de la classe principale de votre plugin.
HashConfig config = new HashConfig(this.getClass(), "configuration_file/config.yml", "plugins/TonPlugin/config.yml", false);
YamlFile yaml = config.getYaml();

String username1 = yaml.getString("users.1.username");
Expand All @@ -92,7 +94,9 @@ users:
**Accéder aux variables d'environnement:**
```java
HashConfig config = new HashConfig("...", "...", true);
HashConfig config = new HashConfig(this.getClass(), "...", "...", true);
/* ^^^^ */
/* Pour charger le .env */
Dotenv env = config.getEnv();
String token = env.get("TOKEN");

Expand All @@ -105,7 +109,7 @@ System.out.println("Token: " + token); // Affiche "YOUR_TOKEN"
Si vous avez modifié votre fichier de configuration à la main et que vous souhaitez le recharger sans pour autant devoir restart le serveur, vous pouvez utiliser la méthode suivante :
```java
HashConfig config = new HashConfig(...);
HashConfig config = new HashConfig(this.getClass(), ...);
config.reload();
```

Expand Down
7 changes: 5 additions & 2 deletions src/fr/hashtek/hashconfig/HashConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class HashConfig

private static HashConfig instance = null;

private final Class<?> plugin;
private Dotenv env = null;
private YamlFile yaml;
private final String resourcePath;
Expand All @@ -28,9 +29,10 @@ public class HashConfig
* @param resourcePath The ABSOLUTE path of the configuration file to load. !! WARNING !! The file must be present in the package (PluginName.jar).
* @throws IOException If the plugin can't read the file
*/
public HashConfig(String resourcePath, String outputPath, boolean withDotEnv) throws IOException
public HashConfig(Class<?> plugin, String resourcePath, String outputPath, boolean withDotEnv) throws IOException
{
HashConfig.instance = this;
this.plugin = plugin;
this.resourcePath = resourcePath;
this.outputPath = outputPath;
this.load(withDotEnv);
Expand Down Expand Up @@ -79,7 +81,7 @@ private File createFileIfNotExists() throws IOException
+ configFile.getName()
+ "'.");

stream = getClass().getResourceAsStream("/" + this.resourcePath);
stream = this.plugin.getResourceAsStream("/" + this.resourcePath);
if (stream == null)
throw new IOException("The resource file '"
+ this.resourcePath
Expand All @@ -102,6 +104,7 @@ private void writeStreamToFile(InputStream stream, File file) throws IOException
line = reader.readLine();

if (line != null) {
System.out.println("Writing line: '" + line + "'.");
writerBuffer.write(line);
writerBuffer.newLine();
}
Expand Down

0 comments on commit 40f23a3

Please sign in to comment.