-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ported annotation-bases NBT handling
- Loading branch information
1 parent
bb4b53c
commit 14ecdde
Showing
24 changed files
with
1,600 additions
and
35 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
165 changes: 165 additions & 0 deletions
165
src/main/java/com/enderio/core/common/autosave/Reader.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,165 @@ | ||
package com.enderio.core.common.autosave; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Set; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import com.enderio.core.common.autosave.annotations.Storable; | ||
import com.enderio.core.common.autosave.annotations.Store; | ||
import com.enderio.core.common.autosave.annotations.Store.StoreFor; | ||
import com.enderio.core.common.autosave.engine.StorableEngine; | ||
import com.enderio.core.common.autosave.exceptions.NoHandlerFoundException; | ||
import com.enderio.core.common.autosave.handlers.IHandler; | ||
import com.enderio.core.common.util.NullHelper; | ||
|
||
import net.minecraft.nbt.NBTTagCompound; | ||
|
||
/** | ||
* Restore an object's fields from NBT data. | ||
* | ||
*/ | ||
public class Reader { | ||
|
||
/** | ||
* Restore an object's fields from NBT data as if its class was annotated | ||
* {@link Storable} without a special handler. | ||
* | ||
* <p> | ||
* See also: {@link Store} for the field annotation. | ||
* | ||
* @param registry | ||
* The {@link Registry} to look up {@link IHandler}s for the fields | ||
* of the given object | ||
* @param phase | ||
* A set of {@link StoreFor}s to indicate which fields to process. | ||
* Only fields that are annotated with a matching {@link StoreFor} | ||
* are restored. | ||
* @param tag | ||
* A {@link NBTTagCompound} to read from. This NBTTagCompound | ||
* represents the whole object, with its fields in the tags. | ||
* @param object | ||
* The object that should be restored | ||
*/ | ||
public static <T> void read(@Nonnull Registry registry, @Nonnull Set<StoreFor> phase, @Nonnull NBTTagCompound tag, @Nonnull T object) { | ||
try { | ||
StorableEngine.read(registry, phase, tag, object); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} catch (InstantiationException e) { | ||
throw new RuntimeException(e); | ||
} catch (NoHandlerFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Restore an object's fields from NBT data as if its class was annotated | ||
* {@link Storable} without a special handler using the {@link Registry} | ||
* {@link Registry#GLOBAL_REGISTRY GLOBAL_REGISTRY}. | ||
* | ||
* <p> | ||
* See also: {@link Store} for the field annotation. | ||
* | ||
* @param phase | ||
* A set of {@link StoreFor}s to indicate which fields to process. | ||
* Only fields that are annotated with a matching {@link StoreFor} | ||
* are restored. | ||
* @param tag | ||
* A {@link NBTTagCompound} to read from. This NBTTagCompound | ||
* represents the whole object, with its fields in the tags. | ||
* @param object | ||
* The object that should be restored | ||
*/ | ||
public static <T> void read(@Nullable Set<Store.StoreFor> phase, @Nullable NBTTagCompound tag, @Nonnull T object) { | ||
read(Registry.GLOBAL_REGISTRY, NullHelper.notnull(phase, "Missing phase"), NullHelper.notnull(tag, "Missing NBT"), object); | ||
} | ||
|
||
/** | ||
* Restore an object's fields from NBT data as if its class was annotated | ||
* {@link Storable} without a special handler. | ||
* | ||
* <p> | ||
* See also: {@link Store} for the field annotation. | ||
* | ||
* @param registry | ||
* The {@link Registry} to look up {@link IHandler}s for the fields | ||
* of the given object | ||
* @param phase | ||
* A s{@link StoreFor} to indicate which fields to process. Only | ||
* fields that are annotated with a matching {@link StoreFor} are | ||
* restored. | ||
* @param tag | ||
* A {@link NBTTagCompound} to read from. This NBTTagCompound | ||
* represents the whole object, with its fields in the tags. | ||
* @param object | ||
* The object that should be restored | ||
*/ | ||
public static <T> void read(@Nonnull Registry registry, @Nonnull StoreFor phase, @Nullable NBTTagCompound tag, @Nonnull T object) { | ||
read(registry, NullHelper.notnullJ(EnumSet.of(phase), "EnumSet.of()"), NullHelper.notnull(tag, "Missing NBT"), object); | ||
} | ||
|
||
/** | ||
* Restore an object's fields from NBT data as if its class was annotated | ||
* {@link Storable} without a special handler using the {@link Registry} | ||
* {@link Registry#GLOBAL_REGISTRY GLOBAL_REGISTRY}. | ||
* | ||
* <p> | ||
* See also: {@link Store} for the field annotation. | ||
* | ||
* @param phase | ||
* A s{@link StoreFor} to indicate which fields to process. Only | ||
* fields that are annotated with a matching {@link StoreFor} are | ||
* restored. | ||
* @param tag | ||
* A {@link NBTTagCompound} to read from. This NBTTagCompound | ||
* represents the whole object, with its fields in the tags. | ||
* @param object | ||
* The object that should be restored | ||
*/ | ||
public static <T> void read(@Nonnull StoreFor phase, @Nullable NBTTagCompound tag, @Nonnull T object) { | ||
read(Registry.GLOBAL_REGISTRY, NullHelper.notnullJ(EnumSet.of(phase), "EnumSet.of()"), NullHelper.notnull(tag, "Missing NBT"), object); | ||
} | ||
|
||
/** | ||
* Restore an object's fields from NBT data as if its class was annotated | ||
* {@link Storable} without a special handler, ignoring {@link StoreFor} | ||
* restrictions. | ||
* | ||
* <p> | ||
* See also: {@link Store} for the field annotation. | ||
* | ||
* @param registry | ||
* The {@link Registry} to look up {@link IHandler}s for the fields | ||
* of the given object | ||
* @param tag | ||
* A {@link NBTTagCompound} to read from. This NBTTagCompound | ||
* represents the whole object, with its fields in the tags. | ||
* @param object | ||
* The object that should be restored | ||
*/ | ||
public static <T> void read(@Nonnull Registry registry, @Nullable NBTTagCompound tag, @Nonnull T object) { | ||
read(registry, NullHelper.notnullJ(EnumSet.allOf(StoreFor.class), "EnumSet.allOf()"), NullHelper.notnull(tag, "Missing NBT"), object); | ||
} | ||
|
||
/** | ||
* Restore an object's fields from NBT data as if its class was annotated | ||
* {@link Storable} without a special handler using the {@link Registry} | ||
* {@link Registry#GLOBAL_REGISTRY GLOBAL_REGISTRY}, ignoring {@link StoreFor} | ||
* restrictions. | ||
* | ||
* <p> | ||
* See also: {@link Store} for the field annotation. | ||
* | ||
* @param tag | ||
* A {@link NBTTagCompound} to read from. This NBTTagCompound | ||
* represents the whole object, with its fields in the tags. | ||
* @param object | ||
* The object that should be restored | ||
*/ | ||
public static <T> void read(@Nullable NBTTagCompound tag, @Nonnull T object) { | ||
read(Registry.GLOBAL_REGISTRY, NullHelper.notnullJ(EnumSet.allOf(StoreFor.class), "EnumSet.allOf()"), NullHelper.notnull(tag, "Missing NBT"), object); | ||
} | ||
|
||
} |
Oops, something went wrong.