Skip to content

Commit

Permalink
switch everything to use codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
Master-Bw3 committed Aug 29, 2024
1 parent 5148240 commit 9eea447
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ADIotaHolder {
default Iota readIota(ServerLevel world) {
var tag = readIotaTag();
if (tag != null) {
return HexUtils.deserializeWithCodec(tag, Iota.getCodec(world));
return HexUtils.deserializeWithCodec(tag, Iota.CODEC.get().codec());
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package at.petrak.hexcasting.api.casting.eval

import at.petrak.hexcasting.api.HexAPI
import at.petrak.hexcasting.api.casting.math.HexCoord
import at.petrak.hexcasting.api.casting.math.HexPattern
import at.petrak.hexcasting.api.utils.NBTBuilder
import at.petrak.hexcasting.api.utils.deserializeWithCodec
import at.petrak.hexcasting.api.utils.serializeWithCodec
import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtOps
import java.util.*


Expand All @@ -23,12 +27,7 @@ data class ResolvedPattern(val pattern: HexPattern, val origin: HexCoord, var ty
get() = origin.r

@Deprecated("Use the CODEC instead.")
fun serializeToNBT() = NBTBuilder {
"Pattern" %= pattern.serializeToNBT()
"OriginQ" %= origin.q
"OriginR" %= origin.r
"Valid" %= type.name.lowercase(Locale.ROOT)
}
fun serializeToNBT() = this.serializeWithCodec(CODEC)

companion object {
@JvmField
Expand All @@ -45,11 +44,6 @@ data class ResolvedPattern(val pattern: HexPattern, val origin: HexCoord, var ty

@Deprecated("Use the CODEC instead.")
@JvmStatic
fun fromNBT(tag: CompoundTag): ResolvedPattern {
val pattern = HexPattern.fromNBT(tag.getCompound("Pattern"))
val origin = HexCoord(tag.getInt("OriginQ"), tag.getInt("OriginR"))
val valid = ResolvedPatternType.fromString(tag.getString("Valid"))
return ResolvedPattern(pattern, origin, valid)
}
fun fromNBT(tag: CompoundTag): ResolvedPattern = tag.deserializeWithCodec(CODEC)!!
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.petrak.hexcasting.api.casting.mishaps

import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
import at.petrak.hexcasting.api.casting.iota.GarbageIota
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.pigment.FrozenPigment
import at.petrak.hexcasting.api.utils.aqua
Expand All @@ -10,13 +11,32 @@ import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.item.ItemEntity
import net.minecraft.world.item.DyeColor

class MishapEntityNotFound : Mishap() {
class MishapEntityNotFound(
val perpetrator: Iota,
val reverseIdx: Int,
) : Mishap() {
override fun accentColor(ctx: CastingEnvironment, errorCtx: Context): FrozenPigment =
dyeColor(DyeColor.BROWN)

override fun execute(env: CastingEnvironment, errorCtx: Context, stack: MutableList<Iota>) {
stack[stack.size - 1 - reverseIdx] = GarbageIota();
}

override fun errorMessage(ctx: CastingEnvironment, errorCtx: Context) =
error("entity_not_found")
error(
"entity_not_found", reverseIdx,
perpetrator.display()
)
companion object {
@JvmStatic
fun ofType(perpetrator: Iota, reverseIdx: Int): MishapEntityNotFound {
return of(perpetrator, reverseIdx)
}

@JvmStatic
fun of(perpetrator: Iota, reverseIdx: Int): MishapEntityNotFound {
val key = "hexcasting.mishap.entity_not_found"
return MishapEntityNotFound(perpetrator, reverseIdx)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,14 @@ class MishapOthersName(val confidant: Player) : Mishap() {
* If `caster` is non-null, it will ignore that when checking.
*/
@JvmStatic
fun getTrueNameFromDatum(datum: Iota, caster: Player?, world: ServerLevel): Player? {
fun getTrueNameFromDatum(datum: Iota, caster: Player?): Player? {
val poolToSearch = ArrayDeque<Iota>()
poolToSearch.addLast(datum)

while (poolToSearch.isNotEmpty()) {
val datumToCheck = poolToSearch.removeFirst()
if (datumToCheck is EntityIota && datumToCheck.isTrueName) {
val player = datumToCheck.getEntity(world) as Player
if (player != caster)
return player
}
if (datumToCheck is EntityIota && datumToCheck.uuid != caster?.uuid)
return datumToCheck.getEntity(caster?.level() as ServerLevel) as? Player
val datumSubIotas = datumToCheck.subIotas()
if (datumSubIotas != null)
poolToSearch.addAll(datumSubIotas)
Expand All @@ -54,8 +51,8 @@ class MishapOthersName(val confidant: Player) : Mishap() {
}

@JvmStatic
fun getTrueNameFromArgs(datums: List<Iota>, caster: Player?, world: ServerLevel): Player? {
return datums.firstNotNullOfOrNull { getTrueNameFromDatum(it, caster, world) }
fun getTrueNameFromArgs(datums: List<Iota>, caster: Player?): Player? {
return datums.firstNotNullOfOrNull { getTrueNameFromDatum(it, caster) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ default Iota readIota(ItemStack stack, ServerLevel world) {

var tag = dh.readIotaTag(stack);
if (tag != null) {
return HexUtils.deserializeWithCodec(tag, Iota.getCodec(world));
return HexUtils.deserializeWithCodec(tag, Iota.CODEC.get().codec());
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CompoundTag getIotaTag() {
public void setNewMapping(HexPattern pattern, Iota iota) {
var previouslyEmpty = this.pattern == null;
this.pattern = pattern;
this.iotaTag = (CompoundTag) HexUtils.serializeWithCodec(iota, Iota.getCodec());
this.iotaTag = (CompoundTag) HexUtils.serializeWithCodec(iota, Iota.CODEC.get().codec());

if (previouslyEmpty) {
var oldBs = this.getBlockState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import at.petrak.hexcasting.api.block.HexBlockEntity;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.client.render.HexPatternPoints;
import at.petrak.hexcasting.common.lib.HexBlockEntities;
import net.minecraft.core.BlockPos;
Expand All @@ -10,6 +11,8 @@
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class BlockEntitySlate extends HexBlockEntity {
public static final String TAG_PATTERN = "pattern";

Expand All @@ -24,7 +27,7 @@ public BlockEntitySlate(BlockPos pos, BlockState state) {
@Override
protected void saveModData(CompoundTag tag) {
if (this.pattern != null) {
tag.put(TAG_PATTERN, this.pattern.serializeToNBT());
tag.put(TAG_PATTERN, Objects.requireNonNull(HexUtils.serializeWithCodec(this.pattern, HexPattern.CODEC)));
} else {
tag.put(TAG_PATTERN, new CompoundTag());
}
Expand All @@ -35,7 +38,7 @@ protected void loadModData(CompoundTag tag) {
if (tag.contains(TAG_PATTERN, Tag.TAG_COMPOUND)) {
CompoundTag patternTag = tag.getCompound(TAG_PATTERN);
if (HexPattern.isPattern(patternTag)) {
this.pattern = HexPattern.fromNBT(patternTag);
this.pattern = HexUtils.deserializeWithCodec(patternTag, HexPattern.CODEC);
} else {
this.pattern = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Pla
if (datumContainer != null) {
var stored = datumContainer.readIota(level);
if (stored instanceof EntityIota eieio) {
var entity = eieio.getEntity();
var entity = eieio.getEntity((ServerLevel) pLevel);
if (entity instanceof Player player) {
// phew, we got something
tile.setPlayer(player.getGameProfile(), entity.getUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static int giveAll(CommandSourceStack source, Collection<ServerPlayer> t

var tag = new CompoundTag();
tag.putString(ItemScroll.TAG_OP_ID, key.location().toString());
tag.put(ItemScroll.TAG_PATTERN, pat.serializeToNBT());
tag.put(ItemScroll.TAG_PATTERN, HexUtils.serializeWithCodec(pat, HexPattern.CODEC));

var stack = new ItemStack(HexItems.SCROLL_LARGE);
stack.setTag(tag);
Expand Down Expand Up @@ -134,7 +134,7 @@ private static int giveOne(CommandSourceStack source, Collection<ServerPlayer> t
if (!targets.isEmpty()) {
var tag = new CompoundTag();
tag.putString(ItemScroll.TAG_OP_ID, patternName.toString());
tag.put(ItemScroll.TAG_PATTERN, pat.serializeToNBT());
tag.put(ItemScroll.TAG_PATTERN, HexUtils.serializeWithCodec(pat, HexPattern.CODEC));

var stack = new ItemStack(HexItems.SCROLL_LARGE);
stack.setTag(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public EntityWallScroll(Level world, BlockPos pos, Direction dir, ItemStack scro
public void recalculateDisplay() {
CompoundTag patternTag = NBTHelper.getCompound(scroll, ItemScroll.TAG_PATTERN);
if (patternTag != null) {
this.pattern = HexPattern.fromNBT(patternTag);
this.pattern = HexUtils.deserializeWithCodec(patternTag, HexPattern.CODEC);
if (this.level().isClientSide) {
var pair = RenderLib.getCenteredPattern(pattern, 128f / 3 * blockSize, 128f / 3 * blockSize,
16f / 3 * blockSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean hasHex(ItemStack stack) {
var out = new ArrayList<Iota>();
for (var patTag : patsTag) {
CompoundTag tag = NBTHelper.getAsCompound(patTag);
out.add(HexUtils.deserializeWithCodec(tag, Iota.getCodec(level)));
out.add(HexUtils.deserializeWithCodec(tag, Iota.CODEC.get().codec()));
}
return out;
}
Expand All @@ -86,7 +86,7 @@ public boolean hasHex(ItemStack stack) {
public void writeHex(ItemStack stack, List<Iota> program, @Nullable FrozenPigment pigment, long media) {
ListTag patsTag = new ListTag();
for (Iota pat : program) {
patsTag.add(HexUtils.serializeWithCodec(pat, Iota.getCodec()));
patsTag.add(HexUtils.serializeWithCodec(pat, Iota.CODEC.get().codec()));
}

NBTHelper.putList(stack, TAG_PROGRAM, patsTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ItemAbacus(Properties pProperties) {
public @Nullable
CompoundTag readIotaTag(ItemStack stack) {
var datum = new DoubleIota(NBTHelper.getDouble(stack, TAG_VALUE));
return (CompoundTag) HexUtils.serializeWithCodec(datum, Iota.getCodec());
return (CompoundTag) HexUtils.serializeWithCodec(datum, Iota.CODEC.get().codec());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void writeDatum(ItemStack stack, Iota datum) {
stack.removeTagKey(TAG_DATA);
stack.removeTagKey(TAG_SEALED);
} else if (!isSealed(stack)) {
NBTHelper.put(stack, TAG_DATA, HexUtils.serializeWithCodec(datum, Iota.getCodec()));
NBTHelper.put(stack, TAG_DATA, HexUtils.serializeWithCodec(datum, Iota.CODEC.get().codec()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import at.petrak.hexcasting.api.casting.iota.PatternIota;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.item.IotaHolderItem;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.api.utils.NBTHelper;
import at.petrak.hexcasting.client.gui.PatternTooltipComponent;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
Expand Down Expand Up @@ -77,7 +78,7 @@ public boolean canWrite(ItemStack stack, Iota datum) {
public void writeDatum(ItemStack stack, Iota datum) {
if (this.canWrite(stack, datum)) {
if (datum instanceof PatternIota pat) {
NBTHelper.putCompound(stack, TAG_PATTERN, pat.getPattern().serializeToNBT());
NBTHelper.put(stack, TAG_PATTERN, HexUtils.serializeWithCodec(pat.getPattern(), HexPattern.CODEC));
} else if (datum == null) {
NBTHelper.remove(stack, TAG_PATTERN);
}
Expand Down Expand Up @@ -144,7 +145,7 @@ public Component getName(ItemStack pStack) {
public Optional<TooltipComponent> getTooltipImage(ItemStack stack) {
var compound = NBTHelper.getCompound(stack, ItemScroll.TAG_PATTERN);
if (compound != null) {
var pattern = HexPattern.fromNBT(compound);
var pattern = HexUtils.deserializeWithCodec(compound, HexPattern.CODEC);
return Optional.of(new PatternTooltip(
pattern,
NBTHelper.hasString(stack, ItemScroll.TAG_OP_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import at.petrak.hexcasting.api.casting.iota.PatternIota;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.item.IotaHolderItem;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.api.utils.NBTHelper;
import at.petrak.hexcasting.client.gui.PatternTooltipComponent;
import at.petrak.hexcasting.common.blocks.circles.BlockEntitySlate;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void writeDatum(ItemStack stack, Iota datum) {
}
} else if (datum instanceof PatternIota pat) {
var beTag = NBTHelper.getOrCreateCompound(stack, "BlockEntityTag");
beTag.put(BlockEntitySlate.TAG_PATTERN, pat.getPattern().serializeToNBT());
beTag.put(BlockEntitySlate.TAG_PATTERN, HexUtils.serializeWithCodec(pat.getPattern(), HexPattern.CODEC));
}
}
}
Expand All @@ -117,7 +118,7 @@ public Optional<TooltipComponent> getTooltipImage(ItemStack stack) {
if (bet != null && bet.contains(BlockEntitySlate.TAG_PATTERN, Tag.TAG_COMPOUND)) {
var patTag = bet.getCompound(BlockEntitySlate.TAG_PATTERN);
if (!patTag.isEmpty()) {
var pattern = HexPattern.fromNBT(patTag);
var pattern = HexUtils.deserializeWithCodec(patTag, HexPattern.CODEC);
return Optional.of(new PatternTooltip(pattern, PatternTooltipComponent.SLATE_BG));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ public void writeDatum(ItemStack stack, Iota datum) {
pages.remove(key);
NBTHelper.remove(NBTHelper.getCompound(stack, TAG_SEALED), key);
} else {
pages.put(key, HexUtils.serializeWithCodec(datum, Iota.getCodec()));
pages.put(key, HexUtils.serializeWithCodec(datum, Iota.CODEC.get().codec()));
}

if (pages.isEmpty()) {
NBTHelper.remove(stack, TAG_PAGES);
}
} else if (datum != null) {
NBTHelper.getOrCreateCompound(stack, TAG_PAGES).put(key, HexUtils.serializeWithCodec(datum, Iota.getCodec()));
NBTHelper.getOrCreateCompound(stack, TAG_PAGES).put(key, HexUtils.serializeWithCodec(datum, Iota.CODEC.get().codec()));
} else {
NBTHelper.remove(NBTHelper.getCompound(stack, TAG_SEALED), key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public boolean canWrite(ItemStack stack, @Nullable Iota iota) {
@Override
public void writeDatum(ItemStack stack, @Nullable Iota iota) {
if (iota != null) {
NBTHelper.put(stack, TAG_DATA, HexUtils.serializeWithCodec(iota, Iota.getCodec()));
NBTHelper.put(stack, TAG_DATA, HexUtils.serializeWithCodec(iota, Iota.CODEC.get().codec()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.petrak.hexcasting.common.loot;

import at.petrak.hexcasting.api.casting.ActionRegistryEntry;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.mod.HexTags;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.common.casting.PatternRegistryManifest;
Expand Down Expand Up @@ -49,7 +50,7 @@ public static ItemStack doStatic(ItemStack stack, LootContext ctx) {
var pat = PatternRegistryManifest.getCanonicalStrokesPerWorld(key, ctx.getLevel().getServer().overworld());
var tag = new CompoundTag();
tag.putString(ItemScroll.TAG_OP_ID, key.location().toString());
tag.put(ItemScroll.TAG_PATTERN, pat.serializeToNBT());
tag.put(ItemScroll.TAG_PATTERN, HexUtils.serializeWithCodec(pat, HexPattern.CODEC));

stack.getOrCreateTag().merge(tag);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import at.petrak.hexcasting.api.casting.eval.ResolvedPattern;
import at.petrak.hexcasting.api.casting.eval.env.StaffCastEnv;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.utils.HexUtils;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -32,23 +34,23 @@ public ResourceLocation getFabricId() {
public static MsgNewSpellPatternC2S deserialize(ByteBuf buffer) {
var buf = new FriendlyByteBuf(buffer);
var hand = buf.readEnum(InteractionHand.class);
var pattern = HexPattern.fromNBT(buf.readNbt());
var pattern = HexUtils.deserializeWithCodec(buf.readNbt(), HexPattern.CODEC);

var resolvedPatternsLen = buf.readInt();
var resolvedPatterns = new ArrayList<ResolvedPattern>(resolvedPatternsLen);
for (int i = 0; i < resolvedPatternsLen; i++) {
resolvedPatterns.add(ResolvedPattern.fromNBT(buf.readNbt()));
resolvedPatterns.add(HexUtils.deserializeWithCodec(buf.readNbt(), ResolvedPattern.CODEC));
}
return new MsgNewSpellPatternC2S(hand, pattern, resolvedPatterns);
}

@Override
public void serialize(FriendlyByteBuf buf) {
buf.writeEnum(handUsed);
buf.writeNbt(this.pattern.serializeToNBT());
buf.writeNbt((CompoundTag) HexUtils.serializeWithCodec(this.pattern, HexPattern.CODEC));
buf.writeInt(this.resolvedPatterns.size());
for (var pat : this.resolvedPatterns) {
buf.writeNbt(pat.serializeToNBT());
buf.writeNbt((CompoundTag) HexUtils.serializeWithCodec(pat, ResolvedPattern.CODEC));
}
}

Expand Down
Loading

0 comments on commit 9eea447

Please sign in to comment.