Skip to content

Commit 63a2e0d

Browse files
committed
Add BlockTransaction#setCustom(BlockSnapshot, BlockChangeFlag)
1 parent 5bd9f8f commit 63a2e0d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/main/java/org/spongepowered/api/block/transaction/BlockTransaction.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import org.spongepowered.api.data.Transaction;
3030
import org.spongepowered.api.data.persistence.DataContainer;
3131
import org.spongepowered.api.data.persistence.Queries;
32+
import org.spongepowered.api.world.BlockChangeFlag;
33+
import org.spongepowered.api.world.BlockChangeFlags;
3234

35+
import java.lang.ref.WeakReference;
3336
import java.util.List;
3437
import java.util.Objects;
3538
import java.util.StringJoiner;
@@ -47,6 +50,9 @@ public final class BlockTransaction extends Transaction<BlockSnapshot> {
4750

4851
private final Operation operation;
4952

53+
private @Nullable WeakReference<BlockSnapshot> custom;
54+
private @Nullable BlockChangeFlag customFlag;
55+
5056
public BlockTransaction(final BlockSnapshot original, final BlockSnapshot defaultReplacement,
5157
final Operation operation
5258
) {
@@ -66,6 +72,37 @@ public Operation operation() {
6672
return this.operation;
6773
}
6874

75+
/**
76+
* Sets the custom snapshot. If setting <code>null</code>, this will
77+
* reset to use the {@link #defaultReplacement()} snapshot.
78+
*
79+
* @param custom The custom snapshot
80+
* @param flag TThe various change flags controlling some interactions
81+
*/
82+
public void setCustom(final @Nullable BlockSnapshot custom, final BlockChangeFlag flag) {
83+
this.setCustom(custom);
84+
if (custom != null) {
85+
this.custom = new WeakReference<>(custom);
86+
this.customFlag = flag;
87+
} else {
88+
this.custom = null;
89+
this.customFlag = null;
90+
}
91+
}
92+
93+
/**
94+
* Gets the {@link BlockChangeFlag flag} that is used to place
95+
* the custom {@link BlockSnapshot}.
96+
*
97+
* @return The flag
98+
*/
99+
public BlockChangeFlag customFlag() {
100+
if (this.custom().isEmpty() || this.custom == null || this.custom.get() != this.custom().get()) {
101+
return BlockChangeFlags.DEFAULT_PLACEMENT;
102+
}
103+
return this.customFlag;
104+
}
105+
69106
@Override
70107
public int hashCode() {
71108
return Objects.hash(super.hashCode(), this.operation);

0 commit comments

Comments
 (0)