|
1 | 1 | package org.cyclops.structuredcrafting.craft.provider; |
2 | 2 |
|
3 | | -import net.minecraft.world.level.block.state.BlockState; |
| 3 | +import com.mojang.authlib.GameProfile; |
| 4 | +import net.minecraft.core.BlockPos; |
| 5 | +import net.minecraft.core.Direction; |
| 6 | +import net.minecraft.server.level.ServerLevel; |
4 | 7 | import net.minecraft.world.item.BlockItem; |
5 | | -import net.minecraft.world.item.Item; |
6 | 8 | import net.minecraft.world.item.ItemStack; |
7 | | -import net.minecraft.core.Direction; |
8 | | -import net.minecraft.core.BlockPos; |
9 | 9 | import net.minecraft.world.level.Level; |
| 10 | +import net.minecraft.world.level.block.state.BlockState; |
| 11 | +import net.minecraft.world.phys.BlockHitResult; |
| 12 | +import net.minecraft.world.phys.Vec3; |
| 13 | +import net.minecraftforge.common.util.FakePlayer; |
10 | 14 | import net.minecraftforge.items.CapabilityItemHandler; |
11 | 15 | import net.minecraftforge.items.IItemHandler; |
12 | | -import org.cyclops.cyclopscore.helper.ItemStackHelpers; |
13 | 16 | import org.cyclops.cyclopscore.helper.BlockEntityHelpers; |
| 17 | +import org.cyclops.cyclopscore.helper.ItemStackHelpers; |
14 | 18 | import org.cyclops.structuredcrafting.block.BlockStructuredCrafterConfig; |
15 | 19 |
|
| 20 | +import java.util.Map; |
| 21 | +import java.util.UUID; |
| 22 | +import java.util.WeakHashMap; |
| 23 | + |
16 | 24 | /** |
17 | 25 | * World that can provide an itemstack. |
18 | 26 | * @author rubensworks |
19 | 27 | */ |
20 | 28 | public class WorldItemStackProvider implements IItemStackProvider { |
| 29 | + |
| 30 | + private static GameProfile PROFILE = new GameProfile(UUID.fromString("41C82C87-7AfB-4024-BB57-13D2C99CAE78"), "[StructuredCrafting]"); |
| 31 | + private static final Map<ServerLevel, FakePlayer> FAKE_PLAYERS = new WeakHashMap<ServerLevel, FakePlayer>(); |
| 32 | + |
| 33 | + public static FakePlayer getFakePlayer(ServerLevel world) { |
| 34 | + FakePlayer fakePlayer = FAKE_PLAYERS.get(world); |
| 35 | + if (fakePlayer == null) { |
| 36 | + fakePlayer = new FakePlayer(world, PROFILE); |
| 37 | + FAKE_PLAYERS.put(world, fakePlayer); |
| 38 | + } |
| 39 | + return fakePlayer; |
| 40 | + } |
| 41 | + |
21 | 42 | @Override |
22 | 43 | public boolean canProvideInput() { |
23 | 44 | return BlockStructuredCrafterConfig.canTakeInputsFromWorld; |
@@ -55,15 +76,10 @@ public boolean hasItemStack(Level world, BlockPos pos, Direction side) { |
55 | 76 | @Override |
56 | 77 | public ItemStack getItemStack(Level world, BlockPos pos, Direction side) { |
57 | 78 | BlockState blockState = world.getBlockState(pos); |
58 | | - |
59 | | - ItemStack itemStack = ItemStack.EMPTY; |
60 | | - if(blockState != null) { |
61 | | - Item item = Item.byBlock(blockState.getBlock()); |
62 | | - if(item != null && hasEmptyItemHandler(world, pos, side)) { |
63 | | - itemStack = new ItemStack(item, 1); |
64 | | - } |
| 79 | + if(blockState != null && hasEmptyItemHandler(world, pos, side)) { |
| 80 | + return blockState.getCloneItemStack(new BlockHitResult(new Vec3(0, 0, 0), side, pos, false), world, pos, getFakePlayer((ServerLevel) world)); |
65 | 81 | } |
66 | | - return itemStack; |
| 82 | + return ItemStack.EMPTY; |
67 | 83 | } |
68 | 84 |
|
69 | 85 | @Override |
|
0 commit comments