Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported annotation-based NBT handling #34

Open
wants to merge 1 commit into
base: 1.8.9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified gradlew
100644 → 100755
Empty file.
82 changes: 47 additions & 35 deletions src/main/java/com/enderio/core/common/TileEntityBase.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.enderio.core.common;

import com.enderio.core.api.common.util.IProgressTile;
import com.enderio.core.common.network.EnderPacketHandler;
import com.enderio.core.common.network.PacketProgress;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
Expand All @@ -16,6 +12,15 @@
import net.minecraft.util.ITickable;
import net.minecraft.world.World;

import com.enderio.core.api.common.util.IProgressTile;
import com.enderio.core.common.autosave.Reader;
import com.enderio.core.common.autosave.Writer;
import com.enderio.core.common.autosave.annotations.Storable;
import com.enderio.core.common.autosave.annotations.Store.StoreFor;
import com.enderio.core.common.network.EnderPacketHandler;
import com.enderio.core.common.network.PacketProgress;

@Storable
public abstract class TileEntityBase extends TileEntity implements ITickable {

private final int checkOffset = (int) (Math.random() * 20);
Expand Down Expand Up @@ -69,37 +74,44 @@ protected int getProgressUpdateFreq() {
return 20;
}

@Override
public final void readFromNBT(NBTTagCompound root) {
super.readFromNBT(root);
readCustomNBT(root);
}

@Override
public final void writeToNBT(NBTTagCompound root) {
super.writeToNBT(root);
writeCustomNBT(root);
}

@Override
public Packet<?> getDescriptionPacket() {
NBTTagCompound tag = new NBTTagCompound();
writeCustomNBT(tag);
return new S35PacketUpdateTileEntity(getPos(), 1, tag);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
readCustomNBT(pkt.getNbtCompound());
}

public boolean canPlayerAccess(EntityPlayer player) {
return !isInvalid() && player.getDistanceSqToCenter(getPos().add(0.5, 0.5, 0.5)) <= 64D;
}

protected abstract void writeCustomNBT(NBTTagCompound root);

protected abstract void readCustomNBT(NBTTagCompound root);
@Override
public final void readFromNBT(NBTTagCompound root) {
super.readFromNBT(root);
Reader.read(StoreFor.SAVE, root, this);
}

@Override
public final void writeToNBT(NBTTagCompound root) {
super.writeToNBT(root);
Writer.write(StoreFor.SAVE, root, this);
}

@Override
public final Packet<?> getDescriptionPacket() {
NBTTagCompound root = new NBTTagCompound();
super.writeToNBT(root);
Writer.write(StoreFor.CLIENT, root, this);
return new S35PacketUpdateTileEntity(getPos(), 1, root);
}

@Override
public final void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
NBTTagCompound root = pkt.getNbtCompound();
super.readFromNBT(root);
Reader.read(StoreFor.CLIENT, root, this);
}

protected final void readItemNBT(NBTTagCompound root) {
Reader.read(StoreFor.ITEM, root, this);
}

protected final void writeItemNBT(NBTTagCompound root) {
Writer.write(StoreFor.ITEM, root, this);
}

public boolean canPlayerAccess(EntityPlayer player) {
return !isInvalid() && player.getDistanceSqToCenter(getPos().add(0.5, 0.5, 0.5)) <= 64D;
}

protected void updateBlock() {
if (worldObj != null) {
Expand Down
165 changes: 165 additions & 0 deletions src/main/java/com/enderio/core/common/autosave/Reader.java
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);
}

}
Loading