
com.flowpowered.nbt.holder.FieldValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flow-nbt Show documentation
Show all versions of flow-nbt Show documentation
Named Binary Tag (NBT) library for Java based on Graham Edgecombe's JNBT library.
The newest version!
/*
* This file is part of Flow NBT, licensed under the MIT License (MIT).
*
* Copyright (c) 2011 Flow Powered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.flowpowered.nbt.holder;
import com.flowpowered.nbt.CompoundMap;
import com.flowpowered.nbt.CompoundTag;
import com.flowpowered.nbt.Tag;
/**
* Represents the value of a field
*/
public class FieldValue {
private T value;
private final Field field;
private final String key;
private final T defaultValue;
public FieldValue(String key, Field field) {
this(key, field, null);
}
public FieldValue(String key, Field field, T defaultValue) {
this.field = field;
this.key = key;
this.defaultValue = defaultValue;
}
/**
* Get this field from a CompoundTag
*
* @param tag The tag to get this field from
* @return The value
*/
public T load(CompoundTag tag) {
Tag subTag = tag.getValue().get(key);
if (subTag == null) {
return (value = defaultValue);
}
return (value = field.getValue(subTag));
}
public void save(CompoundMap tag) {
T value = this.value;
if (value == null) {
if ((value = defaultValue) == null) {
return;
}
}
Tag t = field.getValue(key, value);
tag.put(t);
}
public T get() {
return value;
}
public void set(T value) {
this.value = value;
}
// So generic info doesn't have to be duplicated
public static FieldValue from(String name, Field field, T defaultValue) {
return new FieldValue(name, field, defaultValue);
}
public static FieldValue from(String name, Field field) {
return new FieldValue(name, field);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy