Skip to content

Commit c30f4e4

Browse files
committed
Merge branch 'api-15' into api-16
# Conflicts: # src/main/java/org/spongepowered/common/event/tracking/context/transaction/world/EntityPerformingDropsTransaction.java # src/main/java/org/spongepowered/common/event/tracking/context/transaction/world/SpawnEntityTransaction.java
2 parents 1913408 + 9270804 commit c30f4e4

File tree

25 files changed

+169
-254
lines changed

25 files changed

+169
-254
lines changed

.github/workflows/check-spotless.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ jobs:
1111
uses: SpongePowered/.github/.github/workflows/shared-check-spotless.yaml@master
1212
with:
1313
runtime_version: 21
14-
extra_gradle_params: "-Pprojects=vanilla,neoforge,testplugins"
14+
extra_gradle_params: "-Pprojects=vanilla,forge,neoforge,testplugins"
1515
secrets: inherit

neoforge/src/main/resource-templates/META-INF/neoforge.mods.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ description = "{{ description }}"
1616

1717
[[dependencies.spongeneo]]
1818
modId="neoforge"
19-
mandatory=true
19+
type="required"
2020
versionRange="[{{ neoForgeVersion }},)"
2121
ordering="AFTER"
22-
side="BOTH"
2322

2423
[[dependencies.spongeneo]]
2524
modId="sponge"
26-
mandatory=true
25+
type="required"
2726
versionRange="[{{ version }}]"
2827
ordering="AFTER"
29-
side="BOTH"
3028

3129

3230
[[mods]]
@@ -40,10 +38,9 @@ description="The SpongeAPI implementation targeting vanilla Minecraft and 3rd pa
4038

4139
[[dependencies.sponge]]
4240
modId="spongeapi"
43-
mandatory=true
41+
type="required"
4442
versionRange="[{{ apiVersion }},)"
4543
ordering="AFTER"
46-
side="BOTH"
4744

4845

4946
[[mods]]
@@ -55,6 +52,11 @@ credits="SpongePowered and Contributors"
5552
authors="SpongePowered"
5653
description="A Minecraft plugin API"
5754

55+
5856
["lithium:options"]
5957
"mixin.world.tick_scheduler"=false
6058
"mixin.collections.entity_filtering"=false
59+
60+
[[dependencies.spongeneo]]
61+
modId="adventure_platform_neoforge"
62+
type="incompatible"

src/main/java/org/spongepowered/common/SpongeLifecycle.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void callRegisterBuilderEvent() {
122122
public void establishEarlyGlobalRegistries() {
123123
final SpongeRegistryHolder holder = (SpongeRegistryHolder) this.game;
124124
holder.setRootMinecraftRegistry((Registry<Registry<?>>) BuiltInRegistries.REGISTRY);
125+
holder.streamRegistries().forEach(r -> ((WritableRegistryBridge<?>) r).bridge$setRegistryHolder(this.game));
125126

126127
SpongeRegistries.registerEarlyGlobalRegistries(holder);
127128
}
@@ -136,8 +137,12 @@ public void establishGlobalRegistries() {
136137
// Freeze Sponge Root - Registries are now available
137138
holder.registryHolder().freezeSpongeRootRegistry();
138139

139-
this.game.eventManager().post(new AbstractRegisterRegistryValueEvent.GameScopedImpl(Cause.of(EventContext.empty(), this.game), this.game,
140-
holder.streamRegistries().collect(Collectors.toMap(org.spongepowered.api.registry.Registry::type, Function.identity()))));
140+
final Map<RegistryType<?>, org.spongepowered.api.registry.Registry<?>> map =
141+
holder.streamRegistries().collect(Collectors.toMap(org.spongepowered.api.registry.Registry::type, Function.identity()));
142+
143+
this.game.eventManager().post(new AbstractRegisterRegistryValueEvent.GameScopedImpl(Cause.of(EventContext.empty(), this.game), this.game, map));
144+
map.values().forEach(r -> ((WritableRegistryBridge<?>) r).bridge$markEventCalled());
145+
holder.registryHolder().freezeSpongeDynamicRegistries(false);
141146
}
142147

143148
public void endEstablishGlobalRegistries() {

src/main/java/org/spongepowered/common/data/provider/item/stack/PotionItemStackData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import net.minecraft.core.registries.BuiltInRegistries;
3131
import net.minecraft.world.effect.MobEffectInstance;
3232
import net.minecraft.world.item.ItemStack;
33+
import net.minecraft.world.item.Items;
3334
import net.minecraft.world.item.alchemy.Potion;
3435
import net.minecraft.world.item.alchemy.PotionContents;
3536
import org.spongepowered.api.data.Keys;
@@ -57,6 +58,7 @@ public static void register(final DataProviderRegistrator registrator) {
5758
.get(h -> Color.ofRgb(h.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).getColor()))
5859
.set((h, v) -> h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(contents.potion(), Optional.of(v.rgb()), contents.customEffects(), contents.customName())))
5960
.delete(h -> h.update(DataComponents.POTION_CONTENTS, PotionContents.EMPTY, contents -> new PotionContents(contents.potion(), Optional.empty(), contents.customEffects(), contents.customName())))
61+
.supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW)
6062
.create(Keys.CUSTOM_POTION_EFFECTS)
6163
.get(h -> {
6264
final List<MobEffectInstance> effects = h.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).customEffects();

src/main/java/org/spongepowered/common/data/provider/item/stack/SignItemStackData.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
*/
2525
package org.spongepowered.common.data.provider.item.stack;
2626

27-
import net.kyori.adventure.text.Component;
28-
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
27+
import com.mojang.serialization.DynamicOps;
2928
import net.minecraft.core.component.DataComponents;
3029
import net.minecraft.nbt.CompoundTag;
30+
import net.minecraft.nbt.NbtOps;
31+
import net.minecraft.nbt.Tag;
3132
import net.minecraft.world.item.ItemStack;
3233
import net.minecraft.world.item.component.CustomData;
3334
import net.minecraft.world.level.block.entity.SignText;
34-
import org.checkerframework.checker.nullness.qual.Nullable;
3535
import org.spongepowered.api.data.Keys;
36+
import org.spongepowered.common.SpongeCommon;
3637
import org.spongepowered.common.adventure.SpongeAdventure;
3738
import org.spongepowered.common.data.provider.DataProviderRegistrator;
3839
import org.spongepowered.common.util.Constants;
@@ -66,16 +67,24 @@ public static void register(final DataProviderRegistrator registrator) {
6667
.toList();
6768
})
6869
.set((h, v) -> {
69-
final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
7070
final CompoundTag tag = new CompoundTag();
7171
tag.putString(Constants.Item.BLOCK_ENTITY_ID, Constants.TileEntity.SIGN);
72-
for (int i = 0; i < 4; i++) {
73-
final @Nullable Component line = v.size() > i ? v.get(i) : Component.empty();
74-
if (line == null) {
75-
throw new IllegalArgumentException("A null line was given at index " + i);
72+
DynamicOps<Tag> $$2 = SpongeCommon.vanillaRegistryAccess().createSerializationContext(NbtOps.INSTANCE);
73+
final var text = new SignText();
74+
for (int i = 0; i < v.size(); ++i) {
75+
if (i > 3) {
76+
break;
7677
}
77-
tag.putString("Text" + (i + 1), gcs.serialize(line));
78+
final var translated = SpongeAdventure.asVanilla(v.get(i));
79+
if (translated == null) {
80+
continue;
81+
}
82+
text.setMessage(i, translated);
7883
}
84+
SignText.DIRECT_CODEC.encodeStart($$2, text)
85+
.resultOrPartial(SpongeCommon.logger()::error)
86+
.ifPresent($$1x -> tag.put("front_text", $$1x));
87+
7988
h.set(DataComponents.BLOCK_ENTITY_DATA, CustomData.of(tag));
8089
})
8190
.delete(h -> h.remove(DataComponents.BLOCK_ENTITY_DATA));

src/main/java/org/spongepowered/common/event/lifecycle/AbstractRegisterRegistryEvent.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.spongepowered.api.registry.RegistryHolder;
3737
import org.spongepowered.api.registry.RegistryRoots;
3838
import org.spongepowered.api.registry.RegistryType;
39+
import org.spongepowered.common.SpongeCommon;
40+
import org.spongepowered.common.event.tracking.PhaseTracker;
3941
import org.spongepowered.common.registry.RegistryLoader;
4042
import org.spongepowered.common.registry.SpongeRegistryHolder;
4143

@@ -68,7 +70,7 @@ public <T> RegistryType<T> register(final ResourceKey key, final boolean isDynam
6870

6971
final SpongeRegistryHolder holder = this.getHolder();
7072
final RegistryType<T> type = RegistryType.of(RegistryRoots.SPONGE, key);
71-
holder.createRegistry(type, defaultValues, isDynamic);
73+
holder.createRegistry(type, () -> this.wrapWithScopedHolder(type, holder, defaultValues), isDynamic);
7274
return type;
7375
}
7476

@@ -81,10 +83,20 @@ public <T> RegistryType<T> register(final ResourceKey key, final boolean isDynam
8183

8284
final SpongeRegistryHolder holder = this.getHolder();
8385
final RegistryType<T> type = RegistryType.of(RegistryRoots.SPONGE, key);
84-
holder.createRegistry(type, defaultValues, isDynamic, dependencies);
86+
holder.createRegistry(type, actualHolder -> this.wrapWithScopedHolder(type, holder, () -> defaultValues.apply(actualHolder)), isDynamic, dependencies);
8587
return type;
8688
}
8789

90+
private <T> Map<ResourceKey, T> wrapWithScopedHolder(final RegistryType<T> registry, final SpongeRegistryHolder holder, final Supplier<Map<ResourceKey, T>> defaultValues) {
91+
return PhaseTracker.getInstance().switchStartupRegistryHolder(
92+
holder, defaultValues,
93+
(exception) -> {
94+
// If we don't catch it, vanilla will shut the server with "Failed to load datapacks"
95+
SpongeCommon.logger().error("Could not supply default values for registry {}", registry.toString(), exception);
96+
return Map.of();
97+
});
98+
}
99+
88100
protected abstract SpongeRegistryHolder getHolder();
89101

90102
public static final class GameScopedImpl extends AbstractRegisterRegistryEvent implements RegisterRegistryEvent.GameScoped {

src/main/java/org/spongepowered/common/event/tracking/IPhaseState.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.common.event.tracking;
2626

27-
import com.google.common.collect.ImmutableList;
2827
import net.minecraft.core.BlockPos;
2928
import net.minecraft.server.level.ServerLevel;
3029
import net.minecraft.server.level.ServerPlayer;
@@ -57,7 +56,6 @@
5756
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
5857
import org.spongepowered.api.item.inventory.Slot;
5958
import org.spongepowered.api.item.inventory.transaction.SlotTransaction;
60-
import org.spongepowered.api.util.Tuple;
6159
import org.spongepowered.api.world.BlockChangeFlag;
6260
import org.spongepowered.api.world.World;
6361
import org.spongepowered.api.world.server.ServerWorld;
@@ -70,7 +68,6 @@
7068
import org.spongepowered.common.entity.PlayerTracker;
7169
import org.spongepowered.common.event.tracking.context.transaction.GameTransaction;
7270
import org.spongepowered.common.event.tracking.context.transaction.block.ChangeBlock;
73-
import org.spongepowered.common.event.tracking.context.transaction.world.SpawnEntityTransaction;
7471
import org.spongepowered.common.event.tracking.phase.general.ExplosionContext;
7572
import org.spongepowered.common.event.tracking.phase.packet.PacketPhase;
7673
import org.spongepowered.common.event.tracking.phase.tick.LocationBasedTickContext;
@@ -81,7 +78,6 @@
8178
import java.util.List;
8279
import java.util.function.BiConsumer;
8380
import java.util.function.Supplier;
84-
import java.util.stream.Collectors;
8581

8682
/**
8783
* A literal phase state of which the {@link World} is currently running
@@ -378,14 +374,10 @@ default Supplier<SpawnType> getSpawnTypeForTransaction(final C context, final En
378374

379375
default SpawnEntityEvent createSpawnEvent(final C context,
380376
final @Nullable GameTransaction<@NonNull ?> parent,
381-
final ImmutableList<Tuple<Entity, SpawnEntityTransaction.DummySnapshot>> collect,
377+
final List<Entity> collect,
382378
final Cause currentCause
383379
) {
384-
return SpongeEventFactory.createSpawnEntityEvent(currentCause,
385-
collect.stream()
386-
.map(t -> (org.spongepowered.api.entity.Entity) t.first())
387-
.collect(Collectors.toList())
388-
);
380+
return SpongeEventFactory.createSpawnEntityEvent(currentCause, (List) collect);
389381
}
390382

391383
default void populateLootContext(final C phaseContext, final LootParams.Builder lootBuilder) {

src/main/java/org/spongepowered/common/event/tracking/PhaseStateProxy.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package org.spongepowered.common.event.tracking;
2626

27-
import com.google.common.collect.ImmutableList;
2827
import net.minecraft.core.BlockPos;
2928
import net.minecraft.server.level.ServerLevel;
3029
import net.minecraft.server.level.ServerPlayer;
@@ -51,7 +50,6 @@
5150
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
5251
import org.spongepowered.api.item.inventory.Slot;
5352
import org.spongepowered.api.item.inventory.transaction.SlotTransaction;
54-
import org.spongepowered.api.util.Tuple;
5553
import org.spongepowered.api.world.BlockChangeFlag;
5654
import org.spongepowered.api.world.server.ServerWorld;
5755
import org.spongepowered.common.block.SpongeBlockSnapshot;
@@ -61,7 +59,6 @@
6159
import org.spongepowered.common.entity.PlayerTracker;
6260
import org.spongepowered.common.event.tracking.context.transaction.GameTransaction;
6361
import org.spongepowered.common.event.tracking.context.transaction.block.ChangeBlock;
64-
import org.spongepowered.common.event.tracking.context.transaction.world.SpawnEntityTransaction;
6562
import org.spongepowered.common.event.tracking.phase.general.ExplosionContext;
6663
import org.spongepowered.common.event.tracking.phase.tick.LocationBasedTickContext;
6764
import org.spongepowered.common.world.BlockChange;
@@ -292,7 +289,7 @@ default Supplier<SpawnType> getSpawnTypeForTransaction(final Entity entityToSpaw
292289
}
293290

294291
default SpawnEntityEvent createSpawnEvent(final @Nullable GameTransaction<@NonNull ?> parent,
295-
final ImmutableList<Tuple<Entity, SpawnEntityTransaction.DummySnapshot>> collect,
292+
final List<Entity> collect,
296293
final Cause currentCause
297294
) {
298295
return this.getState().createSpawnEvent(this.asContext(), parent, collect, currentCause);

src/main/java/org/spongepowered/common/event/tracking/PhaseTracker.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
import java.util.StringJoiner;
8383
import java.util.concurrent.CopyOnWriteArrayList;
8484
import java.util.concurrent.atomic.AtomicBoolean;
85+
import java.util.function.Function;
86+
import java.util.function.Supplier;
8587

8688
/**
8789
* The core state machine of Sponge. Acts a as proxy between various engine objects by processing actions through
@@ -873,7 +875,22 @@ public CauseStackManager apiAccess() {
873875
return this.api;
874876
}
875877

876-
public void startupRegistryHolder(final RegistryHolder holder) {
878+
public <T> T switchStartupRegistryHolder(
879+
final SpongeRegistryHolder holder, final Supplier<? extends T> valueSupplier,
880+
final Function<? super RuntimeException, ? extends T> exceptionalValueSupplier
881+
) {
882+
final @Nullable SpongeRegistryHolder previousHolder = this.startupRegistryHolder();
883+
try {
884+
this.startupRegistryHolder(holder);
885+
return valueSupplier.get();
886+
} catch (final RuntimeException e) {
887+
return exceptionalValueSupplier.apply(e);
888+
} finally {
889+
this.startupRegistryHolder(previousHolder);
890+
}
891+
}
892+
893+
public void startupRegistryHolder(final @Nullable RegistryHolder holder) {
877894
if (holder instanceof SpongeRegistryHolder srh) {
878895
this.startupRegistryHolder = srh;
879896
} else {

src/main/java/org/spongepowered/common/event/tracking/context/transaction/world/EntityPerformingDropsTransaction.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@
2525
package org.spongepowered.common.event.tracking.context.transaction.world;
2626

2727
import com.google.common.collect.ImmutableList;
28-
import net.minecraft.nbt.CompoundTag;
29-
import net.minecraft.server.level.ServerLevel;
30-
import net.minecraft.util.ProblemReporter;
3128
import net.minecraft.world.damagesource.CombatEntry;
3229
import net.minecraft.world.damagesource.DamageSource;
3330
import net.minecraft.world.entity.Entity;
3431
import net.minecraft.world.entity.LivingEntity;
35-
import net.minecraft.world.level.storage.TagValueOutput;
3632
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
3733
import org.checkerframework.checker.nullness.qual.NonNull;
3834
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -46,7 +42,6 @@
4642
import org.spongepowered.common.event.tracking.context.transaction.GameTransaction;
4743
import org.spongepowered.common.event.tracking.context.transaction.type.TransactionTypes;
4844
import org.spongepowered.common.util.PrettyPrinter;
49-
import org.spongepowered.common.world.volume.VolumeStreamUtils;
5045

5146
import java.lang.ref.WeakReference;
5247
import java.util.List;
@@ -57,9 +52,7 @@
5752

5853
public final class EntityPerformingDropsTransaction extends WorldBasedTransaction<HarvestEntityEvent> {
5954

60-
private @MonotonicNonNull Supplier<ServerLevel> worldSupplier;
6155
final Entity destroyingEntity;
62-
private @MonotonicNonNull CompoundTag entityTag;
6356
private @MonotonicNonNull Supplier<Optional<DamageSource>> lastAttacker;
6457

6558
public EntityPerformingDropsTransaction(final Entity destroyingEntity) {
@@ -70,16 +63,10 @@ public EntityPerformingDropsTransaction(final Entity destroyingEntity) {
7063
@Override
7164
protected void captureState() {
7265
super.captureState();
73-
final Entity entity = this.destroyingEntity;
74-
this.worldSupplier = VolumeStreamUtils.createWeaklyReferencedSupplier((ServerLevel) entity.level(), "ServerLevel");
75-
76-
final var output = TagValueOutput.createWithContext(ProblemReporter.DISCARDING, entity.level().registryAccess());
77-
entity.saveWithoutId(output);
78-
this.entityTag = output.buildResult();
7966

8067
final @Nullable DamageSource lastAttacker;
81-
if (entity instanceof LivingEntity) {
82-
final List<CombatEntry> entries = ((CombatTrackerAccessor) ((LivingEntity) entity).getCombatTracker()).accessor$entries();
68+
if (this.destroyingEntity instanceof LivingEntity living) {
69+
final List<CombatEntry> entries = ((CombatTrackerAccessor) living.getCombatTracker()).accessor$entries();
8370
if (!entries.isEmpty()) {
8471
final CombatEntry entry = entries.get(entries.size() - 1);
8572
lastAttacker = entry.source();

0 commit comments

Comments
 (0)