Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework ChunkEvent #2497

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 97 additions & 25 deletions src/main/java/org/spongepowered/api/event/world/chunk/ChunkEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.spongepowered.api.event.Event;
import org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.chunk.BlockChunk;
import org.spongepowered.api.world.chunk.Chunk;
import org.spongepowered.api.world.chunk.EntityChunk;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.math.vector.Vector3i;
Expand Down Expand Up @@ -64,58 +66,131 @@ interface WorldScoped extends ChunkEvent {
}

/**
* Called when a {@link WorldChunk chunk} was unloaded.
* Called when a {@link WorldChunk chunk} is performing a block related operation.
*/
interface Unload extends WorldScoped {
interface Blocks extends WorldScoped {

/**
* Called before the {@link WorldChunk chunk} is unloaded.
* Called when a block data of {@link WorldChunk chunk} is loaded.
* This can be called outside the {@link World#engine() main} {@link Thread}.
* It is NOT safe to perform modifications to the {@link World} or via
* {@link org.spongepowered.api.world.server.ServerLocation} as this could
* result in a deadlock.
*/
interface Pre extends Unload {
interface Load extends Blocks {

/**
* Gets the {@link WorldChunk chunk} being changed.
* Gets the {@link WorldChunk chunk} block volume.
*
* @return The chunk
* @return The block chunk
*/
WorldChunk chunk();
BlockChunk chunk();
}

/**
* Called after the {@link WorldChunk chunk} is unloaded.
* Called when a {@link WorldChunk chunk} is performing a block related save.
*/
interface Post extends Unload {
interface Save extends Blocks {

/**
* Called before the {@link WorldChunk chunk} block data is saved. Cancelling this
* will prevent any of the chunk's block data being written to it's storage container.
*/
interface Pre extends Blocks.Save, Cancellable {

/**
* Gets the {@link WorldChunk chunk} block volume.
*
* @return The block chunk
*/
BlockChunk chunk();
}

/**
* Called after the {@link WorldChunk chunk} block data is saved.
* Guaranteed to exist in the chunk's block storage container.
* <p>
* Depending on the implementation, this event may be called off-thread.
*/
interface Post extends Blocks.Save {}
}
}

/**
* Called when a {@link WorldChunk chunk} is performing a save.
* Called when a {@link WorldChunk chunk} is performing a entity related operation.
*/
interface Save extends WorldScoped {
interface Entities extends WorldScoped {

/**
* Called when an entity data of {@link WorldChunk chunk} is loaded.
* This can be called outside the {@link World#engine() main} {@link Thread}.
* It is NOT safe to perform modifications to the {@link World} or via
* {@link org.spongepowered.api.world.server.ServerLocation} as this could
* result in a deadlock.
*/
interface Load extends Entities {
Comment on lines +127 to +131
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the rammification of calling spawnEntity on the EntityChunk?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works and is safe. The entity is appended to be part of the list of entities to be loaded.


/**
* Gets the {@link WorldChunk chunk} entity volume.
*
* @return The entity chunk
*/
EntityChunk chunk();
}

/**
* Called before the {@link WorldChunk chunk} is saved. Cancelling this will prevent any of
* the chunk's data being written to it's storage container.
* Called when a {@link WorldChunk chunk} is performing a entity related save.
*/
interface Pre extends Save, Cancellable {
interface Save extends Entities {

/**
* Called before the {@link WorldChunk chunk} entity data is saved. Cancelling this
* will prevent any of the chunk's entity data being written to it's storage container.
*/
interface Pre extends Entities.Save, Cancellable {

/**
* Gets the {@link WorldChunk chunk} entity volume.
*
* @return The entity chunk
*/
EntityChunk chunk();
}

/**
* Called after the {@link WorldChunk chunk} entity data is saved.
* Guaranteed to exist in the chunk's entity storage container.
* <p>
* Depending on the implementation, this event may be called off-thread.
*/
interface Post extends Entities.Save {}
}
}

/**
* Called when a {@link WorldChunk chunk} was unloaded.
*/
interface Unload extends WorldScoped {

/**
* Called before the {@link WorldChunk chunk} is unloaded.
*/
interface Pre extends Unload {

/**
* Gets the {@link WorldChunk chunk} being changed.
*
* @return The chunk
*/
WorldChunk chunk();

}

/**
* Called after the {@link WorldChunk chunk} is saved. Guaranteed to exist in the chunk's
* storage container.
* <p>
* Depending on the implementation, this event may be called off-thread.
* Called after the {@link WorldChunk chunk} is unloaded.
*/
interface Post extends Save {}
interface Post extends Unload {

}
}

/**
Expand All @@ -126,11 +201,8 @@ interface Generated extends WorldScoped {
}

/**
* Called when a {@link WorldChunk chunk} is loaded. This can be called
* outside the {@link World#engine() main} {@link Thread}. It is NOT safe
* to perform modifications to the {@link World} or via
* {@link org.spongepowered.api.world.server.ServerLocation} as this could
* result in a deadlock.
* Called when a {@link WorldChunk chunk} is loaded. This is called
* from the main thread when the chunk is fully loaded and ready to tick.
*/
interface Load extends WorldScoped {

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/spongepowered/api/world/chunk/BlockChunk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.world.chunk;

import org.spongepowered.api.world.volume.block.BlockVolume;

public interface BlockChunk extends BlockVolume {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the significance of this? Why not just a BlockVolume?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to align with the EntityChunk and it allows us to easily change it to be BlockVolume.Modifiable in the future without it being binary breaking change.

}
33 changes: 33 additions & 0 deletions src/main/java/org/spongepowered/api/world/chunk/EntityChunk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.world.chunk;

import org.spongepowered.api.world.volume.entity.EntityVolume;

/**
* An entity chunk is a portion of a {@link WorldChunk}.
*/
public interface EntityChunk extends EntityVolume.Modifiable<EntityChunk> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may as well split modifiable if it means it's not spawnable, or am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entities are modifiable, thats implemented. Blocks are special case due to all of your pipeline tracking and its not obvious how we would want to alter that to make it possible.

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ interface Streamable<E extends Streamable<E>> extends EntityVolume {

}

interface Modifiable<M extends Modifiable<M>> extends Streamable<M>, MutableVolume, BlockVolume.Modifiable<M> {
interface Modifiable<M extends Modifiable<M>> extends Streamable<M>, MutableVolume {

/**
* Create an entity instance at the given position.
Expand Down