Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
Grasscutter
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
ziqian zhang
Grasscutter
Commits
a0067b66
Commit
a0067b66
authored
3 years ago
by
KingRainbow44
Browse files
Options
Downloads
Patches
Plain Diff
Add JSON-related methods to `Utils.java`
parent
39f23a0c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/emu/grasscutter/utils/Utils.java
+34
-5
34 additions, 5 deletions
src/main/java/emu/grasscutter/utils/Utils.java
with
34 additions
and
5 deletions
src/main/java/emu/grasscutter/utils/Utils.java
+
34
−
5
View file @
a0067b66
...
@@ -6,10 +6,7 @@ import java.nio.file.Files;
...
@@ -6,10 +6,7 @@ import java.nio.file.Files;
import
java.nio.file.StandardCopyOption
;
import
java.nio.file.StandardCopyOption
;
import
java.time.*
;
import
java.time.*
;
import
java.time.temporal.TemporalAdjusters
;
import
java.time.temporal.TemporalAdjusters
;
import
java.util.HashMap
;
import
java.util.*
;
import
java.util.Map
;
import
java.util.Random
;
import
java.util.Locale
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.Grasscutter
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBuf
;
...
@@ -308,10 +305,42 @@ public final class Utils {
...
@@ -308,10 +305,42 @@ public final class Utils {
}
}
/**
/**
* get language code from Locale
* Gets the language code from a given locale.
* @param locale A locale.
* @return A string in the format of 'XX-XX'.
*/
*/
public
static
String
getLanguageCode
(
Locale
locale
)
{
public
static
String
getLanguageCode
(
Locale
locale
)
{
return
String
.
format
(
"%s-%s"
,
locale
.
getLanguage
(),
locale
.
getCountry
());
return
String
.
format
(
"%s-%s"
,
locale
.
getLanguage
(),
locale
.
getCountry
());
}
}
/**
* Base64 encodes a given byte array.
* @param toEncode An array of bytes.
* @return A base64 encoded string.
*/
public
static
String
base64Encode
(
byte
[]
toEncode
)
{
return
Base64
.
getEncoder
().
encodeToString
(
toEncode
);
}
/**
* Base64 decodes a given string.
* @param toDecode A base64 encoded string.
* @return An array of bytes.
*/
public
static
byte
[]
base64Decode
(
String
toDecode
)
{
return
Base64
.
getDecoder
().
decode
(
toDecode
);
}
/**
* Safely JSON decodes a given string.
* @param jsonData The JSON-encoded data.
* @return JSON decoded data, or null if an exception occurred.
*/
public
static
<
T
>
T
jsonDecode
(
String
jsonData
,
Class
<
T
>
classType
)
{
try
{
return
Grasscutter
.
getGsonFactory
().
fromJson
(
jsonData
,
classType
);
}
catch
(
Exception
ignored
)
{
return
null
;
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment