-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deduplicate code and ensure all ID variants have network and NBT meth…
…ods as relevant Technicaly there is a breaking change here with the removal of a method in pattern, but that method was broken so there is no way anyone was using it. The protected class from pattern being removed should not be a breaking change
- Loading branch information
1 parent
5a99461
commit 25baf8d
Showing
7 changed files
with
103 additions
and
89 deletions.
There are no files selected for viewing
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
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
3 changes: 3 additions & 0 deletions
3
src/main/java/slimeknights/tconstruct/library/materials/stats/MaterialStatsId.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
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
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
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
64 changes: 64 additions & 0 deletions
64
src/main/java/slimeknights/tconstruct/library/utils/IdParser.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,64 @@ | ||
package slimeknights.tconstruct.library.utils; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSyntaxException; | ||
import net.minecraft.ResourceLocationException; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.util.GsonHelper; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.function.Function; | ||
|
||
/** Helper to parse variants of resource locations */ | ||
public record IdParser<T extends ResourceLocation>(Function<String, T> constructor, String name) { | ||
/** | ||
* Creates a new ID from the given string | ||
* @param string String | ||
* @return ID, or null if invalid | ||
*/ | ||
@Nullable | ||
public T tryParse(String string) { | ||
try { | ||
return constructor.apply(string); | ||
} catch (ResourceLocationException resourcelocationexception) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Gets an ID from JSON, throwing a nice exception if invalid | ||
* @param json JSON object | ||
* @param key Key to fetch | ||
* @return Resource location parsed | ||
*/ | ||
public T getFromJson(JsonObject json, String key) { | ||
String text = GsonHelper.getAsString(json, key); | ||
T location = tryParse(text); | ||
if (location == null) { | ||
throw new JsonSyntaxException("Expected " + key + " to be a " + name + " ID, was '" + text + "'"); | ||
} | ||
return location; | ||
} | ||
|
||
/** | ||
* Gets an ID from JSON, throwing a nice exception if invalid | ||
* @param json JSON object | ||
* @param key Key to fetch | ||
* @return Resource location parsed | ||
*/ | ||
public T convertFromJson(JsonElement json, String key) { | ||
String text = GsonHelper.convertToString(json, key); | ||
T location = tryParse(text); | ||
if (location == null) { | ||
throw new JsonSyntaxException("Expected " + key + " to be a " + name + " ID, was '" + text + "'"); | ||
} | ||
return location; | ||
} | ||
|
||
/** Reads an ID from the packet buffer */ | ||
public T fromNetwork(FriendlyByteBuf buf) { | ||
return constructor.apply(buf.readUtf(Short.MAX_VALUE)); | ||
} | ||
} |