-
Notifications
You must be signed in to change notification settings - Fork 79
/
VoxelBlobStateReference.java
181 lines (147 loc) · 3.76 KB
/
VoxelBlobStateReference.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package mod.chiselsandbits.chiseledblock.data;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import mod.chiselsandbits.chiseledblock.BoxType;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.fml.common.FMLCommonHandler;
public final class VoxelBlobStateReference implements Comparable<VoxelBlobStateReference>
{
private static Map<VoxelBlobStateInstance, WeakReference<VoxelBlobStateInstance>> serverRefs = Collections.synchronizedMap( new WeakHashMap<VoxelBlobStateInstance, WeakReference<VoxelBlobStateInstance>>() );
private static Map<VoxelBlobStateInstance, WeakReference<VoxelBlobStateInstance>> clientRefs = Collections.synchronizedMap( new WeakHashMap<VoxelBlobStateInstance, WeakReference<VoxelBlobStateInstance>>() );
// optimize air.
private static byte[] airBlob;
private static Map<VoxelBlobStateInstance, WeakReference<VoxelBlobStateInstance>> getRefs()
{
if ( FMLCommonHandler.instance().getEffectiveSide().isClient() )
{
return clientRefs;
}
return serverRefs;
}
private static VoxelBlobStateInstance lookupRef(
final VoxelBlobStateInstance inst )
{
final WeakReference<VoxelBlobStateInstance> ref = getRefs().get( inst );
if ( ref != null )
{
return ref.get();
}
return null;
}
private static byte[] findBytesFor(
final int stateId )
{
if ( stateId == 0 )
{
if ( airBlob == null )
{
final VoxelBlob vb = new VoxelBlob();
airBlob = vb.blobToBytes( VoxelBlob.VERSION_COMPACT );
}
return airBlob;
}
final VoxelBlob vb = new VoxelBlob();
vb.fill( stateId );
return vb.blobToBytes( VoxelBlob.VERSION_COMPACT );
}
private static void addRef(
final VoxelBlobStateInstance inst )
{
getRefs().put( inst, new WeakReference<VoxelBlobStateInstance>( inst ) );
}
private static VoxelBlobStateInstance FindRef(
final byte[] v )
{
final VoxelBlobStateInstance t = new VoxelBlobStateInstance( v );
VoxelBlobStateInstance ref = null;
ref = lookupRef( t );
if ( ref == null )
{
ref = t;
addRef( t );
}
return ref;
}
private final VoxelBlobStateInstance data;
public final long weight;
public VoxelBlobStateInstance getInstance()
{
return data;
}
public byte[] getByteArray()
{
return data.voxelBytes;
}
public VoxelBlob getVoxelBlob()
{
return data.getBlob();
}
public VoxelBlob getVoxelBlobCatchable() throws Exception
{
return data.getBlobCatchable();
}
public VoxelBlobStateReference(
final VoxelBlob blob,
final long weight )
{
this( blob.blobToBytes( VoxelBlob.VERSION_COMPACT ), weight );
data.blob = new SoftReference<VoxelBlob>( new VoxelBlob( blob ) );
}
public VoxelBlobStateReference(
final int stateId,
final long weight )
{
this( findBytesFor( stateId ), weight );
}
public VoxelBlobStateReference(
final byte[] v,
final long weight )
{
data = FindRef( v );
this.weight = weight;
}
@Override
public boolean equals(
final Object obj )
{
if ( !( obj instanceof VoxelBlobStateReference ) )
{
return false;
}
final VoxelBlobStateReference second = (VoxelBlobStateReference) obj;
return data.equals( second.data ) && second.weight == weight;
}
@Override
public int hashCode()
{
return data.hash ^ (int) ( weight ^ weight >>> 32 );
}
@Override
public int compareTo(
final VoxelBlobStateReference o )
{
final int comp = data.compareTo( o.data );
if ( comp == 0 )
{
if ( weight == o.weight )
{
return 0;
}
return weight < o.weight ? -1 : 1;
}
return comp;
}
public Collection<AxisAlignedBB> getBoxes(
final BoxType type )
{
return data.getBoxes( type );
}
public int getFormat()
{
return data.getFormat();
}
}