Skip to content

Commit db55eaf

Browse files
committed
Fix world-based crafting recipes not keeping NBT data, Closes #29
1 parent 1f02c23 commit db55eaf

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

src/main/java/org/cyclops/structuredcrafting/craft/provider/WorldItemStackProvider.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
package org.cyclops.structuredcrafting.craft.provider;
22

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;
47
import net.minecraft.world.item.BlockItem;
5-
import net.minecraft.world.item.Item;
68
import net.minecraft.world.item.ItemStack;
7-
import net.minecraft.core.Direction;
8-
import net.minecraft.core.BlockPos;
99
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;
1014
import net.minecraftforge.items.CapabilityItemHandler;
1115
import net.minecraftforge.items.IItemHandler;
12-
import org.cyclops.cyclopscore.helper.ItemStackHelpers;
1316
import org.cyclops.cyclopscore.helper.BlockEntityHelpers;
17+
import org.cyclops.cyclopscore.helper.ItemStackHelpers;
1418
import org.cyclops.structuredcrafting.block.BlockStructuredCrafterConfig;
1519

20+
import java.util.Map;
21+
import java.util.UUID;
22+
import java.util.WeakHashMap;
23+
1624
/**
1725
* World that can provide an itemstack.
1826
* @author rubensworks
1927
*/
2028
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+
2142
@Override
2243
public boolean canProvideInput() {
2344
return BlockStructuredCrafterConfig.canTakeInputsFromWorld;
@@ -55,15 +76,10 @@ public boolean hasItemStack(Level world, BlockPos pos, Direction side) {
5576
@Override
5677
public ItemStack getItemStack(Level world, BlockPos pos, Direction side) {
5778
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));
6581
}
66-
return itemStack;
82+
return ItemStack.EMPTY;
6783
}
6884

6985
@Override

0 commit comments

Comments
 (0)