diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java index fc74778..b39f207 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java @@ -174,6 +174,11 @@ public class DataBase { pixelBlocksResult.getDouble("entryLocationZ") ); + if(!newPixelBlockLocation.getChunk().isEntitiesLoaded()) { + newPixelBlockLocation.getChunk().load(true); + newPixelBlockLocation.getChunk().getEntities(); + } + entities.stream().filter(entity -> entity. getLocation(). clone(). diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java index 0a2148d..c0b2e3e 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java @@ -71,12 +71,16 @@ public class PixelBlock { } } + public static String getWorldPathFromPixelblock(PixelBlock pixelBlock) { + return plugin.getDataFolder().getPath() + pathSeparator + pixelBlock.uuid.toString(); + } + void createPixelWorld() { - File file = new File(plugin.getDataFolder().getPath()+ pathSeparator +this.uuid.toString()); + File file = new File(getWorldPathFromPixelblock(this)); if(!file.exists() || !file.isDirectory()) { - final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath() + pathSeparator + this.uuid); + final WorldCreator worldCreator = new WorldCreator(getWorldPathFromPixelblock(this)); worldCreator.type(WorldType.FLAT); worldCreator.generator(new EmptyChunkGenerator()); World newWorld = Bukkit.createWorld(worldCreator); @@ -126,7 +130,7 @@ public class PixelBlock { } }); } else { - new WorldCreator(plugin.getDataFolder().getPath() + pathSeparator + this.uuid).createWorld(); + new WorldCreator(getWorldPathFromPixelblock(this)).createWorld(); } } @@ -228,7 +232,6 @@ public class PixelBlock { } public Location getPlayerSpawnLocation(Player player) { -// Location spawnLocation = getPixelWorld().getSpawnLocation().clone().add(8, 0, -3.5); Location spawnLocation = getPixelWorld().getSpawnLocation().clone().add((double) pixelsPerBlock/2, 0, 1.5-worldGrassBorderWidth); // spawnLocation.setYaw(player.getLocation().getYaw()); // spawnLocation.setPitch(player.getLocation().getPitch()); @@ -240,7 +243,7 @@ public class PixelBlock { } public World getPixelWorld() { - return Bukkit.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid); + return Bukkit.getWorld(getWorldPathFromPixelblock(this)); } public void spawnInteraction(boolean fullBlock) { @@ -272,30 +275,55 @@ public class PixelBlock { .max(Comparator.comparing(pixel -> pixel.relativeLocation.getZ())) .orElseThrow().relativeLocation.getZ(); + Location spawnLocation = pixelBlockLocation.clone().add( + ((startingX+endingX+1)/2)/16, + (startingY/16)-hitboxOffset, + ((startingZ+endingZ+1)/2)/16 + ); + + float height = (float) (endingY-startingY+1)/16 + 2*hitboxOffset; + + float width; + if((endingX-startingX) > (endingZ-startingZ)) { + width = (float) (endingX-startingX+1)/16 + 2*hitboxOffset; + } else { + width = (float) (endingZ-startingZ+1)/16 + 2*hitboxOffset; + } + + if(spawnLocation.getX()+width/2 > this.pixelBlockLocation.getX()+1) { + spawnLocation.subtract((spawnLocation.getX()+width/2)-(this.pixelBlockLocation.getX()+1), 0, 0); + } + if(spawnLocation.getX()-width/2 < this.pixelBlockLocation.getX()) { + spawnLocation.add(this.pixelBlockLocation.getX()-(spawnLocation.getX()-width/2), 0, 0); + } + + if(spawnLocation.getZ()+width/2 > this.pixelBlockLocation.getZ()+1) { + spawnLocation.subtract(0, 0, (spawnLocation.getZ()+width/2)-(this.pixelBlockLocation.getZ()+1)); + } + if(spawnLocation.getZ()-width/2 < this.pixelBlockLocation.getZ()) { + spawnLocation.add(0, 0, this.pixelBlockLocation.getZ()-(spawnLocation.getZ()-width/2)); + } + hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity( - pixelBlockLocation.clone().add(((startingX+endingX+1)/2)/16, (startingY/16)-hitboxOffset, ((startingZ+endingZ+1)/2)/16), - EntityType.INTERACTION + spawnLocation, + EntityType.INTERACTION ); - hitbox.setInteractionHeight((float) (endingY-startingY+1)/16 + 2*hitboxOffset); - if((endingX-startingX) > (endingZ-startingZ)) { - hitbox.setInteractionWidth((float) (endingX-startingX+1)/16 + 2*hitboxOffset); - } else { - hitbox.setInteractionWidth((float) (endingZ-startingZ+1)/16 + 2*hitboxOffset); - } + hitbox.setInteractionHeight(height); + hitbox.setInteractionWidth(width); } } public void update() { Bukkit.getScheduler().runTask(plugin, () -> { - this.clearEntities(); + this.clearEntities(true); 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() + pathSeparator + this.uuid)) + .getWorld(getWorldPathFromPixelblock(this))) .getSpawnLocation() .clone() .add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); @@ -334,26 +362,47 @@ public class PixelBlock { }); } - public void clearEntities() { - System.out.println("Clear Entities"); + public void clearEntities(boolean secure) { + if(!this.pixelBlockLocation.getChunk().isEntitiesLoaded()) { + this.pixelBlockLocation.getChunk().load(true); + this.pixelBlockLocation.getChunk().getEntities(); + } if(!pixels.isEmpty()) { - System.out.println("Clear Pixels"); this.pixels.forEach(Pixel::remove); this.pixels.clear(); } if(hitbox != null) { - System.out.println("Clear Hitbox"); this.hitbox.remove(); this.hitbox = null; } if(barrier != null) { - System.out.println("Clear Barrier"); this.barrier.remove(); this.barrier = null; } + + if(secure) { + List entities = new ArrayList<>(); + entities.addAll(Bukkit.getWorlds().get(0).getEntities().stream() + .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY)) + .toList()); + entities.addAll(Bukkit.getWorlds().get(1).getEntities().stream() + .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY)) + .toList()); + entities.addAll(Bukkit.getWorlds().get(2).getEntities().stream() + .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY)) + .toList()); + + entities.stream().filter(entity -> entity. + getLocation(). + clone(). + add(0, PixelBlock.hitboxOffset, 0). + toBlockLocation(). + equals(this.pixelBlockLocation)). + forEach(Entity::remove); + } } public boolean place(Location placeLocation) { @@ -374,7 +423,7 @@ public class PixelBlock { public void remove() { dataBase.removePixelBlock(this); - clearEntities(); + clearEntities(true); placedPixelBlocks.remove(this); } @@ -383,7 +432,7 @@ public class PixelBlock { Bukkit.unloadWorld(getPixelWorld(), true); try { - FileUtils.deleteDirectory(plugin.getDataFolder().getPath() + pathSeparator + this.uuid); + FileUtils.deleteDirectory(getWorldPathFromPixelblock(this)); } catch (IOException e) { System.err.println("World could not be deleted: " + e.getMessage()); }