From 039acc3c64e65f410804d8f3ee18e38ee1a62769 Mon Sep 17 00:00:00 2001 From: jottyfan Date: Sat, 15 Jan 2022 12:42:51 +0100 Subject: [PATCH] corrections in planting saplings --- .../container/BackpackInventory.java | 6 +- .../quickiefabric/items/ItemBag.java | 123 ++++++++++++++---- 2 files changed, 100 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java index 355aad9..4f8b025 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/container/BackpackInventory.java @@ -165,12 +165,12 @@ public class BackpackInventory extends SimpleInventory { */ public static void setItemsToBackpack(ItemStack itemStack, BackpackInventory backpackInventory) { Collection> itemStacks = new ArrayList<>(); - List firstList = new ArrayList<>(); for (int i = 0; i < backpackInventory.size(); i++) { ItemStack stack = backpackInventory.getStack(i); - firstList.add(stack); + List list = new ArrayList<>(); + list.add(stack); + itemStacks.add(list); } - itemStacks.add(firstList); setItemsToBackpack(itemStack, itemStacks); } } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBag.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBag.java index 3dd0d87..d6d27b1 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBag.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/ItemBag.java @@ -1,13 +1,7 @@ package de.jottyfan.minecraft.quickiefabric.items; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - import de.jottyfan.minecraft.quickiefabric.container.BackpackInventory; import de.jottyfan.minecraft.quickiefabric.item.ModdedItemUsageContext; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; @@ -18,7 +12,6 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; /** @@ -33,31 +26,109 @@ public class ItemBag extends ItemBackpack { BlockPos pos = context.getBlockPos(); World world = context.getWorld(); PlayerEntity player = context.getPlayer(); - BlockState blockState = world.getBlockState(pos); - Block block = blockState.getBlock(); - if (Blocks.FARMLAND.equals(block)) { + if (isFreeFarmland(world, pos)) { if (!world.isClient) { player.sendMessage(new TranslatableText("msg.notyetimplemented"), false); } -// BackpackInventory bi = new BackpackInventory(context.getStack()); -// for (int slot = 0; slot < ItemBackpack.SLOTSIZE; slot++) { -// ItemStack stack = bi.getStack(slot); -// if (stack.getCount() > 0) { -// Item item = stack.getItem(); -// BlockPos newPos = pos; // TODO: replace by iteration -// Vec3d newHitPos = context.getHitPos(); // TODO: replace by iteration -// BlockHitResult hit = new BlockHitResult(newHitPos, context.getSide(), newPos, context.hitsInsideBlock()); -// ModdedItemUsageContext iuc = new ModdedItemUsageContext(world, player, Hand.MAIN_HAND, stack, hit); -// if (item.useOnBlock(iuc).equals(ActionResult.PASS)) { -// bi.setStack(slot, stack); -// } -// } -// } -// ItemStack thisBackpack = player.getActiveItem(); -// BackpackInventory.setItemsToBackpack(thisBackpack, bi); + BackpackInventory bi = new BackpackInventory(context.getStack()); + for (int slot = 0; slot < ItemBackpack.SLOTSIZE; slot++) { + ItemStack stack = bi.getStack(slot); + if (stack.getCount() > 0) { + Item item = stack.getItem(); + for (int i = stack.getCount(); i > 0; i--) { + pos = findNextFreeField(world, pos); + if (pos == null) { + break; + } + BlockHitResult hit = new BlockHitResult(context.getHitPos(), context.getSide(), pos, + context.hitsInsideBlock()); + ModdedItemUsageContext iuc = new ModdedItemUsageContext(world, player, Hand.MAIN_HAND, stack, hit); + item.useOnBlock(iuc); + stack.decrement(1); + bi.setStack(slot, stack); + } + } + } + ItemStack thisBackpack = context.getStack(); + BackpackInventory.setItemsToBackpack(thisBackpack, bi); + player.setStackInHand(context.getHand(), thisBackpack); return ActionResult.SUCCESS; } else { return super.useOnBlock(context); } } + + /** + * find the next free field that can be used to set a sapling to... + * + * @param world the world + * @param start the starting position + * @return the next free block of farmland - might be even the one on start + */ + private final BlockPos findNextFreeField(World world, BlockPos start) { + if (isFreeFarmland(world, start)) { + return start; + } else if (isFreeFarmland(world, start.east())) { + return start.east(); + } else if (isFreeFarmland(world, start.west())) { + return start.west(); + } else if (isFreeFarmland(world, start.south())) { + return start.south(); + } else if (isFreeFarmland(world, start.north())) { + return start.north(); + } else if (isFreeFarmland(world, start.east().south())) { + return start.east().south(); + } else if (isFreeFarmland(world, start.west().south())) { + return start.west().south(); + } else if (isFreeFarmland(world, start.east().north())) { + return start.east().north(); + } else if (isFreeFarmland(world, start.west().north())) { + return start.west().north(); + } else if (isFreeFarmland(world, start.east().up())) { + return start.east().up(); + } else if (isFreeFarmland(world, start.west().up())) { + return start.west().up(); + } else if (isFreeFarmland(world, start.south().up())) { + return start.south().up(); + } else if (isFreeFarmland(world, start.north().up())) { + return start.north().up(); + } else if (isFreeFarmland(world, start.east().south().up())) { + return start.east().south().up(); + } else if (isFreeFarmland(world, start.west().south().up())) { + return start.west().south().up(); + } else if (isFreeFarmland(world, start.east().north().up())) { + return start.east().north().up(); + } else if (isFreeFarmland(world, start.west().north().up())) { + return start.west().north().up(); + } else if (isFreeFarmland(world, start.east().down())) { + return start.east().down(); + } else if (isFreeFarmland(world, start.west().down())) { + return start.west().down(); + } else if (isFreeFarmland(world, start.south().down())) { + return start.south().down(); + } else if (isFreeFarmland(world, start.north().down())) { + return start.north().down(); + } else if (isFreeFarmland(world, start.east().south().down())) { + return start.east().south().down(); + } else if (isFreeFarmland(world, start.west().south().down())) { + return start.west().south().down(); + } else if (isFreeFarmland(world, start.east().north().down())) { + return start.east().north().down(); + } else if (isFreeFarmland(world, start.west().north().down())) { + return start.west().north().down(); + } else { + return null; + } + } + + /** + * check if the block on pos is farmland and the one above is air + * + * @param world the world + * @param pos the position + * @return true or false + */ + private final boolean isFreeFarmland(World world, BlockPos pos) { + return Blocks.FARMLAND.equals(world.getBlockState(pos).getBlock()) && world.getBlockState(pos.up()).isAir(); + } }