Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/api-12' into api-13
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Feb 2, 2025
2 parents f913752 + 2e051f9 commit da6edb5
Show file tree
Hide file tree
Showing 25 changed files with 339 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.common.data.holder;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.data.CollectionDataProvider;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataProvider;
import org.spongepowered.api.data.DataTransactionResult;
Expand Down Expand Up @@ -88,6 +89,9 @@ default <E> DataTransactionResult offerSingle(Key<? extends CollectionValue<E, ?
final SpongeKey<? extends CollectionValue<E, Collection<E>>, Collection<E>> key0 =
(SpongeKey<? extends CollectionValue<E, Collection<E>>, Collection<E>>) key;
return this.impl$applyTransaction(key0, (p, m) -> {
if (p instanceof CollectionDataProvider<?, ?, ?>) {
return ((CollectionDataProvider<E, ?, ?>) p).offerSingle(m, element);
}
final Collection<E> collection = p.get(m)
.map(DataUtil::ensureMutable)
.orElseGet(key0.getDefaultValueSupplier());
Expand Down Expand Up @@ -143,6 +147,9 @@ default <E> DataTransactionResult removeSingle(Key<? extends CollectionValue<E,
final SpongeKey<? extends CollectionValue<E, Collection<E>>, Collection<E>> key0 =
(SpongeKey<? extends CollectionValue<E, Collection<E>>, Collection<E>>) key;
return this.impl$applyTransaction(key0, (p, m) -> {
if (p instanceof CollectionDataProvider<?, ?, ?>) {
return ((CollectionDataProvider<E, ?, ?>) p).removeSingle(m, element);
}
final Optional<Collection<E>> optCollection = p.get(m).map(DataUtil::ensureMutable);
if (!optCollection.isPresent()) {
return DataTransactionResult.failNoData();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@

import java.util.function.Supplier;

@SuppressWarnings("unchecked")
public abstract class GenericMutableDataProvider<H, E> extends GenericMutableDataProviderBase<H, Value<E>, E> {
public abstract class GenericMutableDataProvider<H, E, V extends Value<E>> extends GenericMutableDataProviderBase<H, V, E> {

public GenericMutableDataProvider(final Key<? extends Value<E>> key) {
super((Key<Value<E>>) key);
public GenericMutableDataProvider(final Key<V> key) {
super(key);
}

public GenericMutableDataProvider(final Supplier<? extends Key<? extends Value<E>>> key) {
public GenericMutableDataProvider(final Supplier<? extends Key<V>> key) {
this(key.get());
}

public GenericMutableDataProvider(final Key<? extends Value<E>> key, final Class<H> holderType) {
super((Key<Value<E>>) key, holderType);
public GenericMutableDataProvider(final Key<V> key, final Class<H> holderType) {
super(key, holderType);
}

public GenericMutableDataProvider(final Supplier<? extends Key<? extends Value<E>>> key, final Class<H> holderType) {
public GenericMutableDataProvider(final Supplier<Key<V>> key, final Class<H> holderType) {
this(key.get(), holderType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.spongepowered.api.data.persistence.DataQuery;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.util.Identifiable;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.bridge.data.SpongeDataHolderBridge;
import org.spongepowered.common.data.provider.DataProviderRegistrator;
import org.spongepowered.common.entity.player.SpongeUserData;
Expand All @@ -49,12 +50,20 @@ public static void register(final DataProviderRegistrator registrator) {
.get(h -> ((SpongeDataHolderBridge) h).bridge$get(Keys.FIRST_DATE_JOINED).orElse(null))
.set((h, v) -> ((SpongeDataHolderBridge) h).bridge$offer(Keys.FIRST_DATE_JOINED, v))
.supports(h -> h instanceof ServerPlayer || h instanceof SpongeUserData)
.create(Keys.LAST_DATE_JOINED)
.get(h -> ((SpongeDataHolderBridge) h).bridge$get(Keys.LAST_DATE_JOINED)
.or(() -> ((SpongeDataHolderBridge) h).bridge$get(Keys.LAST_DATE_PLAYED)).orElse(null))
.set((h, v) -> ((SpongeDataHolderBridge) h).bridge$offer(Keys.LAST_DATE_JOINED, v))
.supports(h -> h instanceof ServerPlayer || h instanceof SpongeUserData)
.create(Keys.LAST_DATE_PLAYED)
.get(h -> ((SpongeDataHolderBridge) h).bridge$get(Keys.LAST_DATE_PLAYED).orElse(null))
.get(h -> SpongeCommon.server().getPlayerList().getPlayer(h.uniqueId()) != null
? Instant.now()
: ((SpongeDataHolderBridge) h).bridge$get(Keys.LAST_DATE_PLAYED).orElse(null))
.set((h, v) -> ((SpongeDataHolderBridge) h).bridge$offer(Keys.LAST_DATE_PLAYED, v))
.supports(h -> h instanceof ServerPlayer || h instanceof SpongeUserData);

IdentifiableData.registerSpongeDataStore(registrator, Keys.FIRST_DATE_JOINED);
IdentifiableData.registerSpongeDataStore(registrator, Keys.LAST_DATE_JOINED);
IdentifiableData.registerSpongeDataStore(registrator, Keys.LAST_DATE_PLAYED);
}
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.spongepowered.math.vector.Vector3d;

import java.util.Collection;
import java.util.Objects;

public final class LivingData {

Expand Down Expand Up @@ -188,7 +189,7 @@ public static void register(final DataProviderRegistrator registrator) {
.create(Keys.MAX_HEALTH)
.get(h -> (double) h.getMaxHealth())
.set((h, v) -> h.getAttribute(Attributes.MAX_HEALTH).setBaseValue(v))
.create(Keys.POTION_EFFECTS)
.createCollection(Keys.POTION_EFFECTS)
.get(h -> {
final Collection<MobEffectInstance> effects = h.getActiveEffects();
return PotionEffectUtil.copyAsPotionEffects(effects);
Expand All @@ -200,6 +201,17 @@ public static void register(final DataProviderRegistrator registrator) {
h.addEffect(PotionEffectUtil.copyAsEffectInstance(effect));
}
})
.offerSingleAnd((h, v) -> {
h.forceAddEffect(PotionEffectUtil.copyAsEffectInstance(v), null);
return true;
})
.removeSingleAnd((h, v) -> {
if (Objects.equals(h.getEffect(((MobEffectInstance) v).getEffect()), v)) {
h.removeEffect(((MobEffectInstance) v).getEffect());
return true;
}
return false;
})
.create(Keys.SCALE)
.get(h -> (double) h.getScale())
.create(Keys.STUCK_ARROWS)
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/spongepowered/common/data/value/SpongeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.spongepowered.api.data.value.Value;
import org.spongepowered.common.data.key.SpongeKey;

import java.util.Objects;
import java.util.StringJoiner;

public abstract class SpongeValue<E> implements Value<E> {
Expand All @@ -51,6 +52,22 @@ public SpongeKey<? extends Value<E>, E> key() {
return this.key;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof final SpongeValue<?> other)) {
return false;
}
return Objects.equals(this.key, other.key) && Objects.equals(this.element, other.element);
}

@Override
public int hashCode() {
return Objects.hash(this.key, this.element);
}

@Override
public String toString() {
return new StringJoiner(", ", SpongeValue.class.getSimpleName() + "[", "]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class PhaseContext<P extends PhaseContext<P>> implements PhaseStateProxy<
private boolean allowsEntityEvents = true;
private boolean allowsBulkBlockCaptures = true; // Defaults to allow block captures
private boolean allowsBulkEntityCaptures = true;
private boolean requiresImplicitPhase = false;
@Nullable Deque<CauseStackManager.StackFrame> usedFrame;

private @Nullable Object source;
Expand Down Expand Up @@ -377,6 +378,15 @@ public Optional<ServerLocation> containerLocation() {
return Optional.empty();
}

public final boolean requiresImplicitPhase() {
return this.requiresImplicitPhase;
}

protected P requiresImplicitPhase(final boolean requiresImplicitPhase) {
this.requiresImplicitPhase = requiresImplicitPhase;
return (P) this;
}

protected boolean isRunaway(final PhaseContext<?> phaseContext) {
return phaseContext.getClass() == this.getClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.spongepowered.common.applaunch.config.core.SpongeConfigs;
import org.spongepowered.common.event.cause.entity.SpongeSpawnTypes;
import org.spongepowered.common.event.tracking.phase.general.GeneralPhase;
import org.spongepowered.common.event.tracking.phase.plugin.PluginPhase;
import org.spongepowered.common.event.tracking.phase.tick.TickPhase;
import org.spongepowered.common.launch.Launch;
import org.spongepowered.common.util.Constants;
Expand Down Expand Up @@ -607,6 +608,10 @@ public Object peekCause() {

@Override
public StackFrame pushCauseFrame() {
return this.pushCauseFrame0(false);
}

public StackFrame pushCauseFrame0(final boolean skipImplicitPhaseCreation) {
this.enforceMainThread();
// Ensure duplicate causes will be correctly sized.
final int size = this.cause.size();
Expand Down Expand Up @@ -635,14 +640,35 @@ public StackFrame pushCauseFrame() {
// were created.
frame.stackDebug = new Exception();
}
if (!skipImplicitPhaseCreation && this.getPhaseContext().requiresImplicitPhase()) {
frame.implicitContext = PluginPhase.State.PLUGIN.createPhaseContext(this).buildAndSwitch();
}
return frame;
}

@Override
public void popCauseFrame(final StackFrame oldFrame) {
Objects.requireNonNull(oldFrame, "oldFrame");
this.enforceMainThread();
final @Nullable SpongeCauseStackFrame frame = this.frames.peek();
@Nullable SpongeCauseStackFrame frame = this.frames.peek();
if (frame != oldFrame) {
// If implicit context is present, we need to
// first close it as it might have pushed new frames.
// This is an implementation detail so the caller has
// no knowledge of this context, so it can't close it
// by itself and instead closes the outer frame.
if (((SpongeCauseStackFrame) oldFrame).implicitContext != null) {
((SpongeCauseStackFrame) oldFrame).implicitContext.close();
((SpongeCauseStackFrame) oldFrame).implicitContext = null;
frame = this.frames.peek();
}
} else if (frame.implicitContext != null) {
// See above, no frames to close here.
// Fast path to avoid casting.
frame.implicitContext.close();
frame.implicitContext = null;
}

if (frame != oldFrame) {
// If the given frame is not the top frame then some form of
// corruption of the stack has occurred and we do our best to correct
Expand Down Expand Up @@ -802,7 +828,7 @@ private void checkProviders() {
// except for this method call-point.
for (final Iterator<PhaseContext<@NonNull ?>> iterator = this.phaseContextProviders.descendingIterator(); iterator.hasNext(); ) {
final PhaseContext<@NonNull ?> context = iterator.next();
final StackFrame frame = this.pushCauseFrame(); // these should auto close
final StackFrame frame = this.pushCauseFrame0(true); // these should auto close
context.getFrameModifier().accept(frame); // The frame will be auto closed by the phase context
}
// Clear the list since everything is now loaded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public final class SpongeCauseStackFrame implements CauseStackManager.StackFrame
private final Map<EventContextKey<?>, Object> storedContext;
int old_min_depth;
int lastCauseSize;
@Nullable PhaseContext<?> implicitContext = null;

@Nullable Exception stackDebug = null;

Expand All @@ -58,6 +59,7 @@ public void clear() {
this.storedContext.clear();
this.lastCauseSize = -1;
this.old_min_depth = -1;
this.implicitContext = null;
this.stackDebug = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class CommandPhaseContext extends GeneralPhaseContext<CommandPhaseContext

CommandPhaseContext(final IPhaseState<CommandPhaseContext> state, final PhaseTracker tracker) {
super(state, tracker);
this.requiresImplicitPhase(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.event.tracking.TrackingUtil;

import java.util.function.BiConsumer;

Expand All @@ -51,6 +52,6 @@ public BiConsumer<CauseStackManager.StackFrame, BasicPluginContext> getFrameModi

@Override
public void unwind(final BasicPluginContext phaseContext) {

TrackingUtil.processBlockCaptures(phaseContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static class Context extends PluginPhaseContext<Context> {

public Context(final IPhaseState<Context> phaseState, final PhaseTracker tracker) {
super(phaseState, tracker);
this.requiresImplicitPhase(false);
}

public Context container(final PluginContainer container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static final class State {
public static final IPhaseState<VolumeStreamApplicationState.Context> VOLUME_STREAM_APPLICATION = new VolumeStreamApplicationState();
public static final IPhaseState<BasicPluginContext> SCHEDULED_TASK = new ScheduledTaskPhaseState();
public static final IPhaseState<DelayedTaskPhaseState.Context> DELAYED_TASK = new DelayedTaskPhaseState();
public static final IPhaseState<BasicPluginContext> PLUGIN = new BasicPluginState();

private State() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class PluginPhaseContext<P extends PluginPhaseContext<P>> extends PhaseCo

protected PluginPhaseContext(final IPhaseState<P> phaseState, final PhaseTracker tracker) {
super(phaseState, tracker);
this.requiresImplicitPhase(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void readLegacyPlayerData(final ServerPlayer playerEntity, final Compound
}
}
playerEntity.offer(Keys.FIRST_DATE_JOINED, creation);
playerEntity.offer(Keys.LAST_DATE_JOINED, lastPlayed);
playerEntity.offer(Keys.LAST_DATE_PLAYED, lastPlayed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@
import org.spongepowered.common.profile.SpongeGameProfile;
import org.spongepowered.common.util.NetworkUtil;

import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
Expand Down Expand Up @@ -197,16 +194,6 @@ public TabList tabList() {
return ((ServerGamePacketListenerImplBridge) this.connection).bridge$tabList();
}

@Override
public boolean hasPlayedBefore() {
return this.get(Keys.FIRST_DATE_JOINED).map(instant -> {
final Instant toTheMinute = instant.truncatedTo(ChronoUnit.MINUTES);
final Instant now = Instant.now().truncatedTo(ChronoUnit.MINUTES);
final Duration timeSinceFirstJoined = Duration.of(now.minusMillis(toTheMinute.toEpochMilli()).toEpochMilli(), ChronoUnit.MINUTES);
return timeSinceFirstJoined.getSeconds() > 0;
}).orElse(false);
}

@Override
public boolean respawn() {
if (this.impl$isFake) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public String name() {
values.add(this.requireValue(Keys.WALKING_SPEED).asImmutable());

this.getValue(Keys.FIRST_DATE_JOINED).map(Value::asImmutable).ifPresent(values::add);
this.getValue(Keys.LAST_DATE_JOINED).map(Value::asImmutable).ifPresent(values::add);
this.getValue(Keys.LAST_DATE_PLAYED).map(Value::asImmutable).ifPresent(values::add);

return values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.level.storage.PrimaryLevelData;
import net.minecraft.world.ticks.SavedTick;
import net.minecraft.world.ticks.ScheduledTick;
import org.spongepowered.api.data.DataHolder;
Expand All @@ -58,7 +59,8 @@
MapItemSavedData.class,
LevelChunk.class,
ScheduledTick.class,
SavedTick.class})
SavedTick.class,
PrimaryLevelData.class})
public abstract class SpongeDataHolderMixin implements SpongeDataHolderBridge {

private DataManipulator.Mutable impl$manipulator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public abstract class PlayerListMixin implements PlayerListBridge {
if (compound.isEmpty()) {
final Instant now = Instant.now();
((ServerPlayer) playerIn).offer(Keys.FIRST_DATE_JOINED, now);
((ServerPlayer) playerIn).offer(Keys.LAST_DATE_PLAYED, now);
((ServerPlayer) playerIn).offer(Keys.LAST_DATE_JOINED, now);
}
return compound;
}
Expand Down Expand Up @@ -463,6 +463,7 @@ public abstract class PlayerListMixin implements PlayerListBridge {
private void impl$RemovePlayerReferenceFromScoreboard(final net.minecraft.server.level.ServerPlayer player, final CallbackInfo ci) {
((ServerScoreboardBridge) ((ServerPlayer) player).scoreboard()).bridge$removePlayer(player, false);
SpongeAdventure.forEachBossBar(bar -> bar.removePlayer(player));
((ServerPlayer) player).offer(Keys.LAST_DATE_PLAYED, Instant.now());
}

@Redirect(method = "addWorldborderListener",
Expand All @@ -488,7 +489,7 @@ public abstract class PlayerListMixin implements PlayerListBridge {
((SpongeServer) this.shadow$getServer()).getPlayerDataManager().readLegacyPlayerData((ServerPlayer) entity, compound, null);
}

((ServerPlayer) entity).offer(Keys.LAST_DATE_PLAYED, Instant.now());
((ServerPlayer) entity).offer(Keys.LAST_DATE_JOINED, Instant.now());
}

@Inject(method = "respawn",
Expand Down
Loading

0 comments on commit da6edb5

Please sign in to comment.