|
1 | 1 | package nl.motionlesstrain.createcolonies.placementhandlers; |
2 | 2 |
|
3 | 3 | import com.ldtteam.structurize.api.util.constant.Constants; |
| 4 | +import com.ldtteam.structurize.placement.IPlacementContext; |
4 | 5 | import com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler; |
5 | | -import com.ldtteam.structurize.util.PlacementSettings; |
6 | 6 | import com.simibubi.create.content.kinetics.belt.BeltBlock; |
7 | 7 | import com.simibubi.create.content.kinetics.belt.BeltPart; |
8 | 8 | import net.minecraft.core.BlockPos; |
9 | 9 | import net.minecraft.nbt.CompoundTag; |
10 | 10 | import net.minecraft.nbt.Tag; |
| 11 | +import net.minecraft.util.Tuple; |
11 | 12 | import net.minecraft.world.item.ItemStack; |
12 | 13 | import net.minecraft.world.level.Level; |
| 14 | +import net.minecraft.world.level.block.entity.BlockEntity; |
13 | 15 | import net.minecraft.world.level.block.state.BlockState; |
14 | 16 | import nl.motionlesstrain.createcolonies.resources.CreateResources; |
15 | 17 | import nl.motionlesstrain.createcolonies.utils.BlockPosUtil; |
16 | 18 | import nl.motionlesstrain.createcolonies.utils.ItemUtils; |
| 19 | +import org.jetbrains.annotations.NotNull; |
17 | 20 | import org.jetbrains.annotations.Nullable; |
18 | 21 |
|
19 | 22 | import java.util.*; |
20 | 23 |
|
21 | 24 | import static com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers.handleTileEntityPlacement; |
22 | 25 |
|
23 | 26 | public class BeltPlacementHandler implements IPlacementHandler { |
24 | | - @Override |
25 | | - public boolean canHandle(Level level, BlockPos blockPos, BlockState blockState) { |
26 | | - return blockState.is(CreateResources.Blocks.belt); |
27 | | - } |
28 | | - |
29 | | - private final Map<BlockPos, Map<BlockPos, List<ItemStack>>> beltItems = new HashMap<>(); |
30 | | - |
31 | | - @Override |
32 | | - public List<ItemStack> getRequiredItems(Level level, BlockPos blockPos, BlockState blockState, @Nullable CompoundTag compoundTag, boolean b) { |
33 | | - final List<ItemStack> requiredItems = new ArrayList<>(); |
34 | | - |
35 | | - if (blockState.hasProperty(BeltBlock.PART)) { |
36 | | - if (blockState.getValue(BeltBlock.PART) != BeltPart.MIDDLE) { |
37 | | - requiredItems.add(ItemUtils.stackFromNullable(CreateResources.Items.shaft)); |
38 | | - } |
39 | | - if (blockState.getValue(BeltBlock.PART) == BeltPart.START) { |
40 | | - requiredItems.add(ItemUtils.stackFromNullable(CreateResources.Items.belt)); |
41 | | - // The start one seems to have an inventory |
42 | | - if (compoundTag != null && compoundTag.contains("Inventory", Tag.TAG_COMPOUND)) { |
43 | | - final var inventoryCompound = compoundTag.getCompound("Inventory"); |
44 | | - final var itemEntities = inventoryCompound.getList("Items", Tag.TAG_COMPOUND); |
45 | | - for (final var itemEntity : itemEntities) { |
46 | | - if (itemEntity instanceof CompoundTag itemEntityTag) { |
47 | | - var itemTag = itemEntityTag.getCompound("Item"); |
48 | | - requiredItems.add(ItemStack.of(itemTag)); |
49 | | - } |
50 | | - } |
51 | | - } |
| 27 | + @Override |
| 28 | + public boolean canHandle(Level level, BlockPos blockPos, BlockState blockState) { |
| 29 | + return blockState.is(CreateResources.Blocks.belt); |
| 30 | + } |
| 31 | + |
| 32 | + private final Map<BlockPos, Map<BlockPos, List<ItemStack>>> beltItems = new HashMap<>(); |
| 33 | + |
| 34 | + @Override |
| 35 | + public List<ItemStack> getRequiredItems(Level level, BlockPos blockPos, BlockState blockState, @Nullable CompoundTag compoundTag, @NotNull IPlacementContext placementContext) { |
| 36 | + final List<ItemStack> requiredItems = new ArrayList<>(); |
| 37 | + |
| 38 | + if (blockState.hasProperty(BeltBlock.PART)) { |
| 39 | + if (blockState.getValue(BeltBlock.PART) != BeltPart.MIDDLE) { |
| 40 | + requiredItems.add(ItemUtils.stackFromNullable(CreateResources.Items.shaft)); |
| 41 | + } |
| 42 | + if (blockState.getValue(BeltBlock.PART) == BeltPart.START) { |
| 43 | + requiredItems.add(ItemUtils.stackFromNullable(CreateResources.Items.belt)); |
| 44 | + // The start one seems to have an inventory |
| 45 | + if (compoundTag != null && compoundTag.contains("Inventory", Tag.TAG_COMPOUND)) { |
| 46 | + final var inventoryCompound = compoundTag.getCompound("Inventory"); |
| 47 | + final var itemEntities = inventoryCompound.getList("Items", Tag.TAG_COMPOUND); |
| 48 | + for (final var itemEntity : itemEntities) { |
| 49 | + if (itemEntity instanceof CompoundTag itemEntityTag) { |
| 50 | + var itemTag = itemEntityTag.getCompound("Item"); |
| 51 | + requiredItems.add(ItemStack.of(itemTag)); |
52 | 52 | } |
| 53 | + } |
53 | 54 | } |
| 55 | + } |
| 56 | + } |
54 | 57 |
|
55 | | - if (compoundTag != null) { |
| 58 | + if (compoundTag != null) { |
56 | 59 |
|
57 | | - final var controller = compoundTag.getCompound("Controller"); |
58 | | - final var controllerPos = BlockPosUtil.fromNBT(controller); |
| 60 | + final var controller = compoundTag.getCompound("Controller"); |
| 61 | + final var controllerPos = BlockPosUtil.fromNBT(controller); |
59 | 62 |
|
60 | | - final var allBeltItems = beltItems.computeIfAbsent(controllerPos, ignored -> new HashMap<>()); |
| 63 | + final var allBeltItems = beltItems.computeIfAbsent(controllerPos, ignored -> new HashMap<>()); |
61 | 64 |
|
62 | | - final var length = compoundTag.getInt("Length"); |
| 65 | + final var length = compoundTag.getInt("Length"); |
63 | 66 |
|
64 | | - // If we are at the end of the belt, we know the entire belt is within the schematic, and we list the resources for it |
65 | | - if (allBeltItems.size() + 1 == length && !allBeltItems.containsKey(blockPos)) { |
66 | | - for (var items : allBeltItems.values()) { |
67 | | - requiredItems.addAll(items); |
68 | | - items.clear(); |
69 | | - } |
70 | | - allBeltItems.put(blockPos, requiredItems); |
71 | | - } else if (allBeltItems.size() < length) { |
72 | | - allBeltItems.put(blockPos, requiredItems); |
73 | | - return List.of(); |
74 | | - } else { |
75 | | - return allBeltItems.getOrDefault(blockPos, List.of()); |
76 | | - } |
| 67 | + // If we are at the end of the belt, we know the entire belt is within the schematic, and we list the resources for it |
| 68 | + if (allBeltItems.size() + 1 == length && !allBeltItems.containsKey(blockPos)) { |
| 69 | + for (var items : allBeltItems.values()) { |
| 70 | + requiredItems.addAll(items); |
| 71 | + items.clear(); |
77 | 72 | } |
78 | | - return requiredItems; |
| 73 | + allBeltItems.put(blockPos, requiredItems); |
| 74 | + } else if (allBeltItems.size() < length) { |
| 75 | + allBeltItems.put(blockPos, requiredItems); |
| 76 | + return List.of(); |
| 77 | + } else { |
| 78 | + return allBeltItems.getOrDefault(blockPos, List.of()); |
| 79 | + } |
79 | 80 | } |
| 81 | + return requiredItems; |
| 82 | + } |
80 | 83 |
|
81 | | - private record BeltInfo(BlockPos pos, BlockState state, @Nullable CompoundTag tag) {} |
| 84 | + @Override |
| 85 | + public boolean doesWorldStateMatchBlueprintState(BlockState worldState, BlockState blueprintState, @Nullable Tuple<BlockEntity, CompoundTag> blockEntityData, @NotNull IPlacementContext placementContext) { |
| 86 | + return worldState.equals(blueprintState); |
| 87 | + } |
82 | 88 |
|
83 | | - private final Map<BlockPos, SortedMap<BlockPos, BeltInfo>> beltParts = new HashMap<>(); |
84 | | - @Override |
85 | | - public ActionProcessingResult handle(Level world, BlockPos pos, BlockState blockState, @Nullable CompoundTag tileEntityData, boolean complete, BlockPos centerPos, @SuppressWarnings("removal") PlacementSettings settings) { |
86 | | - if (tileEntityData == null) return ActionProcessingResult.DENY; |
| 89 | + private record BeltInfo(BlockPos pos, BlockState state, @Nullable CompoundTag tag) { |
| 90 | + } |
87 | 91 |
|
88 | | - final var controller = tileEntityData.getCompound("Controller"); |
89 | | - final var controllerPos = BlockPosUtil.fromNBT(controller); |
| 92 | + private final Map<BlockPos, SortedMap<BlockPos, BeltInfo>> beltParts = new HashMap<>(); |
90 | 93 |
|
91 | | - final var length = tileEntityData.getInt("Length"); |
| 94 | + @Override |
| 95 | + public ActionProcessingResult handle(Level world, BlockPos pos, BlockState blockState, @Nullable CompoundTag tileEntityData, @NotNull IPlacementContext placementContext) { |
| 96 | + if (tileEntityData == null) return ActionProcessingResult.DENY; |
92 | 97 |
|
93 | | - final var knownBeltParts = beltParts.computeIfAbsent(controllerPos, ignored -> new TreeMap<>()); |
94 | | - knownBeltParts.put(pos, new BeltInfo(pos, blockState, tileEntityData)); |
| 98 | + final var controller = tileEntityData.getCompound("Controller"); |
| 99 | + final var controllerPos = BlockPosUtil.fromNBT(controller); |
95 | 100 |
|
96 | | - if (knownBeltParts.size() == length) { |
97 | | - for (final Map.Entry<BlockPos, BeltInfo> entry : knownBeltParts.entrySet()) { |
98 | | - final var info = entry.getValue(); |
99 | | - if (!world.setBlock(info.pos(), info.state(), Constants.UPDATE_FLAG)) { |
100 | | - for (final var alreadyPlaced : knownBeltParts.headMap(entry.getKey()).values()) { |
101 | | - world.removeBlock(alreadyPlaced.pos(), false); |
102 | | - } |
103 | | - return ActionProcessingResult.DENY; |
104 | | - } |
105 | | - handleTileEntityPlacement(info.tag(), world, info.pos(), settings); |
106 | | - } |
107 | | - beltParts.remove(controllerPos); |
108 | | - beltItems.remove(controllerPos); |
109 | | - } |
| 101 | + final var length = tileEntityData.getInt("Length"); |
110 | 102 |
|
111 | | - return ActionProcessingResult.SUCCESS; |
| 103 | + final var knownBeltParts = beltParts.computeIfAbsent(controllerPos, ignored -> new TreeMap<>()); |
| 104 | + knownBeltParts.put(pos, new BeltInfo(pos, blockState, tileEntityData)); |
| 105 | + |
| 106 | + if (knownBeltParts.size() == length) { |
| 107 | + for (final Map.Entry<BlockPos, BeltInfo> entry : knownBeltParts.entrySet()) { |
| 108 | + final var info = entry.getValue(); |
| 109 | + if (!world.setBlock(info.pos(), info.state(), Constants.UPDATE_FLAG)) { |
| 110 | + for (final var alreadyPlaced : knownBeltParts.headMap(entry.getKey()).values()) { |
| 111 | + world.removeBlock(alreadyPlaced.pos(), false); |
| 112 | + } |
| 113 | + return ActionProcessingResult.DENY; |
| 114 | + } |
| 115 | + handleTileEntityPlacement(info.tag(), world, info.pos(), placementContext.getRotationMirror()); |
| 116 | + } |
| 117 | + beltParts.remove(controllerPos); |
| 118 | + beltItems.remove(controllerPos); |
112 | 119 | } |
| 120 | + |
| 121 | + return ActionProcessingResult.SUCCESS; |
| 122 | + } |
113 | 123 | } |
0 commit comments