aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/tectech/mechanics/dataTransport/DataPacket.java
blob: 96af1f1afba6ad7120b6b606ff11fe0e6611ab5d (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
94
95
96
97
98
99
100
101
102
103
104
package tectech.mechanics.dataTransport;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import net.minecraft.nbt.NBTTagCompound;

import com.gtnewhorizon.structurelib.util.Vec3Impl;

/**
 * Created by Tec on 05.04.2017.
 */
public abstract class DataPacket<T> {

    private static final byte MAX_HISTORY = 64;
    private final Set<Vec3Impl> trace = new LinkedHashSet<>();

    protected T content;

    protected DataPacket(T content) {
        this.content = content;
    }

    protected DataPacket(NBTTagCompound nbt) {
        content = contentFromNBT(nbt.getCompoundTag("qContent"));
        for (int i = 0; i < nbt.getByte("qHistory"); i++) {
            trace.add(new Vec3Impl(nbt.getInteger("qX" + i), nbt.getInteger("qY" + i), nbt.getInteger("qZ" + i)));
        }
    }

    public final NBTTagCompound toNbt() {
        NBTTagCompound nbt = new NBTTagCompound();
        NBTTagCompound contentTag = contentToNBT();
        if (contentTag != null) {
            nbt.setTag("qContent", contentTag);
        }
        nbt.setByte("qHistory", (byte) trace.size());
        int i = 0;
        for (Vec3Impl v : trace) {
            nbt.setInteger("qX" + i, v.get0());
            nbt.setInteger("qY" + i, v.get1());
            nbt.setInteger("qZ" + i, v.get2());
            i++;
        }
        return nbt;
    }

    protected abstract NBTTagCompound contentToNBT();

    protected abstract T contentFromNBT(NBTTagCompound nbt);

    protected abstract T unifyContentWith(T content);

    public final boolean contains(Vec3Impl v) {
        return trace.contains(v);
    }

    public final boolean check() {
        return trace.size() <= MAX_HISTORY;
    }

    public abstract boolean extraCheck();

    protected final DataPacket<T> unifyTrace(Vec3Impl... positions) {
        Collections.addAll(trace, positions);
        return (check() && extraCheck()) ? this : null;
    }

    protected final DataPacket<T> unifyTrace(DataPacket<T> p) {
        if (p == null) return this;
        trace.addAll(p.trace);
        return (check() && extraCheck()) ? this : null;
    }

    protected final DataPacket<T> unifyWith(DataPacket<T> p) {
        if (p == null) return this;
        trace.addAll(p.trace);
        if (check() && extraCheck()) {
            content = unifyContentWith(p.content);
            return this;
        }
        return null;
    }

    public final T contentIfNotInTrace(Vec3Impl pos) {
        if (trace.contains(pos)) {
            return null;
        }
        return getContent();
    }

    public T getContent() {
        return content;
    }

    public String getContentString() {
        return content.toString();
    }

    public final int getTraceSize() {
        return trace.size();
    }
}