Skip to content

Commit 37f54a3

Browse files
authored
Add files via upload
1 parent abc74bf commit 37f54a3

17 files changed

+1488
-1
lines changed

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.7.0-DEVELOP</version>
9+
<version>1.8.0-ALPHA</version>
1010
<packaging>jar</packaging>
1111

1212
<name>PersistentDataAPI</name>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package me.thehandsomeyoni.persistentdataapi;
2+
3+
import org.bukkit.persistence.PersistentDataType;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* A class that represents a persistent data.
9+
* @author TheHandsomeYoni
10+
* @since 1.0
11+
*/
12+
public abstract class AbstractPersistentData {
13+
/** The name of the data associated with the persistent data */ protected String dataName;
14+
/** The value of the data associated with the persistent data */ protected Serializable dataValue;
15+
16+
/**
17+
* Initializes the PersistentData.
18+
* @param dataName The name of the data.
19+
* @param dataValue The value of the data, must be serializable.
20+
*/
21+
public AbstractPersistentData(String dataName, Serializable dataValue) {
22+
this.dataName = dataName;
23+
this.dataValue = dataValue;
24+
}
25+
26+
27+
28+
/*
29+
Old constructor
30+
public <type, value> AbstractPersistentData(PersistentDataType<type, value> dataType, String dataName, value value){
31+
this.dataName = dataName;
32+
this.dataType = dataType;
33+
this.dataValue = dataValue;
34+
}
35+
*/
36+
37+
/**
38+
* Changes the value of the persistent data.
39+
* @param newDataValue The new value of the data.
40+
* @return The persistent data with the new value.
41+
*/
42+
public abstract AbstractPersistentData changeDataValue(Serializable newDataValue);
43+
44+
/**
45+
* Gets the data.
46+
* @return The data.
47+
*/
48+
public abstract AbstractPersistentData getData();
49+
50+
/**
51+
* Gets the value of the data.
52+
* @return The value of the data.
53+
*/
54+
public abstract Serializable getDataValue();
55+
56+
/**
57+
* Gets the name of the data.
58+
* @return The name of the data.
59+
*/
60+
public abstract String getDataName();
61+
62+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package me.thehandsomeyoni.persistentdataapi;
2+
3+
import me.thehandsomeyoni.persistentdataapi.manager.DataRegistry;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
7+
/**
8+
* The class that initializes the API.
9+
* @author TheHandsomeYoni
10+
* @version 1.6.0
11+
*/
12+
public final class PersistentDataAPI {
13+
14+
/** The borrowed java plugin */ protected JavaPlugin plugin;
15+
private static PersistentDataAPI instance;
16+
17+
/**
18+
* Initializes the PersistentDataAPI.
19+
* @param plugin The plugin that the API is being used in.
20+
*/
21+
public PersistentDataAPI(JavaPlugin plugin) {
22+
this.plugin = plugin;
23+
this.instance = this;
24+
}
25+
26+
/**
27+
* Gets the instance of the PersistentDataAPI.
28+
* @return The instance of the PersistentDataAPI.
29+
*/
30+
public DataRegistry getDataRegistry(Player player){
31+
return new DataRegistry(player);
32+
}
33+
34+
35+
36+
/**
37+
* Gets the instance of the PersistentDataAPI.
38+
* @return The instance of the PersistentDataAPI.
39+
*/
40+
public static PersistentDataAPI getInstance() {
41+
return PersistentDataAPI.instance;
42+
}
43+
44+
/**
45+
* Gets the plugin that the API is being used in.
46+
* @return The plugin that the API is being used in.
47+
*/
48+
public static JavaPlugin getJavaPlugin() {
49+
return PersistentDataAPI.getInstance().plugin;
50+
}
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package me.thehandsomeyoni.persistentdataapi.data;
2+
3+
4+
import me.thehandsomeyoni.persistentdataapi.manager.DataSerializer;
5+
import org.bukkit.NamespacedKey;
6+
import org.bukkit.persistence.PersistentDataContainer;
7+
import org.bukkit.persistence.PersistentDataType;
8+
9+
import java.io.Serializable;
10+
import java.util.HashMap;
11+
import java.util.HashSet;
12+
import java.util.Set;
13+
14+
import static me.thehandsomeyoni.persistentdataapi.PersistentDataAPI.getJavaPlugin;
15+
import static me.thehandsomeyoni.persistentdataapi.manager.DataSerializer.serialize;
16+
17+
public class DataContainerManager {
18+
private PersistentDataContainer container;
19+
20+
/**
21+
* Initializes the DataContainerManager for a given persistent data.
22+
* @param container The persistent data container.
23+
*/
24+
public DataContainerManager(PersistentDataContainer container) {
25+
this.container = container;
26+
}
27+
28+
/**
29+
* Serializes and writes the data in the container.
30+
* @param key The key of the data.
31+
* @param value The value of the data.
32+
*/
33+
public void setSerialized (String key, Serializable value) {
34+
container.set(new NamespacedKey(getJavaPlugin(), key),PersistentDataType.BYTE_ARRAY , serialize(value));
35+
}
36+
37+
/**
38+
* Gives the data associated with the given key in bytes.
39+
* @param key The key of the data.
40+
* @return The data in bytes.
41+
*/
42+
public byte[] getSerialized (String key) {
43+
return container.get(new NamespacedKey(getJavaPlugin(), key), PersistentDataType.BYTE_ARRAY);
44+
}
45+
46+
/**
47+
* Gives the data associated with the given key in the given type.
48+
* @param key The key of the data.
49+
* @return The data in the given type.
50+
*/
51+
public Serializable getUnserialized(String key){
52+
return DataSerializer.deserialize(container.get(new NamespacedKey(getJavaPlugin(), key), PersistentDataType.BYTE_ARRAY));
53+
}
54+
55+
/**
56+
* Gets the data associated with the given key in the given type.
57+
* @param key The key of the data.
58+
* @return The data in the given type.
59+
*/
60+
public Serializable getUnserialized(NamespacedKey key){
61+
return DataSerializer.deserialize(container.get(key, PersistentDataType.BYTE_ARRAY));
62+
}
63+
64+
public Set<String> getAllKeys(){
65+
Set<String> keys = new HashSet<>();
66+
67+
container.getKeys().forEach(key -> {
68+
keys.add(key.getKey());
69+
});
70+
71+
return keys;
72+
}
73+
74+
public HashMap<String, Serializable> getAll(){
75+
HashMap<String, Serializable> data = new HashMap<>();
76+
77+
container.getKeys().forEach(key -> {
78+
data.put(key.getKey(), getUnserialized(key));
79+
});
80+
81+
return data;
82+
}
83+
84+
/**
85+
* Deletes the data associated with the given key.
86+
* @param key The key of the data.
87+
*/
88+
public void remove(String key){
89+
container.remove(new NamespacedKey(getJavaPlugin(), key));
90+
}
91+
92+
/**
93+
* Gives the persistent data container.
94+
* @return The persistent data container.
95+
*/
96+
public PersistentDataContainer getContainer() {
97+
return container;
98+
}
99+
100+
/**
101+
* Checks if the data associated with the given key exists.
102+
* @return True if the data exists, false otherwise.
103+
*/
104+
public boolean has(String key){
105+
return container.has(new NamespacedKey(getJavaPlugin(), key), PersistentDataType.BYTE_ARRAY);
106+
}
107+
108+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package me.thehandsomeyoni.persistentdataapi.data;
2+
3+
import me.thehandsomeyoni.persistentdataapi.AbstractPersistentData;
4+
import org.bukkit.persistence.PersistentDataType;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* A class that represents a persistent data.
10+
* @author TheHandsomeYoni
11+
* @since 1.0
12+
*/
13+
public class PersistentData extends AbstractPersistentData {
14+
15+
/**
16+
* Initializes the PersistentData.
17+
* @param dataName The name of the data.
18+
* @param dataValue The value of the data.
19+
*/
20+
public PersistentData(String dataName, Serializable dataValue) {
21+
super(dataName, dataValue);
22+
}
23+
24+
/**
25+
* Changes the value of the data.
26+
* @param newDataValue The new value of the data.
27+
* @return The new data with the new value.
28+
*/
29+
@Override
30+
public AbstractPersistentData changeDataValue(Serializable newDataValue) {
31+
this.dataValue = newDataValue;
32+
return this;
33+
}
34+
35+
/**
36+
* Gets the data.
37+
* @return The data.
38+
*/
39+
@Override
40+
public AbstractPersistentData getData() {
41+
return this;
42+
}
43+
44+
/**
45+
* Gets the value of the data.
46+
* @return The value of the data.
47+
*/
48+
@Override
49+
public Serializable getDataValue() {
50+
return this.dataValue;
51+
}
52+
53+
/**
54+
* Gets the name of the data.
55+
* @return The name of the data.
56+
*/
57+
@Override
58+
public String getDataName() {
59+
return this.dataName;
60+
}
61+
}

0 commit comments

Comments
 (0)