ReplicatedPermissionsContainer.java
2.79 KB
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
105
106
107
108
109
110
111
package net.eq2online.permissions;
import java.io.*;
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
{
/**
* 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;
/**
* List of permissions to replicate, prepend "-" for a negated permission and "+" for a granted permission
*/
public Set<String> permissions = new TreeSet<String>();
/**
* Amount of time in seconds that the client will trust these permissions for before requesting an update
*/
public long remoteCacheTimeSeconds = 600L; // 10 minutes
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;
}
/**
* Serialise this container to a byte array for transmission to a remote host
*/
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
{
ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(data.readByteArray()));
ReplicatedPermissionsContainer object = (ReplicatedPermissionsContainer)inputStream.readObject();
return object;
}
catch (IOException e) { }
catch (ClassNotFoundException e) { }
catch (ClassCastException e) { }
return null;
}
}