added and implemented taskchain

This commit is contained in:
2024-10-04 20:42:17 +02:00
parent 4f4a6aef10
commit ab71f09f8a
12 changed files with 130 additions and 102 deletions

View File

@@ -1,7 +1,8 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
import co.aikar.taskchain.TaskChain;
import eu.mhsl.minecraft.pixelblocks.PixelBlockItem;
import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.utils.Direction;
import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil;
import net.kyori.adventure.text.Component;
@@ -20,8 +21,8 @@ import java.util.*;
public class PixelBlock {
private final PixelBlockWorld pixelWorld;
private final float hitboxOffset = (float) PixelBlocksPlugin.configuration.hitboxOffset();
private final int pixelsPerBlock = PixelBlocksPlugin.configuration.pixelsPerBlock();
private final float hitboxOffset = (float) Main.configuration.hitboxOffset();
private final int pixelsPerBlock = Main.configuration.pixelsPerBlock();
public Location pixelBlockLocation;
public Direction facingDirection;
@@ -38,11 +39,11 @@ public class PixelBlock {
public static final int maxPixelsPerBlock = 2000;
public static @NotNull String getWorldName(@NotNull PixelBlock pixelBlock) {
return PixelBlocksPlugin.plugin.getDataFolder().getPath() + File.separator + pixelBlock.blockUUID;
return Main.plugin.getDataFolder().getPath() + File.separator + pixelBlock.blockUUID;
}
public static @Nullable PixelBlock getPixelBlockFromBlockWorld(World world) {
return PixelBlocksPlugin.pixelBlocks.stream()
return Main.pixelBlocks.stream()
.filter(block -> block.blockUUID.equals(getUUIDFromWorld(world)))
.findFirst()
.orElse(null);
@@ -61,14 +62,14 @@ public class PixelBlock {
searchLocation.setPitch(0);
searchLocation.setYaw(0);
return PixelBlocksPlugin.pixelBlocks.stream()
return Main.pixelBlocks.stream()
.filter(block -> Objects.equals(block.pixelBlockLocation, searchLocation))
.findFirst()
.orElse(null);
}
public PixelBlock(Location originLocation, UUID ownerUUID, UUID blockUUID, Direction direction) {
PixelBlocksPlugin.pixelBlocks.add(this);
Main.pixelBlocks.add(this);
this.ownerUUID = ownerUUID;
this.blockUUID = blockUUID;
@@ -82,17 +83,23 @@ public class PixelBlock {
this.pixelWorld = new PixelBlockWorld(this);
}
public <T> TaskChain<T> getBlockTaskChain() {
return Main.newSharedChain(this.blockUUID.toString());
}
public void enterBlock(@NotNull Player player) {
if(PixelBlocksPlugin.configuration.onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) {
if(Main.configuration.onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) {
player.sendMessage(Component.text("Dieser Pixelblock gehört nicht dir!", NamedTextColor.RED));
return;
}
this.lastEntryLocation = player.getLocation();
this.lastEntryTime = System.currentTimeMillis();
PixelBlocksPlugin.database.savePixelBlock(this);
player.teleport(this.pixelWorld.getSpawnLocation());
getBlockTaskChain()
.async(() -> {
this.lastEntryLocation = player.getLocation();
this.lastEntryTime = System.currentTimeMillis();
Main.database.savePixelBlock(this);
})
.sync(() -> player.teleport(this.pixelWorld.getSpawnLocation()))
.execute();
}
public void setLastEntryLocation(Location lastEntryLocation) {
@@ -156,56 +163,53 @@ public class PixelBlock {
}
public void updateEntities() {
Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> {
this.clearEntities();
this.getBlockTaskChain()
.sync(this::clearEntities)
.async(() -> {
for (int x = 0; x < pixelsPerBlock; x++) {
for (int y = 0; y < pixelsPerBlock; y++) {
for (int z = 0; z < pixelsPerBlock; z++) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), x, y, z);
Location blockLocation = this.pixelWorld.getBuildOrigin();
switch (this.facingDirection) {
case south -> blockLocation.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z());
case north -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.x(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.z());
case east -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.z(), relativeLocation.y(), relativeLocation.x());
case west -> blockLocation.add(relativeLocation.z(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.x());
}
BlockData block = blockLocation.getBlock().getBlockData();
for (int x = 0; x < pixelsPerBlock; x++) {
for (int y = 0; y < pixelsPerBlock; y++) {
for (int z = 0; z < pixelsPerBlock; z++) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), x, y, z);
Location blockLocation = this.pixelWorld.getBuildOrigin();
switch (this.facingDirection) {
case south -> blockLocation.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z());
case north -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.x(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.z());
case east -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.z(), relativeLocation.y(), relativeLocation.x());
case west -> blockLocation.add(relativeLocation.z(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.x());
}
BlockData block = blockLocation.getBlock().getBlockData();
if(block.getMaterial() != Material.AIR) {
pixels.add(new Pixel(relativeLocation, block, ((double) 1/pixelsPerBlock)));
if(block.getMaterial() != Material.AIR) {
pixels.add(new Pixel(relativeLocation, block, ((double) 1/pixelsPerBlock)));
}
}
}
}
}
}
})
.sync(() -> this.pixels.stream()
.limit(maxPixelsPerBlock)
.forEach(pixel -> pixel.place(this.pixelBlockLocation)))
.sync(() -> {
if(this.pixels.size() < 5) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), 0, 0, 0);
BlockData block = Material.GRAY_STAINED_GLASS.createBlockData();
Pixel newPixel = new Pixel(relativeLocation, block, 1);
pixels.add(newPixel);
newPixel.place(this.pixelBlockLocation);
this.pixels.stream()
.limit(maxPixelsPerBlock)
.forEach(pixel -> pixel.place(this.pixelBlockLocation));
Location itemDisplayLocation = this.pixelBlockLocation.clone().add(0.5, 0.5, 0.5);
this.barrier = (ItemDisplay) this.pixelBlockLocation.getWorld().spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
this.barrier.setItemStack(ItemStack.of(Material.BARRIER));
if(this.pixels.size() > maxPixelsPerBlock) {
}
if(this.pixels.size() < 5) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), 0, 0, 0);
BlockData block = Material.GRAY_STAINED_GLASS.createBlockData();
Pixel newPixel = new Pixel(relativeLocation, block, 1);
pixels.add(newPixel);
newPixel.place(this.pixelBlockLocation);
Location itemDisplayLocation = this.pixelBlockLocation.clone().add(0.5, 0.5, 0.5);
this.barrier = (ItemDisplay) this.pixelBlockLocation.getWorld().spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
this.barrier.setItemStack(ItemStack.of(Material.BARRIER));
spawnInteraction(true);
} else {
spawnInteraction(false);
}
});
spawnInteraction(true);
} else {
spawnInteraction(false);
}
})
.execute();
}
public void place(Location placeLocation, Direction direction) {
@@ -219,11 +223,11 @@ public class PixelBlock {
this.pixelBlockLocation = newLocation;
this.facingDirection = direction;
updateEntities();
PixelBlocksPlugin.database.savePixelBlock(this);
Main.database.savePixelBlock(this);
}
public void destroy(Player destroyedBy) {
if(PixelBlocksPlugin.configuration.onlyBreakableByOwner() && !destroyedBy.getUniqueId().equals(ownerUUID)) {
if(Main.configuration.onlyBreakableByOwner() && !destroyedBy.getUniqueId().equals(ownerUUID)) {
destroyedBy.sendMessage("Dieser Pixelblock gehört nicht dir!");
return;
}
@@ -238,7 +242,7 @@ public class PixelBlock {
.forEach(entity -> entity.teleport(this.lastEntryLocation));
this.clearEntities();
PixelBlocksPlugin.database.deletePixelBlock(this);
Main.database.deletePixelBlock(this);
this.pixelBlockLocation.getWorld().playSound(this.pixelBlockLocation, Sound.BLOCK_COPPER_BULB_BREAK, 1.0F, 30);
this.pixelBlockLocation.getWorld().dropItem(this.pixelBlockLocation.add(new Vector(0.5, 0.5, 0.5)), PixelBlockItem.getBlockAsItem(this));
this.pixelBlockLocation = null;

View File

@@ -1,6 +1,6 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.utils.LocationUtil;
import org.bukkit.*;
import org.bukkit.entity.Entity;
@@ -13,14 +13,14 @@ import java.util.List;
import java.util.Objects;
import java.util.Random;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin.plugin;
import static eu.mhsl.minecraft.pixelblocks.Main.plugin;
public class PixelBlockWorld {
private final PixelBlock parentPixelBlock;
private final World world;
int worldGrassBorderWidth = 10;
int pixelsPerBlock = PixelBlocksPlugin.configuration.pixelsPerBlock();
int pixelsPerBlock = Main.configuration.pixelsPerBlock();
public static boolean isPixelWorld(@NotNull World world) {
return world.getName().startsWith(plugin.getDataFolder().getPath());
@@ -49,7 +49,7 @@ public class PixelBlockWorld {
}
public @NotNull String getWorldPathName() {
return PixelBlocksPlugin.plugin.getDataFolder().getPath() + File.separator + "worlds" + File.separator + this.parentPixelBlock.blockUUID;
return Main.plugin.getDataFolder().getPath() + File.separator + "worlds" + File.separator + this.parentPixelBlock.blockUUID;
}
public @NotNull Location getSpawnLocation() {
@@ -114,7 +114,7 @@ public class PixelBlockWorld {
}
private void setBuildingPlatform() {
Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> {
Bukkit.getScheduler().runTask(Main.plugin, () -> {
for (int x = 0; x < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; x++) {
for (int z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) {
getPlatformOrigin().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);