Skip to content

Commit

Permalink
2.2 (#6)
Browse files Browse the repository at this point in the history
* Remove dependency on Item NBT API

* Separate out accessor methods for `ItemMeta`
  • Loading branch information
DanikVitek authored Sep 30, 2024
1 parent 866cf14 commit 8dccc9a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 31 deletions.
4 changes: 1 addition & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.danikvitek"
version = "2.1"
version = "2.2"

repositories {
mavenCentral()
Expand All @@ -32,7 +32,6 @@ dependencies {
paperweight.paperDevBundle("1.18.2-R0.1-SNAPSHOT")
compileOnly(libs.jetbrains.annotations)
implementation(libs.bstats.bukkit)
implementation(libs.itemnbt.api)
implementation(libs.vavr)
}

Expand All @@ -46,7 +45,6 @@ tasks {
}
shadowJar {
relocate("org.bstats", "com.danikvitek.bstats")
relocate("de.tr7zw.changeme.nbtapi", "com.danikvitek.itemnbtapi")
}
build {
dependsOn(shadowJar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
import com.danikvitek.slimeinabukkit.config.ChunkMessageResolver;
import com.danikvitek.slimeinabukkit.config.ConfigAccessor;
import com.danikvitek.slimeinabukkit.config.PluginConfig;
import com.danikvitek.slimeinabukkit.persistence.PersistentContainerAccessor;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public final class SlimeInABukkitPlugin extends JavaPlugin implements ConfigAccessor {
public static final Material SLIME_BUCKET_MATERIAL = Material.SLIME_BALL;
public static final String SLIME_BUCKET_UUID_KEY = "SLIME_UUID";
private static final int PLUGIN_ID = 14716;

private final Scheduler scheduler = new Scheduler(this, getServer().getScheduler());
Expand All @@ -26,7 +27,10 @@ public void onEnable() {
this.getConfig().options().configuration();
this.saveDefaultConfig();

Objects.requireNonNull(getCommand("get_slime")).setExecutor(new GetSlimeCommand(pluginConfig));
final var persistentContainerAccessor = new PersistentContainerAccessor(new NamespacedKey(this, "slime-uuid"));

Objects.requireNonNull(getCommand("get_slime"))
.setExecutor(new GetSlimeCommand(pluginConfig, persistentContainerAccessor));

final var messageResolver = new ChunkMessageResolver(
pluginConfig.getSlimeChunkMessage(),
Expand All @@ -37,7 +41,15 @@ public void onEnable() {

new Metrics(this, PLUGIN_ID);

Bukkit.getPluginManager().registerEvents(new SlimeListener(pluginConfig, this::debugLog, scheduler), this);
Bukkit.getPluginManager().registerEvents(
new SlimeListener(
pluginConfig,
this::debugLog,
scheduler,
persistentContainerAccessor
),
this
);
}

@Override
Expand Down
28 changes: 9 additions & 19 deletions src/main/java/com/danikvitek/slimeinabukkit/SlimeListener.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.danikvitek.slimeinabukkit;

import com.danikvitek.slimeinabukkit.config.PluginConfig;
import com.danikvitek.slimeinabukkit.persistence.PersistentContainerAccessor;
import com.danikvitek.slimeinabukkit.util.ISUtil;
import de.tr7zw.changeme.nbtapi.NBT;
import io.vavr.collection.Iterator;
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
Expand Down Expand Up @@ -35,7 +35,6 @@
import java.util.function.Consumer;

import static com.danikvitek.slimeinabukkit.SlimeInABukkitPlugin.SLIME_BUCKET_MATERIAL;
import static com.danikvitek.slimeinabukkit.SlimeInABukkitPlugin.SLIME_BUCKET_UUID_KEY;

public class SlimeListener implements Listener {
public static final String SLIME_INTERACT_PERMISSION = "slimeinabukkit.interact";
Expand All @@ -47,13 +46,16 @@ public class SlimeListener implements Listener {
private final @NotNull PluginConfig config;
private final @NotNull Consumer<String> debugLog;
private final @NotNull Scheduler scheduler;
private final @NotNull PersistentContainerAccessor persistentContainerAccessor;

public SlimeListener(@NotNull PluginConfig config,
@NotNull Consumer<String> debugLog,
@NotNull Scheduler scheduler) {
@NotNull Scheduler scheduler,
@NotNull PersistentContainerAccessor persistentContainerAccessor) {
this.config = config;
this.debugLog = debugLog;
this.scheduler = scheduler;
this.persistentContainerAccessor = persistentContainerAccessor;
}

@EventHandler
Expand Down Expand Up @@ -147,7 +149,7 @@ private void pickupSlime(final @NotNull Slime slime,

slimeBucketStack.setItemMeta(slimeBucketMeta);
slimeBucketStack.setType(SLIME_BUCKET_MATERIAL);
assignUUID(slimeBucketStack, slime.getUniqueId());
persistentContainerAccessor.setSlimeBucketUUID(slimeBucketStack, slime.getUniqueId());
if (bucketStack.getAmount() > 1) {
bucketStack.setAmount(bucketStack.getAmount() - 1);
final var notFittedItems = player.getInventory().addItem(slimeBucketStack);
Expand All @@ -165,13 +167,6 @@ private void pickupSlime(final @NotNull Slime slime,
scheduler.runTask(() -> interactingPlayers.remove(player.getUniqueId()));
}

private void assignUUID(final @NotNull ItemStack slimeBucketStack,
final @NotNull UUID uuid) {
NBT.modify(slimeBucketStack, nbt -> {
nbt.setUUID(SLIME_BUCKET_UUID_KEY, uuid);
});
}

@EventHandler
public void onClickAtBlock(final @NotNull PlayerInteractEvent event) {
debugLog.accept("PlayerInteractEvent was caught");
Expand Down Expand Up @@ -234,6 +229,7 @@ private void placeSlime(final @NotNull PlayerInteractEvent event,
player.getWorld().spawn(slimeReleaseLocation, Slime.class, slime -> {
slime.setSize(1);
final var serializer = PlainTextComponentSerializer.plainText();

ISUtil.useDisplayName(itemMeta, displayName -> {
if (!serializer.serialize(displayName)
.equals(serializer.serialize(config.getSlimeBucketTitle()))) {
Expand All @@ -244,22 +240,16 @@ private void placeSlime(final @NotNull PlayerInteractEvent event,

itemMeta.setCustomModelData(null);
itemMeta.displayName(null);
persistentContainerAccessor.removeSlimeBucketUUID(itemMeta);
itemStack.setItemMeta(itemMeta);
itemStack.setType(Material.BUCKET);
removeUUID(itemStack);

if (event.getHand() == EquipmentSlot.HAND) player.swingMainHand();
else player.swingOffHand();

scheduler.runTask(() -> interactingPlayers.remove(player.getUniqueId()));
}

private void removeUUID(final @NotNull ItemStack itemStack) {
NBT.modify(itemStack, nbt -> {
nbt.removeKey(SLIME_BUCKET_UUID_KEY);
});
}

@EventHandler(priority = EventPriority.MONITOR)
public void onCraftWithSlimeBucket(final @NotNull CraftItemEvent e) {
final int matrixSize = e.getInventory().getMatrix().length;
Expand Down Expand Up @@ -293,8 +283,8 @@ public void onCraftWithSlimeBucket(final @NotNull CraftItemEvent e) {
assert clonedBucketMeta != null;
clonedBucketMeta.setCustomModelData(null);
clonedBucketMeta.displayName(null);
persistentContainerAccessor.removeSlimeBucketUUID(clonedBucketMeta);
clonedBucket.setItemMeta(clonedBucketMeta);
removeUUID(clonedBucket);
newMatrix[slot] = clonedBucket;
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.danikvitek.slimeinabukkit.command;

import com.danikvitek.slimeinabukkit.config.PluginConfig;
import de.tr7zw.changeme.nbtapi.NBT;
import com.danikvitek.slimeinabukkit.persistence.PersistentContainerAccessor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Location;
Expand All @@ -18,13 +18,15 @@
import java.util.UUID;

import static com.danikvitek.slimeinabukkit.SlimeInABukkitPlugin.SLIME_BUCKET_MATERIAL;
import static com.danikvitek.slimeinabukkit.SlimeInABukkitPlugin.SLIME_BUCKET_UUID_KEY;

public class GetSlimeCommand implements CommandExecutor {
private final @NotNull PluginConfig config;
private final @NotNull PersistentContainerAccessor persistentContainerAccessor;

public GetSlimeCommand(@NotNull PluginConfig config) {
public GetSlimeCommand(@NotNull PluginConfig config,
@NotNull PersistentContainerAccessor persistentContainerAccessor) {
this.config = config;
this.persistentContainerAccessor = persistentContainerAccessor;
}

@Override
Expand Down Expand Up @@ -53,10 +55,8 @@ private void getSlimeImpl(@NotNull CommandSender sender) {
);

slimeBucketMeta.displayName(config.getSlimeBucketTitle());
persistentContainerAccessor.setSlimeBucketUUID(slimeBucketMeta, UUID.randomUUID()); // for it to be not stackable
slimeBucket.setItemMeta(slimeBucketMeta);
NBT.modify(slimeBucket, nbt -> {
nbt.setUUID(SLIME_BUCKET_UUID_KEY, UUID.randomUUID()); // for it to be not stackable
});

final World world = player.getWorld();
world.playSound(location, Sound.ENTITY_ITEM_PICKUP, 1f, 1f);
Expand Down
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);
}
}
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);
}
}

0 comments on commit 8dccc9a

Please sign in to comment.