Skip to content

Commit

Permalink
Use registry ops correctly and translate tags
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed Jun 28, 2024
1 parent f9b10a2 commit 515b62b
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 25 deletions.
2 changes: 2 additions & 0 deletions changelog/3.0.1+1.21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Fixed crashes when saving drawers with enchanted items
* Added translations to item tags
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minecraft_version=1.21
yarn_mappings=1.21+build.2
loader_version=0.15.11

mod_version=3.0.0
mod_version=3.0.1
maven_group=io.github.mattidragon
archives_base_name=ExtendedDrawers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public boolean isEmpty() {

@Override
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
storage.readNbt(nbt.getCompound("storage"));
storage.readNbt(nbt.getCompound("storage"), registryLookup);
}

@Override
public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
var storageNbt = new NbtCompound();
storage.writeNbt(storageNbt);
storage.writeNbt(storageNbt, registryLookup);
nbt.put("storage", storageNbt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public boolean isEmpty() {
protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
var list = nbt.getList("items", NbtElement.COMPOUND_TYPE).stream().map(NbtCompound.class::cast).toList();
for (int i = 0; i < list.size(); i++) {
storages[i].readNbt(list.get(i));
storages[i].readNbt(list.get(i), registryLookup);
}
sortSlots();
}
Expand All @@ -113,7 +113,7 @@ public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLook
var list = new NbtList();
for (var storage : storages) {
var storageNbt = new NbtCompound();
storage.writeNbt(storageNbt);
storage.writeNbt(storageNbt, registryLookup);
list.add(storageNbt);
}
nbt.put("items", list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -83,13 +84,13 @@ public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryL
@Override
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
if (nbt.contains("count")) countCache = nbt.getLong("count");
item = ItemVariant.CODEC.parse(NbtOps.INSTANCE, nbt.getCompound("item")).getOrThrow();
item = ItemVariant.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, registryLookup), nbt.getCompound("item")).getOrThrow();
hidden = nbt.getBoolean("hidden");
}

@Override
public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
nbt.put("item", ItemVariant.CODEC.encodeStart(NbtOps.INSTANCE, item).getOrThrow());
nbt.put("item", ItemVariant.CODEC.encodeStart(RegistryOps.of(NbtOps.INSTANCE, registryLookup), item).getOrThrow());
}

public boolean isHidden() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
Expand Down Expand Up @@ -180,18 +182,18 @@ public void setLocked(boolean locked) {
}

@Override
public void readNbt(NbtCompound nbt) {
DrawerStorage.super.readNbt(nbt);
item = ItemVariant.CODEC.parse(NbtOps.INSTANCE, nbt.getCompound("item")).getOrThrow();
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
DrawerStorage.super.readNbt(nbt, registryLookup);
item = ItemVariant.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, registryLookup), nbt.getCompound("item")).getOrThrow();
amount = nbt.getLong("amount");
if (item.isBlank()) amount = 0; // Avoids dupes with drawers of removed items
updatePending = true;
}

@Override
public void writeNbt(NbtCompound nbt) {
DrawerStorage.super.writeNbt(nbt);
nbt.put("item", ItemVariant.CODEC.encodeStart(NbtOps.INSTANCE, item).getOrThrow());
public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
DrawerStorage.super.writeNbt(nbt, registryLookup);
nbt.put("item", ItemVariant.CODEC.encodeStart(RegistryOps.of(NbtOps.INSTANCE, registryLookup), item).getOrThrow());
nbt.putLong("amount", amount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
Expand Down Expand Up @@ -170,17 +172,17 @@ public void dumpExcess(World world, BlockPos pos, @Nullable Direction side, @Nul
}

@Override
public void readNbt(NbtCompound nbt) {
DrawerStorage.super.readNbt(nbt);
item = ItemVariant.CODEC.parse(NbtOps.INSTANCE, nbt.getCompound("item")).getOrThrow();
public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
DrawerStorage.super.readNbt(nbt, registryLookup);
item = ItemVariant.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, registryLookup), nbt.getCompound("item")).getOrThrow();
amount = nbt.getLong("amount");
if (item.isBlank()) amount = 0; // Avoids dupes with drawers of removed items
}

@Override
public void writeNbt(NbtCompound nbt) {
DrawerStorage.super.writeNbt(nbt);
nbt.put("item", ItemVariant.CODEC.encodeStart(NbtOps.INSTANCE, item).getOrThrow());
public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
DrawerStorage.super.writeNbt(nbt, registryLookup);
nbt.put("item", ItemVariant.CODEC.encodeStart(RegistryOps.of(NbtOps.INSTANCE, registryLookup), item).getOrThrow());
nbt.putLong("amount", amount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
Expand Down Expand Up @@ -92,22 +94,22 @@ default int compareTo(@NotNull DrawerStorage other) {

void dumpExcess(World world, BlockPos pos, @Nullable Direction side, @Nullable PlayerEntity player);

default void readNbt(NbtCompound nbt) {
default void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
settings().locked = nbt.getBoolean("locked");
settings().voiding = nbt.getBoolean("voiding");
settings().hidden = nbt.getBoolean("hidden");
settings().duping = nbt.getBoolean("duping");
settings().upgrade = ItemVariant.CODEC.parse(NbtOps.INSTANCE, nbt.getCompound("capacityUpgrade")).getOrThrow();
settings().limiter = ItemVariant.CODEC.parse(NbtOps.INSTANCE, nbt.getCompound("limiter")).getOrThrow();
settings().upgrade = ItemVariant.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, registryLookup), nbt.getCompound("capacityUpgrade")).getOrThrow();
settings().limiter = ItemVariant.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, registryLookup), nbt.getCompound("limiter")).getOrThrow();
}

default void writeNbt(NbtCompound nbt) {
default void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
nbt.putBoolean("locked", settings().locked);
nbt.putBoolean("voiding", settings().voiding);
nbt.putBoolean("hidden", settings().hidden);
nbt.putBoolean("duping", settings().duping);
nbt.put("capacityUpgrade", ItemVariant.CODEC.encodeStart(NbtOps.INSTANCE, settings().upgrade).getOrThrow());
nbt.put("limiter", ItemVariant.CODEC.encodeStart(NbtOps.INSTANCE, settings().limiter).getOrThrow());
nbt.put("capacityUpgrade", ItemVariant.CODEC.encodeStart(RegistryOps.of(NbtOps.INSTANCE, registryLookup), settings().upgrade).getOrThrow());
nbt.put("limiter", ItemVariant.CODEC.encodeStart(RegistryOps.of(NbtOps.INSTANCE, registryLookup), settings().limiter).getOrThrow());
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/assets/extended_drawers/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
"tooltip.extended_drawers.drawer_contents": "Contains:",
"tooltip.extended_drawers.modifiers": "Modifiers: %s",
"tooltip.extended_drawers.empty": "Empty",

"tag.item.extended_drawers.upgrade": "Drawer Upgrades",
"tag.item.extended_drawers.drawers": "Drawers",
"tag.item.extended_drawers.toggle.voiding": "Voiding Toggles",
"tag.item.extended_drawers.toggle.lock": "Lock Toggles",
"tag.item.extended_drawers.toggle.hidden": "Hiding Toggles",
"tag.item.extended_drawers.toggle.duping": "Duping Toggles",

"itemGroup.extended_drawers.main": "Extended Drawers",

Expand Down

0 comments on commit 515b62b

Please sign in to comment.