refactoring

This commit is contained in:
Elias Müller 2024-10-13 16:26:41 +02:00
parent 70c7059e43
commit 2810db2e93
7 changed files with 54 additions and 51 deletions

View File

@ -56,9 +56,9 @@ public final class Main extends JavaPlugin {
@Override
public void onEnable() {
Main.taskFactory = BukkitTaskChainFactory.create(this);
getLogger().info("Loading existing blocks from Database");
getLogger().info("Start constructing blocks from Database...");
database.loadPixelBlocks();
getLogger().info("Pixelblock loading done");
getLogger().info("Construction done!");
Listener[] listeners = {
new EnterPixelBlockListener(),

View File

@ -119,7 +119,6 @@ public class PixelBlockDatabase {
allPixelBlocks.getDouble("entryLocationZ")
);
Main.plugin.getLogger().info("Pixelblock found ");
Main.pixelBlocks.add(PixelBlock.fromExisting(
UUID.fromString(allPixelBlocks.getString("uuid")),
UUID.fromString(allPixelBlocks.getString("owner")),

View File

@ -30,13 +30,6 @@ public class CreatePixelBlockCommand implements CommandExecutor {
playerLocation.toBlockLocation(),
Direction.south
);
// PixelBlock block = new PixelBlock(
// playerLocation,
// p.getUniqueId(),
// UUID.randomUUID(),
// Direction.south
// );
// block.place(playerLocation, Direction.south);
}
return true;
}

View File

@ -21,10 +21,7 @@ public class Pixel {
private final @NotNull PixelBlock parentBlock;
private final @NotNull BlockDisplay entity;
public final Location location;
public final BlockData blockData;
private final double scale;
private final Location location;
public static List<Pixel> fromExisting(PixelBlock parentBlock) {
return parentBlock.getPixelBlockLocation().getNearbyEntitiesByType(BlockDisplay.class, 1)
@ -37,29 +34,23 @@ public class Pixel {
.map(blockDisplay -> new Pixel(parentBlock, blockDisplay))
.collect(Collectors.toList());
}
public static Pixel newPixel(PixelBlock parentBlock, Vector relativePosition, BlockData blockData, double scale) {
return new Pixel(parentBlock, relativePosition, blockData, scale);
}
private Pixel(@NotNull PixelBlock parentBlock, @NotNull BlockDisplay entity) {
this.parentBlock = parentBlock;
this.entity = entity;
this.location = entity.getLocation();
this.blockData = entity.getBlock();
this.scale = entity.getTransformation().getScale().get(0);
}
public static Pixel newPixel(PixelBlock parentBlock, Vector relativePosition, BlockData blockData, double scale) {
return new Pixel(parentBlock, relativePosition, blockData, scale);
}
private Pixel(@NotNull PixelBlock parentBlock, @NotNull Vector relativePosition, @NotNull BlockData blockData, double scale) {
this.parentBlock = parentBlock;
this.location = parentBlock.getPixelBlockLocation().add(relativePosition.multiply(scale));
this.blockData = blockData;
this.scale = scale;
this.entity = (BlockDisplay) this.location.getWorld().spawnEntity(this.location, EntityType.BLOCK_DISPLAY);
this.entity.setBlock(blockData);
Transformation transform = this.entity.getTransformation();
transform.getScale().set(this.scale);
transform.getScale().set(scale);
this.entity.setTransformation(transform);
this.entity.getPersistentDataContainer().set(pixelOfTag, PersistentDataType.STRING, this.parentBlock.getBlockUUID().toString());
}
@ -67,4 +58,8 @@ public class Pixel {
public void destroy() {
this.entity.remove();
}
public Location getLocation() {
return location.clone();
}
}

View File

@ -79,10 +79,17 @@ public class PixelBlock {
this.facingDirection = direction;
this.lastEntryLocation = lastEntryLocation;
this.pixelWorld = new PixelBlockWorld(this);
this.pixels = Pixel.fromExisting(this);
this.placeholder = PixelBlockPlaceholder.fromExisting(this);
this.hitbox = PixelBlockHitbox.fromExisting(this);
this.ensureChunksLoaded();
try {
this.pixelWorld = new PixelBlockWorld(this);
this.pixels = Pixel.fromExisting(this);
this.placeholder = PixelBlockPlaceholder.fromExisting(this);
this.hitbox = PixelBlockHitbox.fromExisting(this);
Main.plugin.getLogger().info(String.format("Loaded existing pixelblock '%s' with %d pixels", this.blockUUID, this.pixels.size()));
} catch(Exception e) {
Main.plugin.getLogger().info(String.format("Failed initializing existing pixelblock '%s': %s", this.blockUUID, e.getMessage()));
}
}
public static PixelBlock createPixelBlock(UUID blockUUID, UUID ownerUUID, Location pixelBlockLocation, Direction direction) {
@ -121,6 +128,15 @@ public class PixelBlock {
return Main.sharedChain(this.blockUUID.toString());
}
private void ensureChunksLoaded() {
Chunk chunk = this.pixelBlockLocation.getChunk();
if(!chunk.isLoaded() || !chunk.isEntitiesLoaded()) {
Main.plugin.getLogger().info(String.format("Loading chunk '%d, %d' for pixelblock '%s'", chunk.getX(), chunk.getZ(), this.blockUUID));
chunk.load(true);
chunk.getEntities();
}
}
public void enterBlock(@NotNull Player player) {
if(Main.configuration.onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) {
player.sendMessage(Component.text("Dieser Pixelblock gehört nicht dir!", NamedTextColor.RED));
@ -260,17 +276,19 @@ public class PixelBlock {
}
private void scheduleEntityRemove() {
// Chunk chunk = this.pixelBlockLocation.getChunk();
// if(!chunk.isEntitiesLoaded()) {
// chunk.load(true);
// chunk.getEntities();
// }
this.getBlockTaskChain()
.current(() -> Main.plugin.getLogger()
.info(String.format("Removing %d entities for PixelBlock '%s'", this.pixels.size(), this.blockUUID)))
.info(String.format("Removing %d pixels in PixelBlock '%s'", this.pixels.size(), this.blockUUID)))
.sync(this::ensureChunksLoaded)
.delay(1)
.sync(() -> {
List<Entity> entitiesBefore = this.pixelBlockLocation.getWorld().getEntities();
this.pixels.forEach(Pixel::destroy);
List<Entity> entitiesAfter = this.pixelBlockLocation.getWorld().getEntities();
Main.plugin.getLogger().info("Entities im Chunk vor dem Entfernen: " + entitiesBefore.size());
Main.plugin.getLogger().info("Entities im Chunk nach dem Entfernen: " + entitiesAfter.size());
this.pixels.clear();
this.placeholder.destroy();
this.hitbox.destroy();

View File

@ -30,17 +30,16 @@ public class PixelBlockHitbox {
})
.orElseThrow();
Main.plugin.getLogger().info(String.format("Found existing hitbox '%s' for block '%s'", hitBox.getUniqueId(), parentBlock.getBlockUUID()));
return new PixelBlockHitbox(hitBox);
}
private PixelBlockHitbox(Interaction interaction) {
this.interaction = interaction;
}
public static PixelBlockHitbox newHitbox(PixelBlock parentBlock) {
return new PixelBlockHitbox(parentBlock.getPixelBlockLocation(), parentBlock.getPixels(), parentBlock.getBlockUUID());
}
private PixelBlockHitbox(Interaction interaction) {
this.interaction = interaction;
}
private PixelBlockHitbox(Location pixelBlockLocation, List<Pixel> pixels, UUID parentBlockUUID) {
float offset = (float) Main.configuration.hitboxOffset();
int pixelsPerBlock = Main.configuration.pixelsPerBlock();
@ -53,13 +52,13 @@ public class PixelBlockHitbox {
interaction.setInteractionHeight(1 + 2*offset);
interaction.setInteractionWidth(1 + 2*offset);
} else {
double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getX());
double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getY());
double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getZ());
double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.getLocation().getX());
double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.getLocation().getY());
double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.getLocation().getZ());
double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getX());
double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getY());
double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getZ());
double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.getLocation().getX());
double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.getLocation().getY());
double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.getLocation().getZ());
Location spawnLocation = pixelBlockLocation.clone().add(
((startingX+endingX+1)/2)/pixelsPerBlock,

View File

@ -31,17 +31,16 @@ public class PixelBlockPlaceholder {
))
.toList();
Main.plugin.getLogger().info(String.format("Found %d existing placeholders for block '%s'", placeholders.size(), parentBlock.getBlockUUID()));
return new PixelBlockPlaceholder(placeholders);
}
private PixelBlockPlaceholder(List<ItemDisplay> itemDisplays) {
this.placeholders = itemDisplays;
}
public static PixelBlockPlaceholder newPlaceholder(PixelBlock parentBlock) {
return new PixelBlockPlaceholder(parentBlock);
}
private PixelBlockPlaceholder(List<ItemDisplay> itemDisplays) {
this.placeholders = itemDisplays;
}
private PixelBlockPlaceholder(PixelBlock parentBlock) {
if(parentBlock.getPixels().size() > 5) return;