Arctic_Wolfy

BankAccount

Oct 5th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. package com.bbw2.moneyMod.holders;
  2.  
  3. import com.bbw2.moneyMod.blocks.TEBank;
  4. import com.bbw2.moneyMod.reference.NBTTags;
  5. import com.bbw2.moneyMod.utility.LogHelper;
  6. import cpw.mods.fml.common.FMLCommonHandler;
  7. import net.minecraft.client.Minecraft;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.server.MinecraftServer;
  11. import net.minecraft.world.World;
  12.  
  13. import java.util.UUID;
  14.  
  15. /**
  16.  * Created on 9/30/2015.
  17.  */
  18. public class BankAccount {
  19.     private TEBank bank;
  20.     private EntityPlayer player;
  21.     private UUID id;
  22.     private float credit;
  23.  
  24.     public BankAccount(TEBank bank, EntityPlayer player){this(bank, player, 0F);}
  25.     public BankAccount(TEBank bank, UUID id){this(bank, id, 0F);}
  26.  
  27.     public BankAccount(TEBank bank, EntityPlayer player, float credit){
  28.         this.bank = bank;
  29.         this.player = player;
  30.         this.id = player.getGameProfile().getId();
  31.         this.credit = credit;
  32.     }
  33.  
  34.     public BankAccount(TEBank bank, UUID id, float credit){
  35.         this.bank = bank;
  36.         this.id = id;
  37.         this.credit = credit;
  38.     }
  39.  
  40.     public EntityPlayer getPlayer() {
  41.         if (player == null) {
  42.             player = Minecraft.getMinecraft().theWorld.func_152378_a(id);
  43.             if (player==null){}
  44.         }
  45.         return player;
  46.     }
  47.  
  48.     public TEBank getBank() {return bank;}
  49.     public UUID getId() {return id;}
  50.     public float getCredit() {return credit;}
  51.     public void addCredit(float amount){this.credit += amount;}
  52.     public void removeCredit(float amount){this.credit -= amount;}
  53.  
  54.     public static NBTTagCompound toNBT(BankAccount account, boolean forCard){
  55.         NBTTagCompound tag = new NBTTagCompound();
  56.         NBTTagCompound ownerTag = new NBTTagCompound();
  57.         UUID id = account.id;
  58.         TEBank bank = account.bank;
  59.         long ownerIDLeast = id.getLeastSignificantBits();
  60.         long ownerIDMost = id.getMostSignificantBits();
  61.         ownerTag.setLong(NBTTags.Card.UUID_LEAST_TAG,ownerIDLeast);
  62.         ownerTag.setLong(NBTTags.Card.UUID_MOST_TAG, ownerIDMost);
  63.         tag.setTag(NBTTags.Card.UUID_OWNER_TAG, ownerTag);
  64.         tag.setIntArray(NBTTags.Bank.COORDS, new int[]{
  65.                 bank.xCoord,bank.yCoord,bank.zCoord
  66.         });
  67.         if (!forCard)tag.setFloat(NBTTags.Bank.CREDIT,account.credit);
  68.         return tag;
  69.     }
  70.  
  71.  
  72.  
  73.     public static BankAccount fromNBT(World world, NBTTagCompound tag, TEBank bank){
  74.         //TEBank bank;
  75.         float credit;
  76.  
  77.         boolean forCard = false;
  78.  
  79.         LogHelper.info("Bank");
  80.  
  81.         if (bank == null) {
  82.             int[] XYZ = tag.getIntArray(NBTTags.Bank.COORDS);
  83.             if (world==null){
  84.                 if (FMLCommonHandler.instance().getEffectiveSide().isClient()){
  85.                     world = Minecraft.getMinecraft().theWorld;
  86.                 } else if (FMLCommonHandler.instance().getEffectiveSide().isServer()){
  87.                     world = MinecraftServer.getServer().worldServerForDimension(0);
  88.                 }
  89.             }
  90.             if (world !=null) bank = (TEBank) world.getTileEntity(XYZ[0],XYZ[1],XYZ[2]);
  91.  
  92.             forCard = true;
  93.         }
  94.  
  95.         long ownerIDLeast, ownerIDMost;
  96.  
  97.         NBTTagCompound ownerTag = tag.getCompoundTag(NBTTags.Card.UUID_OWNER_TAG);
  98.  
  99.         ownerIDLeast = ownerTag.getLong(NBTTags.Card.UUID_LEAST_TAG);
  100.         ownerIDMost = ownerTag.getLong(NBTTags.Card.UUID_MOST_TAG);
  101.  
  102.         UUID id = new UUID(ownerIDMost,ownerIDLeast);
  103.  
  104.         if (bank!=null) {
  105.             BankAccount account = bank.getAccount(id);
  106.             if (account != null) {
  107.                 return account;
  108.             }
  109.         }
  110.  
  111.         if (forCard){
  112.             return new BankAccount(bank,id);
  113.         } else {
  114.             credit = tag.getFloat(NBTTags.Bank.CREDIT);
  115.             return new BankAccount(bank,id,credit);
  116.         }
  117.     }
  118.  
  119.     public static BankAccount getAccountFromBank(UUID id, int x, int y, int z){
  120.         return getAccountFromBank(id,(TEBank) Minecraft.getMinecraft().theWorld.getTileEntity(x,y,z));
  121.     }
  122.  
  123.     public static BankAccount getAccountFromBank(UUID id, TEBank bank) {
  124.         return bank.getAccount(id);
  125.     }
  126.  
  127.     public static BankAccount fromNBT(NBTTagCompound tag, TEBank bank) {
  128.         return fromNBT(null,tag,bank);
  129.     }
  130. }
Add Comment
Please, Sign In to add comment