corrections in planting saplings

This commit is contained in:
Jörg Henke 2022-01-15 12:42:51 +01:00
parent 033823d036
commit 039acc3c64
2 changed files with 100 additions and 29 deletions

View File

@ -165,12 +165,12 @@ public class BackpackInventory extends SimpleInventory {
*/ */
public static void setItemsToBackpack(ItemStack itemStack, BackpackInventory backpackInventory) { public static void setItemsToBackpack(ItemStack itemStack, BackpackInventory backpackInventory) {
Collection<List<ItemStack>> itemStacks = new ArrayList<>(); Collection<List<ItemStack>> itemStacks = new ArrayList<>();
List<ItemStack> firstList = new ArrayList<>();
for (int i = 0; i < backpackInventory.size(); i++) { for (int i = 0; i < backpackInventory.size(); i++) {
ItemStack stack = backpackInventory.getStack(i); ItemStack stack = backpackInventory.getStack(i);
firstList.add(stack); List<ItemStack> list = new ArrayList<>();
list.add(stack);
itemStacks.add(list);
} }
itemStacks.add(firstList);
setItemsToBackpack(itemStack, itemStacks); setItemsToBackpack(itemStack, itemStacks);
} }
} }

View File

@ -1,13 +1,7 @@
package de.jottyfan.minecraft.quickiefabric.items; 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.container.BackpackInventory;
import de.jottyfan.minecraft.quickiefabric.item.ModdedItemUsageContext; 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.block.Blocks;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -18,7 +12,6 @@ import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
/** /**
@ -33,31 +26,109 @@ public class ItemBag extends ItemBackpack {
BlockPos pos = context.getBlockPos(); BlockPos pos = context.getBlockPos();
World world = context.getWorld(); World world = context.getWorld();
PlayerEntity player = context.getPlayer(); PlayerEntity player = context.getPlayer();
BlockState blockState = world.getBlockState(pos); if (isFreeFarmland(world, pos)) {
Block block = blockState.getBlock();
if (Blocks.FARMLAND.equals(block)) {
if (!world.isClient) { if (!world.isClient) {
player.sendMessage(new TranslatableText("msg.notyetimplemented"), false); player.sendMessage(new TranslatableText("msg.notyetimplemented"), false);
} }
// BackpackInventory bi = new BackpackInventory(context.getStack()); BackpackInventory bi = new BackpackInventory(context.getStack());
// for (int slot = 0; slot < ItemBackpack.SLOTSIZE; slot++) { for (int slot = 0; slot < ItemBackpack.SLOTSIZE; slot++) {
// ItemStack stack = bi.getStack(slot); ItemStack stack = bi.getStack(slot);
// if (stack.getCount() > 0) { if (stack.getCount() > 0) {
// Item item = stack.getItem(); Item item = stack.getItem();
// BlockPos newPos = pos; // TODO: replace by iteration for (int i = stack.getCount(); i > 0; i--) {
// Vec3d newHitPos = context.getHitPos(); // TODO: replace by iteration pos = findNextFreeField(world, pos);
// BlockHitResult hit = new BlockHitResult(newHitPos, context.getSide(), newPos, context.hitsInsideBlock()); if (pos == null) {
// ModdedItemUsageContext iuc = new ModdedItemUsageContext(world, player, Hand.MAIN_HAND, stack, hit); break;
// if (item.useOnBlock(iuc).equals(ActionResult.PASS)) { }
// bi.setStack(slot, stack); 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);
// ItemStack thisBackpack = player.getActiveItem(); stack.decrement(1);
// BackpackInventory.setItemsToBackpack(thisBackpack, bi); bi.setStack(slot, stack);
}
}
}
ItemStack thisBackpack = context.getStack();
BackpackInventory.setItemsToBackpack(thisBackpack, bi);
player.setStackInHand(context.getHand(), thisBackpack);
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} else { } else {
return super.useOnBlock(context); 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();
}
} }