Skip to content

Commit

Permalink
Rework ChunkEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Apr 25, 2024
1 parent cb4ee93 commit d48612a
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 26 deletions.
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 volume
*/
WorldChunk chunk();
BlockChunk blockVolume();
}

/**
* 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 Save, Cancellable {

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

/**
* 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 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 {

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

/**
* 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 Save, Cancellable {

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

/**
* 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 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 {
}
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> {
}
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

0 comments on commit d48612a

Please sign in to comment.