/
ClipboardStorage.java
100 lines (82 loc) · 1.99 KB
/
ClipboardStorage.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
package mod.chiselsandbits.client;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.EncoderException;
import mod.chiselsandbits.core.ChiselsAndBits;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
public class ClipboardStorage extends Configuration
{
public ClipboardStorage(
final File file )
{
super( file );
}
public void write(
final List<NBTTagCompound> myitems )
{
if ( !ChiselsAndBits.getConfig().persistCreativeClipboard )
{
return;
}
for ( final String name : getCategoryNames() )
{
removeCategory( getCategory( name ) );
}
int idx = 0;
for ( final NBTTagCompound nbt : myitems )
{
final PacketBuffer b = new PacketBuffer( Unpooled.buffer() );
b.writeNBTTagCompoundToBuffer( nbt );
final int[] o = new int[b.writerIndex()];
for ( int x = 0; x < b.writerIndex(); x++ )
{
o[x] = b.getByte( x );
}
get( "clipboard", "" + idx++, o ).set( o );
}
save();
}
public List<NBTTagCompound> read()
{
final List<NBTTagCompound> myItems = new ArrayList<NBTTagCompound>();
if ( !ChiselsAndBits.getConfig().persistCreativeClipboard )
{
return myItems;
}
for ( final Property p : getCategory( "clipboard" ).values() )
{
final int[] bytes = p.getIntList();
final byte[] o = new byte[bytes.length];
for ( int x = 0; x < bytes.length; x++ )
{
o[x] = (byte) bytes[x];
}
try
{
final PacketBuffer b = new PacketBuffer( Unpooled.wrappedBuffer( o ) );
final NBTTagCompound c = b.readNBTTagCompoundFromBuffer();
myItems.add( c );
}
catch ( final EncoderException e )
{
// :_ (
}
catch ( final EOFException e )
{
// :_ (
}
catch ( final IOException e )
{
// :_ (
}
}
return myItems;
}
}