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
da398108
Commit
da398108
authored
Nov 25, 2022
by
AnimeGitB
Browse files
Add Position JsonAdapter for [x,y,z] format
Also add serializers for existing JsonAdapters
parent
ad502a85
Changes
3
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/utils/JsonAdapters.java
View file @
da398108
...
...
@@ -10,9 +10,11 @@ import com.google.gson.TypeAdapter;
import
com.google.gson.TypeAdapterFactory
;
import
com.google.gson.reflect.TypeToken
;
import
com.google.gson.stream.JsonReader
;
import
com.google.gson.stream.JsonToken
;
import
com.google.gson.stream.JsonWriter
;
import
emu.grasscutter.data.common.DynamicFloat
;
import
it.unimi.dsi.fastutil.floats.FloatArrayList
;
import
it.unimi.dsi.fastutil.ints.IntArrayList
;
import
it.unimi.dsi.fastutil.ints.IntList
;
import
lombok.val
;
...
...
@@ -65,7 +67,54 @@ public class JsonAdapters {
}
@Override
public
void
write
(
JsonWriter
writer
,
IntList
i
)
{};
public
void
write
(
JsonWriter
writer
,
IntList
l
)
throws
IOException
{
writer
.
beginArray
();
for
(
val
i
:
l
)
// .forEach() doesn't appreciate exceptions
writer
.
value
(
i
);
writer
.
endArray
();
};
}
static
class
PositionAdapter
extends
TypeAdapter
<
Position
>
{
@Override
public
Position
read
(
JsonReader
reader
)
throws
IOException
{
switch
(
reader
.
peek
())
{
case
BEGIN_ARRAY:
// "pos": [x,y,z]
reader
.
beginArray
();
val
array
=
new
FloatArrayList
(
3
);
while
(
reader
.
hasNext
())
array
.
add
(
reader
.
nextInt
());
reader
.
endArray
();
return
new
Position
(
array
);
case
BEGIN_OBJECT:
// "pos": {"x": x, "y": y, "z": z}
float
x
=
0
f
;
float
y
=
0
f
;
float
z
=
0
f
;
reader
.
beginObject
();
for
(
var
next
=
reader
.
peek
();
next
!=
JsonToken
.
END_OBJECT
;
next
=
reader
.
peek
())
{
val
name
=
reader
.
nextName
();
switch
(
name
)
{
case
"x"
,
"X"
,
"_x"
->
x
=
(
float
)
reader
.
nextDouble
();
case
"y"
,
"Y"
,
"_y"
->
y
=
(
float
)
reader
.
nextDouble
();
case
"z"
,
"Z"
,
"_z"
->
z
=
(
float
)
reader
.
nextDouble
();
default
->
throw
new
IOException
(
"Invalid field in Position definition - "
+
name
);
}
}
reader
.
endObject
();
return
new
Position
(
x
,
y
,
z
);
default
:
throw
new
IOException
(
"Invalid Position definition - "
+
reader
.
peek
().
name
());
}
}
@Override
public
void
write
(
JsonWriter
writer
,
Position
i
)
throws
IOException
{
writer
.
beginArray
();
writer
.
value
(
i
.
getX
());
writer
.
value
(
i
.
getY
());
writer
.
value
(
i
.
getZ
());
writer
.
endArray
();
};
}
static
class
EnumTypeAdapterFactory
implements
TypeAdapterFactory
{
...
...
@@ -109,7 +158,9 @@ public class JsonAdapters {
throw
new
IOException
(
"Invalid Enum definition - "
+
reader
.
peek
().
name
());
}
}
public
void
write
(
JsonWriter
writer
,
T
value
)
{}
public
void
write
(
JsonWriter
writer
,
T
value
)
throws
IOException
{
writer
.
value
(
value
.
toString
());
}
};
}
}
...
...
src/main/java/emu/grasscutter/utils/JsonUtils.java
View file @
da398108
...
...
@@ -27,6 +27,7 @@ public final class JsonUtils {
.
setPrettyPrinting
()
.
registerTypeAdapter
(
DynamicFloat
.
class
,
new
DynamicFloatAdapter
())
.
registerTypeAdapter
(
IntList
.
class
,
new
IntListAdapter
())
.
registerTypeAdapter
(
Position
.
class
,
new
PositionAdapter
())
.
registerTypeAdapterFactory
(
new
EnumTypeAdapterFactory
())
.
create
();
...
...
src/main/java/emu/grasscutter/utils/Position.java
View file @
da398108
package
emu.grasscutter.utils
;
import
java.io.Serializable
;
import
java.util.List
;
import
com.google.gson.annotations.SerializedName
;
import
com.github.davidmoten.rtreemulti.geometry.Point
;
...
...
@@ -22,9 +23,7 @@ public class Position implements Serializable {
@SerializedName
(
value
=
"z"
,
alternate
={
"_z"
,
"Z"
})
@Getter
@Setter
private
float
z
;
public
Position
()
{
}
public
Position
()
{}
public
Position
(
float
x
,
float
y
)
{
set
(
x
,
y
);
...
...
@@ -34,6 +33,20 @@ public class Position implements Serializable {
set
(
x
,
y
,
z
);
}
public
Position
(
List
<
Float
>
xyz
)
{
switch
(
xyz
.
size
())
{
default
:
// Might want to error on excess elements, but maybe we want to extend to 3+3 representation later.
case
3
:
this
.
z
=
xyz
.
get
(
2
);
// Fall-through
case
2
:
this
.
y
=
xyz
.
get
(
1
);
// Fall-through
case
1
:
this
.
y
=
xyz
.
get
(
0
);
// pointless fall-through
case
0
:
break
;
}
}
public
Position
(
String
p
)
{
String
[]
split
=
p
.
split
(
","
);
if
(
split
.
length
>=
2
)
{
...
...
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