own database class, added methods to PixelBlock class

This commit is contained in:
2024-07-16 14:06:49 +02:00
parent 89de8b6ab5
commit 5c8bc57123
12 changed files with 378 additions and 416 deletions

View File

@@ -1,7 +1,5 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.Pixel;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -11,7 +9,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import java.util.Arrays;
import java.util.List;
public class BlockBreakListener implements Listener {
@EventHandler
@@ -20,33 +17,10 @@ public class BlockBreakListener implements Listener {
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(blockLocation.getWorld())) {
Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(blockLocation.getWorld());
if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()-1) {
event.setCancelled(true);
return;
} else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+14) {
event.setCancelled(true);
return;
}
Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
String blockUUID = Arrays.stream(blockLocation.getWorld().getName().split("/")).toList().getLast();
List<PixelBlock> pixelBlocks = PixelBlock.placedBlocks.stream().filter(block -> block.uuid.toString().equals(blockUUID)).toList();
if(!pixelBlocks.isEmpty()) {
PixelBlock pixelBlock = pixelBlocks.getFirst();
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z());
relativeLocation.setWorld(pixelBlock.pixelBlockLocation.getWorld());
List<Pixel> pixels = pixelBlock.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList();
if(!pixels.isEmpty()) {
Pixel pixel = pixels.getFirst();
pixel.remove();
pixelBlock.pixels.remove(pixel);
}
}
});
assert pixelBlock != null;
pixelBlock.handleBlockBreak(event, true);
}
}
}

View File

@@ -1,7 +1,5 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.Pixel;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -11,7 +9,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import java.util.Arrays;
import java.util.List;
public class BlockPlaceListener implements Listener {
@EventHandler
@@ -20,34 +17,10 @@ public class BlockPlaceListener implements Listener {
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(blockLocation.getWorld())) {
Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(blockLocation.getWorld());
if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()-1) {
event.setCancelled(true);
return;
} else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+14) {
event.setCancelled(true);
return;
}
Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
String blockUUID = Arrays.stream(blockLocation.getWorld().getName().split("/")).toList().getLast();
List<PixelBlock> pixelBlocks = PixelBlock.placedBlocks.stream().filter(block -> block.uuid.toString().equals(blockUUID)).toList();
if(!pixelBlocks.isEmpty()) {
PixelBlock pixelBlock = pixelBlocks.getFirst();
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z());
relativeLocation.setWorld(pixelBlock.pixelBlockLocation.getWorld());
Pixel newPixel = new Pixel(
relativeLocation,
event.getBlock().getBlockData(),
(1/pixelBlock.pixelsPerBlock)-0.0001,
0.00005*pixelBlock.pixelsPerBlock);
pixelBlock.pixels.add(newPixel);
newPixel.spawn(pixelBlock.pixelBlockLocation);
}
});
assert pixelBlock != null;
pixelBlock.handleBlockPlace(event, true);
}
}
}

View File

@@ -8,7 +8,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class PlayerChangeWorldListener implements Listener {
@EventHandler
@@ -17,13 +17,7 @@ public class PlayerChangeWorldListener implements Listener {
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(worldLeft)) {
String blockUUID = Arrays.stream(worldLeft.getName().split("/")).toList().getLast();
List<PixelBlock> pixelBlocks = PixelBlock.placedBlocks.stream().filter(block -> block.uuid.toString().equals(blockUUID)).toList();
if(!pixelBlocks.isEmpty()) {
PixelBlock pixelBlock = pixelBlocks.getFirst();
pixelBlock.update();
}
Objects.requireNonNull(PixelBlock.getPixelBlockFromWorld(worldLeft)).update();
}
}
}

View File

@@ -1,91 +1,39 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import org.bukkit.*;
import org.bukkit.entity.Interaction;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.*;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
public class PlayerInteractListener implements Listener {
// @EventHandler
// static void onPlayerInteract(PlayerInteractEvent event) {
// if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null) {
// if(event.getClickedBlock().getType() == Material.GLASS) {
//// final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId());
//// worldCreator.type(WorldType.FLAT);
////
//// World newWorld = Bukkit.createWorld(worldCreator);
////
//// event.getPlayer().teleport(newWorld.getSpawnLocation());
// }
// }
// }
@EventHandler
static void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if(event.getRightClicked() instanceof Interaction) {
Location interactionLocation = event.getRightClicked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation();
Location interactionLocation = event.getRightClicked().getLocation().clone().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation();
interactionLocation.setYaw(0);
interactionLocation.setPitch(0);
UUID playerUID = event.getPlayer().getUniqueId();
PixelBlock pixelBlock = PixelBlock.placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(interactionLocation))
.findFirst()
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromLocation(interactionLocation);
if(playerUID.equals(blockOwner)) {
pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
pixelBlock.lastEntryTime = System.currentTimeMillis();
pixelBlock.saveToDB();
World blockWorld = Bukkit.getWorld(plugin.getDataFolder().getPath()+ "/" +pixelBlock.uuid);
assert blockWorld != null;
event.getPlayer().teleport(blockWorld.getSpawnLocation().clone().subtract(0.5, 0, 0.5));
// Bukkit.getScheduler().cancelTask(pixelBlock.updateTaskID);
// pixelBlock.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(
// PixelBlocks.plugin,
// pixelBlock::update,
// 100L,
// 100L
// );
} else {
event.getPlayer().sendMessage("Dieser Block gehört nicht dir!");
}
assert pixelBlock != null;
pixelBlock.handleInteraction(event.getPlayer());
}
}
@EventHandler
static void onPlayerAttackEntity(PrePlayerAttackEntityEvent event) {
if(event.getAttacked() instanceof Interaction) {
Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation();
Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation();
blockLocation.setYaw(0);
blockLocation.setPitch(0);
UUID playerUID = event.getPlayer().getUniqueId();
PixelBlock pixelBlock = PixelBlock.placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(blockLocation))
.findFirst()
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
if(playerUID.equals(blockOwner)) {
Bukkit.getScheduler().runTask(PixelBlocks.plugin, pixelBlock::remove);
} else {
event.getPlayer().sendMessage("Dieser Block gehört nicht dir!");
}
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromLocation(blockLocation);
assert pixelBlock != null;
pixelBlock.handleAttack(event.getPlayer());
}
}
}

View File

@@ -1,12 +0,0 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerJoinListener implements Listener {
@EventHandler
static void onPlayerJoin(PlayerJoinEvent event) {
}
}

View File

@@ -1,15 +0,0 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import com.destroystokyo.paper.event.player.PlayerJumpEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class PlayerJumpListener implements Listener {
@EventHandler
static void onPlayerJump(PlayerJumpEvent event) {
// event.getPlayer().getWorld().spawn(event.getPlayer().getLocation(), BlockDisplay.class, blockDisplay -> {
// blockDisplay.setBlock(Material.OAK_LOG.createBlockData());
// });
}
}