Skip to content

Commit 3382fc2

Browse files
authored
Merge pull request #30 from Sandro642/feature/hook/manager
Feature/hook/manager
2 parents 1a4572d + 78e6977 commit 3382fc2

7 files changed

Lines changed: 116 additions & 53 deletions

File tree

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'fr.sandro642.github'
8-
version = '0.2.6.4-STABLE'
8+
version = '0.2.7-STABLE'
99

1010
tasks.register('printVersion') {
1111
doLast {
@@ -65,11 +65,11 @@ publishing {
6565
pom {
6666
name = 'ConnectLib'
6767
description = 'A library for connecting to APIs'
68-
url = 'https://sandro642.github.io/connectorapi'
68+
url = 'https://sandro642.github.io/connectlib'
6969
licenses {
7070
license {
7171
name = 'The MIT License, Version 2.0'
72-
url = 'https://github.com/Sandro642/ConnectLib/blob/patch/groovy/LICENSE'
72+
url = 'https://github.com/Sandro642/ConnectLib/blob/main/LICENSE'
7373
}
7474
}
7575
developers {

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ And if you thought APIs were complicated, think again! With ConnectLib, it's lik
1717
---
1818

1919
```java
20-
Stable Version: 0.2.6.4-STABLE
20+
Stable Version: 0.2.7-STABLE
2121
```
2222

2323
---
@@ -105,7 +105,7 @@ repositories {
105105

106106
dependencies {
107107

108-
implementation("fr.sandro642.github:ConnectLib:0.2.6.4-STABLE")
108+
implementation("fr.sandro642.github:ConnectLib:0.2.7-STABLE")
109109

110110
}
111111

src/main/java/fr/sandro642/github/ConnectLib.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.sandro642.github;
22

3+
import fr.sandro642.github.hook.HookManager;
34
import fr.sandro642.github.hook.MCSupport;
45
import fr.sandro642.github.utils.*;
56
import fr.sandro642.github.jobs.JobGetInfos;
@@ -18,6 +19,7 @@
1819
* @see ConnectLib#StoreAndRetrieve()
1920
* @see ConnectLib#YamlUtils()
2021
* @see ConnectLib#MCSupport()
22+
* @see ConnectLib#HookManager()
2123
*/
2224

2325
public class ConnectLib {
@@ -49,14 +51,12 @@ public static void initialize(ResourceType resourceType, Class<? extends Enum<?>
4951
yamlUtils = new YamlUtils();
5052
logs = new Logs();
5153

52-
yamlUtils.generateTemplateIfNotExists(resourceType, routesEnums);
53-
logs.setPathFile(resourceType);
54+
HookManager().initHook(resourceType);
55+
HookManager().FILE_LOCATION_KEY();
5456

55-
if (resourceType == ResourceType.MC_RESOURCES) {
56-
storeAndRetrieve.store.put(storeAndRetrieve.FILE_LOCATION_KEY, MCSupport().getPluginPath());
57-
} else {
58-
storeAndRetrieve.store.put(storeAndRetrieve.FILE_LOCATION_KEY, resourceType.getPath());
59-
}
57+
yamlUtils.generateTemplateIfNotExists(routesEnums);
58+
59+
logs.setPathFile();
6060

6161
String baseUrl = yamlUtils.getURL();
6262
if (baseUrl != null) {
@@ -144,4 +144,8 @@ public static YamlUtils YamlUtils() {
144144
public static MCSupport MCSupport() {
145145
return MCSupport.getInstance();
146146
}
147+
148+
public static HookManager HookManager() {
149+
return HookManager.getInstance();
150+
}
147151
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package fr.sandro642.github.hook;
2+
3+
import fr.sandro642.github.ConnectLib;
4+
import fr.sandro642.github.enums.ResourceType;
5+
import fr.sandro642.github.utils.Logger;
6+
7+
/**
8+
* HookManager is a class that manages hooks for different resource types.
9+
* It allows for the initialization and management of hooks based on the specified resource type.
10+
* @see HookManager#BASE_PATH()
11+
* @see HookManager#FILE_LOCATION_KEY()
12+
* @author Sandro642
13+
* @version 1.0
14+
*/
15+
16+
public class HookManager {
17+
18+
/**
19+
* Singleton instance of HookManager.
20+
* This instance is used to manage hooks for different resource types.
21+
*/
22+
private static HookManager instance;
23+
24+
/**
25+
* Logger instance for logging messages.
26+
* This logger is used to log errors and other messages related to the hook management.
27+
*/
28+
private static Logger logger = new Logger();
29+
30+
/**
31+
* Initializes the HookManager with the specified resource type.
32+
*
33+
* @param resourceType the type of resource to initialize the hook for
34+
*/
35+
private static ResourceType resourceType;
36+
37+
/**
38+
* Initializes the hook for the specified resource type.
39+
* * This method sets the resource type for the hook manager and returns the initialized resource type.
40+
*
41+
* @param resourceType
42+
* @return the initialized resource type
43+
*/
44+
public ResourceType initHook(ResourceType resourceType) {
45+
this.resourceType = resourceType;
46+
return this.resourceType;
47+
}
48+
49+
/**
50+
* Sets the file location key based on the resource type.
51+
* This method updates the store with the file location key based on the resource type.
52+
* It handles different resource types such as MC_RESOURCES, MAIN_RESOURCES, and TEST_RESOURCES.
53+
*/
54+
public void FILE_LOCATION_KEY() {
55+
switch (resourceType) {
56+
case MC_RESOURCES:
57+
ConnectLib.StoreAndRetrieve().store.put(ConnectLib.StoreAndRetrieve().FILE_LOCATION_KEY, ConnectLib.MCSupport().getPluginPath());
58+
break;
59+
case MAIN_RESOURCES, TEST_RESOURCES:
60+
ConnectLib.StoreAndRetrieve().store.put(ConnectLib.StoreAndRetrieve().FILE_LOCATION_KEY, resourceType.getPath());
61+
break;
62+
63+
64+
default:
65+
logger.CRITICAL("Unsupported resource type: " + resourceType);
66+
}
67+
}
68+
69+
public String BASE_PATH() {
70+
switch (resourceType) {
71+
case MC_RESOURCES:
72+
return ConnectLib.MCSupport().getPluginPath();
73+
74+
case MAIN_RESOURCES, TEST_RESOURCES:
75+
return resourceType.getPath();
76+
77+
78+
default:
79+
logger.CRITICAL("Unsupported resource type: " + resourceType);
80+
throw new IllegalArgumentException("Unsupported resource type: " + resourceType);
81+
}
82+
}
83+
84+
/**
85+
* Returns the singleton instance of HookManager.
86+
* This method ensures that only one instance of HookManager is created and returned.
87+
*
88+
* @return the singleton instance of HookManager
89+
*/
90+
public static HookManager getInstance() {
91+
if (instance == null) {
92+
instance = new HookManager();
93+
}
94+
return instance;
95+
}
96+
}

src/main/java/fr/sandro642/github/hook/MCSupport.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,6 @@ private MCSupport() {
3030
// Private constructor to prevent instantiation
3131
}
3232

33-
/**
34-
* Method to check if the current project is a Minecraft project.
35-
* This method checks if the file location contains the Minecraft resources path.
36-
* @return true if the project is a Minecraft project, false otherwise.
37-
*/
38-
public boolean isMCProject() {
39-
try {
40-
41-
String fileLocation = (String) ConnectLib.StoreAndRetrieve().store.get(ConnectLib.StoreAndRetrieve().FILE_LOCATION_KEY);
42-
return fileLocation != null && fileLocation.contains(ResourceType.MC_RESOURCES.getPath());
43-
} catch (Exception e) {
44-
ConnectLib.Logger().ERROR("Error while checking if the project is a Minecraft project: " + e.getMessage());
45-
return false;
46-
}
47-
}
48-
4933
/**
5034
* Method to set the plugin instance.
5135
* This method should be called in a Minecraft project to set the plugin instance.
@@ -99,14 +83,6 @@ public Plugin getPlugin() {
9983
return pluginSingleton;
10084
}
10185

102-
/**
103-
* Method to reset the plugin instance.
104-
* This method sets the pluginSingleton to null, effectively resetting it.
105-
*/
106-
public void resetPlugin() {
107-
this.pluginSingleton = null;
108-
}
109-
11086
/**
11187
* Static method to get the singleton instance of MCSupport.
11288
* This method initializes the instance if it is null and returns it.

src/main/java/fr/sandro642/github/utils/Logs.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ public class Logs {
4242
* Static block to initialize the Logs instance.
4343
* This block is executed when the class is loaded, ensuring that the instance is created only once.
4444
*/
45-
public void setPathFile(ResourceType type) {
46-
if (type == ResourceType.MC_RESOURCES) {
47-
this.pathFile = ConnectLib.MCSupport().getPluginPath();
48-
} else {
49-
this.pathFile = type.getPath();
50-
}
45+
public void setPathFile() {
46+
this.pathFile = ConnectLib.HookManager().BASE_PATH();
5147
}
5248

5349
/**

src/main/java/fr/sandro642/github/utils/YamlUtils.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package fr.sandro642.github.utils;
22

33
import fr.sandro642.github.ConnectLib;
4-
import fr.sandro642.github.hook.MCSupport;
5-
import fr.sandro642.github.enums.ResourceType;
64
import org.yaml.snakeyaml.Yaml;
75

86
import java.io.*;
@@ -88,17 +86,10 @@ public Map<String, String> getRoutes() {
8886
* Generates a template `infos.yml` file if it does not already exist.
8987
* If the file exists, it updates the routes section with the provided routes.
9088
*
91-
* @param type the type of resource (e.g., MC_RESOURCES or other types)
9289
* @param routes a map of route names to their corresponding paths
9390
*/
94-
public void generateTemplateIfNotExists(ResourceType type, Map<Enum<?>, String> routes) {
95-
String basePath;
96-
97-
if (type == ResourceType.MC_RESOURCES) {
98-
basePath = MCSupport.getInstance().getPluginPath();
99-
} else {
100-
basePath = type.getPath();
101-
}
91+
public void generateTemplateIfNotExists(Map<Enum<?>, String> routes) {
92+
String basePath = ConnectLib.HookManager().BASE_PATH();
10293

10394
File directory = new File(basePath);
10495

0 commit comments

Comments
 (0)