Search in sources :

Example 1 with SkinSaveException

use of riskyken.armourersWorkshop.common.exception.SkinSaveException in project Armourers-Workshop by RiskyKen.

the class ArmourerWorldHelper method saveSkinFromWorld.

/**
 * Converts blocks in the world into a skin class.
 * @param world The world.
 * @param skinProps The skin properties for this skin.
 * @param skinType The type of skin to save.
 * @param paintData Paint data for this skin.
 * @param xCoord Armourers x location.
 * @param yCoord Armourers y location.
 * @param zCoord Armourers z location.
 * @param directionDirection the armourer is facing.
 * @return
 * @throws InvalidCubeTypeException
 * @throws SkinSaveException
 */
public static Skin saveSkinFromWorld(World world, SkinProperties skinProps, ISkinType skinType, int[] paintData, int xCoord, int yCoord, int zCoord, ForgeDirection direction) throws SkinSaveException {
    ArrayList<SkinPart> parts = new ArrayList<SkinPart>();
    if (skinType == SkinTypeRegistry.skinBlock) {
        ISkinPartType partType = ((SkinBlock) SkinTypeRegistry.skinBlock).partBase;
        if (SkinProperties.PROP_BLOCK_MULTIBLOCK.getValue(skinProps)) {
            partType = ((SkinBlock) SkinTypeRegistry.skinBlock).partMultiblock;
        }
        SkinPart skinPart = saveArmourPart(world, partType, xCoord, yCoord, zCoord, direction, true);
        if (skinPart != null) {
            parts.add(skinPart);
        }
    } else {
        for (int i = 0; i < skinType.getSkinParts().size(); i++) {
            ISkinPartType partType = skinType.getSkinParts().get(i);
            SkinPart skinPart = saveArmourPart(world, partType, xCoord, yCoord, zCoord, direction, true);
            if (skinPart != null) {
                parts.add(skinPart);
            }
        }
    }
    if (paintData != null) {
        paintData = paintData.clone();
    }
    Skin skin = new Skin(skinProps, skinType, paintData, parts);
    // Check if there are any blocks in the build guides.
    if (skin.getParts().size() == 0 && !skin.hasPaintData()) {
        throw new SkinSaveException("Nothing to save.", SkinSaveExceptionType.NO_DATA);
    }
    // Check if the skin has all needed parts.
    for (int i = 0; i < skinType.getSkinParts().size(); i++) {
        ISkinPartType partType = skinType.getSkinParts().get(i);
        if (partType.isPartRequired()) {
            boolean havePart = false;
            for (int j = 0; j < skin.getPartCount(); j++) {
                if (partType == skin.getParts().get(j).getPartType()) {
                    havePart = true;
                    break;
                }
            }
            if (!havePart) {
                throw new SkinSaveException("Skin is missing part " + partType.getPartName(), SkinSaveExceptionType.MISSING_PARTS);
            }
        }
    }
    // Check if the skin is not a seat and a bed.
    if (SkinProperties.PROP_BLOCK_BED.getValue(skinProps) & SkinProperties.PROP_BLOCK_SEAT.getValue(skinProps)) {
        throw new SkinSaveException("Skin can not be a bed and a seat.", SkinSaveExceptionType.BED_AND_SEAT);
    }
    // Check if multiblock is valid.
    if (skinType == SkinTypeRegistry.skinBlock & SkinProperties.PROP_BLOCK_MULTIBLOCK.getValue(skinProps)) {
        SkinPart testPart = saveArmourPart(world, ((SkinBlock) SkinTypeRegistry.skinBlock).partBase, xCoord, yCoord, zCoord, direction, true);
        if (testPart == null) {
            throw new SkinSaveException("Multiblock has no blocks in the yellow area.", SkinSaveExceptionType.INVALID_MULTIBLOCK);
        }
    }
    return skin;
}
Also used : ISkinPartType(riskyken.armourersWorkshop.api.common.skin.type.ISkinPartType) SkinSaveException(riskyken.armourersWorkshop.common.exception.SkinSaveException) SkinBlock(riskyken.armourersWorkshop.common.skin.type.block.SkinBlock) ArrayList(java.util.ArrayList) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) SkinPart(riskyken.armourersWorkshop.common.skin.data.SkinPart)

Example 2 with SkinSaveException

use of riskyken.armourersWorkshop.common.exception.SkinSaveException in project Armourers-Workshop by RiskyKen.

the class ArmourerWorldHelper method saveArmourPart.

private static SkinPart saveArmourPart(World world, ISkinPartType skinPart, int xCoord, int yCoord, int zCoord, ForgeDirection direction, boolean markerCheck) throws SkinSaveException {
    int cubeCount = getNumberOfCubesInPart(world, xCoord, yCoord, zCoord, skinPart);
    if (cubeCount < 1) {
        return null;
    }
    SkinCubeData cubeData = new SkinCubeData();
    cubeData.setCubeCount(cubeCount);
    ArrayList<CubeMarkerData> markerBlocks = new ArrayList<CubeMarkerData>();
    IRectangle3D buildSpace = skinPart.getBuildingSpace();
    IPoint3D offset = skinPart.getOffset();
    int i = 0;
    for (int ix = 0; ix < buildSpace.getWidth(); ix++) {
        for (int iy = 0; iy < buildSpace.getHeight(); iy++) {
            for (int iz = 0; iz < buildSpace.getDepth(); iz++) {
                int x = xCoord + ix + -offset.getX() + buildSpace.getX();
                int y = yCoord + iy + -offset.getY();
                int z = zCoord + iz + offset.getZ() + buildSpace.getZ();
                int xOrigin = -ix + -buildSpace.getX();
                int yOrigin = -iy + -buildSpace.getY();
                int zOrigin = -iz + -buildSpace.getZ();
                if (!world.isAirBlock(x, y, z)) {
                    Block block = world.getBlock(x, y, z);
                    if (CubeRegistry.INSTANCE.isBuildingBlock(block)) {
                        saveArmourBlockToList(world, x, y, z, xOrigin - 1, yOrigin - 1, -zOrigin, cubeData, i, markerBlocks, direction);
                        i++;
                    }
                }
            }
        }
    }
    if (markerCheck) {
        if (skinPart.getMinimumMarkersNeeded() > markerBlocks.size()) {
            throw new SkinSaveException("Missing marker for part " + skinPart.getPartName(), SkinSaveExceptionType.MARKER_ERROR);
        }
        if (markerBlocks.size() > skinPart.getMaximumMarkersNeeded()) {
            throw new SkinSaveException("Too many markers for part " + skinPart.getPartName(), SkinSaveExceptionType.MARKER_ERROR);
        }
    }
    return new SkinPart(cubeData, skinPart, markerBlocks);
}
Also used : SkinSaveException(riskyken.armourersWorkshop.common.exception.SkinSaveException) SkinCubeData(riskyken.armourersWorkshop.common.skin.data.SkinCubeData) IPoint3D(riskyken.armourersWorkshop.api.common.IPoint3D) CubeMarkerData(riskyken.armourersWorkshop.common.skin.cubes.CubeMarkerData) IRectangle3D(riskyken.armourersWorkshop.api.common.IRectangle3D) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) SkinBlock(riskyken.armourersWorkshop.common.skin.type.block.SkinBlock) SkinPart(riskyken.armourersWorkshop.common.skin.data.SkinPart)

Example 3 with SkinSaveException

use of riskyken.armourersWorkshop.common.exception.SkinSaveException in project Armourers-Workshop by RiskyKen.

the class TileEntityArmourer method saveArmourItem.

/**
 * Get blocks in the world and saved them onto an items NBT data.
 * @param player The player that pressed the save button.
 * @param name Custom name for the item.
 */
public void saveArmourItem(EntityPlayerMP player, String customName, String tags) {
    if (getWorldObj().isRemote) {
        return;
    }
    ItemStack stackInput = getStackInSlot(0);
    ItemStack stackOutput = getStackInSlot(1);
    if (stackInput == null) {
        return;
    }
    if (stackOutput != null) {
        return;
    }
    if (!(stackInput.getItem() instanceof ISkinHolder)) {
        return;
    }
    ISkinHolder inputItem = (ISkinHolder) stackInput.getItem();
    Skin armourItemData = null;
    SkinProperties skinProps = new SkinProperties();
    skinProps.setProperty(Skin.KEY_AUTHOR_NAME, player.getCommandSenderName());
    if (player.getGameProfile() != null && player.getGameProfile().getId() != null) {
        skinProps.setProperty(Skin.KEY_AUTHOR_UUID, player.getGameProfile().getId().toString());
    }
    skinProps.setProperty(Skin.KEY_CUSTOM_NAME, customName);
    for (int i = 0; i < skinType.getProperties().size(); i++) {
        SkinProperty skinProp = (SkinProperty) skinType.getProperties().get(i);
        skinProp.setValue(skinProps, skinProp.getValue(this.skinProps));
    }
    try {
        armourItemData = ArmourerWorldHelper.saveSkinFromWorld(worldObj, skinProps, skinType, paintData, xCoord, yCoord + HEIGHT_OFFSET, zCoord, direction);
    } catch (SkinSaveException e) {
        switch(e.getType()) {
            case NO_DATA:
                player.addChatMessage(new ChatComponentText(e.getMessage()));
                break;
            case MARKER_ERROR:
                player.addChatMessage(new ChatComponentText(e.getMessage()));
                break;
            case MISSING_PARTS:
                player.addChatMessage(new ChatComponentText(e.getMessage()));
                break;
            case BED_AND_SEAT:
                player.addChatMessage(new ChatComponentText(e.getMessage()));
                break;
            case INVALID_MULTIBLOCK:
                player.addChatMessage(new ChatComponentText(e.getMessage()));
                break;
        }
    }
    if (armourItemData == null) {
        return;
    }
    CommonSkinCache.INSTANCE.addEquipmentDataToCache(armourItemData, (LibraryFile) null);
    stackOutput = inputItem.makeStackForEquipment(armourItemData);
    if (stackOutput == null) {
        return;
    }
    this.decrStackSize(0, 1);
    setInventorySlotContents(1, stackOutput);
}
Also used : SkinSaveException(riskyken.armourersWorkshop.common.exception.SkinSaveException) ISkinHolder(riskyken.armourersWorkshop.common.skin.ISkinHolder) ItemSkin(riskyken.armourersWorkshop.common.items.ItemSkin) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) ItemStack(net.minecraft.item.ItemStack) SkinProperty(riskyken.armourersWorkshop.common.skin.data.SkinProperty) SkinProperties(riskyken.armourersWorkshop.common.skin.data.SkinProperties) ChatComponentText(net.minecraft.util.ChatComponentText)

Aggregations

SkinSaveException (riskyken.armourersWorkshop.common.exception.SkinSaveException)3 ArrayList (java.util.ArrayList)2 Skin (riskyken.armourersWorkshop.common.skin.data.Skin)2 SkinPart (riskyken.armourersWorkshop.common.skin.data.SkinPart)2 SkinBlock (riskyken.armourersWorkshop.common.skin.type.block.SkinBlock)2 Block (net.minecraft.block.Block)1 ItemStack (net.minecraft.item.ItemStack)1 ChatComponentText (net.minecraft.util.ChatComponentText)1 IPoint3D (riskyken.armourersWorkshop.api.common.IPoint3D)1 IRectangle3D (riskyken.armourersWorkshop.api.common.IRectangle3D)1 ISkinPartType (riskyken.armourersWorkshop.api.common.skin.type.ISkinPartType)1 ItemSkin (riskyken.armourersWorkshop.common.items.ItemSkin)1 ISkinHolder (riskyken.armourersWorkshop.common.skin.ISkinHolder)1 CubeMarkerData (riskyken.armourersWorkshop.common.skin.cubes.CubeMarkerData)1 SkinCubeData (riskyken.armourersWorkshop.common.skin.data.SkinCubeData)1 SkinProperties (riskyken.armourersWorkshop.common.skin.data.SkinProperties)1 SkinProperty (riskyken.armourersWorkshop.common.skin.data.SkinProperty)1