feature-remove-db #8

Merged
Pupsi merged 21 commits from feature-remove-db into main 2024-10-19 13:25:58 +00:00
9 changed files with 140 additions and 91 deletions
Showing only changes of commit 4bda427693 - Show all commits

View File

@ -27,10 +27,10 @@ public final class Main extends JavaPlugin {
public static List<PixelBlock> pixelBlocks = new ArrayList<>(); public static List<PixelBlock> pixelBlocks = new ArrayList<>();
public static <T> TaskChain<T> newChain() { public static <T> TaskChain<T> chain() {
return taskFactory.newChain(); return taskFactory.newChain();
} }
public static <T> TaskChain<T> newSharedChain(String name) { public static <T> TaskChain<T> sharedChain(String name) {
return taskFactory.newSharedChain(name); return taskFactory.newSharedChain(name);
} }

View File

@ -60,7 +60,7 @@ public class PixelBlockDatabase {
public void deletePixelBlock(PixelBlock pixelBlock) { public void deletePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, () -> {
try { try {
this.deletePixelBlock.setString(1, pixelBlock.blockUUID.toString()); this.deletePixelBlock.setString(1, pixelBlock.getBlockUUID().toString());
this.deletePixelBlock.executeUpdate(); this.deletePixelBlock.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException("Failed to delete PixelBlock from the database", e); throw new RuntimeException("Failed to delete PixelBlock from the database", e);
@ -71,27 +71,27 @@ public class PixelBlockDatabase {
public void savePixelBlock(PixelBlock pixelBlock) { public void savePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTask(Main.plugin, () -> { Bukkit.getScheduler().runTask(Main.plugin, () -> {
try { try {
this.insertOrReplacePixelBlock.setString(1, pixelBlock.blockUUID.toString()); this.insertOrReplacePixelBlock.setString(1, pixelBlock.getBlockUUID().toString());
this.insertOrReplacePixelBlock.setString(2, pixelBlock.ownerUUID.toString()); this.insertOrReplacePixelBlock.setString(2, pixelBlock.getOwnerUUID().toString());
this.insertOrReplacePixelBlock.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName()); this.insertOrReplacePixelBlock.setString(3, pixelBlock.getPixelBlockLocation().getWorld().getName());
this.insertOrReplacePixelBlock.setDouble(4, pixelBlock.pixelBlockLocation.getX()); this.insertOrReplacePixelBlock.setDouble(4, pixelBlock.getPixelBlockLocation().getX());
this.insertOrReplacePixelBlock.setDouble(5, pixelBlock.pixelBlockLocation.getY()); this.insertOrReplacePixelBlock.setDouble(5, pixelBlock.getPixelBlockLocation().getY());
this.insertOrReplacePixelBlock.setDouble(6, pixelBlock.pixelBlockLocation.getZ()); this.insertOrReplacePixelBlock.setDouble(6, pixelBlock.getPixelBlockLocation().getZ());
if (pixelBlock.lastEntryLocation != null) { if (pixelBlock.getLastEntryLocation() != null) {
this.insertOrReplacePixelBlock.setString(7, pixelBlock.lastEntryLocation.getWorld().getName()); this.insertOrReplacePixelBlock.setString(7, pixelBlock.getLastEntryLocation().getWorld().getName());
this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.lastEntryLocation.getX()); this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.getLastEntryLocation().getX());
this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.lastEntryLocation.getY()); this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.getLastEntryLocation().getY());
this.insertOrReplacePixelBlock.setDouble(10, pixelBlock.lastEntryLocation.getZ()); this.insertOrReplacePixelBlock.setDouble(10, pixelBlock.getLastEntryLocation().getZ());
} else { } else {
this.insertOrReplacePixelBlock.setString(7, pixelBlock.pixelBlockLocation.getWorld().getName()); this.insertOrReplacePixelBlock.setString(7, pixelBlock.getPixelBlockLocation().getWorld().getName());
this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.pixelBlockLocation.getX()); this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.getPixelBlockLocation().getX());
this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.pixelBlockLocation.getY()); this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.getPixelBlockLocation().getY());
this.insertOrReplacePixelBlock.setDouble(10, pixelBlock.pixelBlockLocation.getZ()); this.insertOrReplacePixelBlock.setDouble(10, pixelBlock.getPixelBlockLocation().getZ());
} }
this.insertOrReplacePixelBlock.setString(11, pixelBlock.facingDirection.toString()); this.insertOrReplacePixelBlock.setString(11, pixelBlock.getFacingDirection().toString());
this.insertOrReplacePixelBlock.executeUpdate(); this.insertOrReplacePixelBlock.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -44,18 +44,18 @@ public class PixelBlockItem {
} }
public static @NotNull ItemStack getBlockAsItem(@NotNull PixelBlock block) { public static @NotNull ItemStack getBlockAsItem(@NotNull PixelBlock block) {
String ownerName = Optional.ofNullable(Bukkit.getOfflinePlayer(block.ownerUUID).getName()).orElseGet(() -> block.ownerUUID.toString()); String ownerName = Optional.ofNullable(Bukkit.getOfflinePlayer(block.getOwnerUUID()).getName()).orElseGet(() -> block.getOwnerUUID().toString());
ItemStack itemStack = HeadUtil.getCustomTextureHead(itemTexture); ItemStack itemStack = HeadUtil.getCustomTextureHead(itemTexture);
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
meta.setMaxStackSize(1); meta.setMaxStackSize(1);
meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, block.blockUUID.toString()); meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, block.getBlockUUID().toString());
meta.getPersistentDataContainer().set(ownerProperty, PersistentDataType.STRING, block.ownerUUID.toString()); meta.getPersistentDataContainer().set(ownerProperty, PersistentDataType.STRING, block.getOwnerUUID().toString());
meta.displayName(Component.text("Pixelblock von " + ownerName)); meta.displayName(Component.text("Pixelblock von " + ownerName));
meta.lore(List.of( meta.lore(List.of(
Component.text(ownerName + " ist der Besitzer dieses Blocks."), Component.text(ownerName + " ist der Besitzer dieses Blocks."),
Component.text("Klicke auf den gesetzten Block, um diesen zu bearbeiten!"), Component.text("Klicke auf den gesetzten Block, um diesen zu bearbeiten!"),
Component.text(block.blockUUID.toString()).color(NamedTextColor.DARK_GRAY) Component.text(block.getBlockUUID().toString()).color(NamedTextColor.DARK_GRAY)
)); ));
itemStack.setItemMeta(meta); itemStack.setItemMeta(meta);

View File

@ -23,7 +23,7 @@ public class ExitWorldCommand implements CommandExecutor {
PixelBlock currentPixelBlock = PixelBlock.getPixelBlockFromBlockWorld(playerWorld); PixelBlock currentPixelBlock = PixelBlock.getPixelBlockFromBlockWorld(playerWorld);
Objects.requireNonNull(currentPixelBlock); Objects.requireNonNull(currentPixelBlock);
p.teleport(currentPixelBlock.lastEntryLocation); p.teleport(currentPixelBlock.getLastEntryLocation());
} }
return true; return true;
} }

View File

@ -47,7 +47,7 @@ public class PlacePixelBlockListener implements Listener {
} else { } else {
UUID itemUUID = info.id(); UUID itemUUID = info.id();
pixelBlock = Main.pixelBlocks.stream() pixelBlock = Main.pixelBlocks.stream()
.filter(block -> block.blockUUID.equals(itemUUID)) .filter(block -> block.getBlockUUID().equals(itemUUID))
.findFirst() .findFirst()
.orElseGet(() -> new PixelBlock( .orElseGet(() -> new PixelBlock(
newBlockLocation, newBlockLocation,

View File

@ -1,57 +1,66 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock; package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.Main;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.NamespacedKey;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.BlockDisplay; import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Transformation; import org.bukkit.util.Transformation;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class Pixel { public class Pixel {
public final Location relativeLocation; private static final NamespacedKey parentBlockTag = new NamespacedKey(Main.plugin, "parent");
private final @NotNull PixelBlock parentBlock;
private final @NotNull BlockDisplay entity;
public final Location location;
public final BlockData blockData; public final BlockData blockData;
private final double scale; private final double scale;
private @Nullable UUID uuid;
public Pixel(@NotNull Location relativeLocation, @NotNull BlockData blockData, double scale) { public static Pixel existingPixel(PixelBlock parentBlock, BlockDisplay entity) {
this.relativeLocation = new Location( PersistentDataContainer dataContainer = entity.getPersistentDataContainer();
relativeLocation.getWorld(), if(!dataContainer.has(parentBlockTag)) throw new IllegalArgumentException("Entity is missing the parent tag in the DataContainer");
relativeLocation.x(), UUID expectedParentUuid = UUID.fromString(Objects.requireNonNull(dataContainer.get(parentBlockTag, PersistentDataType.STRING)));
relativeLocation.y(), if(!parentBlock.getBlockUUID().equals(expectedParentUuid)) throw new IllegalArgumentException("Pixel is from a different parent block");
relativeLocation.z() return new Pixel(parentBlock, entity);
);
this.blockData = blockData;
this.scale = scale;
} }
public void place(@NotNull Location spawnBlockLocation) { public static Pixel newPixel(PixelBlock parentBlock, Vector relativePosition, BlockData blockData, double scale) {
World world = spawnBlockLocation.getWorld(); return new Pixel(parentBlock, relativePosition, blockData, scale);
double positionX = spawnBlockLocation.x() + (relativeLocation.x()*scale); }
double positionY = spawnBlockLocation.y() + (relativeLocation.y()*scale);
double positionZ = spawnBlockLocation.z() + (relativeLocation.z()*scale);
Location spawnLocation = new Location(world, positionX, positionY, positionZ); private Pixel(@NotNull PixelBlock parentBlock, @NotNull BlockDisplay entity) {
this.parentBlock = parentBlock;
this.entity = entity;
this.location = entity.getLocation();
this.blockData = entity.getBlock();
this.scale = entity.getTransformation().getScale().get(0);
}
BlockDisplay entity = (BlockDisplay) world.spawnEntity(spawnLocation, EntityType.BLOCK_DISPLAY); private Pixel(@NotNull PixelBlock parentBlock, @NotNull Vector relativePosition, @NotNull BlockData blockData, double scale) {
this.uuid = entity.getUniqueId(); this.parentBlock = parentBlock;
this.location = parentBlock.getPixelBlockLocation().add(relativePosition.multiply(scale));
this.blockData = blockData;
this.scale = scale;
this.entity = (BlockDisplay) this.location.getWorld().spawnEntity(this.location, EntityType.BLOCK_DISPLAY);
entity.setBlock(blockData); this.entity.setBlock(blockData);
Transformation transform = this.entity.getTransformation();
Transformation transform = entity.getTransformation(); transform.getScale().set(this.scale);
transform.getScale().set(scale); this.entity.setTransformation(transform);
entity.setTransformation(transform); this.entity.getPersistentDataContainer().set(parentBlockTag, PersistentDataType.STRING, this.parentBlock.getBlockUUID().toString());
} }
public void destroy() { public void destroy() {
Objects.requireNonNull(this.uuid); this.entity.remove();
Entity pixelEntity = this.relativeLocation.getWorld().getEntity(this.uuid);
if(pixelEntity != null) pixelEntity.remove();
} }
} }

View File

@ -24,16 +24,16 @@ public class PixelBlock {
private final float hitboxOffset = (float) Main.configuration.hitboxOffset(); private final float hitboxOffset = (float) Main.configuration.hitboxOffset();
private final int pixelsPerBlock = Main.configuration.pixelsPerBlock(); private final int pixelsPerBlock = Main.configuration.pixelsPerBlock();
public Location pixelBlockLocation; private Location pixelBlockLocation;
public Direction facingDirection; private Direction facingDirection;
public List<Pixel> pixels = new ArrayList<>(); private final List<Pixel> pixels = new ArrayList<>();
public PixelBlockHitbox hitbox; private PixelBlockHitbox hitbox;
public List<ItemDisplay> placeholderIcon = new ArrayList<>(); private final List<ItemDisplay> placeholderIcon = new ArrayList<>();
public Location lastEntryLocation; private Location lastEntryLocation;
public UUID ownerUUID; private final UUID ownerUUID;
public UUID blockUUID; private final UUID blockUUID;
public static final int maxPixelsPerBlock = 2000; public static final int maxPixelsPerBlock = 2000;
@ -80,10 +80,16 @@ public class PixelBlock {
this.facingDirection = direction; this.facingDirection = direction;
this.pixelWorld = new PixelBlockWorld(this); this.pixelWorld = new PixelBlockWorld(this);
this.pixelBlockLocation.getNearbyEntitiesByType(BlockDisplay.class, 1).forEach(blockDisplay -> {
try {
this.pixels.add(Pixel.existingPixel(this, blockDisplay));
} catch(IllegalArgumentException ignored) {}
});
} }
public <T> TaskChain<T> getBlockTaskChain() { public <T> TaskChain<T> getBlockTaskChain() {
return Main.newSharedChain(this.blockUUID.toString()); return Main.sharedChain(this.blockUUID.toString());
} }
public void enterBlock(@NotNull Player player) { public void enterBlock(@NotNull Player player) {
@ -118,11 +124,13 @@ public class PixelBlock {
public void updateEntities() { public void updateEntities() {
this.getBlockTaskChain() this.getBlockTaskChain()
.sync(this::clearEntities) .sync(this::clearEntities)
.async(() -> { .sync(() -> {
for (int x = 0; x < pixelsPerBlock; x++) { for (int x = 0; x < pixelsPerBlock; x++) {
for (int y = 0; y < pixelsPerBlock; y++) { for (int y = 0; y < pixelsPerBlock; y++) {
for (int z = 0; z < pixelsPerBlock; z++) { for (int z = 0; z < pixelsPerBlock; z++) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), x, y, z); World world = pixelBlockLocation.getWorld();
Location relativeLocation = new Location(world, x, y, z);
Location blockLocation = this.pixelWorld.getBuildOrigin(); Location blockLocation = this.pixelWorld.getBuildOrigin();
switch (this.facingDirection) { switch (this.facingDirection) {
case south -> blockLocation.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); case south -> blockLocation.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z());
@ -132,23 +140,23 @@ public class PixelBlock {
} }
BlockData block = blockLocation.getBlock().getBlockData(); BlockData block = blockLocation.getBlock().getBlockData();
if(block.getMaterial() != Material.AIR) { if(!block.getMaterial().isEmpty()) {
pixels.add(new Pixel(relativeLocation, block, ((double) 1/pixelsPerBlock))); this.getBlockTaskChain()
.delay(1)
.sync(() -> pixels.add(Pixel.newPixel(this, relativeLocation.toVector(), block, (double) 1 / pixelsPerBlock)))
.execute();
} }
} }
} }
} }
}) })
.sync(() -> this.pixels.stream()
.limit(maxPixelsPerBlock)
.forEach(pixel -> pixel.place(this.pixelBlockLocation)))
.sync(() -> { .sync(() -> {
if(this.pixels.size() < 5) { if(this.pixels.size() < 5) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), 0, 0, 0); // Location relativeLocation = new Location(pixelBlockLocation.getWorld(), 0, 0, 0);
BlockData block = Material.GRAY_STAINED_GLASS.createBlockData(); // BlockData block = Material.GRAY_STAINED_GLASS.createBlockData();
Pixel newPixel = new Pixel(relativeLocation, block, 1); // Pixel newPixel = new Pixel(relativeLocation, block, 1);
pixels.add(newPixel); // pixels.add(newPixel);
newPixel.place(this.pixelBlockLocation); // newPixel.place(this.pixelBlockLocation);
Location itemDisplayLocation = this.pixelBlockLocation.clone().add(0.5, 0.5, 0.5); Location itemDisplayLocation = this.pixelBlockLocation.clone().add(0.5, 0.5, 0.5);
@ -245,13 +253,13 @@ public class PixelBlock {
this.placeholderIcon.clear(); this.placeholderIcon.clear();
} }
this.pixelBlockLocation.getWorld().getEntities().stream() // this.pixelBlockLocation.getWorld().getEntities().stream()
.filter(this::isPixelBlockComponent) // .filter(this::isPixelBlockComponent)
.filter(entity -> entity.getLocation() // .filter(entity -> entity.getLocation()
.add(0, hitboxOffset, 0) // .add(0, hitboxOffset, 0)
.toBlockLocation() // .toBlockLocation()
.equals(this.pixelBlockLocation)) // .equals(this.pixelBlockLocation))
.forEach(Entity::remove); // .forEach(Entity::remove);
} }
private boolean isPixelBlockComponent(Entity entity) { private boolean isPixelBlockComponent(Entity entity) {
@ -264,4 +272,36 @@ public class PixelBlock {
public @NotNull PixelBlockWorld getPixelWorld() { public @NotNull PixelBlockWorld getPixelWorld() {
return pixelWorld; return pixelWorld;
} }
public Location getPixelBlockLocation() {
return pixelBlockLocation.clone();
}
public Direction getFacingDirection() {
return facingDirection;
}
public List<Pixel> getPixels() {
return pixels;
}
public PixelBlockHitbox getHitbox() {
return hitbox;
}
public List<ItemDisplay> getPlaceholderIcon() {
return placeholderIcon;
}
public Location getLastEntryLocation() {
return lastEntryLocation.clone();
}
public UUID getOwnerUUID() {
return ownerUUID;
}
public UUID getBlockUUID() {
return blockUUID;
}
} }

View File

@ -11,7 +11,7 @@ public class PixelBlockHitbox {
private final Interaction hitbox; private final Interaction hitbox;
public PixelBlockHitbox(boolean fullBlock, Location pixelBlockLocation, float hitboxOffset, List<Pixel> pixels, int pixelsPerBlock) { public PixelBlockHitbox(boolean fullBlock, Location pixelBlockLocation, float hitboxOffset, List<Pixel> pixels, int pixelsPerBlock) {
if(fullBlock) { if(fullBlock || true) {
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity( hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5), pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION EntityType.INTERACTION
@ -19,13 +19,13 @@ public class PixelBlockHitbox {
hitbox.setInteractionHeight(1 + 2*hitboxOffset); hitbox.setInteractionHeight(1 + 2*hitboxOffset);
hitbox.setInteractionWidth(1 + 2*hitboxOffset); hitbox.setInteractionWidth(1 + 2*hitboxOffset);
} else { } else {
double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation.getX()); double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getX());
double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation.getY()); double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getY());
double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.relativeLocation.getZ()); double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getZ());
double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation.getX()); double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getX());
double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation.getY()); double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getY());
double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.relativeLocation.getZ()); double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getZ());
Location spawnLocation = pixelBlockLocation.clone().add( Location spawnLocation = pixelBlockLocation.clone().add(
((startingX+endingX+1)/2)/pixelsPerBlock, ((startingX+endingX+1)/2)/pixelsPerBlock,

View File

@ -49,7 +49,7 @@ public class PixelBlockWorld {
} }
public @NotNull String getWorldPathName() { public @NotNull String getWorldPathName() {
return Main.plugin.getDataFolder().getPath() + File.separator + "worlds" + File.separator + this.parentPixelBlock.blockUUID; return Main.plugin.getDataFolder().getPath() + File.separator + "worlds" + File.separator + this.parentPixelBlock.getBlockUUID();
} }
public @NotNull Location getSpawnLocation() { public @NotNull Location getSpawnLocation() {