wip: further code cleanup

This commit is contained in:
2024-07-23 22:25:00 +02:00
parent e5e1f39989
commit 93dc9d8a80
25 changed files with 556 additions and 407 deletions

View File

@@ -38,7 +38,7 @@ public class PixelBlock {
}
public static @Nullable PixelBlock getPixelBlockFromBlockWorld(World world) {
return PixelBlocksPlugin.placedPixelBlocks.stream()
return PixelBlocksPlugin.pixelBlocks.stream()
.filter(block -> block.blockUUID.equals(getUUIDFromWorld(world)))
.findFirst()
.orElse(null);
@@ -57,13 +57,15 @@ public class PixelBlock {
searchLocation.setPitch(0);
searchLocation.setYaw(0);
return PixelBlocksPlugin.placedPixelBlocks.stream()
return PixelBlocksPlugin.pixelBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(searchLocation))
.findFirst()
.orElse(null);
}
public PixelBlock(Location originLocation, UUID ownerUUID, UUID blockUUID) {
public PixelBlock(Location originLocation, UUID ownerUUID, UUID blockUUID, Direction direction) {
PixelBlocksPlugin.pixelBlocks.add(this);
this.ownerUUID = ownerUUID;
this.blockUUID = blockUUID;
@@ -71,7 +73,7 @@ public class PixelBlock {
this.pixelBlockLocation.setYaw(0);
this.pixelBlockLocation.setPitch(0);
this.facingDirection = Direction.south; // TODO ??
this.facingDirection = direction;
this.pixelWorld = new PixelBlockWorld(this);
}
@@ -89,6 +91,10 @@ public class PixelBlock {
player.teleport(this.pixelWorld.getSpawnLocation());
}
public void setLastEntryLocation(Location lastEntryLocation) {
this.lastEntryLocation = lastEntryLocation;
}
public void spawnInteraction(boolean fullBlock) {
if(fullBlock) {
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
@@ -145,7 +151,7 @@ public class PixelBlock {
}
}
public void update() {
public void updateEntities() {
Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> {
this.clearEntities();
@@ -195,20 +201,18 @@ public class PixelBlock {
});
}
public boolean place(Location placeLocation, Direction direction) {
public void place(Location placeLocation, Direction direction) {
Location newLocation = placeLocation.toBlockLocation();
newLocation.setPitch(0);
newLocation.setYaw(0);
if(PixelBlock.getPixelBlockFromPlacedLocation(newLocation) == null || PixelBlock.getPixelBlockFromPlacedLocation(newLocation) == this) {
this.pixelBlockLocation = newLocation;
this.facingDirection = direction;
update();
PixelBlocksPlugin.database.savePixelBlock(this);
PixelBlocksPlugin.placedPixelBlocks.add(this);
return true;
}
return false;
@Nullable PixelBlock blockAtLocation = PixelBlock.getPixelBlockFromPlacedLocation(newLocation);
if(blockAtLocation != null && blockAtLocation != this) throw new IllegalArgumentException("Es können nicht mehrere Pixelblöcke ineinander platziert werden.");
this.pixelBlockLocation = newLocation;
this.facingDirection = direction;
updateEntities();
PixelBlocksPlugin.database.savePixelBlock(this);
}
public void destroy(Player destroyedBy) {
@@ -218,15 +222,16 @@ public class PixelBlock {
}
this.pixelWorld.getPlayersInWorld().forEach(p -> {
p.sendMessage("Der Pixelblock wurde abgebaut!");
p.sendMessage("Der Pixelblock wurde von einem anderen Spieler abgebaut.");
p.teleport(this.lastEntryLocation);
});
this.pixelWorld.getEntitiesInWorld().forEach(entity -> entity.teleport(this.lastEntryLocation));
this.pixelWorld.getEntitiesInWorld().stream()
.filter(entity -> entity instanceof Item)
.forEach(entity -> entity.teleport(this.lastEntryLocation));
this.clearEntities();
PixelBlocksPlugin.database.removePixelBlock(this);
PixelBlocksPlugin.placedPixelBlocks.remove(this);
PixelBlocksPlugin.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));
}
@@ -267,7 +272,11 @@ public class PixelBlock {
}
public PixelBlockWorld getPixelWorld() {
public @NotNull PixelBlockWorld getPixelWorld() {
return pixelWorld;
}
public int getPixelsPerBlock() {
return pixelsPerBlock;
}
}

View File

@@ -1,15 +1,17 @@
package eu.mhsl.minecraft.pixelblocks.pixelblock;
import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin;
import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator;
import eu.mhsl.minecraft.pixelblocks.utils.LocationUtil;
import org.bukkit.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin.plugin;
@@ -17,7 +19,7 @@ public class PixelBlockWorld {
private final PixelBlock parentPixelBlock;
private final World world;
int worldGrassBorderWidth = PixelBlocksPlugin.configuration.worldGrassBorderWidth();
int worldGrassBorderWidth = 10;
int pixelsPerBlock = PixelBlocksPlugin.configuration.pixelsPerBlock();
public static boolean isPixelWorld(@NotNull World world) {
@@ -38,8 +40,16 @@ public class PixelBlockWorld {
this.setBuildingPlatform();
}
public boolean allowPlacements(Location blockLocation) {
Location origin = getBuildOrigin();
int offset = pixelsPerBlock - 1;
return blockLocation.x() >= origin.x() && blockLocation.x() <= origin.x() + offset
&& blockLocation.z() >= origin.z() && blockLocation.z() <= origin.z() + offset
&& blockLocation.y() >= origin.y() && blockLocation.y() <= origin.y() + offset;
}
public @NotNull String getWorldPathName() {
return PixelBlocksPlugin.plugin.getDataFolder().getPath() + File.separator + this.parentPixelBlock.blockUUID;
return PixelBlocksPlugin.plugin.getDataFolder().getPath() + File.separator + "worlds" + File.separator + this.parentPixelBlock.blockUUID;
}
public @NotNull Location getSpawnLocation() {
@@ -47,7 +57,7 @@ public class PixelBlockWorld {
}
public @NotNull Location getPortalLocation() {
return this.getBuildOrigin().add((double) pixelsPerBlock/2 -2, 0, -worldGrassBorderWidth);
return this.getBuildOrigin().add((double) pixelsPerBlock/2 -2, 0, -worldGrassBorderWidth+3);
}
public @NotNull List<Player> getPlayersInWorld() {
@@ -62,11 +72,27 @@ public class PixelBlockWorld {
return new Location(this.world, 0, -60, 0);
}
public @NotNull Location getBuildOriginEnd() {
return getBuildOrigin().add(pixelsPerBlock, pixelsPerBlock, pixelsPerBlock);
}
public @NotNull Location getBorderOrigin() {
return getBuildOrigin().subtract(1, 1, 1);
}
public @NotNull Location getPlatformOrigin() {
return getBorderOrigin().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth);
}
public @NotNull Location getPlatformOriginEnd() {
return getBorderOrigin().add(worldGrassBorderWidth + pixelsPerBlock, 0, worldGrassBorderWidth + pixelsPerBlock);
}
private World loadOrCreatePixelWorld() {
final WorldCreator worldCreator = new WorldCreator(getWorldPathName());
worldCreator.type(WorldType.FLAT);
worldCreator.generator(new EmptyChunkGenerator());
worldCreator.generator(new ChunkGenerator() {});
World world = Bukkit.createWorld(worldCreator);
Objects.requireNonNull(world);
@@ -78,45 +104,71 @@ public class PixelBlockWorld {
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
WorldBorder worldBorder = world.getWorldBorder();
worldBorder.setCenter(getBuildOrigin());
worldBorder.setCenter(getBuildOrigin().add((double) pixelsPerBlock / 2, 0, (double) pixelsPerBlock / 2));
worldBorder.setSize(pixelsPerBlock + (2*worldGrassBorderWidth));
worldBorder.setWarningDistance(0);
worldBorder.setDamageAmount(0);
return world;
}
private void setBuildingPlatform() {
Location borderStartLocation = getBuildOrigin().subtract(1, 1, 1);
Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth);
Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> {
for (int x = 0; x < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; x++) {
for (int z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);
getPlatformOrigin().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);
}
}
for (int x = 0; x < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; x++) {
for (int z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) {
grassStartLocation.clone().add(x, -1, z).getBlock().setType(Material.DIRT);
getPlatformOrigin().add(x, -1, z).getBlock().setType(Material.DIRT);
}
}
for (int x = 0; x < (pixelsPerBlock+2); x++) {
for (int z = 0; z < (pixelsPerBlock+2); z++) {
Location currentLocation = borderStartLocation.clone().add(x, 0, z);
Location currentLocation = getBorderOrigin().add(x, 0, z);
if (currentLocation.x() == borderStartLocation.x() || currentLocation.z() == borderStartLocation.z()) {
if (currentLocation.x() == getBorderOrigin().x() || currentLocation.z() == getBorderOrigin().z()) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
} else if (currentLocation.x() == borderStartLocation.x() + (pixelsPerBlock+1) || currentLocation.z() == borderStartLocation.z() + (pixelsPerBlock+1)) {
} else if (currentLocation.x() == getBorderOrigin().x() + (pixelsPerBlock+1) || currentLocation.z() == getBorderOrigin().z() + (pixelsPerBlock+1)) {
currentLocation.getBlock().setType(Material.RED_CONCRETE);
}
}
}
for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+4; x++) {
Random random = new Random();
LocationUtil.iterateBlocks(getPlatformOrigin().add(1, 1, 1), getPlatformOriginEnd().add(0, 1, 0), location -> {
if (allowPlacements(location)) return;
if (!location.clone().subtract(0, 1, 0).getBlock().getType().equals(Material.GRASS_BLOCK)) return;
if (!location.getBlock().getType().equals(Material.AIR)) return;
if (random.nextInt(10) == 0) {
Material[] flowers = {
Material.DANDELION,
Material.POPPY,
Material.BLUE_ORCHID,
Material.ALLIUM,
Material.AZURE_BLUET,
Material.RED_TULIP,
Material.ORANGE_TULIP,
Material.WHITE_TULIP,
Material.CORNFLOWER,
Material.LILY_OF_THE_VALLEY,
};
Material randomFlower = flowers[random.nextInt(flowers.length)];
location.getBlock().setType(randomFlower);
}
});
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 5; y++) {
grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.OBSIDIAN);
getPortalLocation().add(x, y, 0).getBlock().setType(Material.OBSIDIAN);
}
}
for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+1; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+3; x++) {
for (int x = 1; x < 3; x++) {
for (int y = 1; y < 4; y++) {
grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.NETHER_PORTAL);
getPortalLocation().add(x, y, 0).getBlock().setType(Material.NETHER_PORTAL);
}
}
});