Search in sources :

Example 1 with Skin

use of riskyken.armourersWorkshop.common.skin.data.Skin 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 Skin

use of riskyken.armourersWorkshop.common.skin.data.Skin in project Armourers-Workshop by RiskyKen.

the class ByteBufHelper method readSkinFromByteBuf.

public static Skin readSkinFromByteBuf(ByteBuf buf) {
    boolean compressed = buf.readBoolean();
    byte[] skinData = readByteArrayFromByteBuf(buf);
    if (compressed) {
        skinData = decompressByteArray(skinData);
    }
    if (skinData == null) {
        ModLogger.log(Level.ERROR, "Failed to decompress skin data.");
        return null;
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(skinData);
    DataInputStream dataInputStream = new DataInputStream(bais);
    Skin skin = null;
    try {
        skin = SkinSerializer.readSkinFromStream(dataInputStream);
        skin.requestId = SkinIdentifierSerializer.readFromStream(dataInputStream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(dataInputStream);
        IOUtils.closeQuietly(bais);
    }
    return skin;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException)

Example 3 with Skin

use of riskyken.armourersWorkshop.common.skin.data.Skin in project Armourers-Workshop by RiskyKen.

the class ByteBufHelper method convertByteArrayToSkin.

public static Skin convertByteArrayToSkin(byte[] data) {
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    DataInputStream dataInputStream = new DataInputStream(bais);
    Skin skin = null;
    try {
        skin = SkinSerializer.readSkinFromStream(dataInputStream);
        skin.requestId = SkinIdentifierSerializer.readFromStream(dataInputStream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(dataInputStream);
        IOUtils.closeQuietly(bais);
    }
    return skin;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException)

Example 4 with Skin

use of riskyken.armourersWorkshop.common.skin.data.Skin in project Armourers-Workshop by RiskyKen.

the class SkinUploadHelper method gotSkinPartFromClient.

public static void gotSkinPartFromClient(int skinId, byte packetId, byte[] skinData, EntityPlayerMP player) {
    boolean lastPacket = skinData.length < MAX_PACKET_SIZE;
    byte[] oldSkinData = unfinishedSkins.get(skinId);
    byte[] newSkinData = null;
    if (oldSkinData != null) {
        newSkinData = ArrayUtils.addAll(oldSkinData, skinData);
        unfinishedSkins.remove(skinId);
    } else {
        newSkinData = skinData;
    }
    if (!lastPacket) {
        unfinishedSkins.put(skinId, newSkinData);
    } else {
        Skin skin = ByteBufHelper.convertByteArrayToSkin(newSkinData);
        ModLogger.log("Downloaded skin " + skin + " from client " + player);
        Container container = player.openContainer;
        if (!ConfigHandler.allowClientsToUploadSkins) {
            return;
        }
        if (container != null && container instanceof ContainerArmourLibrary) {
            TileEntitySkinLibrary te = ((ContainerArmourLibrary) container).getTileEntity();
            te.loadArmour(skin, player);
        }
    }
}
Also used : ContainerArmourLibrary(riskyken.armourersWorkshop.common.inventory.ContainerArmourLibrary) TileEntitySkinLibrary(riskyken.armourersWorkshop.common.tileentities.TileEntitySkinLibrary) Container(net.minecraft.inventory.Container) Skin(riskyken.armourersWorkshop.common.skin.data.Skin)

Example 5 with Skin

use of riskyken.armourersWorkshop.common.skin.data.Skin in project Armourers-Workshop by RiskyKen.

the class SkinDownloader method downloadSkin.

/**
 * Downloads a fresh skins from the server.
 * @param name
 * @param serverId
 * @return
 */
public static Skin downloadSkin(String fileName, int serverId) {
    Skin skin = null;
    long startTime = System.currentTimeMillis();
    long maxRate = 10;
    InputStream in = null;
    String data = null;
    try {
        in = new URL(String.format("http://plushie.moe/armourers_workshop/download-skin.php?skinid=%d&skinFileName=%s", serverId, fileName)).openStream();
        skin = SkinIOUtils.loadSkinFromStream(new BufferedInputStream(in));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(in);
    }
    long waitTime = maxRate - (System.currentTimeMillis() - startTime);
    if (waitTime > 0) {
        try {
            Thread.sleep(waitTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if (skin != null) {
        skin.serverId = serverId;
    } else {
        ModLogger.log(String.format("Failed to download skin: %s", fileName));
    }
    return skin;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Skin(riskyken.armourersWorkshop.common.skin.data.Skin) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

Skin (riskyken.armourersWorkshop.common.skin.data.Skin)69 SkinPointer (riskyken.armourersWorkshop.common.skin.data.SkinPointer)19 ItemStack (net.minecraft.item.ItemStack)11 ISkinPointer (riskyken.armourersWorkshop.api.common.skin.data.ISkinPointer)11 LibraryFile (riskyken.armourersWorkshop.common.library.LibraryFile)10 SkinIdentifier (riskyken.armourersWorkshop.common.skin.data.SkinIdentifier)10 SkinPart (riskyken.armourersWorkshop.common.skin.data.SkinPart)10 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)9 IOException (java.io.IOException)6 ISkinIdentifier (riskyken.armourersWorkshop.api.common.skin.data.ISkinIdentifier)6 AbstractModelSkin (riskyken.armourersWorkshop.client.model.skin.AbstractModelSkin)6 ItemSkin (riskyken.armourersWorkshop.common.items.ItemSkin)6 Color (java.awt.Color)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DataInputStream (java.io.DataInputStream)4 AbstractClientPlayer (net.minecraft.client.entity.AbstractClientPlayer)4 BakedSkin (riskyken.armourersWorkshop.client.model.bake.ModelBakery.BakedSkin)4 SideOnly (cpw.mods.fml.relauncher.SideOnly)3 BufferedInputStream (java.io.BufferedInputStream)3 ArrayList (java.util.ArrayList)3