|
| 1 | +package com.ldtteam.structurize.placement.handlers.placement; |
| 2 | + |
| 3 | +import com.ldtteam.structurize.api.util.ItemStackUtils; |
| 4 | +import com.ldtteam.structurize.api.util.Log; |
| 5 | +import com.ldtteam.structurize.blockentities.BlockEntityTagSubstitution; |
| 6 | +import com.ldtteam.structurize.blocks.ModBlocks; |
| 7 | +import com.ldtteam.structurize.placement.IPlacementContext; |
| 8 | +import com.ldtteam.structurize.util.BlockUtils; |
| 9 | +import net.minecraft.core.BlockPos; |
| 10 | +import net.minecraft.nbt.CompoundTag; |
| 11 | +import net.minecraft.util.Tuple; |
| 12 | +import net.minecraft.world.item.ItemStack; |
| 13 | +import net.minecraft.world.level.Level; |
| 14 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 15 | +import net.minecraft.world.level.block.state.BlockState; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static com.ldtteam.structurize.api.util.constant.Constants.UPDATE_FLAG; |
| 24 | +import static com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers.getItemsFromTileEntity; |
| 25 | +import static com.ldtteam.structurize.placement.handlers.placement.PlacementHandlers.handleTileEntityPlacement; |
| 26 | + |
| 27 | +/** |
| 28 | + * Handle the substitution handler. |
| 29 | + */ |
| 30 | +public class BlockTagSubstitutionPlacementHandler implements IPlacementHandler |
| 31 | +{ |
| 32 | + @Override |
| 33 | + public boolean canHandle(@NotNull final Level world, @NotNull final BlockPos pos, @NotNull final BlockState blockState) |
| 34 | + { |
| 35 | + return blockState.getBlock() == ModBlocks.blockTagSubstitution.get(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public ActionProcessingResult handle( |
| 40 | + @NotNull final Level world, |
| 41 | + @NotNull final BlockPos pos, |
| 42 | + @NotNull final BlockState blockState, |
| 43 | + @Nullable final CompoundTag tileEntityData, |
| 44 | + @NotNull final IPlacementContext placementContext) |
| 45 | + { |
| 46 | + if (placementContext.fancyPlacement()) |
| 47 | + { |
| 48 | + if (tileEntityData != null && BlockEntity.loadStatic(pos, blockState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity) |
| 49 | + { |
| 50 | + final IPlacementHandler placementHandler = PlacementHandlers.getHandler(world, pos, tagEntity.getReplacement().getBlockState()); |
| 51 | + if (placementHandler != this) |
| 52 | + { |
| 53 | + return placementHandler.handle(world, pos, tagEntity.getReplacement().getBlockState(), tagEntity.getReplacement().getBlockEntityTag(), placementContext); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + Log.getLogger().warn("Blueprint contains faulty tag substitution block with recursive data."); |
| 58 | + return ActionProcessingResult.PASS; |
| 59 | + } |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + Log.getLogger().warn("Blueprint contains faulty tag substitution block without data."); |
| 64 | + return ActionProcessingResult.PASS; |
| 65 | + } |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + // Normal placement with full handler. |
| 70 | + if (world.getBlockState(pos).equals(blockState)) |
| 71 | + { |
| 72 | + world.removeBlock(pos, false); |
| 73 | + world.setBlock(pos, blockState, UPDATE_FLAG); |
| 74 | + if (tileEntityData != null) |
| 75 | + { |
| 76 | + handleTileEntityPlacement(tileEntityData, world, pos, placementContext.getRotationMirror()); |
| 77 | + } |
| 78 | + return ActionProcessingResult.PASS; |
| 79 | + } |
| 80 | + |
| 81 | + if (!world.setBlock(pos, blockState, UPDATE_FLAG)) |
| 82 | + { |
| 83 | + return ActionProcessingResult.DENY; |
| 84 | + } |
| 85 | + |
| 86 | + if (tileEntityData != null) |
| 87 | + { |
| 88 | + handleTileEntityPlacement(tileEntityData, world, pos, placementContext.getRotationMirror()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return ActionProcessingResult.SUCCESS; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean doesWorldStateMatchBlueprintState(final BlockState worldState, final BlockState blueprintState, final Tuple<BlockEntity, CompoundTag> blockEntityData, final @NotNull IPlacementContext placementContext) |
| 97 | + { |
| 98 | + if (placementContext.fancyPlacement()) |
| 99 | + { |
| 100 | + if (blockEntityData != null && BlockEntity.loadStatic(BlockPos.ZERO, blueprintState, blockEntityData.getB()) instanceof BlockEntityTagSubstitution tagEntity) |
| 101 | + { |
| 102 | + try |
| 103 | + { |
| 104 | + final IPlacementHandler placementHandler = PlacementHandlers.getHandler(null, BlockPos.ZERO, tagEntity.getReplacement().getBlockState()); |
| 105 | + if (placementHandler != this) |
| 106 | + { |
| 107 | + final CompoundTag tileEntityData = tagEntity.getReplacement().getBlockEntityTag(); |
| 108 | + Tuple<BlockEntity, CompoundTag> updatedTETuple = null; |
| 109 | + if (!tileEntityData.isEmpty()) |
| 110 | + { |
| 111 | + updatedTETuple = new Tuple<>(blockEntityData.getA(), tileEntityData); |
| 112 | + } |
| 113 | + |
| 114 | + return placementHandler.doesWorldStateMatchBlueprintState(worldState, tagEntity.getReplacement().getBlockState(), updatedTETuple, placementContext); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + Log.getLogger().warn("Blueprint contains faulty tag substitution block with recursive data."); |
| 119 | + return true; |
| 120 | + } |
| 121 | + } |
| 122 | + catch (final Exception ex) |
| 123 | + { |
| 124 | + Log.getLogger().warn("Blueprint contains tag substitution handler with block that needs world in match. This is deprecated. We will be moving to state only matching in the future."); |
| 125 | + return true; |
| 126 | + } |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + Log.getLogger().warn("Blueprint contains faulty tag substitution block without data."); |
| 131 | + return true; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Always paste over to repair data if necessary. |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public List<ItemStack> getRequiredItems( |
| 141 | + @NotNull final Level world, |
| 142 | + @NotNull final BlockPos pos, |
| 143 | + @NotNull final BlockState blockState, |
| 144 | + @Nullable final CompoundTag tileEntityData, |
| 145 | + @NotNull final IPlacementContext placementContext) |
| 146 | + { |
| 147 | + if (placementContext.fancyPlacement()) |
| 148 | + { |
| 149 | + if (tileEntityData != null && BlockEntity.loadStatic(pos, blockState, tileEntityData) instanceof BlockEntityTagSubstitution tagEntity) |
| 150 | + { |
| 151 | + final IPlacementHandler placementHandler = PlacementHandlers.getHandler(world, pos, tagEntity.getReplacement().getBlockState()); |
| 152 | + if (placementHandler != this) |
| 153 | + { |
| 154 | + return placementHandler.getRequiredItems(world, pos, tagEntity.getReplacement().getBlockState(), tagEntity.getReplacement().getBlockEntityTag(), placementContext); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + Log.getLogger().warn("Blueprint contains faulty tag substitution block with recursive data."); |
| 159 | + return Collections.emptyList(); |
| 160 | + } |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + Log.getLogger().warn("Blueprint contains faulty tag substitution block without data."); |
| 165 | + return Collections.emptyList(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + final List<ItemStack> itemList = new ArrayList<>(getItemsFromTileEntity(tileEntityData, blockState)); |
| 170 | + itemList.add(BlockUtils.getItemStackFromBlockState(blockState)); |
| 171 | + itemList.removeIf(ItemStackUtils::isEmpty); |
| 172 | + return itemList; |
| 173 | + } |
| 174 | +} |
0 commit comments