Skip to content

Commit

Permalink
Allow Enchantment Mover to use Enchanted Book
Browse files Browse the repository at this point in the history
  • Loading branch information
Kotori316 committed Dec 7, 2024
1 parent 1d5b3ae commit d14a1d5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
37 changes: 24 additions & 13 deletions common/src/main/java/com/yogpc/qp/machine/mover/MoverEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -147,34 +148,44 @@ static boolean canMoveEnchantment(@Nullable Predicate<Holder<Enchantment>> predi
}

void moveEnchant(Holder<Enchantment> 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> enchantment, ItemStack from, ItemStack to, Runnable after) {
moveEnchantment(enchantment, from, to, null, after);
static Pair<ItemStack, ItemStack> moveEnchantment(@Nullable Holder<Enchantment> enchantment, ItemStack from, ItemStack to, Runnable after) {
return moveEnchantment(enchantment, from, to, null, after);
}

@VisibleForTesting
static void moveEnchantment(@Nullable Holder<Enchantment> enchantment, ItemStack from, ItemStack to, @Nullable Predicate<Holder<Enchantment>> predicate, Runnable after) {
if (enchantment == null || from.isEmpty() || to.isEmpty()) return;
static Pair<ItemStack, ItemStack> moveEnchantment(@Nullable Holder<Enchantment> enchantment, ItemStack from, ItemStack to, @Nullable Predicate<Holder<Enchantment>> 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> enchantment, ItemStack stack) {
static ItemStack downLevel(Holder<Enchantment> 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> enchantment, ItemStack stack) {
static ItemStack upLevel(Holder<Enchantment> enchantment, ItemStack stack) {
EnchantmentHelper.updateEnchantments(stack, mutable ->
mutable.set(enchantment, mutable.getLevel(enchantment) + 1)
);
return stack;
}
}
2 changes: 1 addition & 1 deletion fabric/game-test/server.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion forge/game-test/server.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion neoforge/game-test/server.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d14a1d5

Please sign in to comment.