own world per pixelblock, live update, owners

This commit is contained in:
Lars Neuhaus 2024-07-07 23:26:23 +02:00
parent ccdff1ed64
commit d97cf35aed
11 changed files with 349 additions and 64 deletions

View File

@ -3,11 +3,9 @@ package eu.mhsl.minecraft.pixelblocks;
import eu.mhsl.minecraft.pixelblocks.commands.ChessCommand;
import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand;
import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand;
import eu.mhsl.minecraft.pixelblocks.listeners.PlayerInteractListener;
import eu.mhsl.minecraft.pixelblocks.listeners.PlayerJumpListener;
import eu.mhsl.minecraft.pixelblocks.listeners.*;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.Objects;
public final class PixelBlocks extends JavaPlugin {
@ -17,17 +15,19 @@ public final class PixelBlocks extends JavaPlugin {
public void onEnable() {
PixelBlocks.plugin = this;
this.getLogger().info("Hello World");
new File(plugin.getDataFolder().getPath() + "/Maps").mkdir();
// new File(plugin.getDataFolder().getPath()).mkdir();
getServer().getPluginManager().registerEvents(new PlayerJumpListener(), this);
getServer().getPluginManager().registerEvents(new PlayerInteractListener(), this);
getServer().getPluginManager().registerEvents(new PlayerMoveListener(), this);
getServer().getPluginManager().registerEvents(new EntityDamageListener(), this);
getServer().getPluginManager().registerEvents(new BlockBreakListener(), this);
getServer().getPluginManager().registerEvents(new BlockPlaceListener(), this);
getServer().getPluginManager().registerEvents(new CreatureSpawnListener(), this);
Objects.requireNonNull(getCommand("chess")).setExecutor(new ChessCommand());
Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand());
Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand());
}
@Override
public void onDisable() {
}
}

View File

@ -0,0 +1,15 @@
package eu.mhsl.minecraft.pixelblocks.chunkGenerators;
import org.bukkit.World;
import org.bukkit.generator.ChunkGenerator;
import javax.annotation.Nonnull;
import java.util.Random;
public class EmptyChunkGenerator extends ChunkGenerator {
@Override
@Nonnull
public ChunkData generateChunkData(@Nonnull World world, @Nonnull Random random, int x, int y, @Nonnull BiomeGrid biome) {
return createChunkData(world);
}
}

View File

@ -19,13 +19,21 @@ public class ExitWorldCommand implements CommandExecutor {
World playerWorld = p.getWorld();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
Location newLocation = PixelBlock.placedBlocks.stream()
if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
List<PixelBlock> ownedBlocks = PixelBlock.placedBlocks.stream()
.filter(pixelBlock -> pixelBlock.owner == p.getUniqueId())
.findFirst()
.orElseThrow()
.lastEntryLocation;
// p.teleport(Bukkit.getServer().getWorlds().getFirst().getSpawnLocation());
.sorted(Comparator.comparing(pixelBlock -> pixelBlock.lastEntryTime))
.toList();
Location newLocation;
if(!ownedBlocks.isEmpty()) {
newLocation = ownedBlocks.getLast().lastEntryLocation;
} else {
newLocation = Bukkit.getWorlds().getFirst().getSpawnLocation();
}
p.teleport(newLocation);
} else {
p.sendMessage("Dieser Command ist nur für PixelBlock Welten verfügbar!");
}
}

View File

@ -0,0 +1,52 @@
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;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
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
static void onBlockBreak(BlockBreakEvent event) {
Location blockLocation = event.getBlock().getLocation();
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();
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);
}
}
});
}
}
}

View File

@ -0,0 +1,53 @@
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;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
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
static void onBlockPlace(BlockPlaceEvent event) {
Location blockLocation = event.getBlock().getLocation();
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();
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);
}
});
}
}
}

View File

@ -0,0 +1,22 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import java.util.Arrays;
public class CreatureSpawnListener implements Listener {
@EventHandler
static void onCreatureSpawn(CreatureSpawnEvent event) {
Location entityLocation = event.getEntity().getLocation();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(entityLocation.getWorld())) {
event.setCancelled(true);
}
}
}

View File

@ -0,0 +1,24 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import java.util.Arrays;
public class EntityDamageListener implements Listener {
@EventHandler
static void onEntityDamage(EntityDamageEvent event) {
if(event.getEntity() instanceof Player player) {
World playerWorld = player.getWorld();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
event.setCancelled(true);
}
}
}
}

View File

@ -1,14 +1,9 @@
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 io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import io.papermc.paper.math.Position;
import org.bukkit.*;
import org.bukkit.entity.Blaze;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Interaction;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -16,9 +11,7 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
@ -43,6 +36,7 @@ public class PlayerInteractListener implements Listener {
Location interactionLocation = event.getRightClicked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation();
interactionLocation.setYaw(0);
interactionLocation.setPitch(0);
UUID playerUID = event.getPlayer().getUniqueId();
PixelBlock pixelBlock = PixelBlock.placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(interactionLocation))
@ -52,14 +46,21 @@ public class PlayerInteractListener implements Listener {
if(playerUID == blockOwner) {
pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
pixelBlock.lastEntryTime = System.currentTimeMillis();
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId());
worldCreator.type(WorldType.FLAT);
World newWorld = Bukkit.createWorld(worldCreator);
World blockWorld = Bukkit.getWorld(plugin.getDataFolder().getPath()+"/"+pixelBlock.uuid);
assert blockWorld != null;
event.getPlayer().teleport(blockWorld.getSpawnLocation());
event.getPlayer().teleport(newWorld.getSpawnLocation());
Bukkit.getScheduler().cancelTask(pixelBlock.updateTaskID);
pixelBlock.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(
PixelBlocks.plugin,
pixelBlock::update,
100L,
100L
);
} else {
event.getPlayer().sendMessage("This Block is not yours!");
event.getPlayer().sendMessage("Dieser Block gehört nicht dir!");
}
}
}
@ -71,14 +72,19 @@ public class PlayerInteractListener implements Listener {
blockLocation.setYaw(0);
blockLocation.setPitch(0);
Bukkit.getScheduler().runTask(
PixelBlocks.plugin,
() -> PixelBlock.placedBlocks.stream()
// .filter(pixelBlock -> pixelBlock.pixelBlockLocation.distance(blockLocation) < 0.5)
.filter(pixelBlock -> pixelBlock.pixelBlockLocation.equals(blockLocation))
UUID playerUID = event.getPlayer().getUniqueId();
PixelBlock pixelBlock = PixelBlock.placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(blockLocation))
.findFirst()
.orElseThrow()
.remove());
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
if(playerUID == blockOwner) {
Bukkit.getScheduler().runTask(PixelBlocks.plugin, pixelBlock::remove);
} else {
event.getPlayer().sendMessage("Dieser Block gehört nicht dir!");
}
}
}
}

View File

@ -0,0 +1,27 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import java.util.Arrays;
public class PlayerMoveListener implements Listener {
@EventHandler
static void onPlayerMove(PlayerMoveEvent event) {
World playerWorld = event.getPlayer().getWorld();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
Location playerLocation = event.getPlayer().getLocation();
if(playerLocation.y() < -65) {
playerWorld.getSpawnLocation().clone().subtract(1, 1, 1).getBlock().setType(Material.WHITE_CONCRETE);
event.getPlayer().teleport(event.getPlayer().getWorld().getSpawnLocation());
}
}
}
}

View File

@ -7,12 +7,11 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.util.Transformation;
import java.util.Objects;
import java.util.UUID;
public class Pixel {
Location relativeLocation;
BlockData blockData;
public Location relativeLocation;
public BlockData blockData;
double scale;
double offset;

View File

@ -1,43 +1,128 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator;
import org.bukkit.*;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
public class PixelBlock {
public static List<PixelBlock> placedBlocks = new ArrayList<>();
public static float hitboxOffset = 0.005F;
public static int worldGrassBorderWidth = 5;
public Location pixelBlockLocation;
double pixelsPerBlock;
public double pixelsPerBlock;
public ArrayList<Pixel> pixels = new ArrayList<>();
public Interaction hitbox;
public static float hitboxOffset = 0.005F;
public long lastEntryTime;
public Location lastEntryLocation;
public UUID owner;
public UUID uuid;
public int updateTaskID;
public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock) {
PixelBlock.placedBlocks.add(this);
this.uuid = UUID.randomUUID();
this.pixelBlockLocation = originLocation.toBlockLocation();
this.pixelBlockLocation.setYaw(0);
this.pixelBlockLocation.setPitch(0);
this.pixelsPerBlock = pixelsPerBlock;
this.owner = owner;
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(originLocation.getWorld(), x, y, z);
Location blockLocation = pixelBlockLocation.clone().add(relativeLocation);
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
Pixel newPixel = new Pixel(
new Location(originLocation.getWorld(), x, 0, z),
Material.GRASS_BLOCK.createBlockData(),
(1/pixelsPerBlock)-0.0001,
0.00005*pixelsPerBlock
);
pixels.add(newPixel);
}
}
pixelBlockLocation.getBlock().setType(Material.GLASS);
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION
);
hitbox.setInteractionHeight(1F + 2*hitboxOffset);
hitbox.setInteractionWidth(1F + 2*hitboxOffset);
createWorld();
}
void createWorld() {
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+this.uuid);
worldCreator.type(WorldType.FLAT);
worldCreator.generator(new EmptyChunkGenerator());
World newWorld = Bukkit.createWorld(worldCreator);
assert newWorld != null;
Location borderStartLocation = newWorld.getSpawnLocation().clone().subtract(1, 1, 1);
Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth);
Bukkit.getScheduler().runTask(plugin, () -> {
for (int x = 0; x < 18+2*worldGrassBorderWidth; x++) {
for (int z = 0; z < 18+2*worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);
}
}
for (int x = 0; x < 18+2*worldGrassBorderWidth; x++) {
for (int z = 0; z < 18+2*worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, -1, z).getBlock().setType(Material.DIRT);
}
}
for (int x = 0; x < 18; x++) {
for (int z = 0; z < 18; z++) {
Location currentLocation = borderStartLocation.clone().add(x, 0, z);
if(currentLocation.x() == borderStartLocation.x() || currentLocation.z() == borderStartLocation.z()) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
} else if (currentLocation.x() == borderStartLocation.x()+17 || currentLocation.z() == borderStartLocation.z()+17) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
}
}
}
borderStartLocation.getBlock().setType(Material.WHITE_CONCRETE);
});
}
public void update() {
Bukkit.getScheduler().runTask(plugin, () -> {
Player owner = null;
for(Player player : Bukkit.getOnlinePlayers()) {
if(player.getUniqueId() == this.owner) {
owner = player;
}
}
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(owner == null || Arrays.stream(standardWorlds).toList().contains(owner.getWorld())) {
Bukkit.getScheduler().cancelTask(this.updateTaskID);
}
for(Pixel pixel : this.pixels) { pixel.remove(); }
pixels.clear();
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), x, y, z);
Location blockLocation = Objects.requireNonNull(Bukkit
.getWorld(plugin.getDataFolder().getPath() + "/" + this.uuid))
.getSpawnLocation()
.clone()
.subtract(0, 1, 0)
.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z());
BlockData block = blockLocation.getBlock().getBlockData();
if(block.getMaterial() != Material.AIR) {
@ -48,27 +133,20 @@ public class PixelBlock {
}
}
pixelBlockLocation.getBlock().setType(Material.GLASS);
// BlockDisplay bd = (BlockDisplay) spawnLocation.getWorld().spawnEntity(
// spawnLocation,
// EntityType.BLOCK_DISPLAY
// );
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION
);
hitbox.setInteractionHeight(1F + 2*hitboxOffset);
hitbox.setInteractionWidth(1F + 2*hitboxOffset);
for(Pixel pixel : this.pixels) {
pixel.spawn(this.pixelBlockLocation);
}
});
}
public void place(Location placeLocation) {
Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
Bukkit.getScheduler().runTask(plugin, () -> {
for(Pixel pixel : pixels) {
pixel.spawn(placeLocation.toBlockLocation());
}
});
this.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::update, 100L, 100L);
}
public void remove() {
@ -76,5 +154,6 @@ public class PixelBlock {
hitbox.remove();
pixelBlockLocation.getBlock().setType(Material.AIR);
placedBlocks.remove(this);
Bukkit.getScheduler().cancelTask(this.updateTaskID);
}
}