-
Notifications
You must be signed in to change notification settings - Fork 79
/
CrossWorldBlobSerializer.java
176 lines (147 loc) · 4.01 KB
/
CrossWorldBlobSerializer.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
package mod.chiselsandbits.chiseledblock.serialization;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import com.google.common.base.Optional;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.core.Log;
import mod.chiselsandbits.helpers.ModUtil;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.ResourceLocation;
public class CrossWorldBlobSerializer extends BlobSerializer
{
public CrossWorldBlobSerializer(
final PacketBuffer toInflate )
{
super( toInflate );
}
public CrossWorldBlobSerializer(
final VoxelBlob toDeflate )
{
super( toDeflate );
}
@Override
protected int readStateID(
final PacketBuffer buffer )
{
final String name = buffer.readStringFromBuffer( 2047 );
buffer.readStringFromBuffer( 2047 );
final String parts[] = name.split( "[?&]" );
try
{
parts[0] = URLDecoder.decode( parts[0], "UTF-8" );
}
catch ( final UnsupportedEncodingException e )
{
Log.logError( "Failed to reload Property from store data : " + name, e );
}
final Block blk = Block.REGISTRY.getObject( new ResourceLocation( parts[0] ) );
if ( blk == null || blk == Blocks.AIR )
{
return 0;
}
IBlockState state = blk.getDefaultState();
if ( state == null )
{
return 0;
}
// rebuild state...
for ( int x = 1; x < parts.length; ++x )
{
try
{
if ( parts[x].length() > 0 )
{
final String nameval[] = parts[x].split( "[=]" );
if ( nameval.length == 2 )
{
nameval[0] = URLDecoder.decode( nameval[0], "UTF-8" );
nameval[1] = URLDecoder.decode( nameval[1], "UTF-8" );
state = withState( state, blk, nameval );
}
}
}
catch ( final Exception err )
{
Log.logError( "Failed to reload Property from store data : " + name, err );
}
}
return ModUtil.getStateId( state );
}
@SuppressWarnings( { "unchecked", "rawtypes" } )
private IBlockState withState(
final IBlockState state,
final Block blk,
final String[] nameval )
{
final IProperty prop = blk.getBlockState().getProperty( nameval[0] );
if ( prop == null )
{
Log.info( nameval[0] + " is not a valid property for " + Block.REGISTRY.getNameForObject( blk ) );
return state;
}
final Optional pv = prop.parseValue( nameval[1] );
if ( pv.isPresent() )
{
return state.withProperty( prop, pv.get() );
}
else
{
Log.info( nameval[1] + " is not a valid value of " + nameval[0] + " for " + Block.REGISTRY.getNameForObject( blk ) );
return state;
}
}
@Override
protected void writeStateID(
final PacketBuffer buffer,
final int key )
{
final IBlockState state = ModUtil.getStateById( key );
final Block blk = state.getBlock();
String sname = "air?";
try
{
final StringBuilder stateName = new StringBuilder( URLEncoder.encode( Block.REGISTRY.getNameForObject( blk ).toString(), "UTF-8" ) );
stateName.append( '?' );
boolean first = true;
for ( final IProperty<?> P : state.getPropertyNames() )
{
if ( !first )
{
stateName.append( '&' );
}
first = false;
final Comparable<?> propVal = state.getProperties().get( P );
String saveAs;
if ( propVal instanceof IStringSerializable )
{
saveAs = ( (IStringSerializable) propVal ).getName();
}
else
{
saveAs = propVal.toString();
}
stateName.append( URLEncoder.encode( P.getName(), "UTF-8" ) );
stateName.append( '=' );
stateName.append( URLEncoder.encode( saveAs, "UTF-8" ) );
}
sname = stateName.toString();
}
catch ( final UnsupportedEncodingException e )
{
Log.logError( "Failed to Serialize State", e );
}
buffer.writeString( sname );
buffer.writeString( "" ); // extra data for later use.
}
@Override
public int getVersion()
{
return VoxelBlob.VERSION_CROSSWORLD;
}
}