Skip to content

Commit f025aa3

Browse files
committed
Merge remote-tracking branch 'origin/api-13' into api-14
2 parents c4ed14f + daeaaab commit f025aa3

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
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);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.event.lifecycle;
26+
27+
import org.spongepowered.api.registry.DefaultedRegistryReference;
28+
import org.spongepowered.api.registry.RegistryKey;
29+
import org.spongepowered.api.tag.Tag;
30+
31+
import java.util.function.BiPredicate;
32+
import java.util.function.Consumer;
33+
import java.util.function.Predicate;
34+
35+
/**
36+
* Lifecycle event for applying modifications to {@link Tag tags}.
37+
*
38+
* <p>This event provides mechanism for plugins to <strong>propose</strong>
39+
* changes to the tags that are processed during tag construction.</p>
40+
*
41+
* <p>The following chain of events take place during tag construction:</p>
42+
* <ul>
43+
* <li>Tags are merged from data packs</li>
44+
* <li>Append proposed tags</li>
45+
* <li>Apply filter to ALL present tags</li>
46+
* <li>Remaining items makes up the final tag</li>
47+
* </ul>
48+
*/
49+
public interface RegisterTagEvent extends LifecycleEvent {
50+
51+
<T> TagStep<T> tag(Tag<T> tag);
52+
53+
interface TagStep<T> {
54+
55+
default TagStep<T> delete() {
56+
return this.filter(k -> false);
57+
}
58+
59+
TagStep<T> filter(Predicate<DefaultedRegistryReference<T>> predicate);
60+
61+
default TagStep<T> filterTags(Predicate<Tag<T>> predicate) {
62+
return this.filterTags((t, k) -> predicate.test(t));
63+
}
64+
65+
TagStep<T> filterTags(BiPredicate<Tag<T>, DefaultedRegistryReference<T>> predicate);
66+
67+
TagStep<T> append(RegistryKey<T> key);
68+
69+
TagStep<T> append(Tag<T> tag);
70+
71+
TagStep<T> test(Predicate<DefaultedRegistryReference<T>> predicate, Consumer<TagStep<T>> consumer);
72+
73+
default TagStep<T> testTags(Predicate<Tag<T>> predicate, Consumer<TagStep<T>> consumer) {
74+
return this.testTags((t, k) -> predicate.test(t), consumer);
75+
}
76+
77+
TagStep<T> testTags(BiPredicate<Tag<T>, DefaultedRegistryReference<T>> predicate, Consumer<TagStep<T>> consumer);
78+
}
79+
}

src/main/java/org/spongepowered/api/tag/Tag.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.spongepowered.api.Sponge;
3131
import org.spongepowered.api.block.BlockType;
3232
import org.spongepowered.api.registry.DefaultedRegistryType;
33+
import org.spongepowered.api.registry.RegistryType;
3334

3435
/**
3536
* A {@link ResourceKey resource keyed} collection of {@link Taggable} values
@@ -49,12 +50,26 @@
4950
*/
5051
public interface Tag<T> extends ResourceKeyed {
5152

53+
/**
54+
* Gets the {@link RegistryType location} defining the parent registry.
55+
*
56+
* @return The location
57+
*/
58+
RegistryType<T> registry();
59+
5260
interface Factory {
53-
<T> Tag<T> of(DefaultedRegistryType<T> registryType, ResourceKey key);
61+
default <T> Tag<T> of(DefaultedRegistryType<T> registryType, ResourceKey key) {
62+
return this.of((RegistryType<T>) registryType, key);
63+
}
64+
65+
<T> Tag<T> of(RegistryType<T> registryType, ResourceKey key);
5466
}
5567

5668
static <T> Tag<T> of(DefaultedRegistryType<T> registryType, ResourceKey key) {
5769
return Sponge.game().factoryProvider().provide(Tag.Factory.class).of(registryType, key);
5870
}
5971

72+
static <T> Tag<T> of(RegistryType<T> registryType, ResourceKey key) {
73+
return Sponge.game().factoryProvider().provide(Tag.Factory.class).of(registryType, key);
74+
}
6075
}

src/main/java/org/spongepowered/api/world/World.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import org.spongepowered.api.effect.Viewer;
3030
import org.spongepowered.api.entity.Entity;
3131
import org.spongepowered.api.entity.living.player.Player;
32+
import org.spongepowered.api.event.CauseStackManager;
3233
import org.spongepowered.api.registry.RegistryHolder;
34+
import org.spongepowered.api.scheduler.Scheduler;
3335
import org.spongepowered.api.service.context.ContextSource;
3436
import org.spongepowered.api.util.annotation.DoNotStore;
3537
import org.spongepowered.api.world.chunk.WorldChunk;
@@ -227,4 +229,38 @@ default Optional<WorldChunk> loadChunk(final Vector3i chunkPosition, final boole
227229
*/
228230
Iterable<WorldChunk> loadedChunks();
229231

232+
/**
233+
* Gets the {@link Scheduler} used to schedule sync tasks on this {@link World}.
234+
*
235+
* <p><strong>Note:</strong> Accessing objects outside of this {@link World}
236+
* from the scheduled tasks can lead to hard to diagnose problems
237+
* or unrecoverable crashes.</p>
238+
*
239+
* @return The sync scheduler
240+
*/
241+
default Scheduler scheduler() {
242+
return this.engine().scheduler();
243+
}
244+
245+
/**
246+
* Gets the {@link CauseStackManager} for handling the current event cause
247+
* stack and context information.
248+
*
249+
* <p><strong>Note:</strong> Accessing this outside of {@link World#onWorldThread()}
250+
* can lead to hard to diagnose problems or unrecoverable crashes.</p>
251+
*
252+
* @return The cause stack manager
253+
*/
254+
default CauseStackManager causeStackManager() {
255+
return this.engine().causeStackManager();
256+
}
257+
258+
/**
259+
* Checks if the {@link Thread#currentThread() current thread} is the main thread of the world.
260+
*
261+
* @return {@code true} if main thread, {@code false} if not
262+
*/
263+
default boolean onWorldThread() {
264+
return this.engine().onMainThread();
265+
}
230266
}

0 commit comments

Comments
 (0)