Package forestry.mail

Source Code of forestry.mail.MailAddress

/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.mail;

import com.mojang.authlib.GameProfile;
import forestry.api.core.INBTTagable;
import forestry.api.mail.EnumAddressee;
import forestry.api.mail.IMailAddress;
import forestry.core.network.EntityNetData;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil;

import java.util.Locale;
import java.util.UUID;

public class MailAddress implements INBTTagable, IMailAddress {
  private static final GameProfile invalidGameProfile = new GameProfile(new UUID(0, 0), "");
  @EntityNetData
  private EnumAddressee type;
  @EntityNetData
  private GameProfile gameProfile; // gameProfile is a fake GameProfile for traders, and real for players

  public MailAddress() {
    this.type = EnumAddressee.PLAYER;
    this.gameProfile = invalidGameProfile;
  }

  public MailAddress(GameProfile gameProfile) {
    if (gameProfile == null)
      throw new IllegalArgumentException("gameProfile must not be null");

    this.type = EnumAddressee.PLAYER;
    this.gameProfile = gameProfile;
  }

  public MailAddress(String name) {
    if (name == null)
      throw new IllegalArgumentException("name must not be null");

    this.type = EnumAddressee.TRADER;
    this.gameProfile = new GameProfile(new UUID(0, 0), name);
  }

  public MailAddress(IMailAddress address) {
    this.type = address.getType();
    if (type == EnumAddressee.TRADER) {
      String name = address.getName();
      this.gameProfile = new GameProfile(new UUID(0, 0), name);
    } else if (type == EnumAddressee.PLAYER) {
      this.gameProfile = address.getPlayerProfile();
    }
  }

  public static MailAddress loadFromNBT(NBTTagCompound nbttagcompound) {
    MailAddress address = new MailAddress();
    address.readFromNBT(nbttagcompound);
    return address;
  }

  @Override
  public EnumAddressee getType() {
    return type;
  }

  @Override
  public String getName() {
    return gameProfile.getName();
  }

  @Override
  public boolean isValid() {
    return !gameProfile.equals(invalidGameProfile);
  }

  @Override
  public boolean isPlayer() {
    return type == EnumAddressee.PLAYER;
  }

  @Override
  public boolean isTrader() {
    return type == EnumAddressee.TRADER;
  }

  @Override
  public GameProfile getPlayerProfile() {
    if (!this.isPlayer())
      return null;
    return gameProfile;
  }

  @Override
  public int hashCode() {
    return gameProfile.hashCode();
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof MailAddress))
      return false;

    MailAddress address = (MailAddress) o;
    return address.gameProfile.equals(gameProfile);
  }

  @Override
  public String toString() {
    String name = getName().toLowerCase(Locale.ENGLISH);
    if (isPlayer()) {
      return type + "-" + name + "-" + gameProfile.getId();
    } else {
      return type + "-" + name;
    }
  }

  @Override
  public void readFromNBT(NBTTagCompound nbttagcompound) {
    type = null;
    if (nbttagcompound.hasKey("TP")) {
      String typeName = nbttagcompound.getString("TP");
      type = EnumAddressee.fromString(typeName);
    }

    if (type == null) {
      type = EnumAddressee.PLAYER;
      gameProfile = invalidGameProfile;
    } else if (nbttagcompound.hasKey("profile")) {
      NBTTagCompound profileTag = nbttagcompound.getCompoundTag("profile");
      gameProfile = NBTUtil.func_152459_a(profileTag);
    }
  }

  @Override
  public void writeToNBT(NBTTagCompound nbttagcompound) {
    nbttagcompound.setString("TP", type.toString());

    if (gameProfile != null) {
      NBTTagCompound profileNbt = new NBTTagCompound();
      NBTUtil.func_152460_a(profileNbt, gameProfile);
      nbttagcompound.setTag("profile", profileNbt);
    }
  }
}
TOP

Related Classes of forestry.mail.MailAddress

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.