fix #91
This commit is contained in:
parent
ac33eca6bf
commit
9013ffe6f1
4 changed files with 64 additions and 25 deletions
|
@ -1,14 +1,15 @@
|
|||
@file:JvmName("NBTHelper")
|
||||
|
||||
package at.petrak.hexcasting.api.utils
|
||||
|
||||
import net.minecraft.nbt.*
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import java.util.*
|
||||
|
||||
private inline fun <T: Any, K, E> T?.getIf(key: K, predicate: T?.(K) -> Boolean, get: T.(K) -> E): E? =
|
||||
private inline fun <T : Any, K, E> T?.getIf(key: K, predicate: T?.(K) -> Boolean, get: T.(K) -> E): E? =
|
||||
getIf(key, predicate, get, null)
|
||||
|
||||
private inline fun <T: Any, K, E> T?.getIf(key: K, predicate: T?.(K) -> Boolean, get: T.(K) -> E, default: E): E {
|
||||
private inline fun <T : Any, K, E> T?.getIf(key: K, predicate: T?.(K) -> Boolean, get: T.(K) -> E, default: E): E {
|
||||
if (this != null && predicate(key))
|
||||
return get(key)
|
||||
return default
|
||||
|
@ -32,7 +33,12 @@ fun CompoundTag?.hasCompound(key: String) = contains(key, Tag.TAG_COMPOUND)
|
|||
fun CompoundTag?.hasString(key: String) = contains(key, Tag.TAG_STRING)
|
||||
fun CompoundTag?.hasList(key: String) = contains(key, Tag.TAG_LIST)
|
||||
fun CompoundTag?.hasList(key: String, objType: Int) = hasList(key, objType.toByte())
|
||||
fun CompoundTag?.hasList(key: String, objType: Byte) = hasList(key) && (get(key) as ListTag).elementType == objType
|
||||
fun CompoundTag?.hasList(key: String, objType: Byte): Boolean {
|
||||
if (!hasList(key)) return false
|
||||
val lt = get(key) as ListTag
|
||||
return lt.elementType == objType || lt.elementType == 0.toByte()
|
||||
}
|
||||
|
||||
fun CompoundTag?.hasUUID(key: String) = this != null && hasUUID(key)
|
||||
|
||||
fun CompoundTag?.contains(key: String, id: Byte) = contains(key, id.toInt())
|
||||
|
@ -64,24 +70,39 @@ fun CompoundTag?.remove(key: String) = this?.remove(key)
|
|||
// Gets
|
||||
|
||||
@JvmOverloads
|
||||
fun CompoundTag?.getBoolean(key: String, defaultExpected: Boolean = false) = getIf(key, CompoundTag?::hasNumber, CompoundTag::getBoolean, defaultExpected)
|
||||
fun CompoundTag?.getBoolean(key: String, defaultExpected: Boolean = false) =
|
||||
getIf(key, CompoundTag?::hasNumber, CompoundTag::getBoolean, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun CompoundTag?.getByte(key: String, defaultExpected: Byte = 0) = getIf(key, CompoundTag?::hasNumber, CompoundTag::getByte, defaultExpected)
|
||||
fun CompoundTag?.getByte(key: String, defaultExpected: Byte = 0) =
|
||||
getIf(key, CompoundTag?::hasNumber, CompoundTag::getByte, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun CompoundTag?.getShort(key: String, defaultExpected: Short = 0) = getIf(key, CompoundTag?::hasNumber, CompoundTag::getShort, defaultExpected)
|
||||
fun CompoundTag?.getShort(key: String, defaultExpected: Short = 0) =
|
||||
getIf(key, CompoundTag?::hasNumber, CompoundTag::getShort, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun CompoundTag?.getInt(key: String, defaultExpected: Int = 0) = getIf(key, CompoundTag?::hasNumber, CompoundTag::getInt, defaultExpected)
|
||||
fun CompoundTag?.getInt(key: String, defaultExpected: Int = 0) =
|
||||
getIf(key, CompoundTag?::hasNumber, CompoundTag::getInt, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun CompoundTag?.getLong(key: String, defaultExpected: Long = 0) = getIf(key, CompoundTag?::hasNumber, CompoundTag::getLong, defaultExpected)
|
||||
fun CompoundTag?.getLong(key: String, defaultExpected: Long = 0) =
|
||||
getIf(key, CompoundTag?::hasNumber, CompoundTag::getLong, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun CompoundTag?.getFloat(key: String, defaultExpected: Float = 0f) = getIf(key, CompoundTag?::hasNumber, CompoundTag::getFloat, defaultExpected)
|
||||
fun CompoundTag?.getFloat(key: String, defaultExpected: Float = 0f) =
|
||||
getIf(key, CompoundTag?::hasNumber, CompoundTag::getFloat, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun CompoundTag?.getDouble(key: String, defaultExpected: Double = 0.0) = getIf(key, CompoundTag?::hasNumber, CompoundTag::getDouble, defaultExpected)
|
||||
fun CompoundTag?.getDouble(key: String, defaultExpected: Double = 0.0) =
|
||||
getIf(key, CompoundTag?::hasNumber, CompoundTag::getDouble, defaultExpected)
|
||||
|
||||
fun CompoundTag?.getLongArray(key: String) = getIf(key, CompoundTag?::hasLongArray, CompoundTag::getLongArray)
|
||||
fun CompoundTag?.getIntArray(key: String) = getIf(key, CompoundTag?::hasIntArray, CompoundTag::getIntArray)
|
||||
fun CompoundTag?.getByteArray(key: String) = getIf(key, CompoundTag?::hasByteArray, CompoundTag::getByteArray)
|
||||
fun CompoundTag?.getCompound(key: String): CompoundTag? = getIf(key, CompoundTag?::hasCompound, CompoundTag::getCompound)
|
||||
fun CompoundTag?.getCompound(key: String): CompoundTag? =
|
||||
getIf(key, CompoundTag?::hasCompound, CompoundTag::getCompound)
|
||||
|
||||
fun CompoundTag?.getString(key: String) = getIf(key, CompoundTag?::hasString, CompoundTag::getString)
|
||||
fun CompoundTag?.getList(key: String, objType: Byte) = getList(key, objType.toInt())
|
||||
fun CompoundTag?.getList(key: String, objType: Int) = getIf(key, { hasList(key, objType) }) { getList(it, objType) }
|
||||
|
@ -149,6 +170,7 @@ val Tag.asByteArray: ByteArray
|
|||
}
|
||||
|
||||
val Tag.asCompound get() = this as? CompoundTag ?: CompoundTag()
|
||||
|
||||
// asString is defined in Tag
|
||||
val Tag.asList get() = this as? ListTag ?: ListTag()
|
||||
val Tag.asUUID: UUID get() = if (this is IntArrayTag && this.size == 4) NbtUtils.loadUUID(this) else UUID(0, 0)
|
||||
|
@ -176,8 +198,10 @@ fun ItemStack.hasUUID(key: String) = tag.hasUUID(key)
|
|||
|
||||
@JvmName("contains")
|
||||
fun ItemStack.containsTag(key: String) = tag.contains(key)
|
||||
|
||||
@JvmName("contains")
|
||||
fun ItemStack.containsTag(key: String, id: Byte) = tag.contains(key, id)
|
||||
|
||||
@JvmName("contains")
|
||||
fun ItemStack.containsTag(key: String, id: Int) = tag.contains(key, id)
|
||||
|
||||
|
@ -198,6 +222,7 @@ fun ItemStack.putCompound(key: String, value: CompoundTag) = putTag(key, value)
|
|||
fun ItemStack.putString(key: String, value: String) = orCreateTag.putString(key, value)
|
||||
fun ItemStack.putList(key: String, value: ListTag) = putTag(key, value)
|
||||
fun ItemStack.putUUID(key: String, value: UUID) = orCreateTag.putUUID(key, value)
|
||||
|
||||
@JvmName("put")
|
||||
fun ItemStack.putTag(key: String, value: Tag) = orCreateTag.put(key, value)
|
||||
|
||||
|
@ -209,16 +234,22 @@ fun ItemStack.remove(key: String) = removeTagKey(key)
|
|||
|
||||
@JvmOverloads
|
||||
fun ItemStack.getBoolean(key: String, defaultExpected: Boolean = false) = tag.getBoolean(key, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun ItemStack.getByte(key: String, defaultExpected: Byte = 0) = tag.getByte(key, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun ItemStack.getShort(key: String, defaultExpected: Short = 0) = tag.getShort(key, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun ItemStack.getInt(key: String, defaultExpected: Int = 0) = tag.getInt(key, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun ItemStack.getLong(key: String, defaultExpected: Long = 0) = tag.getLong(key, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun ItemStack.getFloat(key: String, defaultExpected: Float = 0f) = tag.getFloat(key, defaultExpected)
|
||||
|
||||
@JvmOverloads
|
||||
fun ItemStack.getDouble(key: String, defaultExpected: Double = 0.0) = tag.getDouble(key, defaultExpected)
|
||||
|
||||
|
@ -229,6 +260,7 @@ fun ItemStack.getCompound(key: String) = tag.getCompound(key)
|
|||
fun ItemStack.getString(key: String) = tag.getString(key)
|
||||
fun ItemStack.getList(key: String, objType: Int) = tag.getList(key, objType)
|
||||
fun ItemStack.getUUID(key: String) = tag.getUUID(key)
|
||||
|
||||
@JvmName("get")
|
||||
fun ItemStack.getTag(key: String) = tag.get(key)
|
||||
|
||||
|
|
|
@ -20,19 +20,22 @@ class OpErase : SpellOperator {
|
|||
val hexHolder = IXplatAbstractions.INSTANCE.findHexHolder(it)
|
||||
val datumHolder = IXplatAbstractions.INSTANCE.findDataHolder(it)
|
||||
|
||||
(hexHolder?.hasHex() != true) ||
|
||||
(datumHolder?.writeDatum(null, true) ?: false)
|
||||
(hexHolder?.hasHex() == true) ||
|
||||
(datumHolder?.writeDatum(null, true) == true)
|
||||
}
|
||||
val hexHolder = IXplatAbstractions.INSTANCE.findHexHolder(handStack)
|
||||
val datumHolder = IXplatAbstractions.INSTANCE.findDataHolder(handStack)
|
||||
|
||||
if ((hexHolder?.getHex(ctx.world) == null) &&
|
||||
(datumHolder?.writeDatum(null, true) == true)) {
|
||||
(datumHolder?.writeDatum(null, true) == false)
|
||||
) {
|
||||
throw MishapBadOffhandItem.of(handStack, hand, "eraseable")
|
||||
}
|
||||
|
||||
return Triple(Spell,
|
||||
ManaConstants.DUST_UNIT, listOf())
|
||||
return Triple(
|
||||
Spell,
|
||||
ManaConstants.DUST_UNIT, listOf()
|
||||
)
|
||||
}
|
||||
|
||||
private object Spell : RenderedSpell {
|
||||
|
@ -41,8 +44,8 @@ class OpErase : SpellOperator {
|
|||
val hexHolder = IXplatAbstractions.INSTANCE.findHexHolder(it)
|
||||
val datumHolder = IXplatAbstractions.INSTANCE.findDataHolder(it)
|
||||
|
||||
(hexHolder?.hasHex() != true) ||
|
||||
(datumHolder?.writeDatum(null, true) ?: false)
|
||||
(hexHolder?.hasHex() == true) ||
|
||||
(datumHolder?.writeDatum(null, true) == true)
|
||||
}
|
||||
val hexHolder = IXplatAbstractions.INSTANCE.findHexHolder(handStack)
|
||||
val datumHolder = IXplatAbstractions.INSTANCE.findDataHolder(handStack)
|
||||
|
|
|
@ -16,8 +16,8 @@ import org.jetbrains.annotations.Nullable;
|
|||
import java.util.List;
|
||||
|
||||
public abstract class ItemManaHolder extends Item implements ManaHolderItem {
|
||||
private static final String TAG_MANA = "hexcasting:mana";
|
||||
private static final String TAG_MAX_MANA = "hexcasting:start_mana";
|
||||
public static final String TAG_MANA = "hexcasting:mana";
|
||||
public static final String TAG_MAX_MANA = "hexcasting:start_mana";
|
||||
|
||||
public ItemManaHolder(Properties pProperties) {
|
||||
super(pProperties);
|
||||
|
|
|
@ -61,16 +61,18 @@ public abstract class ItemPackagedHex extends ItemManaHolder implements HexHolde
|
|||
public @Nullable List<SpellDatum<?>> getHex(ItemStack stack, ServerLevel level) {
|
||||
var patsTag = NBTHelper.getList(stack, TAG_PATTERNS, Tag.TAG_COMPOUND);
|
||||
|
||||
if (patsTag == null)
|
||||
if (patsTag == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var out = new ArrayList<SpellDatum<?>>();
|
||||
for (var patTag : patsTag) {
|
||||
CompoundTag tag = NBTHelper.getAsCompound(patTag);
|
||||
if (tag.size() != 1)
|
||||
if (tag.size() != 1) {
|
||||
out.add(SpellDatum.make(HexPattern.fromNBT(tag)));
|
||||
else
|
||||
} else {
|
||||
out.add(SpellDatum.fromNBT(tag, level));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
@ -78,8 +80,9 @@ public abstract class ItemPackagedHex extends ItemManaHolder implements HexHolde
|
|||
@Override
|
||||
public void writeHex(ItemStack stack, List<SpellDatum<?>> patterns, int mana) {
|
||||
ListTag patsTag = new ListTag();
|
||||
for (SpellDatum<?> pat : patterns)
|
||||
for (SpellDatum<?> pat : patterns) {
|
||||
patsTag.add(pat.serializeToNBT());
|
||||
}
|
||||
|
||||
NBTHelper.putList(stack, TAG_PATTERNS, patsTag);
|
||||
|
||||
|
@ -89,7 +92,8 @@ public abstract class ItemPackagedHex extends ItemManaHolder implements HexHolde
|
|||
@Override
|
||||
public void clearHex(ItemStack stack) {
|
||||
NBTHelper.remove(stack, ItemPackagedHex.TAG_PATTERNS);
|
||||
withMana(stack, 0, 0);
|
||||
NBTHelper.remove(stack, TAG_MANA);
|
||||
NBTHelper.remove(stack, TAG_MAX_MANA);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue