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

Initial proposal for server transactions #2409

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.spongepowered.api.world.server;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.entity.SpawnEntityEvent;

import java.util.Optional;

public final class ServerTransaction {

@Nullable private final SpawnEntityEvent entities;
private final ChangeBlockEvent.@Nullable Post changeBlockEvent;

public ServerTransaction(
@Nullable final SpawnEntityEvent entities,
final ChangeBlockEvent.@Nullable Post changeBlockEvent
) {
this.entities = entities;
this.changeBlockEvent = changeBlockEvent;
}

public Optional<SpawnEntityEvent> entities() {
return Optional.ofNullable(this.entities);
}

public Optional<ChangeBlockEvent.Post> blocks() {
return Optional.ofNullable(this.changeBlockEvent);
}

}
23 changes: 23 additions & 0 deletions src/main/java/org/spongepowered/api/world/server/ServerWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
import org.spongepowered.api.world.volume.game.InteractableVolume;
import org.spongepowered.api.world.weather.WeatherUniverse;
import org.spongepowered.math.vector.Vector3i;
import org.spongepowered.plugin.PluginContainer;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;

public interface ServerWorld extends World<ServerWorld, ServerLocation>, Identifiable, InteractableVolume,
ServerLocationCreator, WeatherUniverse.Mutable {
Expand Down Expand Up @@ -82,6 +84,27 @@ default UUID uniqueId() {
return this.properties().uniqueId();
}

/**
* Creates a contained {@link ServerTransaction} for the given
* {@link Runnable}, provided the runnable is working on the same thread as
* this world's {@link ServerWorld#engine() server}. Changes can be made to
* this world within the {@link Runnable#run()} that can have side effects,
* and the given {@link Predicate} serves as a check for the entire
* transaction succeeding or not. If the predicate returns {@code false} at
* any point during the runnable, all changes since the beginning of the
* transaction will be reverted.
* <p>It is intended for the predicate to be called multiple times based on
* the number of side effects within the world with potentially several
* changes being made in succession. Several
* {@link org.spongepowered.api.event.Event events} can also be thrown
* during the operation based on the transaction flow, and the
* {@link PluginContainer} will remain in the associated
* {@link org.spongepowered.api.event.Cause cause} for such events.</p>
* @param operation
* @param shouldRollback
*/
boolean transact(PluginContainer container, Runnable operation, Predicate<ServerTransaction> shouldRollback);

@Override
default WorldChunk chunkAtBlock(final int bx, final int by, final int bz) {
final Vector3i chunkPos = this.engine().chunkLayout().forceToChunk(bx, by, bz);
Expand Down