Skip to content

Commit

Permalink
[endec] completely remove NbtKey (superseded by KeyedEndec)
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed Dec 5, 2023
1 parent fc48514 commit 6ecc469
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 324 deletions.
31 changes: 2 additions & 29 deletions src/main/java/io/wispforest/owo/mixin/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.nbt.NbtCarrier;
import io.wispforest.owo.nbt.NbtKey;
import io.wispforest.owo.serialization.MapCarrier;
import io.wispforest.owo.serialization.impl.KeyedEndec;
import io.wispforest.owo.serialization.impl.forwarding.ForwardingDeserializer;
Expand All @@ -17,42 +15,17 @@

@SuppressWarnings("AddedMixinMembersNamePattern")
@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements NbtCarrier, MapCarrier {
public abstract class ItemStackMixin implements MapCarrier {

@Shadow
private @Nullable NbtCompound nbt;

@Shadow
public abstract NbtCompound getOrCreateNbt();

// --- NbtCarrier (deprecated) ---

@Override
public <T> T get(@NotNull NbtKey<T> key) {
return key.get(this.getOrCreateNbt());
}

@Override
public <T> void put(@NotNull NbtKey<T> key, @NotNull T value) {
key.put(this.getOrCreateNbt(), value);
}

@Override
public <T> void delete(@NotNull NbtKey<T> key) {
if (this.nbt == null) return;
key.delete(this.nbt);
}

@Override
public <T> boolean has(@NotNull NbtKey<T> key) {
return this.nbt != null && key.isIn(this.nbt);
}

// --- MapCarrier ---

@Override
public <T> T getWithErrors(@NotNull KeyedEndec<T> key) {
if(!this.has(key)) return key.defaultValue();
if (!this.has(key)) return key.defaultValue();
return key.endec()
.decodeFully(e -> ForwardingDeserializer.humanReadable(NbtDeserializer.of(e)), this.nbt.get(key.key()));
}
Expand Down
30 changes: 2 additions & 28 deletions src/main/java/io/wispforest/owo/mixin/NbtCompoundMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.nbt.NbtCarrier;
import io.wispforest.owo.nbt.NbtKey;
import io.wispforest.owo.serialization.MapCarrier;
import io.wispforest.owo.serialization.impl.KeyedEndec;
import io.wispforest.owo.serialization.impl.forwarding.ForwardingDeserializer;
Expand All @@ -17,7 +15,7 @@

@SuppressWarnings("AddedMixinMembersNamePattern")
@Mixin(NbtCompound.class)
public abstract class NbtCompoundMixin implements NbtCarrier, MapCarrier {
public abstract class NbtCompoundMixin implements MapCarrier {

@Shadow
public abstract @Nullable NbtElement get(String key);
Expand All @@ -28,33 +26,9 @@ public abstract class NbtCompoundMixin implements NbtCarrier, MapCarrier {
@Shadow
public abstract boolean contains(String key);

// --- NbtCarrier (deprecated) ---

@Override
public <T> T get(@NotNull NbtKey<T> key) {
return key.get((NbtCompound) (Object) this);
}

@Override
public <T> void put(@NotNull NbtKey<T> key, @NotNull T value) {
key.put((NbtCompound) (Object) this, value);
}

@Override
public <T> void delete(@NotNull NbtKey<T> key) {
key.delete((NbtCompound) (Object) this);
}

@Override
public <T> boolean has(@NotNull NbtKey<T> key) {
return key.isIn((NbtCompound) (Object) this);
}

// --- MapCarrier ---

@Override
public <T> T getWithErrors(@NotNull KeyedEndec<T> key) {
if(!this.has(key)) return key.defaultValue();
if (!this.has(key)) return key.defaultValue();
return key.endec()
.decodeFully(e -> ForwardingDeserializer.humanReadable(NbtDeserializer.of(e)), this.get(key.key()));
}
Expand Down
52 changes: 0 additions & 52 deletions src/main/java/io/wispforest/owo/nbt/NbtCarrier.java

This file was deleted.

200 changes: 0 additions & 200 deletions src/main/java/io/wispforest/owo/nbt/NbtKey.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interface Struct {
* Decode the value of field {@code name} using {@code endec}. If no
* such field exists in the serialized data, an exception is thrown
*/
<F> @NotNull F field(String name, Endec<F> endec);
<F> @Nullable F field(String name, Endec<F> endec);

/**
* Decode the value of field {@code name} using {@code endec}. If no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ public V next() {
}

@Override
public <F> @NotNull F field(String name, Endec<F> endec) {
public <F> @Nullable F field(String name, Endec<F> endec) {
return this.field(name, endec, null);
}

@Override
public <F> @NotNull F field(String name, Endec<F> endec, @Nullable F defaultValue) {
public <F> @Nullable F field(String name, Endec<F> endec, @Nullable F defaultValue) {
return endec.decode(ByteBufDeserializer.this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ public V next() {
}

@Override
public <F> @NotNull F field(String name, Endec<F> endec) {
public <F> @Nullable F field(String name, Endec<F> endec) {
return endec.decode(DataInputDeserializer.this);
}

@Override
public <F> F field(@Nullable String field, Endec<F> endec, @Nullable F defaultValue) {
public <F> @Nullable F field(@Nullable String field, Endec<F> endec, @Nullable F defaultValue) {
return endec.decode(DataInputDeserializer.this);
}
}
Expand Down
Loading

0 comments on commit 6ecc469

Please sign in to comment.