Skip to content

Commit 4db7f7f

Browse files
authored
fix: Fix issue #39 by excluding clientside-only mods from copy operation. Deprecates deleteClientMods
fix: Fix issue #39 by excluding clientside-only mods from copy operation. Deprecates deleteClientMods
2 parents b537ae0 + f057e58 commit 4db7f7f

File tree

15 files changed

+152
-69
lines changed

15 files changed

+152
-69
lines changed

.github/workflows/tag_release_publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ jobs:
5656
NEW_VERSION=$(echo "${{ steps.compute_tag.outputs.next_tag }}")
5757
echo "New version: ${NEW_VERSION}"
5858
echo "Github username: ${GITHUB_ACTOR}"
59+
./gradlew clean
5960
./gradlew -Pversion=${NEW_VERSION} publish
6061
6162
# UPLOAD ASSETS TO RELEASE

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,6 @@ src/test/resources/tests/server_pack/forge-installer.jar
146146
/src/test/resources/forge_tests/Vanilla Forge
147147
serverpackcreator.conf
148148
server_files
149+
/fabric-manifest.xml
150+
/forge-manifest.json
151+
/mcmanifest.json

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ dependencies {
4343
embed group: 'com.github.TheRandomLabs', name:'CurseAPI', version:'master-SNAPSHOT'
4444
embed group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.2'
4545

46+
testImplementation 'org.mockito:mockito-core:3.8.0'
4647
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
4748
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
48-
testImplementation 'org.mockito:mockito-core:3.8.0'
4949
}
5050

5151
test {

src/main/java/de/griefed/ServerPackCreator/CopyFiles.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import java.io.IOException;
88
import java.nio.file.*;
99
import java.nio.file.attribute.BasicFileAttributes;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
1012
import java.util.List;
13+
import java.util.stream.Collectors;
1114
import java.util.stream.Stream;
1215

1316
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
@@ -83,14 +86,15 @@ static void copyStartScripts(String modpackDir, String modLoader, boolean includ
8386
/** Copies all specified folders and their files to the modpackDir.
8487
* @param modpackDir String. /server_pack. Directory where all directories listed in copyDirs will be copied into.
8588
* @param copyDirs String List. The folders and files within to copy.
89+
* @param clientMods String List. List of clientside-only mods NOT to copy to server pack.
8690
* @throws IOException Only print stacktrace if it does not start with java.nio.file.DirectoryNotEmptyException.
8791
*/
88-
static void copyFiles(String modpackDir, List<String> copyDirs) throws IOException {
92+
static void copyFiles(String modpackDir, List<String> copyDirs, List<String> clientMods) throws IOException {
8993
String serverPath = String.format("%s/server_pack", modpackDir);
9094
Files.createDirectories(Paths.get(serverPath));
91-
for (int i = 0; i<copyDirs.toArray().length; i++) {
92-
String clientDir = modpackDir + "/" + copyDirs.get(i);
93-
String serverDir = serverPath + "/" + copyDirs.get(i);
95+
for (int i = 0; i < copyDirs.toArray().length; i++) {
96+
String clientDir = String.format("%s/%s", modpackDir,copyDirs.get(i));
97+
String serverDir = String.format("%s/%s", serverPath,copyDirs.get(i));
9498
appLogger.info(String.format("Setting up %s files.", serverDir));
9599
if (copyDirs.get(i).startsWith("saves/")) {
96100
String savesDir = String.format("%s/%s", serverPath, copyDirs.get(i).substring(6));
@@ -109,6 +113,19 @@ static void copyFiles(String modpackDir, List<String> copyDirs) throws IOExcepti
109113
} catch (IOException ex) {
110114
appLogger.error("An error occurred copying the specified world.", ex);
111115
}
116+
} else if (copyDirs.get(i).startsWith("mods") && clientMods.toArray().length > 0) {
117+
List<String> listOfFiles = excludeClientMods(clientDir, clientMods);
118+
Files.createDirectories(Paths.get(serverDir));
119+
for (int in = 0; in < listOfFiles.toArray().length; in++) {
120+
try {
121+
Files.copy(Paths.get(listOfFiles.get(in)), Paths.get(serverDir, new File(listOfFiles.get(in)).getName()), REPLACE_EXISTING);
122+
appLogger.debug(String.format("Copying: %s", listOfFiles.get(in)));
123+
} catch (IOException ex) {
124+
if (!ex.toString().startsWith("java.nio.file.DirectoryNotEmptyException")) {
125+
appLogger.error("An error occurred copying files to the serverpack.", ex);
126+
}
127+
}
128+
}
112129
} else {
113130
try {
114131
Stream<Path> files = Files.walk(Paths.get(clientDir));
@@ -129,6 +146,33 @@ static void copyFiles(String modpackDir, List<String> copyDirs) throws IOExcepti
129146
}
130147
}
131148
}
149+
/** Generate a list of all mods in a modpack EXCEPT clientside-only mods. This list is then used by copyFiles.
150+
* @param modsDir String. /mods The directory in which to generate a list of all available mods.
151+
* @param clientMods List String. A list of all clientside-only mods passed by copyFiles, which is then removed from the list generated in this method.
152+
* @return List String. A list of all mods inside the modpack excluding the specified clientside-only mods.
153+
*/
154+
@SuppressWarnings("UnusedAssignment")
155+
private static List<String> excludeClientMods(String modsDir, List<String> clientMods) {
156+
appLogger.info("Preparing a list of mods to include in server pack...");
157+
String[] copyMods = new String[0];
158+
List<String> listOfFiles = new ArrayList<>();
159+
try (Stream<Path> walk = Files.walk(Paths.get(modsDir))) {
160+
listOfFiles = walk.filter(Files::isRegularFile).map(Path::toString).collect(Collectors.toList());
161+
for (int in = 0; in < listOfFiles.toArray().length; in++) {
162+
for (int i = 0; i < clientMods.toArray().length; i++) {
163+
String mod = listOfFiles.get(in);
164+
if (mod.contains(clientMods.get(i))) {
165+
listOfFiles.remove(in);
166+
}
167+
}
168+
}
169+
copyMods = listOfFiles.toArray(new String[0]);
170+
return Arrays.asList(copyMods.clone());
171+
} catch (IOException ex) {
172+
appLogger.error("Error: There was an error during the acquisition of files in mods directory.", ex);
173+
}
174+
return Arrays.asList(copyMods.clone());
175+
}
132176
/** Copies the server-icon.png into server_pack.
133177
* @param modpackDir String. /server_pack. Directory where the server-icon.png will be copied to.
134178
*/

src/main/java/de/griefed/ServerPackCreator/CurseForgeModpack/CreateModpack.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ static String setModloader(String modloader) {
291291
* @param modpackDir String. The directory in which to deletes should be made.
292292
*/
293293
@SuppressWarnings("unused")
294+
@Deprecated
294295
private static void deleteDirs(String modpackDir) {
295296
appLogger.info("Deleting directories not needed in server pack from modpack...");
296297
String[] dirsToBeDeleted = {"overrides", "packmenu", "resourcepacks"};

src/main/java/de/griefed/ServerPackCreator/Main.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,10 @@ public static void main(String[] args) {
4141
if (!ConfigCheck.checkConfig()) {
4242
CopyFiles.cleanupEnvironment(Reference.modpackDir);
4343
try {
44-
CopyFiles.copyFiles(Reference.modpackDir, Reference.copyDirs);
44+
CopyFiles.copyFiles(Reference.modpackDir, Reference.copyDirs, Reference.clientMods);
4545
} catch (IOException ex) {
4646
appLogger.error("There was an error calling the copyFiles method.", ex);
4747
}
48-
if (Reference.clientMods.toArray().length != 0) {
49-
ServerSetup.deleteClientMods(Reference.modpackDir, Reference.clientMods);
50-
}
5148
CopyFiles.copyStartScripts(Reference.modpackDir, Reference.modLoader, Reference.includeStartScripts);
5249
if (Reference.includeServerInstallation) {
5350
ServerSetup.installServer(Reference.modLoader, Reference.modpackDir, Reference.minecraftVersion, Reference.modLoaderVersion, Reference.javaPath);
@@ -71,7 +68,7 @@ public static void main(String[] args) {
7168
}
7269
appLogger.info("Serverpack available at: " + Reference.modpackDir + "/serverpack");
7370
appLogger.info("Done!");
74-
System.exit(0); // Just in case.
71+
System.exit(0);
7572
} else {
7673
appLogger.error("ERROR: Please check your serverpackcreator.conf for any incorrect settings. This message is also displayed if ServerPackCreator downloaded and setup a modpack from a projectID,fileID for modpackDir.");
7774
System.exit(1);

src/main/java/de/griefed/ServerPackCreator/ServerSetup.java

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,6 @@
1515
class ServerSetup {
1616
private static final Logger appLogger = LogManager.getLogger(FilesSetup.class);
1717
private static final Logger installerLogger = LogManager.getLogger("InstallerLogger");
18-
/** Deletes client-side-only mods in server_pack, if specified.
19-
* @param modpackDir String. /server_pack/mods The directory where the files will be deleted.
20-
* @param clientMods String List. Client mods to delete.
21-
*/
22-
static void deleteClientMods(String modpackDir, List<String> clientMods) {
23-
appLogger.info("Deleting client-side mods from serverpack:");
24-
File serverMods = new File(String.format("%s/server_pack/mods", modpackDir));
25-
Set<String> fileList = new HashSet<>();
26-
try {
27-
Files.walkFileTree(Paths.get(String.format("%s/server_pack/mods", modpackDir)), new SimpleFileVisitor<Path>() {
28-
@Override
29-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
30-
if (!Files.isDirectory(file)) {
31-
fileList.add(file.getFileName().toString());
32-
for (int i = 0; i < clientMods.toArray().length; i++) {
33-
if (file.getFileName().toString().contains(clientMods.get(i))) {
34-
boolean isDeleted = file.toFile().delete();
35-
if (isDeleted) {
36-
appLogger.info(String.format(" %s", file.getFileName().toString()));
37-
} else {
38-
appLogger.error(String.format("Could not delete: %s", file.getFileName().toString()));
39-
}
40-
}
41-
}
42-
}
43-
return FileVisitResult.CONTINUE;
44-
}
45-
});
46-
} catch (IOException ex) {
47-
appLogger.error("Error: There was an error during deletion of clientside mods.", ex);
48-
}
49-
for (int i = 0; i < fileList.toArray().length; i++) {
50-
appLogger.debug(String.format("DEBUG: Remaining: %s", fileList.toArray()[i]));
51-
}
52-
}
5318
/** Installs the files for a Forge/Fabric server.
5419
* @param modLoader String. The modloader for which to install the server.
5520
* @param modpackDir String. /server_pack The directory where the modloader server will be installed in.
@@ -151,4 +116,41 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
151116
}
152117
appLogger.info("Finished creation of zip archive.");
153118
}
119+
/** Deletes client-side-only mods in server_pack, if specified.
120+
* @param modpackDir String. /server_pack/mods The directory where the files will be deleted.
121+
* @param clientMods String List. Client mods to delete.
122+
*/
123+
@SuppressWarnings("unused")
124+
@Deprecated
125+
static void deleteClientMods(String modpackDir, List<String> clientMods) {
126+
appLogger.info("Deleting client-side mods from serverpack:");
127+
File serverMods = new File(String.format("%s/server_pack/mods", modpackDir));
128+
Set<String> fileList = new HashSet<>();
129+
try {
130+
Files.walkFileTree(Paths.get(String.format("%s/server_pack/mods", modpackDir)), new SimpleFileVisitor<Path>() {
131+
@Override
132+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
133+
if (!Files.isDirectory(file)) {
134+
fileList.add(file.getFileName().toString());
135+
for (int i = 0; i < clientMods.toArray().length; i++) {
136+
if (file.getFileName().toString().contains(clientMods.get(i))) {
137+
boolean isDeleted = file.toFile().delete();
138+
if (isDeleted) {
139+
appLogger.info(String.format(" %s", file.getFileName().toString()));
140+
} else {
141+
appLogger.error(String.format("Could not delete: %s", file.getFileName().toString()));
142+
}
143+
}
144+
}
145+
}
146+
return FileVisitResult.CONTINUE;
147+
}
148+
});
149+
} catch (IOException ex) {
150+
appLogger.error("Error: There was an error during deletion of clientside mods.", ex);
151+
}
152+
for (int i = 0; i < fileList.toArray().length; i++) {
153+
appLogger.debug(String.format("DEBUG: Remaining: %s", fileList.toArray()[i]));
154+
}
155+
}
154156
}

src/test/java/de/griefed/ServerPackCreator/CLISetupTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CLISetupTest {
1919

2020
@BeforeEach
2121
void setUp() {
22-
MockitoAnnotations.initMocks(this);
22+
MockitoAnnotations.openMocks(this);
2323
}
2424
@Test
2525
void testBuildString() {

src/test/java/de/griefed/ServerPackCreator/ConfigCheckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ConfigCheckTest {
2626

2727
@BeforeEach
2828
void setUp() {
29-
MockitoAnnotations.initMocks(this);
29+
MockitoAnnotations.openMocks(this);
3030
}
3131

3232
@SuppressWarnings("ResultOfMethodCallIgnored")

src/test/java/de/griefed/ServerPackCreator/CopyFilesTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.nio.file.Files;
1414
import java.nio.file.Path;
1515
import java.nio.file.Paths;
16+
import java.util.ArrayList;
1617
import java.util.Arrays;
1718
import java.util.Comparator;
1819
import java.util.List;
@@ -27,7 +28,7 @@ class CopyFilesTest {
2728

2829
@BeforeEach
2930
void setUp() {
30-
MockitoAnnotations.initMocks(this);
31+
MockitoAnnotations.openMocks(this);
3132
}
3233

3334
@Test
@@ -75,12 +76,14 @@ void testCopyStartScriptsForge() throws IOException {
7576
}
7677
new File("./serverpackcreator.conf").delete();
7778
}
79+
7880
@SuppressWarnings("ResultOfMethodCallIgnored")
7981
@Test
8082
void testCopyFiles() throws IOException {
8183
String modpackDir = "./src/test/resources/forge_tests";
84+
List<String> clientMods = Arrays.asList("AmbientSounds","BackTools","BetterAdvancement","BetterPing","cherished","ClientTweaks","Controlling","DefaultOptions","durability","DynamicSurroundings","itemzoom","jei-professions","jeiintegration","JustEnoughResources","MouseTweaks","Neat","OldJavaWarning","PackMenu","preciseblockplacing","SimpleDiscordRichPresence","SpawnerFix","TipTheScales","WorldNameRandomizer");
8285
List<String> copyDirs = Arrays.asList("config","mods","scripts","seeds","defaultconfigs");
83-
CopyFiles.copyFiles(modpackDir, copyDirs);
86+
CopyFiles.copyFiles(modpackDir, copyDirs, clientMods);
8487
Assertions.assertTrue(new File(String.format("%s/server_pack/config",modpackDir)).isDirectory());
8588
Assertions.assertTrue(new File(String.format("%s/server_pack/mods",modpackDir)).isDirectory());
8689
Assertions.assertTrue(new File(String.format("%s/server_pack/scripts",modpackDir)).isDirectory());
@@ -99,6 +102,32 @@ void testCopyFiles() throws IOException {
99102
}
100103
}
101104
}
105+
@SuppressWarnings("ResultOfMethodCallIgnored")
106+
@Test
107+
void testCopyFilesEmptyClients() throws IOException {
108+
String modpackDir = "./src/test/resources/forge_tests";
109+
List<String> clientMods = new ArrayList<>();
110+
List<String> copyDirs = Arrays.asList("config","mods","scripts","seeds","defaultconfigs");
111+
CopyFiles.copyFiles(modpackDir, copyDirs, clientMods);
112+
Assertions.assertTrue(new File(String.format("%s/server_pack/config",modpackDir)).isDirectory());
113+
Assertions.assertTrue(new File(String.format("%s/server_pack/mods",modpackDir)).isDirectory());
114+
Assertions.assertTrue(new File(String.format("%s/server_pack/scripts",modpackDir)).isDirectory());
115+
Assertions.assertTrue(new File(String.format("%s/server_pack/seeds",modpackDir)).isDirectory());
116+
Assertions.assertTrue(new File(String.format("%s/server_pack/defaultconfigs",modpackDir)).isDirectory());
117+
Assertions.assertTrue(new File(String.format("%s/server_pack/config/testfile.txt",modpackDir)).exists());
118+
Assertions.assertTrue(new File(String.format("%s/server_pack/defaultconfigs/testfile.txt",modpackDir)).exists());
119+
Assertions.assertTrue(new File(String.format("%s/server_pack/mods/testmod.jar",modpackDir)).exists());
120+
Assertions.assertTrue(new File(String.format("%s/server_pack/scripts/testscript.zs",modpackDir)).exists());
121+
Assertions.assertTrue(new File(String.format("%s/server_pack/seeds/testjson.json",modpackDir)).exists());
122+
for (String s : copyDirs) {
123+
String deleteMe = (String.format("%s/server_pack/%s",modpackDir,s));
124+
if (new File(deleteMe).isDirectory()) {
125+
Path pathToBeDeleted = Paths.get(deleteMe);
126+
Files.walk(pathToBeDeleted).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
127+
}
128+
}
129+
}
130+
102131
@SuppressWarnings("ResultOfMethodCallIgnored")
103132
@Test
104133
void testCopyIcon() throws IOException {

0 commit comments

Comments
 (0)