ever heard of ItemNBTHelper? me neither
this would look way prettier if more of the mod was in kotlin. just trust me on that
This commit is contained in:
parent
e790dc78e3
commit
40045d79c4
16 changed files with 329 additions and 260 deletions
|
@ -1,10 +1,10 @@
|
|||
package at.petrak.hexcasting.api.item;
|
||||
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
|
@ -53,12 +53,10 @@ public interface DataHolderItem {
|
|||
if (pIsAdvanced.isAdvanced()) {
|
||||
pTooltipComponents.add(NbtUtils.toPrettyComponent(datumTag));
|
||||
}
|
||||
} else if (pStack.hasTag()) {
|
||||
var tag = pStack.getTag();
|
||||
if (tag != null && tag.contains(DataHolderItem.TAG_OVERRIDE_VISUALLY, Tag.TAG_STRING)) {
|
||||
pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem",
|
||||
new TranslatableComponent("hexcasting.spelldata.anything").withStyle(ChatFormatting.LIGHT_PURPLE)));
|
||||
}
|
||||
} else if (NBTHelper.hasString(pStack, DataHolderItem.TAG_OVERRIDE_VISUALLY)) {
|
||||
pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem",
|
||||
new TranslatableComponent("hexcasting.spelldata.anything").withStyle(ChatFormatting.LIGHT_PURPLE)));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
177
src/main/java/at/petrak/hexcasting/api/utils/NBTHelper.kt
Normal file
177
src/main/java/at/petrak/hexcasting/api/utils/NBTHelper.kt
Normal file
|
@ -0,0 +1,177 @@
|
|||
@file:JvmName("NBTHelper")
|
||||
package at.petrak.hexcasting.api.utils
|
||||
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.ListTag
|
||||
import net.minecraft.nbt.Tag
|
||||
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? =
|
||||
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 {
|
||||
if (this != null && predicate(key))
|
||||
return get(key)
|
||||
return default
|
||||
}
|
||||
|
||||
// ======================================================================================================== CompoundTag
|
||||
|
||||
// Checks for containment
|
||||
|
||||
fun CompoundTag?.hasNumber(key: String) = contains(key, Tag.TAG_ANY_NUMERIC)
|
||||
fun CompoundTag?.hasByte(key: String) = contains(key, Tag.TAG_BYTE)
|
||||
fun CompoundTag?.hasShort(key: String) = contains(key, Tag.TAG_SHORT)
|
||||
fun CompoundTag?.hasInt(key: String) = contains(key, Tag.TAG_INT)
|
||||
fun CompoundTag?.hasLong(key: String) = contains(key, Tag.TAG_LONG)
|
||||
fun CompoundTag?.hasFloat(key: String) = contains(key, Tag.TAG_FLOAT)
|
||||
fun CompoundTag?.hasDouble(key: String) = contains(key, Tag.TAG_DOUBLE)
|
||||
fun CompoundTag?.hasLongArray(key: String) = contains(key, Tag.TAG_LONG_ARRAY)
|
||||
fun CompoundTag?.hasIntArray(key: String) = contains(key, Tag.TAG_INT_ARRAY)
|
||||
fun CompoundTag?.hasByteArray(key: String) = contains(key, Tag.TAG_BYTE_ARRAY)
|
||||
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?.hasUUID(key: String) = this != null && hasUUID(key)
|
||||
|
||||
fun CompoundTag?.contains(key: String, id: Byte) = contains(key, id.toInt())
|
||||
fun CompoundTag?.contains(key: String, id: Int) = this != null && contains(key, id)
|
||||
fun CompoundTag?.contains(key: String) = this != null && contains(key)
|
||||
|
||||
// Puts
|
||||
|
||||
fun CompoundTag?.putBoolean(key: String, value: Boolean) = this?.putBoolean(key, value)
|
||||
fun CompoundTag?.putByte(key: String, value: Byte) = this?.putByte(key, value)
|
||||
fun CompoundTag?.putShort(key: String, value: Short) = this?.putShort(key, value)
|
||||
fun CompoundTag?.putInt(key: String, value: Int) = this?.putInt(key, value)
|
||||
fun CompoundTag?.putLong(key: String, value: Long) = this?.putLong(key, value)
|
||||
fun CompoundTag?.putFloat(key: String, value: Float) = this?.putFloat(key, value)
|
||||
fun CompoundTag?.putDouble(key: String, value: Double) = this?.putDouble(key, value)
|
||||
fun CompoundTag?.putLongArray(key: String, value: LongArray) = this?.putLongArray(key, value)
|
||||
fun CompoundTag?.putIntArray(key: String, value: IntArray) = this?.putIntArray(key, value)
|
||||
fun CompoundTag?.putByteArray(key: String, value: ByteArray) = this?.putByteArray(key, value)
|
||||
fun CompoundTag?.putCompound(key: String, value: CompoundTag) = put(key, value)
|
||||
fun CompoundTag?.putString(key: String, value: String) = this?.putString(key, value)
|
||||
fun CompoundTag?.putList(key: String, value: ListTag) = put(key, value)
|
||||
fun CompoundTag?.putUUID(key: String, value: UUID) = this?.putUUID(key, value)
|
||||
fun CompoundTag?.put(key: String, value: Tag) = this?.put(key, value)
|
||||
|
||||
// Remove
|
||||
|
||||
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)
|
||||
@JvmOverloads
|
||||
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)
|
||||
@JvmOverloads
|
||||
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)
|
||||
@JvmOverloads
|
||||
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?.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?.getString(key: String) = getIf(key, CompoundTag?::hasString, CompoundTag::getString)
|
||||
fun CompoundTag?.getList(key: String, objType: Int) = getIf(key, { hasList(key, objType) }) { getList(it, objType) }
|
||||
fun CompoundTag?.getUUID(key: String) = getIf(key, CompoundTag?::hasUUID, CompoundTag::getUUID)
|
||||
fun CompoundTag?.get(key: String) = getIf(key, CompoundTag?::contains, CompoundTag::get)
|
||||
|
||||
// Get-or-create
|
||||
|
||||
fun CompoundTag.getOrCreateCompound(key: String) = getCompound(key) ?: CompoundTag().also { putCompound(key, this) }
|
||||
|
||||
// ========================================================================================================== ItemStack
|
||||
|
||||
// Checks for containment
|
||||
|
||||
fun ItemStack.hasNumber(key: String) = tag.hasNumber(key)
|
||||
fun ItemStack.hasByte(key: String) = tag.hasByte(key)
|
||||
fun ItemStack.hasShort(key: String) = tag.hasShort(key)
|
||||
fun ItemStack.hasInt(key: String) = tag.hasInt(key)
|
||||
fun ItemStack.hasLong(key: String) = tag.hasLong(key)
|
||||
fun ItemStack.hasFloat(key: String) = tag.hasFloat(key)
|
||||
fun ItemStack.hasDouble(key: String) = tag.hasDouble(key)
|
||||
fun ItemStack.hasLongArray(key: String) = tag.hasLongArray(key)
|
||||
fun ItemStack.hasIntArray(key: String) = tag.hasIntArray(key)
|
||||
fun ItemStack.hasByteArray(key: String) = tag.hasByteArray(key)
|
||||
fun ItemStack.hasCompound(key: String) = tag.hasCompound(key)
|
||||
fun ItemStack.hasString(key: String) = tag.hasString(key)
|
||||
fun ItemStack.hasList(key: String) = tag.hasList(key)
|
||||
fun ItemStack.hasList(key: String, objType: Int) = tag.hasList(key, objType)
|
||||
fun ItemStack.hasList(key: String, objType: Byte) = tag.hasList(key, objType)
|
||||
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)
|
||||
|
||||
// Puts
|
||||
|
||||
fun ItemStack.putBoolean(key: String, value: Boolean) = orCreateTag.putBoolean(key, value)
|
||||
fun ItemStack.putByte(key: String, value: Byte) = orCreateTag.putByte(key, value)
|
||||
fun ItemStack.putShort(key: String, value: Short) = orCreateTag.putShort(key, value)
|
||||
fun ItemStack.putInt(key: String, value: Int) = orCreateTag.putInt(key, value)
|
||||
fun ItemStack.putLong(key: String, value: Long) = orCreateTag.putLong(key, value)
|
||||
fun ItemStack.putFloat(key: String, value: Float) = orCreateTag.putFloat(key, value)
|
||||
fun ItemStack.putDouble(key: String, value: Double) = orCreateTag.putDouble(key, value)
|
||||
|
||||
fun ItemStack.putLongArray(key: String, value: LongArray) = orCreateTag.putLongArray(key, value)
|
||||
fun ItemStack.putIntArray(key: String, value: IntArray) = orCreateTag.putIntArray(key, value)
|
||||
fun ItemStack.putByteArray(key: String, value: ByteArray) = orCreateTag.putByteArray(key, value)
|
||||
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)
|
||||
|
||||
// Remove
|
||||
|
||||
fun ItemStack.remove(key: String) = removeTagKey(key)
|
||||
|
||||
// Gets
|
||||
|
||||
@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)
|
||||
|
||||
fun ItemStack.getLongArray(key: String) = tag.getLongArray(key)
|
||||
fun ItemStack.getIntArray(key: String) = tag.getIntArray(key)
|
||||
fun ItemStack.getByteArray(key: String) = tag.getByteArray(key)
|
||||
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)
|
||||
|
||||
// Get-or-create
|
||||
|
||||
fun ItemStack.getOrCreateCompound(key: String): CompoundTag = getOrCreateTagElement(key)
|
|
@ -1,5 +1,6 @@
|
|||
package at.petrak.hexcasting.client;
|
||||
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.client.gui.PatternTooltipGreeble;
|
||||
import at.petrak.hexcasting.common.blocks.circles.BlockEntitySlate;
|
||||
import at.petrak.hexcasting.common.items.HexItems;
|
||||
|
@ -8,6 +9,7 @@ import at.petrak.hexcasting.common.items.ItemSlate;
|
|||
import at.petrak.hexcasting.api.spell.math.HexPattern;
|
||||
import com.mojang.datafixers.util.Either;
|
||||
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.world.inventory.tooltip.TooltipComponent;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
@ -35,23 +37,22 @@ public class HexTooltips {
|
|||
ItemStack stack = evt.getItemStack();
|
||||
if (!stack.isEmpty()) {
|
||||
if (stack.is(HexItems.SCROLL.get())) {
|
||||
var tag = stack.getOrCreateTag();
|
||||
if (tag.contains(ItemScroll.TAG_PATTERN, Tag.TAG_COMPOUND)) {
|
||||
var pattern = HexPattern.DeserializeFromNBT(tag.getCompound(ItemScroll.TAG_PATTERN));
|
||||
CompoundTag patternTag = NBTHelper.getCompound(stack, ItemScroll.TAG_PATTERN);
|
||||
if (patternTag != null) {
|
||||
var pattern = HexPattern.DeserializeFromNBT(patternTag);
|
||||
evt.getTooltipElements().add(Either.right(new PatternTooltipGreeble(
|
||||
pattern,
|
||||
tag.contains(ItemScroll.TAG_OP_ID, Tag.TAG_STRING)
|
||||
NBTHelper.contains(stack, ItemScroll.TAG_OP_ID, Tag.TAG_STRING)
|
||||
? PatternTooltipGreeble.ANCIENT_BG : PatternTooltipGreeble.PRISTINE_BG)));
|
||||
}
|
||||
} else if (stack.is(HexItems.SLATE.get()) && ItemSlate.hasPattern(stack)) {
|
||||
var tag = stack.getOrCreateTag()
|
||||
.getCompound("BlockEntityTag")
|
||||
.getCompound(BlockEntitySlate.TAG_PATTERN);
|
||||
var pattern = HexPattern.DeserializeFromNBT(tag);
|
||||
evt.getTooltipElements().add(Either.right(new PatternTooltipGreeble(
|
||||
pattern,
|
||||
PatternTooltipGreeble.SLATE_BG)));
|
||||
|
||||
var tag = NBTHelper.getCompound(NBTHelper.getOrCreateCompound(stack, "BlockEntityTag"), BlockEntitySlate.TAG_PATTERN);
|
||||
if (tag != null) {
|
||||
var pattern = HexPattern.DeserializeFromNBT(tag);
|
||||
evt.getTooltipElements().add(Either.right(new PatternTooltipGreeble(
|
||||
pattern,
|
||||
PatternTooltipGreeble.SLATE_BG)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import at.petrak.hexcasting.api.item.ManaHolderItem;
|
|||
import at.petrak.hexcasting.api.misc.ManaConstants;
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.spell.Widget;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.client.be.BlockEntityAkashicBookshelfRenderer;
|
||||
import at.petrak.hexcasting.client.be.BlockEntitySlateRenderer;
|
||||
import at.petrak.hexcasting.client.entity.WallScrollRenderer;
|
||||
|
@ -29,8 +30,6 @@ import net.minecraft.client.renderer.RenderType;
|
|||
import net.minecraft.client.renderer.entity.EntityRenderers;
|
||||
import net.minecraft.client.renderer.item.ItemProperties;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.chat.Style;
|
||||
import net.minecraft.network.chat.TextColor;
|
||||
import net.minecraft.network.chat.TextComponent;
|
||||
|
@ -77,7 +76,7 @@ public class RegisterClientStuff {
|
|||
});
|
||||
|
||||
ItemProperties.register(HexItems.SCROLL.get(), ItemScroll.ANCIENT_PREDICATE,
|
||||
(stack, level, holder, holderID) -> stack.getOrCreateTag().contains(ItemScroll.TAG_OP_ID) ? 1f : 0f);
|
||||
(stack, level, holder, holderID) -> NBTHelper.hasString(stack, ItemScroll.TAG_OP_ID) ? 1f : 0f);
|
||||
|
||||
ItemProperties.register(HexItems.SLATE.get(), ItemSlate.WRITTEN_PRED,
|
||||
(stack, level, holder, holderID) -> ItemSlate.hasPattern(stack) ? 1f : 0f);
|
||||
|
@ -253,15 +252,12 @@ public class RegisterClientStuff {
|
|||
ItemProperties.register((Item) item, ItemFocus.DATATYPE_PRED,
|
||||
(stack, level, holder, holderID) -> {
|
||||
var datum = item.readDatumTag(stack);
|
||||
String override = NBTHelper.getString(stack, DataHolderItem.TAG_OVERRIDE_VISUALLY);
|
||||
String typename = null;
|
||||
if (datum != null) {
|
||||
if (override != null) {
|
||||
typename = override;
|
||||
} else if (datum != null) {
|
||||
typename = datum.getAllKeys().iterator().next();
|
||||
|
||||
} else if (stack.hasTag()) {
|
||||
CompoundTag tag = stack.getTag();
|
||||
if (tag != null && tag.contains(DataHolderItem.TAG_OVERRIDE_VISUALLY, Tag.TAG_STRING)) {
|
||||
typename = tag.getString(DataHolderItem.TAG_OVERRIDE_VISUALLY);
|
||||
}
|
||||
}
|
||||
|
||||
return typename == null ? 0f : switch (typename) {
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package at.petrak.hexcasting.common.entities;
|
||||
|
||||
import at.petrak.hexcasting.api.spell.math.HexPattern;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.client.RenderLib;
|
||||
import at.petrak.hexcasting.common.items.HexItems;
|
||||
import at.petrak.hexcasting.common.items.ItemScroll;
|
||||
import at.petrak.hexcasting.common.lib.HexSounds;
|
||||
import at.petrak.hexcasting.api.spell.math.HexPattern;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.syncher.EntityDataAccessor;
|
||||
|
@ -58,16 +58,16 @@ public class EntityWallScroll extends HangingEntity implements IEntityAdditional
|
|||
private void loadDataFromScrollItem(ItemStack scroll) {
|
||||
this.scroll = scroll;
|
||||
|
||||
var tag = scroll.getTag();
|
||||
if (tag != null && tag.contains(ItemScroll.TAG_PATTERN, Tag.TAG_COMPOUND)) {
|
||||
this.pattern = HexPattern.DeserializeFromNBT(tag.getCompound(ItemScroll.TAG_PATTERN));
|
||||
CompoundTag patternTag = NBTHelper.getCompound(scroll, ItemScroll.TAG_PATTERN);
|
||||
if (patternTag != null) {
|
||||
this.pattern = HexPattern.DeserializeFromNBT(patternTag);
|
||||
if (this.level.isClientSide) {
|
||||
var pair = RenderLib.getCenteredPattern(pattern, 128, 128, 16f);
|
||||
var dots = pair.getSecond();
|
||||
this.zappyPoints = RenderLib.makeZappy(dots, 10f, 0.8f, 0f);
|
||||
}
|
||||
|
||||
this.isAncient = tag.contains(ItemScroll.TAG_OP_ID, Tag.TAG_STRING);
|
||||
this.isAncient = NBTHelper.hasString(scroll, ItemScroll.TAG_OP_ID);
|
||||
} else {
|
||||
this.pattern = null;
|
||||
this.zappyPoints = null;
|
||||
|
|
|
@ -2,9 +2,9 @@ package at.petrak.hexcasting.common.items;
|
|||
|
||||
import at.petrak.hexcasting.api.item.DataHolderItem;
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.common.lib.HexSounds;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
|
@ -27,14 +27,7 @@ public class ItemAbacus extends Item implements DataHolderItem {
|
|||
|
||||
@Override
|
||||
public @Nullable CompoundTag readDatumTag(ItemStack stack) {
|
||||
double num;
|
||||
var tag = stack.getTag();
|
||||
if (tag == null) {
|
||||
num = 0d;
|
||||
} else {
|
||||
num = tag.getDouble(TAG_VALUE);
|
||||
}
|
||||
var datum = SpellDatum.make(num);
|
||||
var datum = SpellDatum.make(NBTHelper.getDouble(stack, TAG_VALUE));
|
||||
return datum.serializeToNBT();
|
||||
}
|
||||
|
||||
|
@ -52,13 +45,8 @@ public class ItemAbacus extends Item implements DataHolderItem {
|
|||
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
|
||||
var stack = player.getItemInHand(hand);
|
||||
if (player.isShiftKeyDown()) {
|
||||
|
||||
var tag = stack.getOrCreateTag();
|
||||
double oldNum = 0d;
|
||||
if (tag.contains(TAG_VALUE, Tag.TAG_ANY_NUMERIC)) {
|
||||
oldNum = tag.getDouble(TAG_VALUE);
|
||||
}
|
||||
tag.putDouble(TAG_VALUE, 0d);
|
||||
double oldNum = NBTHelper.getDouble(stack, TAG_VALUE);
|
||||
stack.removeTagKey(TAG_VALUE);
|
||||
|
||||
player.playSound(HexSounds.ABACUS_SHAKE.get(), 1f, 1f);
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@ import at.petrak.hexcasting.HexMod;
|
|||
import at.petrak.hexcasting.api.item.DataHolderItem;
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.spell.Widget;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.Item;
|
||||
|
@ -29,20 +29,12 @@ public class ItemFocus extends Item implements DataHolderItem {
|
|||
|
||||
@Override
|
||||
public @Nullable CompoundTag readDatumTag(ItemStack stack) {
|
||||
if (!stack.hasTag()) {
|
||||
return null;
|
||||
}
|
||||
var tag = stack.getTag();
|
||||
if (!tag.contains(TAG_DATA, Tag.TAG_COMPOUND)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return tag.getCompound(TAG_DATA);
|
||||
return NBTHelper.getCompound(stack, TAG_DATA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescriptionId(ItemStack stack) {
|
||||
return super.getDescriptionId(stack) + (stack.hasTag() && stack.getTag().getBoolean(TAG_SEALED) ? ".sealed" : "");
|
||||
return super.getDescriptionId(stack) + (NBTHelper.getBoolean(stack, TAG_SEALED) ? ".sealed" : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -52,18 +44,16 @@ public class ItemFocus extends Item implements DataHolderItem {
|
|||
|
||||
@Override
|
||||
public boolean canWrite(ItemStack stack, SpellDatum<?> datum) {
|
||||
return datum == null || !stack.hasTag() || !stack.getTag().getBoolean(TAG_SEALED);
|
||||
return datum == null || !NBTHelper.getBoolean(stack, TAG_SEALED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDatum(ItemStack stack, SpellDatum<?> datum) {
|
||||
CompoundTag tag = stack.getOrCreateTag();
|
||||
|
||||
if (datum == null) {
|
||||
tag.remove(TAG_DATA);
|
||||
tag.remove(TAG_SEALED);
|
||||
} else if (!tag.getBoolean(TAG_SEALED))
|
||||
tag.put(TAG_DATA, datum.serializeToNBT());
|
||||
stack.removeTagKey(TAG_DATA);
|
||||
stack.removeTagKey(TAG_SEALED);
|
||||
} else if (!NBTHelper.getBoolean(stack, TAG_SEALED))
|
||||
NBTHelper.put(stack, TAG_DATA, datum.serializeToNBT());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,12 +4,12 @@ import at.petrak.hexcasting.HexMod;
|
|||
import at.petrak.hexcasting.api.item.DataHolderItem;
|
||||
import at.petrak.hexcasting.api.spell.DatumType;
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.common.entities.EntityWallScroll;
|
||||
import at.petrak.hexcasting.api.spell.math.HexPattern;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.common.entities.EntityWallScroll;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
|
@ -42,26 +42,23 @@ public class ItemScroll extends Item implements DataHolderItem {
|
|||
|
||||
@Override
|
||||
public @Nullable CompoundTag readDatumTag(ItemStack stack) {
|
||||
var stackTag = stack.getTag();
|
||||
if (stackTag == null || !stackTag.contains(TAG_PATTERN, Tag.TAG_COMPOUND)) {
|
||||
CompoundTag pattern = NBTHelper.getCompound(stack, TAG_PATTERN);
|
||||
if (pattern == null)
|
||||
return null;
|
||||
}
|
||||
|
||||
var patTag = stackTag.getCompound(TAG_PATTERN);
|
||||
var out = new CompoundTag();
|
||||
out.put(SpellDatum.TAG_PATTERN, patTag);
|
||||
out.put(SpellDatum.TAG_PATTERN, pattern);
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(ItemStack stack, SpellDatum<?> datum) {
|
||||
return datum != null && datum.getType() == DatumType.PATTERN && (!stack.hasTag() || !stack.getTag().contains(TAG_PATTERN, Tag.TAG_COMPOUND));
|
||||
return datum != null && datum.getType() == DatumType.PATTERN && NBTHelper.hasCompound(stack, TAG_PATTERN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDatum(ItemStack stack, SpellDatum<?> datum) {
|
||||
if (this.canWrite(stack, datum) && datum.getPayload() instanceof HexPattern pat) {
|
||||
stack.getOrCreateTag().put(TAG_PATTERN, pat.serializeToNBT());
|
||||
NBTHelper.putCompound(stack, TAG_PATTERN, pat.serializeToNBT());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,9 +78,9 @@ public class ItemScroll extends Item implements DataHolderItem {
|
|||
var scrollEntity = new EntityWallScroll(level, posInFront, direction, scrollStack);
|
||||
|
||||
// i guess
|
||||
var compoundtag = itemstack.getTag();
|
||||
if (compoundtag != null) {
|
||||
EntityType.updateCustomEntityTag(level, player, scrollEntity, compoundtag);
|
||||
var stackTag = itemstack.getTag();
|
||||
if (stackTag != null) {
|
||||
EntityType.updateCustomEntityTag(level, player, scrollEntity, stackTag);
|
||||
}
|
||||
|
||||
if (scrollEntity.survives()) {
|
||||
|
@ -106,11 +103,11 @@ public class ItemScroll extends Item implements DataHolderItem {
|
|||
|
||||
@Override
|
||||
public Component getName(ItemStack pStack) {
|
||||
var tag = pStack.getOrCreateTag();
|
||||
if (tag.contains(TAG_OP_ID, Tag.TAG_STRING)) {
|
||||
var ancientId = NBTHelper.getString(pStack, TAG_OP_ID);
|
||||
if (ancientId != null) {
|
||||
return new TranslatableComponent("item.hexcasting.scroll.of",
|
||||
new TranslatableComponent("hexcasting.spell." + ResourceLocation.tryParse(tag.getString(TAG_OP_ID))));
|
||||
} else if (tag.contains(TAG_PATTERN, Tag.TAG_COMPOUND)) {
|
||||
new TranslatableComponent("hexcasting.spell." + ResourceLocation.tryParse(ancientId)));
|
||||
} else if (NBTHelper.hasCompound(pStack, TAG_PATTERN)) {
|
||||
return new TranslatableComponent("item.hexcasting.scroll");
|
||||
} else {
|
||||
return new TranslatableComponent("item.hexcasting.scroll.empty");
|
||||
|
|
|
@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.item.DataHolderItem;
|
|||
import at.petrak.hexcasting.api.spell.DatumType;
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.spell.math.HexPattern;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.common.blocks.circles.BlockEntitySlate;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
@ -33,11 +34,10 @@ public class ItemSlate extends BlockItem implements DataHolderItem {
|
|||
}
|
||||
|
||||
public static boolean hasPattern(ItemStack stack) {
|
||||
var tag = stack.getTag();
|
||||
if (tag != null && tag.contains("BlockEntityTag", Tag.TAG_COMPOUND)) {
|
||||
var bet = tag.getCompound("BlockEntityTag");
|
||||
return bet.contains(BlockEntitySlate.TAG_PATTERN, Tag.TAG_COMPOUND) && !bet.getCompound(
|
||||
BlockEntitySlate.TAG_PATTERN).isEmpty();
|
||||
var bet = NBTHelper.getCompound(stack, "BlockEntityTag");
|
||||
if (bet != null) {
|
||||
return bet.contains(BlockEntitySlate.TAG_PATTERN, Tag.TAG_COMPOUND) &&
|
||||
!bet.getCompound(BlockEntitySlate.TAG_PATTERN).isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -45,28 +45,25 @@ public class ItemSlate extends BlockItem implements DataHolderItem {
|
|||
@Override
|
||||
public boolean onEntityItemUpdate(ItemStack stack, ItemEntity entity) {
|
||||
if (!hasPattern(stack))
|
||||
stack.removeTagKey("BlockEntityTag");
|
||||
NBTHelper.remove(stack, "BlockEntityTag");
|
||||
return super.onEntityItemUpdate(stack, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inventoryTick(ItemStack pStack, Level pLevel, Entity pEntity, int pSlotId, boolean pIsSelected) {
|
||||
if (!hasPattern(pStack))
|
||||
pStack.removeTagKey("BlockEntityTag");
|
||||
NBTHelper.remove(pStack, "BlockEntityTag");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CompoundTag readDatumTag(ItemStack stack) {
|
||||
var stackTag = stack.getTag();
|
||||
if (stackTag == null || !stackTag.contains("BlockEntityTag")) {
|
||||
return null;
|
||||
}
|
||||
var beTag = stackTag.getCompound("BlockEntityTag");
|
||||
if (!beTag.contains(BlockEntitySlate.TAG_PATTERN, Tag.TAG_COMPOUND)) {
|
||||
var bet = NBTHelper.getCompound(stack, "BlockEntityTag");
|
||||
|
||||
if (bet == null || !bet.contains(BlockEntitySlate.TAG_PATTERN, Tag.TAG_COMPOUND)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var patTag = beTag.getCompound(BlockEntitySlate.TAG_PATTERN);
|
||||
var patTag = bet.getCompound(BlockEntitySlate.TAG_PATTERN);
|
||||
if (patTag.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
@ -84,12 +81,12 @@ public class ItemSlate extends BlockItem implements DataHolderItem {
|
|||
public void writeDatum(ItemStack stack, SpellDatum<?> datum) {
|
||||
if (this.canWrite(stack, datum))
|
||||
if (datum == null) {
|
||||
var beTag = stack.getOrCreateTagElement("BlockEntityTag");
|
||||
var beTag = NBTHelper.getOrCreateCompound(stack, "BlockEntityTag");
|
||||
beTag.remove(BlockEntitySlate.TAG_PATTERN);
|
||||
if (beTag.isEmpty())
|
||||
stack.removeTagKey("BlockEntityTag");
|
||||
NBTHelper.remove(stack, "BlockEntityTag");
|
||||
} else if (datum.getPayload() instanceof HexPattern pat) {
|
||||
var beTag = stack.getOrCreateTagElement("BlockEntityTag");
|
||||
var beTag = NBTHelper.getOrCreateCompound(stack, "BlockEntityTag");
|
||||
beTag.put(BlockEntitySlate.TAG_PATTERN, pat.serializeToNBT());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package at.petrak.hexcasting.common.items;
|
|||
import at.petrak.hexcasting.api.item.DataHolderItem;
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.spell.Widget;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
@ -42,9 +43,8 @@ public class ItemSpellbook extends Item implements DataHolderItem {
|
|||
TooltipFlag isAdvanced) {
|
||||
boolean sealed = IsSealed(stack);
|
||||
boolean empty = false;
|
||||
var tag = stack.getOrCreateTag();
|
||||
if (tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
var pageIdx = tag.getInt(TAG_SELECTED_PAGE);
|
||||
if (NBTHelper.hasNumber(stack, TAG_SELECTED_PAGE)) {
|
||||
var pageIdx = NBTHelper.getInt(stack, TAG_SELECTED_PAGE);
|
||||
int highest = HighestPage(stack);
|
||||
if (highest != 0) {
|
||||
if (sealed)
|
||||
|
@ -66,7 +66,7 @@ public class ItemSpellbook extends Item implements DataHolderItem {
|
|||
}
|
||||
|
||||
if (empty) {
|
||||
boolean overridden = tag.contains(DataHolderItem.TAG_OVERRIDE_VISUALLY, Tag.TAG_STRING);
|
||||
boolean overridden = NBTHelper.hasString(stack, TAG_OVERRIDE_VISUALLY);
|
||||
if (sealed) {
|
||||
if (overridden) {
|
||||
tooltip.add(new TranslatableComponent("hexcasting.tooltip.spellbook.sealed").withStyle(ChatFormatting.GOLD));
|
||||
|
@ -86,58 +86,31 @@ public class ItemSpellbook extends Item implements DataHolderItem {
|
|||
|
||||
@Override
|
||||
public void inventoryTick(ItemStack stack, Level pLevel, Entity pEntity, int pSlotId, boolean pIsSelected) {
|
||||
var tag = stack.getOrCreateTag();
|
||||
int index;
|
||||
if (ArePagesEmpty(stack)) {
|
||||
index = 0;
|
||||
} else if (!tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
index = 1;
|
||||
} else {
|
||||
index = tag.getInt(TAG_SELECTED_PAGE);
|
||||
if (index == 0) index = 1;
|
||||
}
|
||||
tag.putInt(TAG_SELECTED_PAGE, index);
|
||||
int index = GetPage(stack, 0);
|
||||
NBTHelper.putInt(stack, TAG_SELECTED_PAGE, index);
|
||||
|
||||
int shiftedIdx = Math.max(1, index);
|
||||
String nameKey = String.valueOf(shiftedIdx);
|
||||
CompoundTag names = tag.getCompound(TAG_PAGE_NAMES);
|
||||
CompoundTag names = NBTHelper.getOrCreateCompound(stack, TAG_PAGE_NAMES);
|
||||
if (stack.hasCustomHoverName()) {
|
||||
names.putString(nameKey, Component.Serializer.toJson(stack.getHoverName()));
|
||||
} else {
|
||||
names.remove(nameKey);
|
||||
}
|
||||
tag.put(TAG_PAGE_NAMES, names);
|
||||
}
|
||||
|
||||
public static boolean ArePagesEmpty(ItemStack stack) {
|
||||
if (!stack.hasTag())
|
||||
return true;
|
||||
CompoundTag tag = stack.getTag();
|
||||
return !tag.contains(ItemSpellbook.TAG_PAGES, Tag.TAG_COMPOUND) ||
|
||||
tag.getCompound(ItemSpellbook.TAG_PAGES).isEmpty();
|
||||
CompoundTag tag = NBTHelper.getCompound(stack, TAG_PAGES);
|
||||
return tag == null || tag.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable CompoundTag readDatumTag(ItemStack stack) {
|
||||
if (!stack.hasTag()) {
|
||||
return null;
|
||||
}
|
||||
var tag = stack.getTag();
|
||||
|
||||
int idx;
|
||||
if (tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
idx = tag.getInt(TAG_SELECTED_PAGE);
|
||||
} else {
|
||||
idx = 0;
|
||||
}
|
||||
int idx = GetPage(stack, 1);
|
||||
var key = String.valueOf(idx);
|
||||
if (tag.contains(TAG_PAGES, Tag.TAG_COMPOUND)) {
|
||||
var pagesTag = tag.getCompound(TAG_PAGES);
|
||||
if (pagesTag.contains(key)) {
|
||||
return pagesTag.getCompound(key);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
var tag = NBTHelper.getCompound(stack, TAG_PAGES);
|
||||
if (tag != null && tag.contains(key, Tag.TAG_COMPOUND)) {
|
||||
return tag.getCompound(key);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
@ -158,48 +131,42 @@ public class ItemSpellbook extends Item implements DataHolderItem {
|
|||
if (datum != null && IsSealed(stack))
|
||||
return;
|
||||
|
||||
CompoundTag tag = stack.getOrCreateTag();
|
||||
|
||||
int idx;
|
||||
if (tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
idx = tag.getInt(TAG_SELECTED_PAGE);
|
||||
// But we want to write to page *1* to start if this is our first page
|
||||
if (idx == 0 && ArePagesEmpty(stack)) {
|
||||
idx = 1;
|
||||
}
|
||||
} else {
|
||||
idx = 1;
|
||||
}
|
||||
int idx = GetPage(stack, 1);
|
||||
var key = String.valueOf(idx);
|
||||
if (tag.contains(TAG_PAGES, Tag.TAG_COMPOUND)) {
|
||||
CompoundTag pages = NBTHelper.getCompound(stack, TAG_PAGES);
|
||||
if (pages != null) {
|
||||
if (datum == null) {
|
||||
tag.getCompound(TAG_PAGES).remove(key);
|
||||
tag.getCompound(TAG_SEALED).remove(key);
|
||||
pages.remove(key);
|
||||
NBTHelper.remove(NBTHelper.getCompound(stack, TAG_SEALED), key);
|
||||
} else
|
||||
tag.getCompound(TAG_PAGES).put(key, datum.serializeToNBT());
|
||||
pages.put(key, datum.serializeToNBT());
|
||||
|
||||
if (tag.getCompound(TAG_PAGES).isEmpty())
|
||||
tag.remove(TAG_SELECTED_PAGE);
|
||||
if (pages.isEmpty())
|
||||
NBTHelper.remove(stack, TAG_PAGES);
|
||||
} else if (datum != null) {
|
||||
var pagesTag = new CompoundTag();
|
||||
pagesTag.put(key, datum.serializeToNBT());
|
||||
tag.put(TAG_PAGES, pagesTag);
|
||||
NBTHelper.getOrCreateCompound(stack, TAG_PAGES).put(key, datum.serializeToNBT());
|
||||
} else {
|
||||
tag.getCompound(TAG_SEALED).remove(key);
|
||||
NBTHelper.remove(NBTHelper.getCompound(stack, TAG_SEALED), key);
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetPage(ItemStack stack, int ifEmpty) {
|
||||
if (ArePagesEmpty(stack))
|
||||
return ifEmpty;
|
||||
else if (NBTHelper.hasNumber(stack, TAG_SELECTED_PAGE)) {
|
||||
int index = NBTHelper.getInt(stack, TAG_SELECTED_PAGE);
|
||||
if (index == 0) index = 1;
|
||||
return index;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetSealed(ItemStack stack, boolean sealed) {
|
||||
CompoundTag tag = stack.getOrCreateTag();
|
||||
|
||||
int index = 1;
|
||||
if (!ArePagesEmpty(stack) && tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
index = tag.getInt(TAG_SELECTED_PAGE);
|
||||
if (index == 0) index = 1;
|
||||
}
|
||||
int index = GetPage(stack, 1);
|
||||
|
||||
String nameKey = String.valueOf(index);
|
||||
CompoundTag names = tag.getCompound(TAG_SEALED);
|
||||
CompoundTag names = NBTHelper.getOrCreateCompound(stack, TAG_SEALED);
|
||||
|
||||
if (!sealed)
|
||||
names.remove(nameKey);
|
||||
|
@ -207,85 +174,51 @@ public class ItemSpellbook extends Item implements DataHolderItem {
|
|||
names.putBoolean(nameKey, true);
|
||||
|
||||
if (names.isEmpty())
|
||||
tag.remove(TAG_SEALED);
|
||||
NBTHelper.remove(stack, TAG_SEALED);
|
||||
else
|
||||
tag.put(TAG_SEALED, names);
|
||||
NBTHelper.putCompound(stack, TAG_SEALED, names);
|
||||
|
||||
}
|
||||
|
||||
public static boolean HasDatum(ItemStack stack) {
|
||||
if (!stack.hasTag()) {
|
||||
return false;
|
||||
}
|
||||
var tag = stack.getTag();
|
||||
|
||||
int idx;
|
||||
if (tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
idx = tag.getInt(TAG_SELECTED_PAGE);
|
||||
} else {
|
||||
idx = 0;
|
||||
}
|
||||
var key = String.valueOf(idx);
|
||||
if (tag.contains(TAG_PAGES, Tag.TAG_COMPOUND)) {
|
||||
var pagesTag = tag.getCompound(TAG_PAGES);
|
||||
return pagesTag.contains(key);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean IsSealed(ItemStack stack) {
|
||||
if (!stack.hasTag())
|
||||
return false;
|
||||
CompoundTag tag = stack.getOrCreateTag();
|
||||
|
||||
int index = 1;
|
||||
if (!ArePagesEmpty(stack) && tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
index = tag.getInt(TAG_SELECTED_PAGE);
|
||||
if (index == 0) index = 1;
|
||||
}
|
||||
int index = GetPage(stack, 1);
|
||||
|
||||
String nameKey = String.valueOf(index);
|
||||
CompoundTag names = tag.getCompound(TAG_SEALED);
|
||||
return names.getBoolean(nameKey);
|
||||
CompoundTag names = NBTHelper.getCompound(stack, TAG_SEALED);
|
||||
return NBTHelper.getBoolean(names, nameKey);
|
||||
}
|
||||
|
||||
public static int HighestPage(ItemStack stack) {
|
||||
if (!stack.hasTag())
|
||||
return 0;
|
||||
CompoundTag tag = stack.getTagElement(TAG_PAGES);
|
||||
CompoundTag tag = NBTHelper.getCompound(stack, TAG_PAGES);
|
||||
if (tag == null)
|
||||
return 0;
|
||||
var highestKey = tag.getAllKeys().stream().flatMap(s -> {
|
||||
return tag.getAllKeys().stream().flatMap(s -> {
|
||||
try {
|
||||
return Stream.of(Integer.parseInt(s));
|
||||
} catch (NumberFormatException e) {
|
||||
return Stream.empty();
|
||||
}
|
||||
}).max(Integer::compare);
|
||||
return highestKey.orElse(0);
|
||||
}).max(Integer::compare).orElse(0);
|
||||
}
|
||||
|
||||
public static void RotatePageIdx(ItemStack stack, boolean increase) {
|
||||
CompoundTag tag = stack.getOrCreateTag();
|
||||
int newIdx;
|
||||
if (ArePagesEmpty(stack)) {
|
||||
newIdx = 0;
|
||||
} else if (tag.contains(TAG_SELECTED_PAGE, Tag.TAG_ANY_NUMERIC)) {
|
||||
var delta = increase ? 1 : -1;
|
||||
newIdx = Math.max(1, tag.getInt(TAG_SELECTED_PAGE) + delta);
|
||||
} else {
|
||||
newIdx = 1;
|
||||
public static int RotatePageIdx(ItemStack stack, boolean increase) {
|
||||
int idx = GetPage(stack, 0);
|
||||
if (idx != 0) {
|
||||
idx += increase ? 1 : -1;
|
||||
idx = Math.max(1, idx);
|
||||
}
|
||||
tag.putInt(TAG_SELECTED_PAGE, newIdx);
|
||||
NBTHelper.putInt(stack, TAG_SELECTED_PAGE, idx);
|
||||
|
||||
CompoundTag names = tag.getCompound(TAG_PAGE_NAMES);
|
||||
int shiftedIdx = Math.max(1, newIdx);
|
||||
CompoundTag names = NBTHelper.getCompound(stack, TAG_PAGE_NAMES);
|
||||
int shiftedIdx = Math.max(1, idx);
|
||||
String nameKey = String.valueOf(shiftedIdx);
|
||||
if (names.contains(nameKey, Tag.TAG_STRING)) {
|
||||
stack.setHoverName(Component.Serializer.fromJson(names.getString(nameKey)));
|
||||
String name = NBTHelper.getString(names, nameKey);
|
||||
if (name != null) {
|
||||
stack.setHoverName(Component.Serializer.fromJson(name));
|
||||
} else {
|
||||
stack.resetHoverName();
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ package at.petrak.hexcasting.common.items.magic;
|
|||
|
||||
import at.petrak.hexcasting.api.item.ManaHolderItem;
|
||||
import at.petrak.hexcasting.api.utils.ManaHelper;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.util.Mth;
|
||||
|
@ -26,9 +26,8 @@ public abstract class ItemManaHolder extends Item implements ManaHolderItem {
|
|||
public static ItemStack withMana(ItemStack stack, int mana, int maxMana) {
|
||||
Item item = stack.getItem();
|
||||
if (item instanceof ItemManaHolder) {
|
||||
CompoundTag tag = stack.getOrCreateTag();
|
||||
tag.putInt(TAG_MANA, mana);
|
||||
tag.putInt(TAG_MAX_MANA, maxMana);
|
||||
NBTHelper.putInt(stack, TAG_MANA, mana);
|
||||
NBTHelper.putInt(stack, TAG_MAX_MANA, maxMana);
|
||||
}
|
||||
|
||||
return stack;
|
||||
|
@ -36,21 +35,17 @@ public abstract class ItemManaHolder extends Item implements ManaHolderItem {
|
|||
|
||||
@Override
|
||||
public int getMana(ItemStack stack) {
|
||||
if (!stack.hasTag())
|
||||
return 0;
|
||||
return stack.getTag().getInt(TAG_MANA);
|
||||
return NBTHelper.getInt(stack, TAG_MANA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxMana(ItemStack stack) {
|
||||
if (!stack.hasTag())
|
||||
return 0;
|
||||
return stack.getTag().getInt(TAG_MAX_MANA);
|
||||
return NBTHelper.getInt(stack, TAG_MAX_MANA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMana(ItemStack stack, int mana) {
|
||||
stack.getOrCreateTag().putInt(TAG_MANA, Mth.clamp(mana, 0, getMaxMana(stack)));
|
||||
NBTHelper.putInt(stack, TAG_MANA, Mth.clamp(mana, 0, getMaxMana(stack)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,6 +4,7 @@ import at.petrak.hexcasting.HexMod;
|
|||
import at.petrak.hexcasting.api.item.SpellHolderItem;
|
||||
import at.petrak.hexcasting.api.spell.casting.CastingContext;
|
||||
import at.petrak.hexcasting.api.spell.casting.CastingHarness;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.common.lib.HexSounds;
|
||||
import at.petrak.hexcasting.api.spell.math.HexPattern;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
@ -50,14 +51,12 @@ public abstract class ItemPackagedSpell extends ItemManaHolder implements SpellH
|
|||
|
||||
@Override
|
||||
public @Nullable List<HexPattern> getPatterns(ItemStack stack) {
|
||||
if (!stack.hasTag())
|
||||
return null;
|
||||
CompoundTag tag = stack.getTag();
|
||||
if (!tag.contains(TAG_PATTERNS, Tag.TAG_LIST))
|
||||
var patsTag = NBTHelper.getList(stack, TAG_PATTERNS, Tag.TAG_COMPOUND);
|
||||
|
||||
if (patsTag == null)
|
||||
return null;
|
||||
|
||||
var out = new ArrayList<HexPattern>();
|
||||
var patsTag = tag.getList(TAG_PATTERNS, Tag.TAG_COMPOUND);
|
||||
for (var patTag : patsTag) {
|
||||
out.add(HexPattern.DeserializeFromNBT((CompoundTag) patTag));
|
||||
}
|
||||
|
@ -70,14 +69,14 @@ public abstract class ItemPackagedSpell extends ItemManaHolder implements SpellH
|
|||
for (HexPattern pat : patterns)
|
||||
patsTag.add(pat.serializeToNBT());
|
||||
|
||||
stack.getOrCreateTag().put(ItemPackagedSpell.TAG_PATTERNS, patsTag);
|
||||
NBTHelper.putList(stack, TAG_PATTERNS, patsTag);
|
||||
|
||||
withMana(stack, mana, mana);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearPatterns(ItemStack stack) {
|
||||
stack.removeTagKey(ItemPackagedSpell.TAG_PATTERNS);
|
||||
NBTHelper.remove(stack, ItemPackagedSpell.TAG_PATTERNS);
|
||||
withMana(stack, 0, 0);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package at.petrak.hexcasting.common.network;
|
||||
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.common.items.HexItems;
|
||||
import at.petrak.hexcasting.common.items.ItemAbacus;
|
||||
import at.petrak.hexcasting.common.items.ItemSpellbook;
|
||||
|
@ -56,10 +57,8 @@ public record MsgShiftScrollSyn(InteractionHand hand, double scrollDelta, boolea
|
|||
}
|
||||
|
||||
private void spellbook(ServerPlayer sender, ItemStack stack) {
|
||||
var tag = stack.getOrCreateTag();
|
||||
ItemSpellbook.RotatePageIdx(stack, this.scrollDelta < 0.0);
|
||||
var newIdx = ItemSpellbook.RotatePageIdx(stack, this.scrollDelta < 0.0);
|
||||
|
||||
var newIdx = tag.getInt(ItemSpellbook.TAG_SELECTED_PAGE);
|
||||
var len = ItemSpellbook.HighestPage(stack);
|
||||
|
||||
var sealed = ItemSpellbook.IsSealed(stack);
|
||||
|
@ -95,11 +94,7 @@ public record MsgShiftScrollSyn(InteractionHand hand, double scrollDelta, boolea
|
|||
|
||||
private void abacus(ServerPlayer sender, ItemStack stack) {
|
||||
var increase = this.scrollDelta < 0;
|
||||
var tag = stack.getTag();
|
||||
double num = 0d;
|
||||
if (tag != null) {
|
||||
num = tag.getDouble(ItemAbacus.TAG_VALUE);
|
||||
}
|
||||
double num = NBTHelper.getDouble(stack, ItemAbacus.TAG_VALUE);
|
||||
|
||||
double delta;
|
||||
float pitch;
|
||||
|
@ -112,7 +107,7 @@ public record MsgShiftScrollSyn(InteractionHand hand, double scrollDelta, boolea
|
|||
}
|
||||
|
||||
num += delta * (increase ? 1 : -1);
|
||||
stack.getOrCreateTag().putDouble(ItemAbacus.TAG_VALUE, num);
|
||||
NBTHelper.putDouble(stack, ItemAbacus.TAG_VALUE, num);
|
||||
|
||||
pitch *= (increase ? 1.05f : 0.95f);
|
||||
pitch += (Math.random() - 0.5) * 0.1;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package at.petrak.hexcasting.common.recipe;
|
||||
|
||||
import at.petrak.hexcasting.api.item.DataHolderItem;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.common.items.HexItems;
|
||||
import at.petrak.hexcasting.common.items.ItemFocus;
|
||||
import at.petrak.hexcasting.common.recipe.ingredient.UnsealedIngredient;
|
||||
|
@ -21,8 +22,8 @@ public class SealFocusRecipe extends ShapelessRecipe {
|
|||
|
||||
private static ItemStack getSealedStack() {
|
||||
ItemStack output = new ItemStack(HexItems.FOCUS::get);
|
||||
output.getOrCreateTag().putBoolean(ItemFocus.TAG_SEALED, true);
|
||||
output.getOrCreateTag().putString(DataHolderItem.TAG_OVERRIDE_VISUALLY, "any");
|
||||
NBTHelper.putBoolean(output, ItemFocus.TAG_SEALED, true);
|
||||
NBTHelper.putString(output, DataHolderItem.TAG_OVERRIDE_VISUALLY, "any");
|
||||
return output;
|
||||
}
|
||||
|
||||
|
@ -50,7 +51,7 @@ public class SealFocusRecipe extends ShapelessRecipe {
|
|||
}
|
||||
|
||||
if (!out.isEmpty()) {
|
||||
out.getOrCreateTag().putBoolean(ItemFocus.TAG_SEALED, true);
|
||||
NBTHelper.putBoolean(out, ItemFocus.TAG_SEALED, true);
|
||||
out.setCount(1);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package at.petrak.hexcasting.common.recipe;
|
||||
|
||||
import at.petrak.hexcasting.api.item.DataHolderItem;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import at.petrak.hexcasting.common.items.HexItems;
|
||||
import at.petrak.hexcasting.common.items.ItemSpellbook;
|
||||
import at.petrak.hexcasting.common.recipe.ingredient.UnsealedIngredient;
|
||||
|
@ -22,7 +23,7 @@ public class SealSpellbookRecipe extends ShapelessRecipe {
|
|||
private static ItemStack getSealedStack() {
|
||||
ItemStack output = new ItemStack(HexItems.SPELLBOOK::get);
|
||||
ItemSpellbook.SetSealed(output, true);
|
||||
output.getOrCreateTag().putString(DataHolderItem.TAG_OVERRIDE_VISUALLY, "any");
|
||||
NBTHelper.putString(output, DataHolderItem.TAG_OVERRIDE_VISUALLY, "any");
|
||||
return output;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import at.petrak.hexcasting.api.item.DataHolderItem;
|
|||
import at.petrak.hexcasting.api.spell.DatumType;
|
||||
import at.petrak.hexcasting.api.spell.SpellDatum;
|
||||
import at.petrak.hexcasting.api.spell.Widget;
|
||||
import at.petrak.hexcasting.api.utils.NBTHelper;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
|
@ -30,7 +31,7 @@ public class UnsealedIngredient extends AbstractIngredient {
|
|||
.filter((it) -> it != DatumType.EMPTY && it != DatumType.OTHER)
|
||||
.map((type) -> {
|
||||
ItemStack newStack = stack.copy();
|
||||
newStack.getOrCreateTag().putString(DataHolderItem.TAG_OVERRIDE_VISUALLY, SpellDatum.GetTagName(type));
|
||||
NBTHelper.putString(newStack, DataHolderItem.TAG_OVERRIDE_VISUALLY, SpellDatum.GetTagName(type));
|
||||
return new Ingredient.ItemValue(newStack);
|
||||
}));
|
||||
this.stack = stack;
|
||||
|
|
Loading…
Reference in a new issue