refactoring
This commit is contained in:
parent
70c7059e43
commit
2810db2e93
@ -56,9 +56,9 @@ public final class Main extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
Main.taskFactory = BukkitTaskChainFactory.create(this);
|
Main.taskFactory = BukkitTaskChainFactory.create(this);
|
||||||
getLogger().info("Loading existing blocks from Database");
|
getLogger().info("Start constructing blocks from Database...");
|
||||||
database.loadPixelBlocks();
|
database.loadPixelBlocks();
|
||||||
getLogger().info("Pixelblock loading done");
|
getLogger().info("Construction done!");
|
||||||
|
|
||||||
Listener[] listeners = {
|
Listener[] listeners = {
|
||||||
new EnterPixelBlockListener(),
|
new EnterPixelBlockListener(),
|
||||||
|
@ -119,7 +119,6 @@ public class PixelBlockDatabase {
|
|||||||
allPixelBlocks.getDouble("entryLocationZ")
|
allPixelBlocks.getDouble("entryLocationZ")
|
||||||
);
|
);
|
||||||
|
|
||||||
Main.plugin.getLogger().info("Pixelblock found ");
|
|
||||||
Main.pixelBlocks.add(PixelBlock.fromExisting(
|
Main.pixelBlocks.add(PixelBlock.fromExisting(
|
||||||
UUID.fromString(allPixelBlocks.getString("uuid")),
|
UUID.fromString(allPixelBlocks.getString("uuid")),
|
||||||
UUID.fromString(allPixelBlocks.getString("owner")),
|
UUID.fromString(allPixelBlocks.getString("owner")),
|
||||||
|
@ -30,13 +30,6 @@ public class CreatePixelBlockCommand implements CommandExecutor {
|
|||||||
playerLocation.toBlockLocation(),
|
playerLocation.toBlockLocation(),
|
||||||
Direction.south
|
Direction.south
|
||||||
);
|
);
|
||||||
// PixelBlock block = new PixelBlock(
|
|
||||||
// playerLocation,
|
|
||||||
// p.getUniqueId(),
|
|
||||||
// UUID.randomUUID(),
|
|
||||||
// Direction.south
|
|
||||||
// );
|
|
||||||
// block.place(playerLocation, Direction.south);
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -21,10 +21,7 @@ public class Pixel {
|
|||||||
private final @NotNull PixelBlock parentBlock;
|
private final @NotNull PixelBlock parentBlock;
|
||||||
private final @NotNull BlockDisplay entity;
|
private final @NotNull BlockDisplay entity;
|
||||||
|
|
||||||
public final Location location;
|
private final Location location;
|
||||||
public final BlockData blockData;
|
|
||||||
private final double scale;
|
|
||||||
|
|
||||||
|
|
||||||
public static List<Pixel> fromExisting(PixelBlock parentBlock) {
|
public static List<Pixel> fromExisting(PixelBlock parentBlock) {
|
||||||
return parentBlock.getPixelBlockLocation().getNearbyEntitiesByType(BlockDisplay.class, 1)
|
return parentBlock.getPixelBlockLocation().getNearbyEntitiesByType(BlockDisplay.class, 1)
|
||||||
@ -37,29 +34,23 @@ public class Pixel {
|
|||||||
.map(blockDisplay -> new Pixel(parentBlock, blockDisplay))
|
.map(blockDisplay -> new Pixel(parentBlock, blockDisplay))
|
||||||
.collect(Collectors.toList());
|
.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) {
|
private Pixel(@NotNull PixelBlock parentBlock, @NotNull BlockDisplay entity) {
|
||||||
this.parentBlock = parentBlock;
|
this.parentBlock = parentBlock;
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this.location = entity.getLocation();
|
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) {
|
private Pixel(@NotNull PixelBlock parentBlock, @NotNull Vector relativePosition, @NotNull BlockData blockData, double scale) {
|
||||||
this.parentBlock = parentBlock;
|
this.parentBlock = parentBlock;
|
||||||
this.location = parentBlock.getPixelBlockLocation().add(relativePosition.multiply(scale));
|
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 = (BlockDisplay) this.location.getWorld().spawnEntity(this.location, EntityType.BLOCK_DISPLAY);
|
||||||
|
|
||||||
this.entity.setBlock(blockData);
|
this.entity.setBlock(blockData);
|
||||||
Transformation transform = this.entity.getTransformation();
|
Transformation transform = this.entity.getTransformation();
|
||||||
transform.getScale().set(this.scale);
|
transform.getScale().set(scale);
|
||||||
this.entity.setTransformation(transform);
|
this.entity.setTransformation(transform);
|
||||||
this.entity.getPersistentDataContainer().set(pixelOfTag, PersistentDataType.STRING, this.parentBlock.getBlockUUID().toString());
|
this.entity.getPersistentDataContainer().set(pixelOfTag, PersistentDataType.STRING, this.parentBlock.getBlockUUID().toString());
|
||||||
}
|
}
|
||||||
@ -67,4 +58,8 @@ public class Pixel {
|
|||||||
public void destroy() {
|
public void destroy() {
|
||||||
this.entity.remove();
|
this.entity.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Location getLocation() {
|
||||||
|
return location.clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,10 +79,17 @@ public class PixelBlock {
|
|||||||
this.facingDirection = direction;
|
this.facingDirection = direction;
|
||||||
this.lastEntryLocation = lastEntryLocation;
|
this.lastEntryLocation = lastEntryLocation;
|
||||||
|
|
||||||
|
this.ensureChunksLoaded();
|
||||||
|
try {
|
||||||
this.pixelWorld = new PixelBlockWorld(this);
|
this.pixelWorld = new PixelBlockWorld(this);
|
||||||
this.pixels = Pixel.fromExisting(this);
|
this.pixels = Pixel.fromExisting(this);
|
||||||
this.placeholder = PixelBlockPlaceholder.fromExisting(this);
|
this.placeholder = PixelBlockPlaceholder.fromExisting(this);
|
||||||
this.hitbox = PixelBlockHitbox.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) {
|
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());
|
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) {
|
public void enterBlock(@NotNull Player player) {
|
||||||
if(Main.configuration.onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) {
|
if(Main.configuration.onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) {
|
||||||
player.sendMessage(Component.text("Dieser Pixelblock gehört nicht dir!", NamedTextColor.RED));
|
player.sendMessage(Component.text("Dieser Pixelblock gehört nicht dir!", NamedTextColor.RED));
|
||||||
@ -260,17 +276,19 @@ public class PixelBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void scheduleEntityRemove() {
|
private void scheduleEntityRemove() {
|
||||||
// Chunk chunk = this.pixelBlockLocation.getChunk();
|
|
||||||
// if(!chunk.isEntitiesLoaded()) {
|
|
||||||
// chunk.load(true);
|
|
||||||
// chunk.getEntities();
|
|
||||||
// }
|
|
||||||
|
|
||||||
this.getBlockTaskChain()
|
this.getBlockTaskChain()
|
||||||
.current(() -> Main.plugin.getLogger()
|
.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(() -> {
|
.sync(() -> {
|
||||||
|
List<Entity> entitiesBefore = this.pixelBlockLocation.getWorld().getEntities();
|
||||||
this.pixels.forEach(Pixel::destroy);
|
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.pixels.clear();
|
||||||
this.placeholder.destroy();
|
this.placeholder.destroy();
|
||||||
this.hitbox.destroy();
|
this.hitbox.destroy();
|
||||||
|
@ -30,17 +30,16 @@ public class PixelBlockHitbox {
|
|||||||
})
|
})
|
||||||
.orElseThrow();
|
.orElseThrow();
|
||||||
|
|
||||||
|
Main.plugin.getLogger().info(String.format("Found existing hitbox '%s' for block '%s'", hitBox.getUniqueId(), parentBlock.getBlockUUID()));
|
||||||
return new PixelBlockHitbox(hitBox);
|
return new PixelBlockHitbox(hitBox);
|
||||||
}
|
}
|
||||||
|
private PixelBlockHitbox(Interaction interaction) {
|
||||||
|
this.interaction = interaction;
|
||||||
|
}
|
||||||
|
|
||||||
public static PixelBlockHitbox newHitbox(PixelBlock parentBlock) {
|
public static PixelBlockHitbox newHitbox(PixelBlock parentBlock) {
|
||||||
return new PixelBlockHitbox(parentBlock.getPixelBlockLocation(), parentBlock.getPixels(), parentBlock.getBlockUUID());
|
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) {
|
private PixelBlockHitbox(Location pixelBlockLocation, List<Pixel> pixels, UUID parentBlockUUID) {
|
||||||
float offset = (float) Main.configuration.hitboxOffset();
|
float offset = (float) Main.configuration.hitboxOffset();
|
||||||
int pixelsPerBlock = Main.configuration.pixelsPerBlock();
|
int pixelsPerBlock = Main.configuration.pixelsPerBlock();
|
||||||
@ -53,13 +52,13 @@ public class PixelBlockHitbox {
|
|||||||
interaction.setInteractionHeight(1 + 2*offset);
|
interaction.setInteractionHeight(1 + 2*offset);
|
||||||
interaction.setInteractionWidth(1 + 2*offset);
|
interaction.setInteractionWidth(1 + 2*offset);
|
||||||
} else {
|
} else {
|
||||||
double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getX());
|
double startingX = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.getLocation().getX());
|
||||||
double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getY());
|
double startingY = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.getLocation().getY());
|
||||||
double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.location.getZ());
|
double startingZ = MinMaxUtil.getMinProperty(pixels, pixel -> pixel.getLocation().getZ());
|
||||||
|
|
||||||
double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getX());
|
double endingX = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.getLocation().getX());
|
||||||
double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getY());
|
double endingY = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.getLocation().getY());
|
||||||
double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.location.getZ());
|
double endingZ = MinMaxUtil.getMaxProperty(pixels, pixel -> pixel.getLocation().getZ());
|
||||||
|
|
||||||
Location spawnLocation = pixelBlockLocation.clone().add(
|
Location spawnLocation = pixelBlockLocation.clone().add(
|
||||||
((startingX+endingX+1)/2)/pixelsPerBlock,
|
((startingX+endingX+1)/2)/pixelsPerBlock,
|
||||||
|
@ -31,17 +31,16 @@ public class PixelBlockPlaceholder {
|
|||||||
))
|
))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
Main.plugin.getLogger().info(String.format("Found %d existing placeholders for block '%s'", placeholders.size(), parentBlock.getBlockUUID()));
|
||||||
return new PixelBlockPlaceholder(placeholders);
|
return new PixelBlockPlaceholder(placeholders);
|
||||||
}
|
}
|
||||||
|
private PixelBlockPlaceholder(List<ItemDisplay> itemDisplays) {
|
||||||
|
this.placeholders = itemDisplays;
|
||||||
|
}
|
||||||
|
|
||||||
public static PixelBlockPlaceholder newPlaceholder(PixelBlock parentBlock) {
|
public static PixelBlockPlaceholder newPlaceholder(PixelBlock parentBlock) {
|
||||||
return new PixelBlockPlaceholder(parentBlock);
|
return new PixelBlockPlaceholder(parentBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
private PixelBlockPlaceholder(List<ItemDisplay> itemDisplays) {
|
|
||||||
this.placeholders = itemDisplays;
|
|
||||||
}
|
|
||||||
|
|
||||||
private PixelBlockPlaceholder(PixelBlock parentBlock) {
|
private PixelBlockPlaceholder(PixelBlock parentBlock) {
|
||||||
if(parentBlock.getPixels().size() > 5) return;
|
if(parentBlock.getPixels().size() > 5) return;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user