Skip to content

Commit

Permalink
untested: use math location instead of bukkit location so worlds load…
Browse files Browse the repository at this point in the history
…ed after the plugin can have treasurechests
  • Loading branch information
jorisguffens committed Oct 18, 2024
1 parent d53e741 commit 510f04e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
1 change: 1 addition & 0 deletions spigot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.+'
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -269,15 +270,15 @@ public BTreasureChest chestAt(Block block) {
private BTreasureChest chestAt(Collection<Block> 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<BTreasureChest> 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<BTreasureChest> cf = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -61,6 +62,10 @@ public Location location() {
return location;
}

public org.bukkit.Location bukkitLocation() {
return SpigotAdapter.adapt(this.location);
}

public int respawnTime() {
return respawnTime;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -14,26 +14,29 @@ public class LocationConverter implements AttributeConverter<Location, String> {
private final static JsonDeserializer<Location> 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<Location> 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;
};

private final static Gson gson = new GsonBuilder()
.registerTypeAdapter(Location.class, deserializer)
.registerTypeAdapter(Location.class, serializer)
.create();

@Override
public String convertToDatabaseColumn(Location attribute) {
return gson.toJson(attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
}

0 comments on commit 510f04e

Please sign in to comment.