Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
f86b4bec
Commit
f86b4bec
authored
Jun 26, 2022
by
KingRainbow44
Browse files
Reformat
parent
269149bb
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/utils/Utils.java
View file @
f86b4bec
...
@@ -26,19 +26,19 @@ import static emu.grasscutter.utils.Language.translate;
...
@@ -26,19 +26,19 @@ import static emu.grasscutter.utils.Language.translate;
@SuppressWarnings
({
"UnusedReturnValue"
,
"BooleanMethodIsAlwaysInverted"
})
@SuppressWarnings
({
"UnusedReturnValue"
,
"BooleanMethodIsAlwaysInverted"
})
public
final
class
Utils
{
public
final
class
Utils
{
public
static
final
Random
random
=
new
Random
();
public
static
final
Random
random
=
new
Random
();
public
static
int
randomRange
(
int
min
,
int
max
)
{
public
static
int
randomRange
(
int
min
,
int
max
)
{
return
random
.
nextInt
(
max
-
min
+
1
)
+
min
;
return
random
.
nextInt
(
max
-
min
+
1
)
+
min
;
}
}
public
static
float
randomFloatRange
(
float
min
,
float
max
)
{
public
static
float
randomFloatRange
(
float
min
,
float
max
)
{
return
random
.
nextFloat
()
*
(
max
-
min
)
+
min
;
return
random
.
nextFloat
()
*
(
max
-
min
)
+
min
;
}
}
public
static
double
getDist
(
Position
pos1
,
Position
pos2
)
{
public
static
double
getDist
(
Position
pos1
,
Position
pos2
)
{
double
xs
=
pos1
.
getX
()
-
pos2
.
getX
();
double
xs
=
pos1
.
getX
()
-
pos2
.
getX
();
xs
=
xs
*
xs
;
xs
=
xs
*
xs
;
double
ys
=
pos1
.
getY
()
-
pos2
.
getY
();
double
ys
=
pos1
.
getY
()
-
pos2
.
getY
();
ys
=
ys
*
ys
;
ys
=
ys
*
ys
;
...
@@ -51,13 +51,13 @@ public final class Utils {
...
@@ -51,13 +51,13 @@ public final class Utils {
public
static
int
getCurrentSeconds
()
{
public
static
int
getCurrentSeconds
()
{
return
(
int
)
(
System
.
currentTimeMillis
()
/
1000.0
);
return
(
int
)
(
System
.
currentTimeMillis
()
/
1000.0
);
}
}
public
static
String
lowerCaseFirstChar
(
String
s
)
{
public
static
String
lowerCaseFirstChar
(
String
s
)
{
StringBuilder
sb
=
new
StringBuilder
(
s
);
StringBuilder
sb
=
new
StringBuilder
(
s
);
sb
.
setCharAt
(
0
,
Character
.
toLowerCase
(
sb
.
charAt
(
0
)));
sb
.
setCharAt
(
0
,
Character
.
toLowerCase
(
sb
.
charAt
(
0
)));
return
sb
.
toString
();
return
sb
.
toString
();
}
}
public
static
String
toString
(
InputStream
inputStream
)
throws
IOException
{
public
static
String
toString
(
InputStream
inputStream
)
throws
IOException
{
BufferedInputStream
bis
=
new
BufferedInputStream
(
inputStream
);
BufferedInputStream
bis
=
new
BufferedInputStream
(
inputStream
);
ByteArrayOutputStream
buf
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
buf
=
new
ByteArrayOutputStream
();
...
@@ -66,13 +66,13 @@ public final class Utils {
...
@@ -66,13 +66,13 @@ public final class Utils {
}
}
return
buf
.
toString
();
return
buf
.
toString
();
}
}
public
static
void
logByteArray
(
byte
[]
array
)
{
public
static
void
logByteArray
(
byte
[]
array
)
{
ByteBuf
b
=
Unpooled
.
wrappedBuffer
(
array
);
ByteBuf
b
=
Unpooled
.
wrappedBuffer
(
array
);
Grasscutter
.
getLogger
().
info
(
"\n"
+
ByteBufUtil
.
prettyHexDump
(
b
));
Grasscutter
.
getLogger
().
info
(
"\n"
+
ByteBufUtil
.
prettyHexDump
(
b
));
b
.
release
();
b
.
release
();
}
}
private
static
final
char
[]
HEX_ARRAY
=
"0123456789abcdef"
.
toCharArray
();
private
static
final
char
[]
HEX_ARRAY
=
"0123456789abcdef"
.
toCharArray
();
public
static
String
bytesToHex
(
byte
[]
bytes
)
{
public
static
String
bytesToHex
(
byte
[]
bytes
)
{
if
(
bytes
==
null
)
return
""
;
if
(
bytes
==
null
)
return
""
;
...
@@ -84,17 +84,17 @@ public final class Utils {
...
@@ -84,17 +84,17 @@ public final class Utils {
}
}
return
new
String
(
hexChars
);
return
new
String
(
hexChars
);
}
}
public
static
String
bytesToHex
(
ByteBuf
buf
)
{
public
static
String
bytesToHex
(
ByteBuf
buf
)
{
return
bytesToHex
(
byteBufToArray
(
buf
));
return
bytesToHex
(
byteBufToArray
(
buf
));
}
}
public
static
byte
[]
byteBufToArray
(
ByteBuf
buf
)
{
public
static
byte
[]
byteBufToArray
(
ByteBuf
buf
)
{
byte
[]
bytes
=
new
byte
[
buf
.
capacity
()];
byte
[]
bytes
=
new
byte
[
buf
.
capacity
()];
buf
.
getBytes
(
0
,
bytes
);
buf
.
getBytes
(
0
,
bytes
);
return
bytes
;
return
bytes
;
}
}
public
static
int
abilityHash
(
String
str
)
{
public
static
int
abilityHash
(
String
str
)
{
int
v7
=
0
;
int
v7
=
0
;
int
v8
=
0
;
int
v8
=
0
;
...
@@ -146,22 +146,12 @@ public final class Utils {
...
@@ -146,22 +146,12 @@ public final class Utils {
Files
.
copy
(
stream
,
new
File
(
destination
).
toPath
(),
StandardCopyOption
.
REPLACE_EXISTING
);
Files
.
copy
(
stream
,
new
File
(
destination
).
toPath
(),
StandardCopyOption
.
REPLACE_EXISTING
);
return
true
;
return
true
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
xception
)
{
Grasscutter
.
getLogger
().
warn
(
"Unable to copy resource "
+
resource
+
" to "
+
destination
,
e
);
Grasscutter
.
getLogger
().
warn
(
"Unable to copy resource "
+
resource
+
" to "
+
destination
,
e
xception
);
return
false
;
return
false
;
}
}
}
}
/**
* Get object with null fallback.
* @param nonNull The object to return if not null.
* @param fallback The object to return if null.
* @return One of the two provided objects.
*/
public
static
<
T
>
T
requireNonNullElseGet
(
T
nonNull
,
T
fallback
)
{
return
nonNull
!=
null
?
nonNull
:
fallback
;
}
/**
/**
* Logs an object to the console.
* Logs an object to the console.
* @param object The object to log.
* @param object The object to log.
...
@@ -170,7 +160,7 @@ public final class Utils {
...
@@ -170,7 +160,7 @@ public final class Utils {
String
asJson
=
Grasscutter
.
getGsonFactory
().
toJson
(
object
);
String
asJson
=
Grasscutter
.
getGsonFactory
().
toJson
(
object
);
Grasscutter
.
getLogger
().
info
(
asJson
);
Grasscutter
.
getLogger
().
info
(
asJson
);
}
}
/**
/**
* Checks for required files and folders before startup.
* Checks for required files and folders before startup.
*/
*/
...
@@ -261,7 +251,7 @@ public final class Utils {
...
@@ -261,7 +251,7 @@ public final class Utils {
*/
*/
public
static
String
readFromInputStream
(
@Nullable
InputStream
stream
)
{
public
static
String
readFromInputStream
(
@Nullable
InputStream
stream
)
{
if
(
stream
==
null
)
return
"empty"
;
if
(
stream
==
null
)
return
"empty"
;
StringBuilder
stringBuilder
=
new
StringBuilder
();
StringBuilder
stringBuilder
=
new
StringBuilder
();
try
(
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
stream
,
StandardCharsets
.
UTF_8
)))
{
try
(
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
stream
,
StandardCharsets
.
UTF_8
)))
{
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
...
@@ -276,7 +266,7 @@ public final class Utils {
...
@@ -276,7 +266,7 @@ public final class Utils {
/**
/**
* Performs a linear interpolation using a table of fixed points to create an effective piecewise f(x) = y function.
* Performs a linear interpolation using a table of fixed points to create an effective piecewise f(x) = y function.
* @param x
* @param x
The x value.
* @param xyArray Array of points in [[x0,y0], ... [xN, yN]] format
* @param xyArray Array of points in [[x0,y0], ... [xN, yN]] format
* @return f(x) = y
* @return f(x) = y
*/
*/
...
@@ -294,7 +284,7 @@ public final class Utils {
...
@@ -294,7 +284,7 @@ public final class Utils {
}
}
if
(
x
<
xyArray
[
i
+
1
][
0
])
{
if
(
x
<
xyArray
[
i
+
1
][
0
])
{
// We are between [i] and [i+1], interpolation time!
// We are between [i] and [i+1], interpolation time!
// Using floats would be slightly cleaner but we can just as easily use ints if we're careful with order of operations.
// Using floats would be slightly cleaner but we can just as easily use ints if we're careful with order of operations.
int
position
=
x
-
xyArray
[
i
][
0
];
int
position
=
x
-
xyArray
[
i
][
0
];
int
fullDist
=
xyArray
[
i
+
1
][
0
]
-
xyArray
[
i
][
0
];
int
fullDist
=
xyArray
[
i
+
1
][
0
]
-
xyArray
[
i
][
0
];
int
prevValue
=
xyArray
[
i
][
1
];
int
prevValue
=
xyArray
[
i
][
1
];
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment