Skip to content

Commit 1ae1407

Browse files
authored
Minor fix
Created DataRegistry instance for items and blocks
1 parent 6e131d7 commit 1ae1407

3 files changed

Lines changed: 74 additions & 7 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.thehandsomeyoni</groupId>
88
<artifactId>PersistentDataAPI</artifactId>
9-
<version>1.8.0-ALPHA</version>
9+
<version>1.8.1-ALPHA</version>
1010
<packaging>jar</packaging>
1111

1212
<name>PersistentDataAPI</name>

src/main/java/me/thehandsomeyoni/persistentdataapi/PersistentDataAPI.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.thehandsomeyoni.persistentdataapi;
22

3+
import me.thehandsomeyoni.persistentdataapi.exceptions.UnacceptableBlockException;
34
import me.thehandsomeyoni.persistentdataapi.manager.DataRegistry;
5+
import org.bukkit.block.Block;
46
import org.bukkit.entity.Player;
7+
import org.bukkit.inventory.ItemStack;
58
import org.bukkit.plugin.java.JavaPlugin;
69

710
/**
@@ -24,13 +27,27 @@ public PersistentDataAPI(JavaPlugin plugin) {
2427
}
2528

2629
/**
27-
* Gets the instance of the PersistentDataAPI.
28-
* @return The instance of the PersistentDataAPI.
30+
* Gets the instance of the DataRegistry.
31+
* @return The DataRegistry of the given player.
2932
*/
3033
public DataRegistry getDataRegistry(Player player){
3134
return new DataRegistry(player);
3235
}
3336

37+
/**
38+
* Gets the instance of the DataRegistry.
39+
* @param item The DataRegistry of the given item.
40+
* @return
41+
*/
42+
public DataRegistry getDataRegistry(ItemStack item){ return new DataRegistry(item); }
43+
44+
/**
45+
* Gets the instance of the DataRegistry.
46+
* @param block The DataRegistry of the given block.
47+
* @return
48+
*/
49+
public DataRegistry getDataRegistry(Block block) throws UnacceptableBlockException { return new DataRegistry(block); }
50+
3451

3552

3653
/**

src/main/java/me/thehandsomeyoni/persistentdataapi/data/DataContainerManager.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package me.thehandsomeyoni.persistentdataapi.data;
22

33

4-
import me.thehandsomeyoni.persistentdataapi.manager.DataSerializer;
54
import org.bukkit.NamespacedKey;
65
import org.bukkit.persistence.PersistentDataContainer;
76
import org.bukkit.persistence.PersistentDataType;
87

9-
import java.io.Serializable;
8+
import java.io.*;
109
import java.util.HashMap;
1110
import java.util.HashSet;
1211
import java.util.Set;
1312

1413
import static me.thehandsomeyoni.persistentdataapi.PersistentDataAPI.getJavaPlugin;
14+
import static me.thehandsomeyoni.persistentdataapi.data.DataContainerManager.DataSerializer.deserialize;
1515
import static me.thehandsomeyoni.persistentdataapi.manager.DataSerializer.serialize;
1616

1717
public class DataContainerManager {
@@ -49,7 +49,7 @@ public byte[] getSerialized (String key) {
4949
* @return The data in the given type.
5050
*/
5151
public Serializable getUnserialized(String key){
52-
return DataSerializer.deserialize(container.get(new NamespacedKey(getJavaPlugin(), key), PersistentDataType.BYTE_ARRAY));
52+
return deserialize(container.get(new NamespacedKey(getJavaPlugin(), key), PersistentDataType.BYTE_ARRAY));
5353
}
5454

5555
/**
@@ -58,7 +58,7 @@ public Serializable getUnserialized(String key){
5858
* @return The data in the given type.
5959
*/
6060
public Serializable getUnserialized(NamespacedKey key){
61-
return DataSerializer.deserialize(container.get(key, PersistentDataType.BYTE_ARRAY));
61+
return deserialize(container.get(key, PersistentDataType.BYTE_ARRAY));
6262
}
6363

6464
public Set<String> getAllKeys(){
@@ -105,4 +105,54 @@ public boolean has(String key){
105105
return container.has(new NamespacedKey(getJavaPlugin(), key), PersistentDataType.BYTE_ARRAY);
106106
}
107107

108+
/**
109+
* Simple serializer for serializing and deserializing data.
110+
* @author TheHandsomeYoni
111+
* @version 1.5.0
112+
*/
113+
static class DataSerializer {
114+
private static ByteArrayOutputStream byteArrayOutput;
115+
private static ObjectOutputStream objectOutput;
116+
117+
private static ByteArrayInputStream byteArrayInput;
118+
private static ObjectInputStream objectInput;
119+
120+
121+
/**
122+
* Turns the raw data into byte array.
123+
*
124+
* @param object The data.
125+
* @return The data in bytes.
126+
*/
127+
public static byte[] serialize(Object object) {
128+
try {
129+
byteArrayOutput = new ByteArrayOutputStream();
130+
objectOutput = new ObjectOutputStream(byteArrayOutput);
131+
objectOutput.writeObject(object);
132+
objectOutput.flush();
133+
return byteArrayOutput.toByteArray();
134+
} catch (IOException e) {
135+
e.printStackTrace();
136+
return null;
137+
}
138+
}
139+
140+
/**
141+
* Turns the byte array into data.
142+
*
143+
* @param bytes The bytes.
144+
* @return The data.
145+
*/
146+
public static Serializable deserialize(byte[] bytes) {
147+
byteArrayInput = new ByteArrayInputStream(bytes);
148+
try {
149+
objectInput = new ObjectInputStream(byteArrayInput);
150+
return (Serializable) objectInput.readObject();
151+
} catch (IOException | ClassNotFoundException e) {
152+
e.printStackTrace();
153+
return null;
154+
}
155+
}
156+
}
157+
108158
}

0 commit comments

Comments
 (0)