Blame view

src/main/java/net/eq2online/permissions/ReplicatedPermissionsContainer.java 3.61 KB
1
2
3
4
5
/*
 * This file is part of LiteLoader.
 * Copyright (C) 2012-16 Adam Mummery-Smith
 * All Rights Reserved.
 */
Mumfrey authored
6
7
package net.eq2online.permissions;
8
9
10
11
12
13
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
Mumfrey authored
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;

import net.minecraft.network.PacketBuffer;

/**
 * Serializable container object
 * 
 * @author Adam Mummery-Smith
 */
public class ReplicatedPermissionsContainer implements Serializable
{
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    /**
     * Serial version UID to suppoer Serializable interface 
     */
    private static final long serialVersionUID = -764940324881984960L;

    /**
     * Mod name
     */
    public String modName = "all";

    /**
     * Mod version
     */
    public Float modVersion = 0.0F;

    /**
43
44
     * List of permissions to replicate, prepend "-" for a negated permission
     * and "+" for a granted permission.
45
46
47
48
     */
    public Set<String> permissions = new TreeSet<String>();

    /**
49
50
     * Amount of time in seconds that the client will trust these permissions
     * for before requesting an update. 
51
     */
52
    public long remoteCacheTimeSeconds = 600L;  // 10 minutes
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

    public static final String CHANNEL = "PERMISSIONSREPL";

    public ReplicatedPermissionsContainer()
    {
    }

    public ReplicatedPermissionsContainer(String modName, Float modVersion, Collection<String> permissions)
    {
        this.modName = modName;
        this.modVersion = modVersion;
        this.permissions.addAll(permissions);
    }

    /**
     * Add all of the listed permissions to this container
     * 
     * @param permissions
     */
    public void addAll(Collection<String> permissions)
    {
        this.permissions.addAll(permissions);
    }

    /**
     * Check and correct  
     */
    public void sanitise()
    {
        if (this.modName == null || this.modName.length() < 1) this.modName = "all";
        if (this.modVersion == null || this.modVersion < 0.0F) this.modVersion = 0.0F;
        if (this.remoteCacheTimeSeconds < 0) this.remoteCacheTimeSeconds = 600L;
    }

    /**
88
89
     * Serialise this container to a byte array for transmission to a remote
     * host.
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
     */
    public byte[] getBytes()
    {
        try
        {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            new ObjectOutputStream(byteStream).writeObject(this);
            return byteStream.toByteArray();
        }
        catch (IOException e) {}

        return new byte[0];
    }

    /**
     * Deserialises a replicated permissions container from a byte array
     * 
     * @param data Byte array containing the serialised data
     * @return new container or null if deserialisation failed
     */
    public static ReplicatedPermissionsContainer fromPacketBuffer(PacketBuffer data)
    {
        try
        {
            int readableBytes = data.readableBytes();
            if (readableBytes == 0) return null;

            byte[] payload = new byte[readableBytes];
            data.readBytes(payload);

            ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(payload));
            ReplicatedPermissionsContainer object = (ReplicatedPermissionsContainer)inputStream.readObject();
            return object;
        }
124
125
126
127
128
129
130
131
132
133
134
135
        catch (IOException e)
        {
            // Don't care
        }
        catch (ClassNotFoundException e)
        {
            // Don't care
        }
        catch (ClassCastException e)
        {
            // Don't care
        }
136
137
138

        return null;
    }
Mumfrey authored
139
}