From 510f04e71021ceda416fa3b2c7f40cfc869a6066 Mon Sep 17 00:00:00 2001 From: Joris Guffens Date: Fri, 18 Oct 2024 14:26:14 +0200 Subject: [PATCH] untested: use math location instead of bukkit location so worlds loaded after the plugin can have treasurechests --- build.gradle | 6 ++++- spigot/build.gradle | 1 + .../spigot/TreasureChestManager.java | 11 ++++---- .../spigot/data/beans/BTreasureChest.java | 7 +++++- .../data/converters/LocationConverter.java | 25 +++++++++++-------- .../listeners/PlayerChestSetupListener.java | 9 ++++--- .../spigot/particle/ParticleJobManager.java | 14 +++++------ 7 files changed, 44 insertions(+), 29 deletions(-) diff --git a/build.gradle b/build.gradle index 488dec6..fcf4674 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,10 @@ plugins { id 'com.github.johnrengelman.shadow' version '7.1.2' apply false - id 'pl.allegro.tech.build.axion-release' version '1.13.6' + id 'pl.allegro.tech.build.axion-release' version '1.18.3' +} + +scmVersion { + ignoreUncommittedChanges.set(false) } group 'com.guflimc.treasurechests' diff --git a/spigot/build.gradle b/spigot/build.gradle index d091be8..ed5a513 100644 --- a/spigot/build.gradle +++ b/spigot/build.gradle @@ -27,6 +27,7 @@ dependencies { implementation('com.guflimc.brick.orm:jpa-converters:+') { exclude group: 'jakarta.persistence' } + implementation 'com.guflimc.brick.math:spigot:+' // adventure implementation 'net.kyori:adventure-api:4.+' diff --git a/spigot/src/main/java/com/guflimc/treasurechests/spigot/TreasureChestManager.java b/spigot/src/main/java/com/guflimc/treasurechests/spigot/TreasureChestManager.java index df07781..3746eb7 100644 --- a/spigot/src/main/java/com/guflimc/treasurechests/spigot/TreasureChestManager.java +++ b/spigot/src/main/java/com/guflimc/treasurechests/spigot/TreasureChestManager.java @@ -1,5 +1,6 @@ package com.guflimc.treasurechests.spigot; +import com.guflimc.brick.math.spigot.SpigotAdapter; import com.guflimc.treasurechests.spigot.data.DatabaseContext; import com.guflimc.treasurechests.spigot.data.beans.*; import net.kyori.adventure.platform.bukkit.BukkitComponentSerializer; @@ -269,15 +270,15 @@ public BTreasureChest chestAt(Block block) { private BTreasureChest chestAt(Collection blocks) { return chests.stream() .filter(c -> blocks.stream().anyMatch(b -> - c.location().getWorld().getUID().equals(b.getWorld().getUID()) && - c.location().getBlockX() == b.getX() && - c.location().getBlockY() == b.getY() && - c.location().getBlockZ() == b.getZ())) + b.getWorld().getUID().equals(c.location().worldId()) && + c.location().blockX() == b.getX() && + c.location().blockY() == b.getY() && + c.location().blockZ() == b.getZ())) .findAny().orElse(null); } public CompletableFuture addChest(Location location) { - BTreasureChest chest = new BTreasureChest(location); + BTreasureChest chest = new BTreasureChest(SpigotAdapter.adapt(location)); return databaseContext.persistAsync(chest).thenCompose((v) -> { chests.add(chest); CompletableFuture cf = new CompletableFuture<>(); diff --git a/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/beans/BTreasureChest.java b/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/beans/BTreasureChest.java index d00d5c2..4486da8 100644 --- a/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/beans/BTreasureChest.java +++ b/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/beans/BTreasureChest.java @@ -1,11 +1,12 @@ package com.guflimc.treasurechests.spigot.data.beans; +import com.guflimc.brick.math.common.geometry.pos3.Location; +import com.guflimc.brick.math.spigot.SpigotAdapter; import com.guflimc.brick.orm.jpa.converters.ComponentConverter; import com.guflimc.treasurechests.spigot.data.converters.LocationConverter; import com.guflimc.treasurechests.spigot.data.converters.ParticleEffectConverter; import io.ebean.annotation.DbDefault; import net.kyori.adventure.text.Component; -import org.bukkit.Location; import org.bukkit.inventory.ItemStack; import jakarta.persistence.*; @@ -61,6 +62,10 @@ public Location location() { return location; } + public org.bukkit.Location bukkitLocation() { + return SpigotAdapter.adapt(this.location); + } + public int respawnTime() { return respawnTime; } diff --git a/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/converters/LocationConverter.java b/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/converters/LocationConverter.java index 79e13f5..a5b13a5 100644 --- a/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/converters/LocationConverter.java +++ b/spigot/src/main/java/com/guflimc/treasurechests/spigot/data/converters/LocationConverter.java @@ -1,11 +1,11 @@ package com.guflimc.treasurechests.spigot.data.converters; import com.google.gson.*; -import org.bukkit.Bukkit; -import org.bukkit.Location; - +import com.guflimc.brick.math.common.geometry.pos3.Location; +import com.guflimc.brick.math.common.geometry.pos3.Vector3; import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; + import java.util.UUID; @Converter(autoApply = true) @@ -14,19 +14,21 @@ public class LocationConverter implements AttributeConverter { private final static JsonDeserializer deserializer = (json, typeOfT, context) -> { JsonObject obj = json.getAsJsonObject(); return new Location( - Bukkit.getWorld(UUID.fromString(obj.get("world").getAsString())), - obj.get("x").getAsDouble(), - obj.get("y").getAsDouble(), - obj.get("z").getAsDouble() + UUID.fromString(obj.get("world").getAsString()), + new Vector3( + obj.get("x").getAsDouble(), + obj.get("y").getAsDouble(), + obj.get("z").getAsDouble() + ) ); }; private final static JsonSerializer serializer = (src, typeOfSrc, context) -> { JsonObject obj = new JsonObject(); - obj.addProperty("world", src.getWorld().getUID().toString()); - obj.addProperty("x", src.getX()); - obj.addProperty("y", src.getY()); - obj.addProperty("z", src.getZ()); + obj.addProperty("world", src.worldId().toString()); + obj.addProperty("x", src.x()); + obj.addProperty("y", src.y()); + obj.addProperty("z", src.z()); return obj; }; @@ -34,6 +36,7 @@ public class LocationConverter implements AttributeConverter { .registerTypeAdapter(Location.class, deserializer) .registerTypeAdapter(Location.class, serializer) .create(); + @Override public String convertToDatabaseColumn(Location attribute) { return gson.toJson(attribute); diff --git a/spigot/src/main/java/com/guflimc/treasurechests/spigot/listeners/PlayerChestSetupListener.java b/spigot/src/main/java/com/guflimc/treasurechests/spigot/listeners/PlayerChestSetupListener.java index 0c06ad3..33458a0 100644 --- a/spigot/src/main/java/com/guflimc/treasurechests/spigot/listeners/PlayerChestSetupListener.java +++ b/spigot/src/main/java/com/guflimc/treasurechests/spigot/listeners/PlayerChestSetupListener.java @@ -5,6 +5,7 @@ import com.guflimc.brick.gui.spigot.item.ItemStackBuilder; import com.guflimc.brick.gui.spigot.menu.SpigotMenu; import com.guflimc.brick.gui.spigot.menu.SpigotMenuItem; +import com.guflimc.brick.math.spigot.SpigotAdapter; import com.guflimc.treasurechests.spigot.TreasureChestManager; import com.guflimc.treasurechests.spigot.data.beans.BTreasureChest; import com.guflimc.treasurechests.spigot.data.beans.BTreasureLoot; @@ -66,8 +67,8 @@ public void onBreak(BlockBreakEvent event) { event.setCancelled(true); return; } - - if (event.getBlock().getLocation().equals(chest.location())) { +; + if (event.getBlock().getLocation().equals(chest.bukkitLocation()) ) { particleJobManager.stop(chest); manager.delete(chest); } @@ -303,7 +304,7 @@ private void info(Player player, BTreasureChest chest) { }); // CHEST ID - Location loc = chest.location(); + Location loc = chest.bukkitLocation(); ItemStack info = ItemStackBuilder.of(Material.PAPER) .withName(ChatColor.YELLOW + "Chest info") .withLore( @@ -372,7 +373,7 @@ private void paste(Player player, BTreasureChest chest) { private final ItemStack back = ItemStackBuilder.of(Material.PAPER).withName(ChatColor.GREEN + "Back").build(); private void loot(Player player, BTreasureChest chest) { - int size = manager.isDoubleChest(chest.location()) ? 54 : 36; + int size = manager.isDoubleChest(chest.bukkitLocation()) ? 54 : 36; // remove excess loot if (chest.loot().size() > size) { diff --git a/spigot/src/main/java/com/guflimc/treasurechests/spigot/particle/ParticleJobManager.java b/spigot/src/main/java/com/guflimc/treasurechests/spigot/particle/ParticleJobManager.java index aec9d32..582c59f 100644 --- a/spigot/src/main/java/com/guflimc/treasurechests/spigot/particle/ParticleJobManager.java +++ b/spigot/src/main/java/com/guflimc/treasurechests/spigot/particle/ParticleJobManager.java @@ -33,16 +33,16 @@ public void start(BTreasureChest chest) { } AbstractParticleJob job = chest.particleEffect().pattern().creator() - .create(plugin, chest.location(), chest.particleEffect().type()); + .create(plugin, chest.bukkitLocation(), chest.particleEffect().type()); job.start(); jobs.put(chest, job); } public void start(Chunk chunk) { manager.chests().stream() - .filter(chest -> chunk.getWorld().equals(chest.location().getWorld())) - .filter(chest -> chunk.getX() == chest.location().getBlockX() >> 4) - .filter(chest -> chunk.getZ() == chest.location().getBlockZ() >> 4) + .filter(chest -> chunk.getWorld().equals(chest.bukkitLocation().getWorld())) + .filter(chest -> chunk.getX() == chest.location().blockX() >> 4) + .filter(chest -> chunk.getZ() == chest.location().blockZ() >> 4) .forEach(this::start); } @@ -55,9 +55,9 @@ public void stop(BTreasureChest chest) { public void stop(Chunk chunk) { new HashSet<>(jobs.keySet()).stream() - .filter(chest -> chunk.getWorld().equals(chest.location().getWorld())) - .filter(chest -> chunk.getX() == chest.location().getBlockX() >> 4) - .filter(chest -> chunk.getZ() == chest.location().getBlockZ() >> 4) + .filter(chest -> chunk.getWorld().equals(chest.bukkitLocation().getWorld())) + .filter(chest -> chunk.getX() == chest.location().blockX() >> 4) + .filter(chest -> chunk.getZ() == chest.location().blockZ() >> 4) .forEach(this::stop); } }