Created
July 17, 2016 21:14
-
-
Save Caellian/c602709305b01d2c7d41194448586e1b to your computer and use it in GitHub Desktop.
Property gist
This file contains hidden or 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
package xyz.openmodloader.api.flow.data; | |
import net.minecraft.nbt.NBTBase; | |
import net.minecraft.nbt.NBTTagCompound; | |
import xyz.openmodloader.OpenModLoader; | |
import xyz.openmodloader.nbt.NBTCompatible; | |
import xyz.openmodloader.nbt.NBTHelper; | |
/** | |
* @author Caellian | |
* Created on 17.7.2016. at 20:44. | |
*/ | |
public class Property<T extends NBTBase> implements NBTCompatible<NBTTagCompound>, Comparable<Property<T>> { | |
protected String id; | |
protected T data; | |
public Property(String id, T data){ | |
if (NBTHelper.isNBTCompatible(data)){ | |
OpenModLoader.getLogger().error("Type of '" + id + "' property isn't supported by NBT format!"); | |
} | |
this.id = id; | |
this.data = data; | |
} | |
public String getID() { | |
return id; | |
} | |
public T get() { | |
return data; | |
} | |
public T set(T newData){ | |
T old = this.data; | |
this.data = newData; | |
return old != null && old != newData ? old : newData; | |
} | |
@Override | |
public int compareTo(Property<T> o) { | |
if (o == null) { | |
return 1; | |
} | |
return this.getID().compareTo(o.getID()); | |
} | |
@Override | |
public void modifyFromNBT(NBTTagCompound in) { | |
if (in == null || !in.hasKey("id") || !in.hasKey("data")) { | |
return; | |
} | |
try { | |
//noinspection unchecked | |
this.id = in.getString("id"); | |
this.data = (T) NBTHelper.unwrapNBT(in.getTag("data")); | |
} catch (ClassCastException cce){ | |
OpenModLoader.getLogger().warn("Unable to cast data object to property type. Returning null!"); | |
} | |
} | |
public NBTTagCompound toNBT() { | |
NBTTagCompound result = new NBTTagCompound(); | |
result.setString("id", id); | |
result.setTag("data", data); | |
return result; | |
} | |
@Override | |
public String toString() { | |
return "{id:" + id + ",data:" + data.toString() +"}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment