own world per pixelblock, live update, owners
This commit is contained in:
		| @@ -3,11 +3,9 @@ package eu.mhsl.minecraft.pixelblocks; | |||||||
| import eu.mhsl.minecraft.pixelblocks.commands.ChessCommand; | import eu.mhsl.minecraft.pixelblocks.commands.ChessCommand; | ||||||
| import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand; | import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand; | ||||||
| import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand; | import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand; | ||||||
| import eu.mhsl.minecraft.pixelblocks.listeners.PlayerInteractListener; | import eu.mhsl.minecraft.pixelblocks.listeners.*; | ||||||
| import eu.mhsl.minecraft.pixelblocks.listeners.PlayerJumpListener; |  | ||||||
| import org.bukkit.plugin.java.JavaPlugin; | import org.bukkit.plugin.java.JavaPlugin; | ||||||
|  |  | ||||||
| import java.io.File; |  | ||||||
| import java.util.Objects; | import java.util.Objects; | ||||||
|  |  | ||||||
| public final class PixelBlocks extends JavaPlugin { | public final class PixelBlocks extends JavaPlugin { | ||||||
| @@ -17,17 +15,19 @@ public final class PixelBlocks extends JavaPlugin { | |||||||
|     public void onEnable() { |     public void onEnable() { | ||||||
|         PixelBlocks.plugin = this; |         PixelBlocks.plugin = this; | ||||||
|         this.getLogger().info("Hello World"); |         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 PlayerJumpListener(), this); | ||||||
|         getServer().getPluginManager().registerEvents(new PlayerInteractListener(), 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("chess")).setExecutor(new ChessCommand()); | ||||||
|         Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand()); |         Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand()); | ||||||
|         Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand()); |         Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void onDisable() { |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -19,13 +19,21 @@ public class ExitWorldCommand implements CommandExecutor { | |||||||
|             World playerWorld = p.getWorld(); |             World playerWorld = p.getWorld(); | ||||||
|             World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)}; |             World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)}; | ||||||
|  |  | ||||||
|             if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) { |             if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) { | ||||||
|                 Location newLocation = PixelBlock.placedBlocks.stream() |                 List<PixelBlock> ownedBlocks = PixelBlock.placedBlocks.stream() | ||||||
|                         .filter(pixelBlock -> pixelBlock.owner == p.getUniqueId()) |                         .filter(pixelBlock -> pixelBlock.owner == p.getUniqueId()) | ||||||
|                         .findFirst() |                         .sorted(Comparator.comparing(pixelBlock -> pixelBlock.lastEntryTime)) | ||||||
|                         .orElseThrow() |                         .toList(); | ||||||
|                         .lastEntryLocation; |  | ||||||
| //                p.teleport(Bukkit.getServer().getWorlds().getFirst().getSpawnLocation()); |                 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!"); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -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); | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -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); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,14 +1,9 @@ | |||||||
| package eu.mhsl.minecraft.pixelblocks.listeners; | package eu.mhsl.minecraft.pixelblocks.listeners; | ||||||
|  |  | ||||||
| import eu.mhsl.minecraft.pixelblocks.PixelBlocks; | import eu.mhsl.minecraft.pixelblocks.PixelBlocks; | ||||||
| import eu.mhsl.minecraft.pixelblocks.pixelblock.Pixel; |  | ||||||
| import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; | import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; | ||||||
| import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; | import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; | ||||||
| import io.papermc.paper.math.Position; |  | ||||||
| import org.bukkit.*; | 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.entity.Interaction; | ||||||
| import org.bukkit.event.EventHandler; | import org.bukkit.event.EventHandler; | ||||||
| import org.bukkit.event.Listener; | 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.PlayerInteractEntityEvent; | ||||||
| import org.bukkit.event.player.PlayerInteractEvent; | import org.bukkit.event.player.PlayerInteractEvent; | ||||||
|  |  | ||||||
| import java.util.List; | import java.util.*; | ||||||
| import java.util.Objects; |  | ||||||
| import java.util.UUID; |  | ||||||
|  |  | ||||||
| import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin; | 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(); |             Location interactionLocation = event.getRightClicked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation(); | ||||||
|             interactionLocation.setYaw(0); |             interactionLocation.setYaw(0); | ||||||
|             interactionLocation.setPitch(0); |             interactionLocation.setPitch(0); | ||||||
|  |  | ||||||
|             UUID playerUID = event.getPlayer().getUniqueId(); |             UUID playerUID = event.getPlayer().getUniqueId(); | ||||||
|             PixelBlock pixelBlock = PixelBlock.placedBlocks.stream() |             PixelBlock pixelBlock = PixelBlock.placedBlocks.stream() | ||||||
|                     .filter(block -> block.pixelBlockLocation.equals(interactionLocation)) |                     .filter(block -> block.pixelBlockLocation.equals(interactionLocation)) | ||||||
| @@ -52,14 +46,21 @@ public class PlayerInteractListener implements Listener { | |||||||
|  |  | ||||||
|             if(playerUID == blockOwner) { |             if(playerUID == blockOwner) { | ||||||
|                 pixelBlock.lastEntryLocation = event.getPlayer().getLocation(); |                 pixelBlock.lastEntryLocation = event.getPlayer().getLocation(); | ||||||
|  |                 pixelBlock.lastEntryTime = System.currentTimeMillis(); | ||||||
|  |  | ||||||
|                 final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId()); |                 World blockWorld = Bukkit.getWorld(plugin.getDataFolder().getPath()+"/"+pixelBlock.uuid); | ||||||
|                 worldCreator.type(WorldType.FLAT); |                 assert blockWorld != null; | ||||||
|                 World newWorld = Bukkit.createWorld(worldCreator); |                 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 { |             } 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.setYaw(0); | ||||||
|             blockLocation.setPitch(0); |             blockLocation.setPitch(0); | ||||||
|  |  | ||||||
|             Bukkit.getScheduler().runTask( |             UUID playerUID = event.getPlayer().getUniqueId(); | ||||||
|                 PixelBlocks.plugin, |             PixelBlock pixelBlock = PixelBlock.placedBlocks.stream() | ||||||
|                 () -> PixelBlock.placedBlocks.stream() |                     .filter(block -> block.pixelBlockLocation.equals(blockLocation)) | ||||||
| //                    .filter(pixelBlock -> pixelBlock.pixelBlockLocation.distance(blockLocation) < 0.5) |  | ||||||
|                     .filter(pixelBlock -> pixelBlock.pixelBlockLocation.equals(blockLocation)) |  | ||||||
|                     .findFirst() |                     .findFirst() | ||||||
|                     .orElseThrow() |                     .orElseThrow(); | ||||||
|                     .remove()); |             UUID blockOwner = pixelBlock.owner; | ||||||
|  |  | ||||||
|  |             if(playerUID == blockOwner) { | ||||||
|  |                 Bukkit.getScheduler().runTask(PixelBlocks.plugin, pixelBlock::remove); | ||||||
|  |             } else { | ||||||
|  |                 event.getPlayer().sendMessage("Dieser Block gehört nicht dir!"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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()); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -7,12 +7,11 @@ import org.bukkit.entity.Entity; | |||||||
| import org.bukkit.entity.EntityType; | import org.bukkit.entity.EntityType; | ||||||
| import org.bukkit.util.Transformation; | import org.bukkit.util.Transformation; | ||||||
|  |  | ||||||
| import java.util.Objects; |  | ||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
|  |  | ||||||
| public class Pixel { | public class Pixel { | ||||||
|     Location relativeLocation; |     public Location relativeLocation; | ||||||
|     BlockData blockData; |     public BlockData blockData; | ||||||
|     double scale; |     double scale; | ||||||
|     double offset; |     double offset; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,74 +1,152 @@ | |||||||
| package eu.mhsl.minecraft.pixelblocks.pixelblock; | package eu.mhsl.minecraft.pixelblocks.pixelblock; | ||||||
|  |  | ||||||
| import eu.mhsl.minecraft.pixelblocks.PixelBlocks; | import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator; | ||||||
| import org.bukkit.Bukkit; | import org.bukkit.*; | ||||||
| import org.bukkit.Location; |  | ||||||
| import org.bukkit.Material; |  | ||||||
| import org.bukkit.block.data.BlockData; | import org.bukkit.block.data.BlockData; | ||||||
| import org.bukkit.entity.EntityType; | import org.bukkit.entity.EntityType; | ||||||
| import org.bukkit.entity.Interaction; | import org.bukkit.entity.Interaction; | ||||||
|  | import org.bukkit.entity.Player; | ||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.*; | ||||||
| import java.util.List; |  | ||||||
| import java.util.UUID; | import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin; | ||||||
|  |  | ||||||
| public class PixelBlock { | public class PixelBlock { | ||||||
|     public static List<PixelBlock> placedBlocks = new ArrayList<>(); |     public static List<PixelBlock> placedBlocks = new ArrayList<>(); | ||||||
|  |     public static float hitboxOffset = 0.005F; | ||||||
|  |     public static int worldGrassBorderWidth = 5; | ||||||
|  |  | ||||||
|     public Location pixelBlockLocation; |     public Location pixelBlockLocation; | ||||||
|     double pixelsPerBlock; |     public double pixelsPerBlock; | ||||||
|     public ArrayList<Pixel> pixels = new ArrayList<>(); |     public ArrayList<Pixel> pixels = new ArrayList<>(); | ||||||
|     public Interaction hitbox; |     public Interaction hitbox; | ||||||
|  |  | ||||||
|     public static float hitboxOffset = 0.005F; |     public long lastEntryTime; | ||||||
|  |  | ||||||
|     public Location lastEntryLocation; |     public Location lastEntryLocation; | ||||||
|     public UUID owner; |     public UUID owner; | ||||||
|  |     public UUID uuid; | ||||||
|  |     public int updateTaskID; | ||||||
|  |  | ||||||
|     public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock) { |     public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock) { | ||||||
|         PixelBlock.placedBlocks.add(this); |         PixelBlock.placedBlocks.add(this); | ||||||
|  |         this.uuid = UUID.randomUUID(); | ||||||
|  |  | ||||||
|         this.pixelBlockLocation = originLocation.toBlockLocation(); |         this.pixelBlockLocation = originLocation.toBlockLocation(); | ||||||
|         this.pixelBlockLocation.setYaw(0); |         this.pixelBlockLocation.setYaw(0); | ||||||
|         this.pixelBlockLocation.setPitch(0); |         this.pixelBlockLocation.setPitch(0); | ||||||
|         this.pixelsPerBlock = pixelsPerBlock; |         this.pixelsPerBlock = pixelsPerBlock; | ||||||
|         this.owner = owner; |         this.owner = owner; | ||||||
|  |  | ||||||
|         for (int x = 0; x < pixelsPerBlock; x++) { |         for (int x = 0; x < 16; x++) { | ||||||
|             for (int y = 0; y < pixelsPerBlock; y++) { |             for (int z = 0; z < 16; z++) { | ||||||
|                 for (int z = 0; z < pixelsPerBlock; z++) { |                 Pixel newPixel = new Pixel( | ||||||
|                     Location relativeLocation = new Location(originLocation.getWorld(), x, y, z); |                     new Location(originLocation.getWorld(), x, 0, z), | ||||||
|                     Location blockLocation = pixelBlockLocation.clone().add(relativeLocation); |                     Material.GRASS_BLOCK.createBlockData(), | ||||||
|                     BlockData block = blockLocation.getBlock().getBlockData(); |                     (1/pixelsPerBlock)-0.0001, | ||||||
|  |                     0.00005*pixelsPerBlock | ||||||
|                     if(block.getMaterial() != Material.AIR) { |                 ); | ||||||
|                         Pixel newPixel = new Pixel(relativeLocation, block, (1/pixelsPerBlock)-0.0001, 0.00005*pixelsPerBlock); |                 pixels.add(newPixel); | ||||||
|                         pixels.add(newPixel); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         pixelBlockLocation.getBlock().setType(Material.GLASS); |         pixelBlockLocation.getBlock().setType(Material.GLASS); | ||||||
|  |  | ||||||
| //        BlockDisplay bd = (BlockDisplay) spawnLocation.getWorld().spawnEntity( |  | ||||||
| //                spawnLocation, |  | ||||||
| //                EntityType.BLOCK_DISPLAY |  | ||||||
| //        ); |  | ||||||
|  |  | ||||||
|         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 | ||||||
|         ); |         ); | ||||||
|         hitbox.setInteractionHeight(1F + 2*hitboxOffset); |         hitbox.setInteractionHeight(1F + 2*hitboxOffset); | ||||||
|         hitbox.setInteractionWidth(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) { | ||||||
|  |                             Pixel newPixel = new Pixel(relativeLocation, block, (1/pixelsPerBlock)-0.0001, 0.00005*pixelsPerBlock); | ||||||
|  |                             pixels.add(newPixel); | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             for(Pixel pixel : this.pixels) { | ||||||
|  |                 pixel.spawn(this.pixelBlockLocation); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void place(Location placeLocation) { |     public void place(Location placeLocation) { | ||||||
|         Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> { |         Bukkit.getScheduler().runTask(plugin, () -> { | ||||||
|             for(Pixel pixel : pixels) { |             for(Pixel pixel : pixels) { | ||||||
|                 pixel.spawn(placeLocation.toBlockLocation()); |                 pixel.spawn(placeLocation.toBlockLocation()); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|  |         this.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::update, 100L, 100L); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void remove() { |     public void remove() { | ||||||
| @@ -76,5 +154,6 @@ public class PixelBlock { | |||||||
|         hitbox.remove(); |         hitbox.remove(); | ||||||
|         pixelBlockLocation.getBlock().setType(Material.AIR); |         pixelBlockLocation.getBlock().setType(Material.AIR); | ||||||
|         placedBlocks.remove(this); |         placedBlocks.remove(this); | ||||||
|  |         Bukkit.getScheduler().cancelTask(this.updateTaskID); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user