aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kubatech/api/tea/TeaNetwork.java
blob: 68c8275bfd63796150dc11ba9dc4eacc17007f44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package kubatech.api.tea;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.UUID;

import net.minecraft.nbt.NBTTagCompound;

import kubatech.savedata.PlayerData;
import kubatech.savedata.PlayerDataManager;
import kubatech.tileentity.TeaStorageTile;

public class TeaNetwork {

    // TODO: Optimize later :P
    public BigInteger teaAmount = BigInteger.ZERO;
    public BigInteger teaLimit = BigInteger.valueOf(Long.MAX_VALUE);
    PlayerData owner;
    private HashSet<TeaStorageTile> teaStorageExtenders = new HashSet<>();

    public static TeaNetwork getNetwork(UUID player) {
        PlayerData p = PlayerDataManager.getPlayer(player);
        if (p == null) return null;
        TeaNetwork n = p.teaNetwork;
        if (n == null) {
            p.teaNetwork = new TeaNetwork();
            p.teaNetwork.owner = p;
            return p.teaNetwork;
        }
        n.owner = p;
        return n;
    }

    public boolean canAfford(long price, boolean take) {
        return canAfford(BigInteger.valueOf(price), take);
    }

    public boolean canAfford(BigInteger price, boolean take) {
        if (teaAmount.compareTo(price) >= 0) {
            if (take) {
                teaAmount = teaAmount.subtract(price);
                markDirty();
            }
            return true;
        }
        return false;
    }

    public boolean addTea(long toAdd) {
        return addTea(BigInteger.valueOf(toAdd));
    }

    public boolean addTea(BigInteger toAdd) {
        BigInteger newValue = teaAmount.add(toAdd);
        if (newValue.compareTo(teaLimit) > 0) return false;
        teaAmount = teaAmount.add(toAdd);
        markDirty();
        return true;
    }

    public boolean canAdd(long toAdd) {
        return canAdd(BigInteger.valueOf(toAdd));
    }

    public boolean canAdd(BigInteger toAdd) {
        return teaAmount.add(toAdd)
            .compareTo(teaLimit) <= 0;
    }

    public void registerTeaStorageExtender(TeaStorageTile storageTile) {
        if (teaStorageExtenders.add(storageTile)) teaLimit = teaLimit.add(storageTile.teaExtendAmount());
    }

    public void unregisterTeaStorageExtender(TeaStorageTile storageTile) {
        if (teaStorageExtenders.remove(storageTile)) teaLimit = teaLimit.subtract(storageTile.teaExtendAmount());
    }

    public void markDirty() {
        owner.markDirty();
    }

    public NBTTagCompound toNBT() {
        NBTTagCompound nbt = new NBTTagCompound();
        nbt.setByteArray("teaAmount", teaAmount.toByteArray());
        return nbt;
    }

    public static TeaNetwork fromNBT(NBTTagCompound nbt) {
        TeaNetwork teaNetwork = new TeaNetwork();
        teaNetwork.teaAmount = new BigInteger(nbt.getByteArray("teaAmount"));
        return teaNetwork;
    }
}