Search in sources :

Example 1 with IPlacementHandler

use of com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler in project Structurize by ldtteam.

the class StructurePlacer method getResourceRequirements.

/**
 * This method handles the block placement.
 * When we extract this into another mod, we have to override the method.
 *  @param world          the world.
 * @param worldPos       the world position.
 * @param localPos       the local pos.
 * @param localState     the local state.
 * @param tileEntityData the tileEntity.
 */
public BlockPlacementResult getResourceRequirements(final World world, final BlockPos worldPos, final BlockPos localPos, BlockState localState, final CompoundNBT tileEntityData) {
    final BlockState worldState = world.getBlockState(worldPos);
    boolean sameBlockInWorld = false;
    if (worldState.getBlock() == localState.getBlock()) {
        sameBlockInWorld = true;
    }
    final List<ItemStack> requiredItems = new ArrayList<>();
    for (final CompoundNBT compound : iterator.getBluePrintPositionInfo(localPos).getEntities()) {
        if (compound != null) {
            try {
                final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
                final Optional<EntityType<?>> type = EntityType.by(compound);
                if (type.isPresent()) {
                    final Entity entity = type.get().create(world);
                    if (entity != null) {
                        entity.deserializeNBT(compound);
                        final Vector3d posInWorld = entity.position().add(pos.getX(), pos.getY(), pos.getZ());
                        final List<? extends Entity> list = world.getEntitiesOfClass(entity.getClass(), new AxisAlignedBB(posInWorld.add(1, 1, 1), posInWorld.add(-1, -1, -1)));
                        boolean foundEntity = false;
                        for (Entity worldEntity : list) {
                            if (worldEntity.position().equals(posInWorld)) {
                                foundEntity = true;
                                break;
                            }
                        }
                        if (foundEntity) {
                            continue;
                        }
                        requiredItems.addAll(ItemStackUtils.getListOfStackForEntity(entity, pos));
                    }
                }
            } catch (final RuntimeException e) {
                Log.getLogger().info("Couldn't restore entity", e);
            }
        }
    }
    if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement()) {
        localState = this.handler.getSolidBlockForPos(worldPos);
    }
    if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement()) {
        localState = Blocks.AIR.defaultBlockState();
    }
    for (final IPlacementHandler placementHandler : PlacementHandlers.handlers) {
        if (placementHandler.canHandle(world, worldPos, localState)) {
            if (!sameBlockInWorld) {
                for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false)) {
                    if (!stack.isEmpty() && !this.handler.isStackFree(stack)) {
                        requiredItems.add(stack);
                    }
                }
            }
            return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
        }
    }
    return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) IPlacementHandler(com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler) CompoundNBT(net.minecraft.nbt.CompoundNBT) ArrayList(java.util.ArrayList) EntityType(net.minecraft.entity.EntityType) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 2 with IPlacementHandler

use of com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler in project Structurize by ldtteam.

the class StructurePlacer method handleBlockPlacement.

/**
 * This method handles the block placement.
 * When we extract this into another mod, we have to override the method.
 * @param world          the world.
 * @param worldPos       the world position.
 * @param localPos       the local pos
 * @param storage        the change storage.
 * @param localState     the local state.
 * @param tileEntityData the tileEntity.
 */
public BlockPlacementResult handleBlockPlacement(final World world, final BlockPos worldPos, final BlockPos localPos, final ChangeStorage storage, BlockState localState, final CompoundNBT tileEntityData) {
    final BlockState worldState = world.getBlockState(worldPos);
    boolean sameBlockInWorld = worldState.getBlock() == localState.getBlock();
    if (!(worldState.getBlock() instanceof AirBlock)) {
        if (!handler.allowReplace()) {
            return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.BREAK_BLOCK);
        }
    }
    for (final CompoundNBT compound : this.iterator.getBluePrintPositionInfo(localPos).getEntities()) {
        if (compound != null) {
            try {
                final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
                final Optional<EntityType<?>> type = EntityType.by(compound);
                if (type.isPresent()) {
                    final Entity entity = type.get().create(world);
                    if (entity != null) {
                        entity.deserializeNBT(compound);
                        entity.setUUID(UUID.randomUUID());
                        final Vector3d posInWorld = entity.position().add(pos.getX(), pos.getY(), pos.getZ());
                        entity.setPos(posInWorld.x, posInWorld.y, posInWorld.z);
                        final List<? extends Entity> list = world.getEntitiesOfClass(entity.getClass(), new AxisAlignedBB(posInWorld.add(1, 1, 1), posInWorld.add(-1, -1, -1)));
                        boolean foundEntity = false;
                        for (Entity worldEntity : list) {
                            if (worldEntity.position().equals(posInWorld)) {
                                foundEntity = true;
                                break;
                            }
                        }
                        if (foundEntity || (entity instanceof MobEntity && !handler.isCreative())) {
                            continue;
                        }
                        final List<ItemStack> requiredItems = new ArrayList<>();
                        if (!handler.isCreative()) {
                            requiredItems.addAll(ItemStackUtils.getListOfStackForEntity(entity, pos));
                            if (!InventoryUtils.hasRequiredItems(handler.getInventory(), requiredItems)) {
                                return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
                            }
                        }
                        world.addFreshEntity(entity);
                        if (storage != null) {
                            storage.addToBeKilledEntity(entity);
                        }
                        for (final ItemStack tempStack : requiredItems) {
                            if (!ItemStackUtils.isEmpty(tempStack)) {
                                InventoryUtils.consumeStack(tempStack, handler.getInventory());
                            }
                        }
                        this.handler.triggerEntitySuccess(localPos, requiredItems, true);
                    }
                }
            } catch (final RuntimeException e) {
                Log.getLogger().info("Couldn't restore entity", e);
            }
        }
    }
    if (BlockUtils.areBlockStatesEqual(localState, worldState, handler::replaceWithSolidBlock, handler.fancyPlacement(), handler::shouldBlocksBeConsideredEqual)) {
        return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
    }
    if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement()) {
        localState = this.handler.getSolidBlockForPos(worldPos);
    }
    if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement()) {
        localState = Blocks.AIR.defaultBlockState();
    }
    for (final IPlacementHandler placementHandler : PlacementHandlers.handlers) {
        if (placementHandler.canHandle(world, worldPos, localState)) {
            final List<ItemStack> requiredItems = new ArrayList<>();
            if (!sameBlockInWorld && !this.handler.isCreative()) {
                for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false)) {
                    if (!stack.isEmpty() && !this.handler.isStackFree(stack)) {
                        requiredItems.add(stack);
                    }
                }
                if (!this.handler.hasRequiredItems(requiredItems)) {
                    return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
                }
            }
            if (!(worldState.getBlock() instanceof AirBlock)) {
                if (!sameBlockInWorld && worldState.getMaterial() != Material.AIR && !(worldState.getBlock() instanceof DoublePlantBlock && worldState.getValue(DoublePlantBlock.HALF).equals(DoubleBlockHalf.UPPER))) {
                    placementHandler.handleRemoval(handler, world, worldPos, tileEntityData);
                }
            }
            this.handler.prePlacementLogic(worldPos, localState, requiredItems);
            final IPlacementHandler.ActionProcessingResult result = placementHandler.handle(world, worldPos, localState, tileEntityData, !this.handler.fancyPlacement(), this.handler.getWorldPos(), this.handler.getSettings());
            if (result == IPlacementHandler.ActionProcessingResult.DENY) {
                return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.FAIL);
            }
            this.handler.triggerSuccess(localPos, requiredItems, true);
            if (result == IPlacementHandler.ActionProcessingResult.PASS) {
                return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
            }
            if (!this.handler.isCreative() && !sameBlockInWorld) {
                for (final ItemStack tempStack : requiredItems) {
                    if (!ItemStackUtils.isEmpty(tempStack)) {
                        InventoryUtils.consumeStack(tempStack, handler.getInventory());
                    }
                }
            }
            return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
        }
    }
    return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.FAIL);
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) IPlacementHandler(com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler) CompoundNBT(net.minecraft.nbt.CompoundNBT) ArrayList(java.util.ArrayList) EntityType(net.minecraft.entity.EntityType) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) MobEntity(net.minecraft.entity.MobEntity)

Aggregations

IPlacementHandler (com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler)2 ArrayList (java.util.ArrayList)2 Entity (net.minecraft.entity.Entity)2 EntityType (net.minecraft.entity.EntityType)2 MobEntity (net.minecraft.entity.MobEntity)2 ItemStack (net.minecraft.item.ItemStack)2 CompoundNBT (net.minecraft.nbt.CompoundNBT)2 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)2 BlockPos (net.minecraft.util.math.BlockPos)2 Vector3d (net.minecraft.util.math.vector.Vector3d)2