-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a persistent metadata API to User and Island classes.
This is modeled after the Bukkit metadata API with the difference that it is persistent, i.e., metadata is stored to the database. Metadata can be placed on Islands or Users. This API should be useful for addons that do not want or need to create their own database tables and instead just want to tag the user with some data, or tag the island with some data. It is intended for small amounts of data, like boolean tags or other values.
- Loading branch information
1 parent
e84b1f1
commit d7c7559
Showing
7 changed files
with
449 additions
and
10 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/world/bentobox/bentobox/api/metadata/MetaDataAble.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package world.bentobox.bentobox.api.metadata; | ||
|
||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
|
||
/** | ||
* This interface is for all BentoBox objects that have meta data | ||
* @author tastybento | ||
* @since 1.15.4 | ||
*/ | ||
public interface MetaDataAble { | ||
/** | ||
* @return the metaData | ||
*/ | ||
public Map<String, MetaDataValue> getMetaData(); | ||
|
||
/** | ||
* Get meta data by key | ||
* @param key - key | ||
* @return the value to which the specified key is mapped, or null if there is no mapping for the key | ||
* @since 1.15.4 | ||
*/ | ||
public MetaDataValue getMetaData(@NonNull String key); | ||
|
||
/** | ||
* @param metaData the metaData to set | ||
* @since 1.15.4 | ||
*/ | ||
public void setMetaData(Map<String, MetaDataValue> metaData); | ||
|
||
/** | ||
* Put a key, value string pair into the object's meta data | ||
* @param key - key | ||
* @param value - value | ||
* @return the previous value associated with key, or null if there was no mapping for key. | ||
* @since 1.15.4 | ||
*/ | ||
public MetaDataValue putMetaData(@NonNull String key, @NonNull MetaDataValue value); | ||
|
||
/** | ||
* Remove meta data | ||
* @param key - key to remove | ||
* @return the previous value associated with key, or null if there was no mapping for key. | ||
* @since 1.15.4 | ||
*/ | ||
public MetaDataValue removeMetaData(@NonNull String key); | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/world/bentobox/bentobox/api/metadata/MetaDataValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package world.bentobox.bentobox.api.metadata; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
|
||
import com.google.gson.annotations.Expose; | ||
|
||
/** | ||
* Stores meta data value in a GSON friendly way so it can be serialized and deserialized. | ||
* Values that are null are not stored in the database, so only the appropriate type is stored. | ||
* @author tastybento | ||
* @since 1.15.4 | ||
* | ||
*/ | ||
public class MetaDataValue { | ||
|
||
// Use classes so null value is supported | ||
@Expose | ||
private Integer intValue; | ||
@Expose | ||
private Float floatValue; | ||
@Expose | ||
private Double doubleValue; | ||
@Expose | ||
private Long longValue; | ||
@Expose | ||
private Short shortValue; | ||
@Expose | ||
private Byte byteValue; | ||
@Expose | ||
private Boolean booleanValue; | ||
@Expose | ||
private @NonNull String stringValue; | ||
|
||
/** | ||
* Initialize this meta data value | ||
* @param value the value assigned to this metadata value | ||
*/ | ||
public MetaDataValue(@NonNull Object value) { | ||
if (value instanceof Integer) { | ||
intValue = (int)value; | ||
} else if (value instanceof Float) { | ||
floatValue = (float)value; | ||
} else if (value instanceof Double) { | ||
doubleValue = (double)value; | ||
} else if (value instanceof Long) { | ||
longValue = (long)value; | ||
} else if (value instanceof Short) { | ||
shortValue = (short)value; | ||
} else if (value instanceof Byte) { | ||
byteValue = (byte)value; | ||
} else if (value instanceof Boolean) { | ||
booleanValue = (boolean)value; | ||
} else if (value instanceof String) { | ||
stringValue = (String)value; | ||
} | ||
} | ||
|
||
public int asInt() { | ||
return intValue; | ||
} | ||
|
||
public float asFloat() { | ||
return floatValue; | ||
} | ||
|
||
public double asDouble() { | ||
return doubleValue; | ||
} | ||
|
||
public long asLong() { | ||
return longValue; | ||
} | ||
|
||
public short asShort() { | ||
return shortValue; | ||
} | ||
|
||
public byte asByte() { | ||
return byteValue; | ||
} | ||
|
||
public boolean asBoolean() { | ||
return booleanValue; | ||
} | ||
|
||
@NonNull | ||
public String asString() { | ||
return stringValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
d7c7559
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I was waiting for something like that a while.
Now I can get rid of some tables.
d7c7559
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was working on Border and realized it was silly to create a database table for a single boolean.