Skip to content

Commit ebff316

Browse files
committed
v2.0.0 1.21.x update
1 parent 5cb155a commit ebff316

File tree

11 files changed

+316
-149
lines changed

11 files changed

+316
-149
lines changed

build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
//file:noinspection GroovyAssignabilityCheck
12
plugins {
2-
id 'fabric-loom' version '1.2-SNAPSHOT'
3+
id 'fabric-loom' version '1.7-SNAPSHOT'
34
id 'maven-publish'
45
}
56

@@ -54,7 +55,7 @@ processResources {
5455
}
5556

5657
tasks.withType(JavaCompile).configureEach {
57-
it.options.release = 17
58+
it.options.release = 21
5859
}
5960

6061
java {
@@ -63,8 +64,8 @@ java {
6364
// If you remove this line, sources will not be generated.
6465
withSourcesJar()
6566

66-
sourceCompatibility = JavaVersion.VERSION_17
67-
targetCompatibility = JavaVersion.VERSION_17
67+
sourceCompatibility = JavaVersion.VERSION_21
68+
targetCompatibility = JavaVersion.VERSION_21
6869
}
6970

7071
jar {

gradle.properties

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ org.gradle.parallel=true
44

55
# Fabric Properties
66
# check these on https://fabricmc.net/develop
7-
minecraft_version=1.20.1
8-
# yarn_mappings=1.20.1+build.1
9-
loader_version=0.15.6
7+
minecraft_version=1.21.3
8+
loader_version=0.16.9
109

1110
# Mod Properties
12-
mod_version=1.0.0
11+
mod_version=2.0.0
1312
maven_group=io.github.qwerty770
1413
archives_base_name=AdvancementHelper
1514

1615
# Dependencies
17-
fabric_version=0.91.0+1.20.1
16+
fabric_version=0.109.0+1.21.3
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://crystal.app.lss233.com/repositories/gradle-dist/gradle-8.1.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
44
networkTimeout=10000
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

src/client/java/io/github/qwerty770/AdvancementCommand.java

Lines changed: 0 additions & 134 deletions
This file was deleted.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package io.github.qwerty770.advhelper;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.internal.Streams;
5+
import com.google.gson.stream.JsonWriter;
6+
import com.mojang.brigadier.CommandDispatcher;
7+
import com.mojang.brigadier.arguments.StringArgumentType;
8+
import com.mojang.brigadier.context.CommandContext;
9+
import net.fabricmc.api.EnvType;
10+
import net.fabricmc.api.Environment;
11+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
12+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
13+
import net.minecraft.advancements.*;
14+
import net.minecraft.network.chat.Component;
15+
16+
import java.io.File;
17+
import java.io.FileWriter;
18+
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
20+
import java.text.SimpleDateFormat;
21+
import java.util.*;
22+
23+
import static io.github.qwerty770.advhelper.AdvancementHelper.version;
24+
import static io.github.qwerty770.advhelper.AdvancementTool.*;
25+
26+
public class AdvancementCommand {
27+
@Environment(EnvType.CLIENT)
28+
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
29+
dispatcher.register(ClientCommandManager.literal("advhelper")
30+
.executes(context -> {
31+
context.getSource().sendFeedback(Component.literal("Advancement Helper " + version + " running!"));
32+
return 0;
33+
})
34+
.then(ClientCommandManager.literal("export").executes(context -> {
35+
exportAllAdvancements(context);
36+
return 1;
37+
}).then(ClientCommandManager.argument("namespace", StringArgumentType.string()).executes(context -> {
38+
exportAdvancements(context.getArgument("namespace", String.class), context);
39+
return 1;
40+
})))
41+
.then(ClientCommandManager.literal("progress").executes(context -> {
42+
exportAllProgress(context);
43+
return 1;
44+
}).then(ClientCommandManager.argument("namespace", StringArgumentType.string()).executes(context -> {
45+
exportProgress(context.getArgument("namespace", String.class), context);
46+
return 1;
47+
})))
48+
);
49+
}
50+
51+
private static void exportAdvancements(String namespace, CommandContext<FabricClientCommandSource> context) {
52+
AdvancementTree tree = getAdvancementTree();
53+
List<AdvancementHolder> advancements = getAdvancements();
54+
try {
55+
JsonObject json = new JsonObject();
56+
for (AdvancementHolder adv : advancements){
57+
if (!adv.id().getNamespace().equals(namespace)) continue;
58+
json.add(adv.id().toString(), getJson(adv, tree));
59+
}
60+
FileWriter fileWriter = createFile(namespace, "adv");
61+
writeJson(fileWriter, json);
62+
fileWriter.close();
63+
AdvancementHelper.LOGGER.info("Advancement Helper exported the advancements of namespace {}", namespace);
64+
context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success"));
65+
}
66+
catch (IOException exception){
67+
sendFailure(exception, context);
68+
}
69+
}
70+
71+
private static void exportAllAdvancements(CommandContext<FabricClientCommandSource> context) {
72+
AdvancementTree tree = getAdvancementTree();
73+
List<AdvancementHolder> advancements = getAdvancements();
74+
Map<String, JsonObject> jsonMap = new HashMap<>();
75+
Map<String, FileWriter> fileWriterMap = new HashMap<>();
76+
try {
77+
for (AdvancementHolder adv : advancements){
78+
String ns = adv.id().getNamespace();
79+
if (!fileWriterMap.containsKey(ns)){
80+
FileWriter writer = createFile(ns, "adv");
81+
fileWriterMap.put(ns, writer);
82+
}
83+
if (!jsonMap.containsKey(ns)){
84+
jsonMap.put(ns, new JsonObject());
85+
}
86+
jsonMap.get(ns).add(adv.id().toString(), getJson(adv, tree));
87+
}
88+
for (String ns : jsonMap.keySet()){
89+
writeJson(fileWriterMap.get(ns), jsonMap.get(ns));
90+
}
91+
for (FileWriter writer : fileWriterMap.values()){
92+
writer.close();
93+
}
94+
AdvancementHelper.LOGGER.info("Advancement Helper exported {} advancements", advancements.size());
95+
context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success"));
96+
}
97+
catch (IOException exception){
98+
sendFailure(exception, context);
99+
}
100+
}
101+
102+
private static void exportProgress(String namespace, CommandContext<FabricClientCommandSource> context) {
103+
Map<AdvancementHolder, AdvancementProgress> progress = getProgress();
104+
try {
105+
JsonObject json = new JsonObject();
106+
for (AdvancementHolder adv : progress.keySet()){
107+
if (!adv.id().getNamespace().equals(namespace)) continue;
108+
json.add(adv.id().toString(), getJson(progress.get(adv)));
109+
}
110+
FileWriter fileWriter = createFile(namespace, "progress");
111+
writeJson(fileWriter, json);
112+
fileWriter.close();
113+
AdvancementHelper.LOGGER.info("Advancement Helper exported the advancements' progress of namespace {}", namespace);
114+
context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success"));
115+
}
116+
catch (IOException exception){
117+
sendFailure(exception, context);
118+
}
119+
}
120+
121+
private static void exportAllProgress(CommandContext<FabricClientCommandSource> context) {
122+
Map<AdvancementHolder, AdvancementProgress> progress = getProgress();
123+
Map<String, JsonObject> jsonMap = new HashMap<>();
124+
Map<String, FileWriter> fileWriterMap = new HashMap<>();
125+
try {
126+
for (AdvancementHolder adv : progress.keySet()){
127+
String ns = adv.id().getNamespace();
128+
if (!fileWriterMap.containsKey(ns)){
129+
FileWriter writer = createFile(ns, "progress");
130+
fileWriterMap.put(ns, writer);
131+
}
132+
if (!jsonMap.containsKey(ns)){
133+
jsonMap.put(ns, new JsonObject());
134+
}
135+
jsonMap.get(ns).add(adv.id().toString(), getJson(progress.get(adv)));
136+
}
137+
for (String ns : jsonMap.keySet()){
138+
writeJson(fileWriterMap.get(ns), jsonMap.get(ns));
139+
}
140+
for (FileWriter writer : fileWriterMap.values()){
141+
writer.close();
142+
}
143+
AdvancementHelper.LOGGER.info("Advancement Helper exported {} advancements' progress", progress.size());
144+
context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success"));
145+
}
146+
catch (IOException exception){
147+
sendFailure(exception, context);
148+
}
149+
}
150+
151+
private static FileWriter createFile(String namespace, String type) throws IOException {
152+
File file = new File("export/" + namespace + "-" + type + "-" +
153+
new SimpleDateFormat("yyyy-MM-dd-HHmmss").format(new Date()) + ".json");
154+
if (!file.getParentFile().exists()) {
155+
if(file.getParentFile().mkdirs()){
156+
AdvancementHelper.LOGGER.debug("Created path: {}", file.getParentFile().getPath());
157+
}
158+
}
159+
if (!file.createNewFile()) {
160+
AdvancementHelper.LOGGER.warn("File already exists: {}.", file.getName());
161+
}
162+
return new FileWriter(file, StandardCharsets.UTF_8);
163+
}
164+
165+
private static void sendFailure(IOException exception, CommandContext<FabricClientCommandSource> context){
166+
AdvancementHelper.LOGGER.error("Failed to export to a file");
167+
AdvancementHelper.LOGGER.error(exception.getMessage());
168+
context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.fail"));
169+
}
170+
171+
private static void writeJson(FileWriter fileWriter, JsonObject jsonObject) throws IOException {
172+
JsonWriter jsonWriter = new JsonWriter(fileWriter);
173+
jsonWriter.setLenient(true);
174+
jsonWriter.setIndent(" ");
175+
Streams.write(jsonObject, jsonWriter);
176+
}
177+
}

src/client/java/io/github/qwerty770/AdvancementHelper.java renamed to src/client/java/io/github/qwerty770/advhelper/AdvancementHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.qwerty770;
1+
package io.github.qwerty770.advhelper;
22

33
import net.fabricmc.api.ClientModInitializer;
44
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
@@ -8,6 +8,7 @@
88
@SuppressWarnings("unused")
99
public class AdvancementHelper implements ClientModInitializer {
1010
public static final Logger LOGGER = LoggerFactory.getLogger("advhelper");
11+
public static final String version = "2.0.0";
1112
@Override
1213
public void onInitializeClient() {
1314
ClientCommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess) -> AdvancementCommand.register(dispatcher)));

0 commit comments

Comments
 (0)