added and implemented taskchain

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

View File

@ -26,5 +26,15 @@
<option name="name" value="sonatype" />
<option name="url" value="https://oss.sonatype.org/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven2" />
<option name="name" value="maven2" />
<option name="url" value="https://hub.spigotmc.org/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven" />
<option name="name" value="maven" />
<option name="url" value="https://repo.aikar.co/content/groups/aikar/" />
</remote-repository>
</component>
</project>

View File

@ -1,5 +1,6 @@
plugins {
id 'java'
id 'com.gradleup.shadow' version '8.3.1'
}
group = 'eu.mhsl.minecraft'
@ -15,10 +16,14 @@ repositories {
name = "sonatype"
url = "https://oss.sonatype.org/content/groups/public/"
}
maven {
url = "https://repo.aikar.co/content/groups/aikar/"
}
}
dependencies {
compileOnly "io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT"
implementation "co.aikar:taskchain-bukkit:3.7.2"
}
def targetJavaVersion = 21
@ -31,14 +36,6 @@ java {
}
}
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
options.release.set(targetJavaVersion)
}
}
processResources {
def props = [version: version]
inputs.properties props
@ -49,8 +46,12 @@ processResources {
}
tasks.register('copyJarToTestServer', Exec) {
dependsOn jar
mustRunAfter jar
commandLine 'cp', 'build/libs/PixelBlocks-1.0-SNAPSHOT.jar', '/home/elias/Dokumente/mcTestServer/plugins/pixelblocks.jar'
commandLine 'cp', 'build/libs/PixelBlocks-1.0-SNAPSHOT-all.jar', '/home/elias/Dokumente/mcTestServer/plugins/pixelblocks.jar'
}
shadowJar {
relocate 'co.aikar.taskchain', 'eu.mhsl.minecraft.pixelblocks.taskchain'
}
jar.dependsOn shadowJar
copyJarToTestServer.dependsOn jar

View File

@ -1,5 +1,8 @@
package eu.mhsl.minecraft.pixelblocks;
import co.aikar.taskchain.BukkitTaskChainFactory;
import co.aikar.taskchain.TaskChain;
import co.aikar.taskchain.TaskChainFactory;
import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand;
import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand;
import eu.mhsl.minecraft.pixelblocks.listeners.*;
@ -15,22 +18,31 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public final class PixelBlocksPlugin extends JavaPlugin {
public static PixelBlocksPlugin plugin;
public final class Main extends JavaPlugin {
public static Main plugin;
public static PixelBlockConfiguration configuration;
public static PixelBlockDatabase database;
public static TaskChainFactory taskFactory;
public static List<PixelBlock> pixelBlocks = new ArrayList<>();
public static <T> TaskChain<T> newChain() {
return taskFactory.newChain();
}
public static <T> TaskChain<T> newSharedChain(String name) {
return taskFactory.newSharedChain(name);
}
@Override
public void onLoad() {
PixelBlocksPlugin.plugin = this;
Main.plugin = this;
FileConfiguration config = this.getConfig();
PixelBlockConfiguration.setDefaults(config);
this.saveConfig();
PixelBlocksPlugin.configuration = new PixelBlockConfiguration(
Main.configuration = new PixelBlockConfiguration(
config.getInt(PixelBlockConfiguration.Keys.PixelsPerBlock.getKey()),
config.getDouble(PixelBlockConfiguration.Keys.HitboxOffset.getKey()),
config.getBoolean(PixelBlockConfiguration.Keys.OnlyBreakableByOwners.getKey()),
@ -38,11 +50,12 @@ public final class PixelBlocksPlugin extends JavaPlugin {
);
File databaseFile = new File(plugin.getDataFolder(), "blocks.db");
PixelBlocksPlugin.database = new PixelBlockDatabase("jdbc:sqlite:" + databaseFile);
Main.database = new PixelBlockDatabase("jdbc:sqlite:" + databaseFile);
}
@Override
public void onEnable() {
Main.taskFactory = BukkitTaskChainFactory.create(this);
database.loadPixelBlocks();
Listener[] listeners = {

View File

@ -68,11 +68,11 @@ public class PixelBlockDatabase {
}
public void deletePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTaskAsynchronously(PixelBlocksPlugin.plugin, () -> {
Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, () -> {
try {
this.deletePixelBlock.setString(1, pixelBlock.blockUUID.toString());
this.deletePixelBlock.executeUpdate();
PixelBlocksPlugin.plugin.getLogger().info("DB: Deleted PixelBlock: " + pixelBlock.blockUUID);
Main.plugin.getLogger().info("DB: Deleted PixelBlock: " + pixelBlock.blockUUID);
} catch (SQLException e) {
throw new RuntimeException("Failed to delete PixelBlock", e);
}
@ -80,7 +80,7 @@ public class PixelBlockDatabase {
}
public void savePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> {
Bukkit.getScheduler().runTask(Main.plugin, () -> {
List<UUID> storedPixelBlocks = new ArrayList<>();
try {
@ -118,7 +118,7 @@ public class PixelBlockDatabase {
this.insertNewPixelBlock.setString(11, pixelBlock.facingDirection.toString());
this.insertNewPixelBlock.executeUpdate();
PixelBlocksPlugin.plugin.getLogger().info("DB: Created PixelBlock: " + pixelBlock.blockUUID);
Main.plugin.getLogger().info("DB: Created PixelBlock: " + pixelBlock.blockUUID);
} catch (SQLException e) {
throw new RuntimeException("Failed to save PixelBlock", e);
}
@ -149,7 +149,7 @@ public class PixelBlockDatabase {
this.updateExistingPixelBlock.setString(11, pixelBlock.facingDirection.toString());
this.updateExistingPixelBlock.executeUpdate();
PixelBlocksPlugin.plugin.getLogger().info("DB: Updated PixelBlock: " + pixelBlock.blockUUID);
Main.plugin.getLogger().info("DB: Updated PixelBlock: " + pixelBlock.blockUUID);
} catch (SQLException e) {
throw new RuntimeException("Failed updating PixelBlock", e);
}
@ -183,7 +183,7 @@ public class PixelBlockDatabase {
Direction.valueOf(allPixelBlocks.getString("direction"))
);
block.setLastEntryLocation(entryLocation);
PixelBlocksPlugin.plugin.getLogger().info("DB: Loaded PixelBlock: " + block.blockUUID);
Main.plugin.getLogger().info("DB: Loaded PixelBlock: " + block.blockUUID);
}
} catch (SQLException e) {
throw new RuntimeException("Failed loading PixelBlocks", e);

View File

@ -22,9 +22,9 @@ import java.util.Optional;
import java.util.UUID;
public class PixelBlockItem {
public static final NamespacedKey recipeKey = new NamespacedKey(PixelBlocksPlugin.plugin, "pixelblock");
public static NamespacedKey idProperty = new NamespacedKey(PixelBlocksPlugin.plugin, "id");
public static NamespacedKey ownerProperty = new NamespacedKey(PixelBlocksPlugin.plugin, "owner");
public static final NamespacedKey recipeKey = new NamespacedKey(Main.plugin, "pixelblock");
public static NamespacedKey idProperty = new NamespacedKey(Main.plugin, "id");
public static NamespacedKey ownerProperty = new NamespacedKey(Main.plugin, "owner");
public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";

View File

@ -1,6 +1,6 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import org.bukkit.Location;
@ -13,7 +13,7 @@ public class BreakPixelBlockListener implements Listener {
static void destroyPixelBlock(PrePlayerAttackEntityEvent event) {
if(!(event.getAttacked() instanceof Interaction)) return;
Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlocksPlugin.configuration.hitboxOffset(), 0).toBlockLocation();
Location blockLocation = event.getAttacked().getLocation().add(0, Main.configuration.hitboxOffset(), 0).toBlockLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(blockLocation);
if(pixelBlock == null) return;
pixelBlock.destroy(event.getPlayer());

View File

@ -1,7 +1,7 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlockItem;
import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin;
import eu.mhsl.minecraft.pixelblocks.Main;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -20,7 +20,7 @@ public class DiscoverRecipesListener implements Listener {
if(!(event.getWhoClicked() instanceof Player player)) return;
if(!List.of(Material.HEART_OF_THE_SEA, Material.END_CRYSTAL).contains(clickedItem.getType())) return;
if(player.hasDiscoveredRecipe(PixelBlockItem.recipeKey)) return;
PixelBlocksPlugin.plugin.getLogger().log(Level.INFO, String.format("%s unlocked tne PixelBlock recipe!", player.getName()));
Main.plugin.getLogger().log(Level.INFO, String.format("%s unlocked tne PixelBlock recipe!", player.getName()));
player.discoverRecipe(PixelBlockItem.recipeKey);
}
}

View File

@ -1,6 +1,6 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.*;
import org.bukkit.entity.Interaction;
@ -16,7 +16,7 @@ public class EnterPixelBlockListener implements Listener {
Location interactionLocation = event
.getRightClicked()
.getLocation()
.add(0, PixelBlocksPlugin.configuration.hitboxOffset(), 0)
.add(0, Main.configuration.hitboxOffset(), 0)
.toBlockLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(interactionLocation);
if(pixelBlock == null) return;

View File

@ -1,7 +1,7 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlockItem;
import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin;
import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlockWorld;
import eu.mhsl.minecraft.pixelblocks.utils.Direction;
@ -46,7 +46,7 @@ public class PlacePixelBlockListener implements Listener {
);
} else {
UUID itemUUID = info.id();
pixelBlock = PixelBlocksPlugin.pixelBlocks.stream()
pixelBlock = Main.pixelBlocks.stream()
.filter(block -> block.blockUUID.equals(itemUUID))
.findFirst()
.orElseGet(() -> new PixelBlock(

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);

View File

@ -1,6 +1,6 @@
name: PixelBlocks
version: '${version}'
main: eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin
main: eu.mhsl.minecraft.pixelblocks.Main
api-version: '1.21'
commands:
createpixelblock: