This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17e8292
commit 5b59d77
Showing
26 changed files
with
191 additions
and
281 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
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 was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
.../io/github/xiefrish2021/NBTException.java → ...ithub/xiefrish2021/core/NBTException.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
91 changes: 91 additions & 0 deletions
91
src/main/java/io/github/xiefrish2021/core/ObjectNBTMapper.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,91 @@ | ||
package io.github.xiefrish2021.core; | ||
|
||
import io.github.xiefrish2021.ITag; | ||
import io.github.xiefrish2021.tag.*; | ||
import io.github.xiefrish2021.tag.array.ByteArrayTag; | ||
import io.github.xiefrish2021.tag.array.IntArrayTag; | ||
import io.github.xiefrish2021.tag.array.LongArrayTag; | ||
import io.github.xiefrish2021.tag.compound.CompoundTag; | ||
import io.github.xiefrish2021.tag.list.ListTag; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.lang.reflect.RecordComponent; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
@ApiStatus.Internal | ||
public class ObjectNBTMapper { | ||
private final Object object; | ||
|
||
public ObjectNBTMapper(Object object) { | ||
this.object = object; | ||
} | ||
|
||
public CompoundTag toNBT() { | ||
CompoundTag entries = new CompoundTag(); | ||
Class<?> objectRef = object.getClass(); | ||
try { | ||
if (objectRef.isRecord()) { | ||
for (RecordComponent component : objectRef.getRecordComponents()) { | ||
String key = component.getName(); | ||
Object invoked = objectRef.getMethod(key).invoke(object); | ||
if (invoked != null) { | ||
entries.put(key, writePrimitives(invoked)); | ||
} | ||
} | ||
} else if (isPlainClass(objectRef)){ | ||
for (Field field : objectRef.getFields()) { | ||
String key = field.getName(); | ||
Object invoked = objectRef.getField(key).get(object); | ||
|
||
if (invoked != null) { | ||
entries.put(key, writePrimitives(invoked)); | ||
} | ||
} | ||
} else { | ||
throw new NBTException("This feature can only support simple Java classes and Java record classes."); | ||
} | ||
} catch (Exception e) { | ||
throw new NBTException(e); | ||
} | ||
|
||
return entries; | ||
} | ||
|
||
private static boolean isPlainClass(Class<?> clazz) { | ||
return !clazz.isAnnotation() && !clazz.isInterface() && !clazz.isEnum() && !Modifier.isAbstract(clazz.getModifiers()); | ||
} | ||
|
||
private static ITag writePrimitives(Object object) { | ||
return switch (object) { | ||
case String string -> new StringTag(string); | ||
case Integer integer -> new IntTag(integer); | ||
case Float f -> new FloatTag(f); | ||
case Long l -> new LongTag(l); | ||
case Double d -> new DoubleTag(d); | ||
case Short s -> new ShortTag(s); | ||
case Byte b -> new ByteTag(b); | ||
case byte[] array -> new ByteArrayTag(array); | ||
case int[] array -> new IntArrayTag(array); | ||
case long[] array -> new LongArrayTag(array); | ||
case ITag tag -> tag; | ||
case Collection<?> collection -> { | ||
ListTag listTag = new ListTag(); | ||
for (Object o : collection) { | ||
listTag.add(writePrimitives(o)); | ||
} | ||
|
||
yield listTag; | ||
} | ||
case Boolean bool -> { | ||
if (bool) { | ||
yield new ByteTag((byte) 1); | ||
} yield new ByteTag((byte) 0); | ||
} | ||
case Map<?, ?> ignored -> throw new NBTException("Map type is not supported."); | ||
case Object o -> new ObjectNBTMapper(o).toNBT(); | ||
}; | ||
} | ||
} |
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
109 changes: 0 additions & 109 deletions
109
src/main/java/io/github/xiefrish2021/object/ObjectToNbt.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.