diff --git a/common/src/gameTest/java/com/yogpc/qp/machine/mover/PlaceMoverTest.java b/common/src/gameTest/java/com/yogpc/qp/machine/mover/PlaceMoverTest.java index 016d2eed1..6ac5997eb 100644 --- a/common/src/gameTest/java/com/yogpc/qp/machine/mover/PlaceMoverTest.java +++ b/common/src/gameTest/java/com/yogpc/qp/machine/mover/PlaceMoverTest.java @@ -3,8 +3,10 @@ import com.yogpc.qp.PlatformAccess; import net.minecraft.core.BlockPos; import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraft.world.item.EnchantedBookItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; +import net.minecraft.world.item.enchantment.EnchantmentInstance; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.level.GameType; @@ -89,4 +91,23 @@ public static void drop(GameTestHelper helper) { helper.assertItemEntityPresent(PlatformAccess.getAccess().registerObjects().quarryBlock().get().blockItem, base, 4); helper.succeed(); } + + public static void moveFromEnchantedBook(GameTestHelper helper) { + helper.setBlock(base, PlatformAccess.getAccess().registerObjects().moverBlock().get()); + MoverEntity mover = helper.getBlockEntity(base); + + var enchantment = getEnchantment(helper, Enchantments.EFFICIENCY); + var stack = EnchantedBookItem.createForEnchantment(new EnchantmentInstance(enchantment, 1)); + var quarry = new ItemStack(PlatformAccess.getAccess().registerObjects().quarryBlock().get()); + mover.inventory.setItem(0, stack); + mover.inventory.setItem(1, quarry); + + mover.moveEnchant(enchantment); + assertAll( + () -> assertEquals(1, quarry.getEnchantments().getLevel(enchantment)), + () -> assertTrue(mover.inventory.getItem(0).isEmpty()), + () -> assertTrue(mover.movableEnchantments.isEmpty()) + ); + helper.succeed(); + } } diff --git a/common/src/main/java/com/yogpc/qp/machine/mover/MoverEntity.java b/common/src/main/java/com/yogpc/qp/machine/mover/MoverEntity.java index b3baf5f83..39f8e22c2 100644 --- a/common/src/main/java/com/yogpc/qp/machine/mover/MoverEntity.java +++ b/common/src/main/java/com/yogpc/qp/machine/mover/MoverEntity.java @@ -15,14 +15,12 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.SimpleContainer; -import net.minecraft.world.item.BowItem; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.TieredItem; -import net.minecraft.world.item.Tiers; +import net.minecraft.world.item.*; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.ItemEnchantments; import net.minecraft.world.level.block.state.BlockState; +import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.VisibleForTesting; @@ -86,6 +84,9 @@ public Inventory(int size) { public boolean canPlaceItem(int slot, ItemStack stack) { return switch (slot) { case 0 -> { + if (stack.getItem() instanceof EnchantedBookItem) { + yield true; + } if (!stack.isEnchanted()) { yield false; } @@ -147,34 +148,44 @@ static boolean canMoveEnchantment(@Nullable Predicate> predi } void moveEnchant(Holder enchantment) { - moveEnchantment(enchantment, inventory.getItem(0), inventory.getItem(1), this::updateMovableEnchantments); + var moved = moveEnchantment(enchantment, inventory.getItem(0), inventory.getItem(1), this::updateMovableEnchantments); + inventory.setItem(0, moved.getLeft()); + inventory.setItem(1, moved.getRight()); } - static void moveEnchantment(@Nullable Holder enchantment, ItemStack from, ItemStack to, Runnable after) { - moveEnchantment(enchantment, from, to, null, after); + static Pair moveEnchantment(@Nullable Holder enchantment, ItemStack from, ItemStack to, Runnable after) { + return moveEnchantment(enchantment, from, to, null, after); } @VisibleForTesting - static void moveEnchantment(@Nullable Holder enchantment, ItemStack from, ItemStack to, @Nullable Predicate> predicate, Runnable after) { - if (enchantment == null || from.isEmpty() || to.isEmpty()) return; + static Pair moveEnchantment(@Nullable Holder enchantment, ItemStack from, ItemStack to, @Nullable Predicate> predicate, Runnable after) { + if (enchantment == null || from.isEmpty() || to.isEmpty()) return Pair.of(from, to); if (canMoveEnchantment(predicate, EnchantmentHelper.getEnchantmentsForCrafting(to), enchantment)) { - upLevel(enchantment, to); - downLevel(enchantment, from); + var right = upLevel(enchantment, to); + var left = downLevel(enchantment, from); after.run(); + return Pair.of(left, right); } + return Pair.of(from, to); } @VisibleForTesting - static void downLevel(Holder enchantment, ItemStack stack) { + static ItemStack downLevel(Holder enchantment, ItemStack stack) { EnchantmentHelper.updateEnchantments(stack, mutable -> mutable.set(enchantment, mutable.getLevel(enchantment) - 1) ); + if (stack.is(Items.ENCHANTED_BOOK) && EnchantmentHelper.getEnchantmentsForCrafting(stack).isEmpty()) { + // Remove empty enchanted book + return ItemStack.EMPTY; + } + return stack; } @VisibleForTesting - static void upLevel(Holder enchantment, ItemStack stack) { + static ItemStack upLevel(Holder enchantment, ItemStack stack) { EnchantmentHelper.updateEnchantments(stack, mutable -> mutable.set(enchantment, mutable.getLevel(enchantment) + 1) ); + return stack; } } diff --git a/fabric/game-test/server.properties b/fabric/game-test/server.properties index 6512ff3b8..d7ca81ba4 100644 --- a/fabric/game-test/server.properties +++ b/fabric/game-test/server.properties @@ -1,5 +1,5 @@ #Minecraft server properties -#Sat Dec 07 23:11:08 JST 2024 +#Sun Dec 08 00:31:24 JST 2024 accepts-transfers=false allow-flight=true allow-nether=true diff --git a/forge/game-test/server.properties b/forge/game-test/server.properties index 0372ec5e2..b875a65b7 100644 --- a/forge/game-test/server.properties +++ b/forge/game-test/server.properties @@ -1,5 +1,5 @@ #Minecraft server properties -#Sat Dec 07 23:11:09 JST 2024 +#Sun Dec 08 00:31:26 JST 2024 accepts-transfers=false allow-flight=true allow-nether=true diff --git a/neoforge/game-test/server.properties b/neoforge/game-test/server.properties index 681ecd383..a980b8b3a 100644 --- a/neoforge/game-test/server.properties +++ b/neoforge/game-test/server.properties @@ -1,5 +1,5 @@ #Minecraft server properties -#Sat Dec 07 23:11:33 JST 2024 +#Sun Dec 08 00:31:25 JST 2024 accepts-transfers=false allow-flight=true allow-nether=true