use of net.minecraft.nbt.NBTTagFloat in project MorePlanets by SteveKunG.
the class ItemElectricHoeMP method getElectricityStored.
@Override
public float getElectricityStored(ItemStack itemStack) {
if (itemStack.getTagCompound() == null) {
itemStack.setTagCompound(new NBTTagCompound());
}
float energyStored = 0f;
if (itemStack.getTagCompound().hasKey("Electricity")) {
NBTBase obj = itemStack.getTagCompound().getTag("Electricity");
if (obj instanceof NBTTagDouble) {
energyStored = ((NBTTagDouble) obj).getFloat();
} else if (obj instanceof NBTTagFloat) {
energyStored = ((NBTTagFloat) obj).getFloat();
}
}
itemStack.setItemDamage((int) (100 - energyStored / this.getMaxElectricityStored(itemStack) * 100));
return energyStored;
}
use of net.minecraft.nbt.NBTTagFloat in project Galacticraft by micdoodle8.
the class ItemElectricBase method getElectricityStored.
/**
* Gets the energy stored in the item. Energy is stored using item NBT
*/
@Override
public float getElectricityStored(ItemStack itemStack) {
if (itemStack.getTagCompound() == null) {
itemStack.setTagCompound(new NBTTagCompound());
}
float energyStored = 0f;
if (itemStack.getTagCompound().hasKey("electricity")) {
NBTBase obj = itemStack.getTagCompound().getTag("electricity");
if (obj instanceof NBTTagDouble) {
energyStored = ((NBTTagDouble) obj).getFloat();
} else if (obj instanceof NBTTagFloat) {
energyStored = ((NBTTagFloat) obj).getFloat();
}
} else // First time check item - maybe from addInformation() in a JEI recipe display?
{
if (itemStack.getItemDamage() == DAMAGE_RANGE)
return 0F;
energyStored = this.getMaxElectricityStored(itemStack) * (DAMAGE_RANGE - itemStack.getItemDamage()) / DAMAGE_RANGE;
itemStack.getTagCompound().setFloat("electricity", energyStored);
}
/**
* Sets the damage as a percentage to render the bar properly.
*/
itemStack.setItemDamage(DAMAGE_RANGE - (int) (energyStored / this.getMaxElectricityStored(itemStack) * DAMAGE_RANGE));
return energyStored;
}
use of net.minecraft.nbt.NBTTagFloat in project Galacticraft by micdoodle8.
the class SchematicEntity method newFloatNBTList.
protected NBTTagList newFloatNBTList(float... par1ArrayOfFloat) {
NBTTagList nbttaglist = new NBTTagList();
float[] afloat = par1ArrayOfFloat;
int i = par1ArrayOfFloat.length;
for (int j = 0; j < i; ++j) {
float f1 = afloat[j];
nbttaglist.appendTag(new NBTTagFloat(f1));
}
return nbttaglist;
}
use of net.minecraft.nbt.NBTTagFloat in project BuildCraft by BuildCraft.
the class JsonUtil method registerNbtSerializersDeserializers.
public static GsonBuilder registerNbtSerializersDeserializers(GsonBuilder gsonBuilder) {
return gsonBuilder.registerTypeAdapterFactory(new TypeAdapterFactory() {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return type.getRawType() == NBTBase.class ? new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
// noinspection unchecked, RedundantCast
Streams.write(((JsonSerializer<T>) (JsonSerializer<NBTBase>) (src, typeOfSrc, context) -> {
if (src == NBTUtilBC.NBT_NULL) {
return JsonNull.INSTANCE;
}
switch(src.getId()) {
case Constants.NBT.TAG_BYTE:
return context.serialize(src, NBTTagByte.class);
case Constants.NBT.TAG_SHORT:
return context.serialize(src, NBTTagShort.class);
case Constants.NBT.TAG_INT:
return context.serialize(src, NBTTagInt.class);
case Constants.NBT.TAG_LONG:
return context.serialize(src, NBTTagLong.class);
case Constants.NBT.TAG_FLOAT:
return context.serialize(src, NBTTagFloat.class);
case Constants.NBT.TAG_DOUBLE:
return context.serialize(src, NBTTagDouble.class);
case Constants.NBT.TAG_BYTE_ARRAY:
return context.serialize(src, NBTTagByteArray.class);
case Constants.NBT.TAG_STRING:
return context.serialize(src, NBTTagString.class);
case Constants.NBT.TAG_LIST:
return context.serialize(src, NBTTagList.class);
case Constants.NBT.TAG_COMPOUND:
return context.serialize(src, NBTTagCompound.class);
case Constants.NBT.TAG_INT_ARRAY:
return context.serialize(src, NBTTagIntArray.class);
default:
throw new IllegalArgumentException(src.toString());
}
}).serialize(value, type.getType(), new JsonSerializationContext() {
@Override
public JsonElement serialize(Object src) {
return gson.toJsonTree(src);
}
@Override
public JsonElement serialize(Object src, Type typeOfSrc) {
return gson.toJsonTree(src, typeOfSrc);
}
}), out);
}
@Override
public T read(JsonReader in) throws IOException {
return ((JsonDeserializer<T>) (json, typeOfT, context) -> {
if (json.isJsonNull()) {
// noinspection unchecked
return (T) NBTUtilBC.NBT_NULL;
}
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
Number number = json.getAsJsonPrimitive().getAsNumber();
if (number instanceof BigInteger || number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) {
return context.deserialize(json, NBTTagLong.class);
} else {
return context.deserialize(json, NBTTagDouble.class);
}
}
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isBoolean()) {
return context.deserialize(new JsonPrimitive(json.getAsJsonPrimitive().getAsBoolean() ? (byte) 1 : (byte) 0), NBTTagByte.class);
}
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
return context.deserialize(json, NBTTagString.class);
}
if (json.isJsonArray()) {
return context.deserialize(json, NBTTagList.class);
}
if (json.isJsonObject()) {
return context.deserialize(json, NBTTagCompound.class);
}
throw new IllegalArgumentException(json.toString());
}).deserialize(Streams.parse(in), type.getType(), gson::fromJson);
}
} : null;
}
}).registerTypeAdapter(NBTTagByte.class, (JsonSerializer<NBTTagByte>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getByte())).registerTypeAdapter(NBTTagByte.class, (JsonDeserializer<NBTTagByte>) (json, typeOfT, context) -> new NBTTagByte(json.getAsJsonPrimitive().getAsByte())).registerTypeAdapter(NBTTagShort.class, (JsonSerializer<NBTTagShort>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getShort())).registerTypeAdapter(NBTTagShort.class, (JsonDeserializer<NBTTagShort>) (json, typeOfT, context) -> new NBTTagShort(json.getAsJsonPrimitive().getAsShort())).registerTypeAdapter(NBTTagInt.class, (JsonSerializer<NBTTagInt>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getInt())).registerTypeAdapter(NBTTagInt.class, (JsonDeserializer<NBTTagInt>) (json, typeOfT, context) -> new NBTTagInt(json.getAsJsonPrimitive().getAsInt())).registerTypeAdapter(NBTTagLong.class, (JsonSerializer<NBTTagLong>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getLong())).registerTypeAdapter(NBTTagLong.class, (JsonDeserializer<NBTTagLong>) (json, typeOfT, context) -> new NBTTagLong(json.getAsJsonPrimitive().getAsLong())).registerTypeAdapter(NBTTagFloat.class, (JsonSerializer<NBTTagFloat>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getFloat())).registerTypeAdapter(NBTTagFloat.class, (JsonDeserializer<NBTTagFloat>) (json, typeOfT, context) -> new NBTTagFloat(json.getAsJsonPrimitive().getAsFloat())).registerTypeAdapter(NBTTagDouble.class, (JsonSerializer<NBTTagDouble>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getDouble())).registerTypeAdapter(NBTTagDouble.class, (JsonDeserializer<NBTTagDouble>) (json, typeOfT, context) -> new NBTTagDouble(json.getAsJsonPrimitive().getAsDouble())).registerTypeAdapter(NBTTagByteArray.class, (JsonSerializer<NBTTagByteArray>) (src, typeOfSrc, context) -> {
JsonArray jsonArray = new JsonArray();
for (byte element : src.getByteArray()) {
jsonArray.add(new JsonPrimitive(element));
}
return jsonArray;
}).registerTypeAdapter(NBTTagByteArray.class, (JsonDeserializer<NBTTagByteArray>) (json, typeOfT, context) -> new NBTTagByteArray(ArrayUtils.toPrimitive(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(JsonElement::getAsByte).toArray(Byte[]::new)))).registerTypeAdapter(NBTTagString.class, (JsonSerializer<NBTTagString>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getString())).registerTypeAdapter(NBTTagString.class, (JsonDeserializer<NBTTagString>) (json, typeOfT, context) -> new NBTTagString(json.getAsJsonPrimitive().getAsString())).registerTypeAdapter(NBTTagList.class, (JsonSerializer<NBTTagList>) (src, typeOfSrc, context) -> {
JsonArray jsonArray = new JsonArray();
for (int i = 0; i < src.tagCount(); i++) {
NBTBase element = src.get(i);
jsonArray.add(context.serialize(element, NBTBase.class));
}
return jsonArray;
}).registerTypeAdapter(NBTTagList.class, (JsonDeserializer<NBTTagList>) (json, typeOfT, context) -> {
NBTTagList nbtTagList = new NBTTagList();
StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(element -> context.<NBTBase>deserialize(element, NBTBase.class)).forEach(nbtTagList::appendTag);
return nbtTagList;
}).registerTypeAdapter(NBTTagCompound.class, (JsonSerializer<NBTTagCompound>) (src, typeOfSrc, context) -> {
JsonObject jsonObject = new JsonObject();
for (String key : src.getKeySet()) {
jsonObject.add(key, context.serialize(src.getTag(key), NBTBase.class));
}
return jsonObject;
}).registerTypeAdapter(NBTTagCompound.class, (JsonDeserializer<NBTTagCompound>) (json, typeOfT, context) -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
nbtTagCompound.setTag(entry.getKey(), context.deserialize(entry.getValue(), NBTBase.class));
}
return nbtTagCompound;
}).registerTypeAdapter(NBTTagIntArray.class, (JsonSerializer<NBTTagIntArray>) (src, typeOfSrc, context) -> {
JsonArray jsonArray = new JsonArray();
for (int element : src.getIntArray()) {
jsonArray.add(new JsonPrimitive(element));
}
return jsonArray;
}).registerTypeAdapter(NBTTagIntArray.class, (JsonDeserializer<NBTTagIntArray>) (json, typeOfT, context) -> new NBTTagIntArray(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).mapToInt(JsonElement::getAsByte).toArray()));
}
use of net.minecraft.nbt.NBTTagFloat in project LogisticsPipes by RS485.
the class ItemIdentifier method debugDumpTag.
private void debugDumpTag(NBTBase nbt, StringBuilder sb) {
if (nbt == null) {
sb.append("null");
return;
}
if (nbt instanceof NBTTagByte) {
sb.append("TagByte(data=").append(((NBTTagByte) nbt).getByte()).append(")");
} else if (nbt instanceof NBTTagShort) {
sb.append("TagShort(data=").append(((NBTTagShort) nbt).getShort()).append(")");
} else if (nbt instanceof NBTTagInt) {
sb.append("TagInt(data=").append(((NBTTagInt) nbt).getInt()).append(")");
} else if (nbt instanceof NBTTagLong) {
sb.append("TagLong(data=").append(((NBTTagLong) nbt).getLong()).append(")");
} else if (nbt instanceof NBTTagFloat) {
sb.append("TagFloat(data=").append(((NBTTagFloat) nbt).getFloat()).append(")");
} else if (nbt instanceof NBTTagDouble) {
sb.append("TagDouble(data=").append(((NBTTagDouble) nbt).getDouble()).append(")");
} else if (nbt instanceof NBTTagString) {
sb.append("TagString(data=\"").append(((NBTTagString) nbt).getString()).append("\")");
} else if (nbt instanceof NBTTagByteArray) {
sb.append("TagByteArray(data=");
for (int i = 0; i < ((NBTTagByteArray) nbt).getByteArray().length; i++) {
sb.append(((NBTTagByteArray) nbt).getByteArray()[i]);
if (i < ((NBTTagByteArray) nbt).getByteArray().length - 1) {
sb.append(",");
}
}
sb.append(")");
} else if (nbt instanceof NBTTagIntArray) {
sb.append("TagIntArray(data=");
for (int i = 0; i < ((NBTTagIntArray) nbt).getIntArray().length; i++) {
sb.append(((NBTTagIntArray) nbt).getIntArray()[i]);
if (i < ((NBTTagIntArray) nbt).getIntArray().length - 1) {
sb.append(",");
}
}
sb.append(")");
} else if (nbt instanceof NBTTagList) {
sb.append("TagList(data=");
for (int i = 0; i < ((NBTTagList) nbt).tagCount(); i++) {
debugDumpTag((((NBTTagList) nbt).get(i)), sb);
if (i < ((NBTTagList) nbt).tagCount() - 1) {
sb.append(",");
}
}
sb.append(")");
} else if (nbt instanceof NBTTagCompound) {
sb.append("TagCompound(data=");
for (Iterator<String> iter = ((NBTTagCompound) nbt).getKeySet().iterator(); iter.hasNext(); ) {
String key = iter.next();
NBTBase value = ((NBTTagCompound) nbt).getTag(key);
sb.append("\"").append(key).append("\"=");
debugDumpTag((value), sb);
if (iter.hasNext()) {
sb.append(",");
}
}
sb.append(")");
} else {
sb.append(nbt.getClass().getName()).append("(?)");
}
}
Aggregations