-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove dependency on Item NBT API * Separate out accessor methods for `ItemMeta`
- Loading branch information
1 parent
866cf14
commit 8dccc9a
Showing
6 changed files
with
109 additions
and
31 deletions.
There are no files selected for viewing
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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/danikvitek/slimeinabukkit/persistence/PersistentContainerAccessor.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,37 @@ | ||
package com.danikvitek.slimeinabukkit.persistence; | ||
|
||
import com.danikvitek.slimeinabukkit.persistence.datatype.UUIDTagType; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.UUID; | ||
|
||
public class PersistentContainerAccessor { | ||
private final @NotNull NamespacedKey slimeBucketUuidKey; | ||
|
||
public PersistentContainerAccessor(@NotNull NamespacedKey slimeBucketUuidKey) { | ||
this.slimeBucketUuidKey = slimeBucketUuidKey; | ||
} | ||
|
||
public void setSlimeBucketUUID(final @NotNull ItemStack itemStack, final @NotNull UUID uuid) { | ||
final ItemMeta itemMeta = itemStack.getItemMeta(); | ||
setSlimeBucketUUID(itemMeta, uuid); | ||
itemStack.setItemMeta(itemMeta); | ||
} | ||
|
||
public void setSlimeBucketUUID(final @NotNull ItemMeta itemMeta, final @NotNull UUID uuid) { | ||
itemMeta.getPersistentDataContainer().set(slimeBucketUuidKey, UUIDTagType.INSTANCE, uuid); | ||
} | ||
|
||
public void removeSlimeBucketUUID(final @NotNull ItemStack itemStack) { | ||
final ItemMeta itemMeta = itemStack.getItemMeta(); | ||
removeSlimeBucketUUID(itemMeta); | ||
itemStack.setItemMeta(itemMeta); | ||
} | ||
|
||
public void removeSlimeBucketUUID(final @NotNull ItemMeta itemMeta) { | ||
itemMeta.getPersistentDataContainer().remove(slimeBucketUuidKey); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/danikvitek/slimeinabukkit/persistence/datatype/UUIDTagType.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,41 @@ | ||
package com.danikvitek.slimeinabukkit.persistence.datatype; | ||
|
||
import org.bukkit.persistence.PersistentDataAdapterContext; | ||
import org.bukkit.persistence.PersistentDataType; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.UUID; | ||
|
||
public final class UUIDTagType implements PersistentDataType<byte[], UUID> { | ||
public static final UUIDTagType INSTANCE = new UUIDTagType(); | ||
|
||
private UUIDTagType() { | ||
} | ||
|
||
@Override | ||
public @NotNull Class<byte[]> getPrimitiveType() { | ||
return byte[].class; | ||
} | ||
|
||
@Override | ||
public @NotNull Class<UUID> getComplexType() { | ||
return UUID.class; | ||
} | ||
|
||
@Override | ||
public byte @NotNull [] toPrimitive(final @NotNull UUID complex, final @NotNull PersistentDataAdapterContext context) { | ||
final ByteBuffer bb = ByteBuffer.wrap(new byte[16]); | ||
bb.putLong(complex.getMostSignificantBits()); | ||
bb.putLong(complex.getLeastSignificantBits()); | ||
return bb.array(); | ||
} | ||
|
||
@Override | ||
public @NotNull UUID fromPrimitive(final byte @NotNull [] primitive, final @NotNull PersistentDataAdapterContext context) { | ||
final ByteBuffer bb = ByteBuffer.wrap(primitive); | ||
final long firstLong = bb.getLong(); | ||
final long secondLong = bb.getLong(); | ||
return new UUID(firstLong, secondLong); | ||
} | ||
} |