diff --git a/CHANGELOG.md b/CHANGELOG.md index c4a9d77..fcb5fe8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ### Fixes * Fixed mod clearing player inventory when trying to claim a grave that had generated before removing a compatible inventory mod -* Improved compat and fixed issues from update of Beans Backpacks +* Improved compat and fixed crash with Beans Backpacks * No longer sending error message to player when claiming the grave by breaking it --- diff --git a/gradle.properties b/gradle.properties index f7e286c..cfefb1b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -34,7 +34,7 @@ org.gradle.parallel=true numismatic_version=568316:4996365 apoli_version=v2.9.0 levelz_version=517130:5024636 - beans_backpacks_version=hT2LiZSl + beans_backpacks_version=H4JcgrtM # Dev dependencies cardinal_components_version=5.2.2 diff --git a/src/main/java/com/b1n_ry/yigd/compat/BeansBackpacksCompat.java b/src/main/java/com/b1n_ry/yigd/compat/BeansBackpacksCompat.java index 1446f3a..62e85be 100644 --- a/src/main/java/com/b1n_ry/yigd/compat/BeansBackpacksCompat.java +++ b/src/main/java/com/b1n_ry/yigd/compat/BeansBackpacksCompat.java @@ -5,16 +5,18 @@ import com.b1n_ry.yigd.data.DeathContext; import com.b1n_ry.yigd.events.DropRuleEvent; import com.b1n_ry.yigd.util.DropRule; -import com.beansgalaxy.backpacks.core.BackData; -import com.beansgalaxy.backpacks.platform.Services; +import com.beansgalaxy.backpacks.data.BackData; +import com.beansgalaxy.backpacks.platform.services.CompatHelper; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Pair; import net.minecraft.util.collection.DefaultedList; +import net.minecraft.util.math.Direction; import net.minecraft.util.math.Vec3d; +import java.util.UUID; import java.util.function.Predicate; public class BeansBackpacksCompat implements InvModCompat { @@ -25,10 +27,7 @@ public String getModName() { @Override public void clear(ServerPlayerEntity player) { - BackData backData = BackData.get(player); - - backData.set(ItemStack.EMPTY); - backData.backpackInventory.clear(); + CompatHelper.setBackStack(player, ItemStack.EMPTY); } @Override @@ -36,10 +35,13 @@ public CompatComponent readNbt(NbtCompound nbt) { ItemStack stack = ItemStack.fromNbt(nbt.getCompound("stack")); DropRule rule = DropRule.valueOf(nbt.getString("dropRule")); NbtCompound backpackContentsNbt = nbt.getCompound("backpackContents"); + UUID ownerId = nbt.getUuid("ownerId"); + float yaw = nbt.getFloat("yaw"); + Direction direction = Direction.byId(nbt.getInt("direction")); DefaultedList backpackContents = InventoryComponent.listFromNbt(backpackContentsNbt, ItemStack::fromNbt, ItemStack.EMPTY); - BeansBackpackInv inv = new BeansBackpackInv(stack, rule, backpackContents); + BeansBackpackInv inv = new BeansBackpackInv(stack, rule, backpackContents, ownerId, yaw, direction); return new BeansBackpacksComponent(inv); } @@ -52,11 +54,21 @@ public static class BeansBackpackInv { private ItemStack stack; private DropRule dropRule; private DefaultedList backpackContents; + private final UUID ownerId; + + private final float yaw; + private final Direction direction; - public BeansBackpackInv(ItemStack stack, DropRule dropRule, DefaultedList backpackContents) { + public BeansBackpackInv(ItemStack stack, DropRule dropRule, DefaultedList backpackContents, + UUID ownerId, float yaw, Direction direction) { this.stack = stack; this.dropRule = dropRule; this.backpackContents = backpackContents; + this.ownerId = ownerId; + + // less important values, but they're cool I guess + this.yaw = yaw; + this.direction = direction; } public ItemStack getBackpack() { return this.stack; @@ -89,13 +101,11 @@ public BeansBackpacksComponent(BeansBackpackInv inventory) { @Override public BeansBackpackInv getInventory(ServerPlayerEntity player) { - BackData backData = BackData.get(player); - ItemStack stack = backData.getStack().copy(); - DefaultedList backpackContents = DefaultedList.of(); - for (ItemStack item : backData.backpackInventory.getItemStacks()) { - backpackContents.add(item.copy()); - } - return new BeansBackpackInv(stack, DropRule.PUT_IN_GRAVE, backpackContents); + ItemStack stack = CompatHelper.getBackStack(player).copy(); + DefaultedList backpackContents = CompatHelper.getBackpackInventory(player); + + return new BeansBackpackInv(stack, DropRule.PUT_IN_GRAVE, backpackContents, player.getUuid(), + player.headYaw, player.getHorizontalFacing()); } @Override @@ -112,9 +122,9 @@ public DefaultedList storeToPlayer(ServerPlayerEntity player) { return extraItems; } - BackData backData = BackData.get(player); - backData.set(backpack); + CompatHelper.setBackStack(player, backpack); + BackData backData = CompatHelper.getBackData(player); backData.backpackInventory.clear(); for (ItemStack stack : backpackContents) { if (!stack.isEmpty()) { @@ -125,7 +135,7 @@ public DefaultedList storeToPlayer(ServerPlayerEntity player) { } } - Services.NETWORK.backpackInventory2C(player); + CompatHelper.updateBackpackInventory2C(player); return extraItems; } @@ -167,6 +177,10 @@ public NbtCompound writeNbt() { NbtCompound nbt = new NbtCompound(); nbt.put("stack", this.inventory.stack.writeNbt(new NbtCompound())); nbt.putString("dropRule", this.inventory.dropRule.name()); + nbt.putUuid("ownerId", this.inventory.ownerId); + + nbt.putFloat("yaw", this.inventory.yaw); + nbt.putInt("direction", this.inventory.direction.getId()); NbtCompound backpackContentsNbt = InventoryComponent.listToNbt(this.inventory.getBackpackContents(), itemStack -> itemStack.writeNbt(new NbtCompound()), ItemStack::isEmpty); nbt.put("backpackContents", backpackContentsNbt); @@ -184,14 +198,20 @@ public boolean removeItem(Predicate predicate, int itemCount) { return false; } - // TODO: If it becomes possible, implement drop to drop the backpack entities @Override public void dropItems(ServerWorld world, Vec3d pos) { - super.dropItems(world, pos); + CompatHelper.createBackpackEntity(this.inventory.getBackpack(), (int) pos.x, (int) pos.y, (int) pos.z, + this.inventory.yaw, true, this.inventory.direction, world, + this.inventory.ownerId, this.inventory.getBackpackContents()); } @Override public void dropGraveItems(ServerWorld world, Vec3d pos) { - super.dropGraveItems(world, pos); + if (this.inventory.getDropRule() == DropRule.KEEP || this.inventory.getDropRule() == DropRule.DESTROY) return; + + this.inventory.setDropRule(DropRule.DROP); + CompatHelper.createBackpackEntity(this.inventory.getBackpack(), (int) pos.x, (int) pos.y, (int) pos.z, + this.inventory.yaw, true, this.inventory.direction, world, + this.inventory.ownerId, this.inventory.getBackpackContents()); } @Override @@ -199,7 +219,8 @@ public CompatComponent filterInv(Predicate predicate if (predicate.test(this.inventory.getDropRule())) { return new BeansBackpacksComponent(this.inventory); } else { - return new BeansBackpacksComponent(new BeansBackpackInv(ItemStack.EMPTY, DropRule.PUT_IN_GRAVE, DefaultedList.of())); + return new BeansBackpacksComponent(new BeansBackpackInv(ItemStack.EMPTY, DropRule.PUT_IN_GRAVE, + DefaultedList.of(), this.inventory.ownerId, this.inventory.yaw, this.inventory.direction)); } } diff --git a/src/main/java/com/b1n_ry/yigd/compat/CompatComponent.java b/src/main/java/com/b1n_ry/yigd/compat/CompatComponent.java index a0ff136..3f67308 100644 --- a/src/main/java/com/b1n_ry/yigd/compat/CompatComponent.java +++ b/src/main/java/com/b1n_ry/yigd/compat/CompatComponent.java @@ -78,7 +78,7 @@ public void dropItems(ServerWorld world, Vec3d pos) { } /** - * Drop all items in the component to the world, but only items that should be placed in a grave or dropped anyway + * Drop items in the component to the world, but only items that should be placed in a grave or dropped anyway * @param world The world to drop items in * @param pos The position to drop items at */ diff --git a/src/main/java/com/b1n_ry/yigd/components/InventoryComponent.java b/src/main/java/com/b1n_ry/yigd/components/InventoryComponent.java index 0799793..9d4772e 100644 --- a/src/main/java/com/b1n_ry/yigd/components/InventoryComponent.java +++ b/src/main/java/com/b1n_ry/yigd/components/InventoryComponent.java @@ -224,7 +224,7 @@ private void loseRandomItem() { if (itemSlots.isEmpty()) return; - int random = this.RANDOM.nextInt(itemSlots.size()); + int random = RANDOM.nextInt(itemSlots.size()); int slot = itemSlots.get(random); if (itemLoss.affectStacks) {