com.turt2live.xmail.mail.attachment.Attachment.java Source code

Java tutorial

Introduction

Here is the source code for com.turt2live.xmail.mail.attachment.Attachment.java

Source

/*******************************************************************************
 * Copyright (C) 2014 Travis Ralston (turt2live)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

package com.turt2live.xmail.mail.attachment;

import com.turt2live.xmail.XMail;
import com.turt2live.xmail.entity.XMailEntity;
import com.turt2live.xmail.mail.Mail;
import com.turt2live.xmail.utils.General;
import com.turt2live.xmail.utils.MoneyFormat;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.json.simple.JSONValue;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Represents an attachment
 *
 * @author turt2live
 */
public abstract class Attachment implements Cloneable, Serializable {

    private static final long serialVersionUID = 8143127181955049611L;

    /**
     * Represents an attachment type
     *
     * @author turt2live
     */
    public static enum AttachmentType {
        COD, CUSTOM, ITEM, MAIL, MESSAGE, MONEY, UNKNOWN;

        public static AttachmentType fromName(String type) {
            for (AttachmentType t : values()) {
                if (t.name().equalsIgnoreCase(type)) {
                    return t;
                }
            }
            return AttachmentType.UNKNOWN;
        }
    }

    protected XMail plugin = XMail.getInstance();

    @Override
    public abstract Attachment clone();

    /**
     * Gets the value (content) of this attachment
     *
     * @return the value
     */
    public abstract Object getContent();

    /**
     * Gets the value (content) of this attachment when serialized
     *
     * @return the value
     */
    public abstract Object getSerializedContent();

    /**
     * Gets the attachment type
     *
     * @return the attachment type
     */
    public abstract AttachmentType getType();

    /**
     * Gives this attachment to an xMail entity
     *
     * @param entity the entity to give the attachment to
     *
     * @return true if the attachment was given to the entity
     */
    public abstract boolean giveTo(XMailEntity entity);

    /**
     * Called when the attachment is given to someone
     *
     * @param to   who the attachment was given to
     * @param mail the mail object this attachment was applied to
     */
    public abstract void onGive(XMailEntity to, Mail mail);

    /**
     * Converts this attachment to JSON in to format <br>
     * <code>{"type":{@link #getType()}.name(), "content":{@link #getSerializedContent()}}</code>
     *
     * @return the valid JSON
     */
    public final String toJson() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("type", getType().name());
        map.put("content", getSerializedContent());
        return JSONValue.toJSONString(map);
    }

    @Override
    public String toString() {
        return "Attachment [toJson()=" + toJson() + ", getType()=" + getType() + ", getContent()=" + getContent()
                + "]";
    }

    // TODO: Attempt to reduce copy/paste

    /**
     * Gets the receive text for a mail item (Eg: "You got 4 items!")
     *
     * @param mail   the mail object
     * @param failed the attachments that did not send to the player
     *
     * @return the text to send to the player (no colors)
     */
    public static String getReceiveText(Mail mail, List<Attachment> failed) {
        double cod, money = cod = 0;
        int items = 0, unknown = 0;
        Map<Material, Integer> itemCount = new HashMap<Material, Integer>();
        for (Attachment a : mail.getAttachments()) {
            if (!failed.contains(a)) {
                switch (a.getType()) {
                case MONEY:
                    money += ((MoneyAttachment) a).getAmount();
                    break;
                case COD:
                    cod += ((CODAttachment) a).getAmount();
                    break;
                case ITEM:
                    ItemStack stack = ((ItemAttachment) a).getItemStack();
                    int c = 1;
                    if (itemCount.containsKey(stack.getType())) {
                        c += itemCount.get(stack.getType());
                    }
                    itemCount.put(stack.getType(), c);
                    items += stack.getAmount();
                case MESSAGE:
                    break;
                default:
                    unknown++;
                    break;
                }
            }
        }
        if (cod == 0 && money == 0 && items == 0 && unknown == 0) {
            return "You got no attachments!";
        }
        StringBuilder message = new StringBuilder();
        message.append("You ");
        if (cod > 0) {
            message.append("paid $").append(MoneyFormat.format(cod)).append(" in C.O.D and got ");
        } else {
            message.append("got ");
        }
        if (money > 0) {
            message.append("$").append(MoneyFormat.format(money)).append(", ");
        }
        if (items > 0) {
            if (itemCount.keySet().size() >= 30) {
                message.append(items).append(" items, ");
            } else {
                for (Material item : itemCount.keySet()) {
                    message.append(itemCount.get(item)).append("x ").append(General.convertItem(item)).append(", ");
                }
            }
        }
        if (unknown > 0) {
            message.append(unknown).append(" other thing").append(unknown > 1 ? "s, " : ", ");
        }
        String msg = message.toString().trim();
        return msg.substring(0, msg.length() - 1);
    }

    /**
     * Gets a signature (for attachments) of a mail message. This will return something like "1x Dirt, $4.00"
     *
     * @param mail the mail object
     *
     * @return the message, may be an empty string
     */
    public static String getSignature(Mail mail) {
        double cod, money = cod = 0;
        int items = 0, unknown = 0;
        Map<Material, Integer> itemCount = new HashMap<Material, Integer>();
        for (Attachment a : mail.getAttachments()) {
            switch (a.getType()) {
            case MONEY:
                money += ((MoneyAttachment) a).getAmount();
                break;
            case COD:
                cod += ((CODAttachment) a).getAmount();
                break;
            case ITEM:
                ItemStack stack = ((ItemAttachment) a).getItemStack();
                int c = 1;
                if (itemCount.containsKey(stack.getType())) {
                    c += itemCount.get(stack.getType());
                }
                itemCount.put(stack.getType(), c);
                items += stack.getAmount();
            case MESSAGE:
                break;
            default:
                unknown++;
                break;
            }
        }
        if (cod == 0 && money == 0 && items == 0 && unknown == 0) {
            return "No attachments";
        }
        StringBuilder message = new StringBuilder();
        if (cod > 0) {
            message.append("$").append(MoneyFormat.format(cod)).append(" C.O.D,");
        }
        if (money > 0) {
            message.append("$").append(MoneyFormat.format(money)).append(", ");
        }
        if (items > 0) {
            if (itemCount.keySet().size() >= 30) {
                message.append(items).append(" items, ");
            } else {
                for (Material item : itemCount.keySet()) {
                    message.append(itemCount.get(item)).append("x ").append(General.convertItem(item)).append(", ");
                }
            }
        }
        if (unknown > 0) {
            message.append(unknown).append(" other thing").append(unknown > 1 ? "s, " : ", ");
        }
        String msg = message.toString().trim();
        return msg.substring(0, msg.length() - 1);
    }

}