Skip to content

Commit a40ace1

Browse files
committed
Ãfeat: environmental attribute api
1 parent a9f79b1 commit a40ace1

File tree

17 files changed

+262
-19
lines changed

17 files changed

+262
-19
lines changed

paper-api/src/main/java/io/papermc/paper/InternalAPIBridge.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.destroystokyo.paper.SkinParts;
44
import io.papermc.paper.command.brigadier.CommandSourceStack;
55
import io.papermc.paper.datacomponent.item.ResolvableProfile;
6+
import io.papermc.paper.world.attribute.EnvironmentalAttributeContext;
67
import io.papermc.paper.world.damagesource.CombatEntry;
78
import io.papermc.paper.world.damagesource.FallLocationType;
89
import java.util.function.Function;
@@ -100,4 +101,6 @@ class Holder {
100101
Component defaultMannequinDescription();
101102

102103
<MODERN, LEGACY> GameRule<LEGACY> legacyGameRuleBridge(GameRule<MODERN> rule, Function<LEGACY, MODERN> fromLegacyToModern, Function<MODERN, LEGACY> toLegacyFromModern, Class<LEGACY> legacyClass);
104+
105+
EnvironmentalAttributeContext.Builder environmentalAttributeContextBuilder();
103106
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.papermc.paper.world.attribute;
2+
3+
import io.papermc.paper.math.Position;
4+
import org.jetbrains.annotations.ApiStatus;
5+
6+
@ApiStatus.Experimental
7+
@ApiStatus.NonExtendable
8+
public interface EnvironmentalAttribute<T> {
9+
10+
T getGlobal();
11+
12+
default T getPositioned(Position position) {
13+
return this.getValue(EnvironmentalAttributeContext.builder().position(position).build());
14+
}
15+
16+
default T getTimed(long time) {
17+
return this.getValue(EnvironmentalAttributeContext.builder().time(time).build());
18+
}
19+
20+
T getValue(EnvironmentalAttributeContext context);
21+
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.papermc.paper.world.attribute;
2+
3+
import io.papermc.paper.InternalAPIBridge;
4+
import io.papermc.paper.math.Position;
5+
import org.bukkit.WeatherType;
6+
import org.jetbrains.annotations.ApiStatus;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
@ApiStatus.Experimental
10+
@ApiStatus.NonExtendable
11+
public interface EnvironmentalAttributeContext {
12+
13+
@Nullable Long time();
14+
15+
@Nullable Position position();
16+
17+
@Nullable Float rainLevel();
18+
19+
@Nullable Float thunderLevel();
20+
21+
static Builder builder() {
22+
return InternalAPIBridge.get().environmentalAttributeContextBuilder();
23+
}
24+
25+
@ApiStatus.NonExtendable
26+
interface Builder {
27+
28+
Builder time(@Nullable Long time);
29+
30+
Builder position(@Nullable Position position);
31+
32+
Builder rainLevel(@Nullable Float rainLevel);
33+
34+
Builder raining(boolean raining);
35+
36+
Builder thunderLevel(@Nullable Float thunderLevel);
37+
38+
Builder thundering(boolean thundering);
39+
40+
EnvironmentalAttributeContext build();
41+
42+
}
43+
}

paper-api/src/main/java/io/papermc/paper/world/attribute/EnvironmentalAttributeType.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import org.bukkit.Keyed;
44
import org.jetbrains.annotations.ApiStatus;
5-
import org.jspecify.annotations.NullMarked;
65

7-
@NullMarked
86
@ApiStatus.Experimental
97
@ApiStatus.NonExtendable
108
public interface EnvironmentalAttributeType<T> extends Keyed {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package io.papermc.paper.world.attribute;
3+
4+
import org.jspecify.annotations.NullMarked;

paper-api/src/main/java/org/bukkit/World.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.Set;
1212
import java.util.function.Consumer;
1313
import java.util.function.Predicate;
14+
import io.papermc.paper.world.attribute.EnvironmentalAttribute;
15+
import io.papermc.paper.world.attribute.EnvironmentalAttributeType;
1416
import org.bukkit.block.Biome;
1517
import org.bukkit.block.Block;
1618
import org.bukkit.block.data.BlockData;
@@ -4592,6 +4594,8 @@ default void setNoTickViewDistance(int viewDistance) {
45924594
@NotNull
45934595
public Collection<GeneratedStructure> getStructures(int x, int z, @NotNull Structure structure);
45944596

4597+
@NotNull <T> EnvironmentalAttribute<T> getEnvironmentalAttribute(@NotNull EnvironmentalAttributeType<T> type);
4598+
45954599
/**
45964600
* Represents various map environment types that a world may be
45974601
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--- a/net/minecraft/world/attribute/WeatherAttributes.java
2+
+++ b/net/minecraft/world/attribute/WeatherAttributes.java
3+
@@ -69,12 +_,12 @@
4+
return new WeatherAttributes.WeatherAccess() {
5+
@Override
6+
public float rainLevel() {
7+
- return level.getRainLevel(1.0F);
8+
+ return io.papermc.paper.world.attribute.PaperEnvironmentalAttributeContext.CURRENT_CONTEXT.get().rainLevel(() -> level.getRainLevel(1.0F)); // Paper - Environmental Attribute API
9+
}
10+
11+
@Override
12+
public float thunderLevel() {
13+
- return level.getThunderLevel(1.0F);
14+
+ return io.papermc.paper.world.attribute.PaperEnvironmentalAttributeContext.CURRENT_CONTEXT.get().thunderLevel(() -> level.getThunderLevel(1.0F)); // Paper - Environmental Attribute API
15+
}
16+
};
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- a/net/minecraft/world/timeline/AttributeTrackSampler.java
2+
+++ b/net/minecraft/world/timeline/AttributeTrackSampler.java
3+
@@ -32,7 +_,7 @@
4+
public Value applyTimeBased(Value baseValue, int cacheTickId) {
5+
if (this.cachedArgument == null || cacheTickId != this.cachedTickId) {
6+
this.cachedTickId = cacheTickId;
7+
- this.cachedArgument = this.argumentSampler.sample(this.dayTimeGetter.getAsLong());
8+
+ this.cachedArgument = this.argumentSampler.sample(io.papermc.paper.world.attribute.PaperEnvironmentalAttributeContext.CURRENT_CONTEXT.get().time(this.dayTimeGetter)); // Paper - Environmental Attribute API
9+
}
10+
11+
return this.modifier.apply(baseValue, this.cachedArgument);

paper-server/src/main/java/io/papermc/paper/PaperServerInternalAPIBridge.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import io.papermc.paper.command.brigadier.CommandSourceStack;
77
import io.papermc.paper.datacomponent.item.PaperResolvableProfile;
88
import io.papermc.paper.datacomponent.item.ResolvableProfile;
9+
import io.papermc.paper.world.attribute.EnvironmentalAttributeContext;
10+
import io.papermc.paper.world.attribute.PaperEnvironmentalAttributeContext;
911
import io.papermc.paper.world.damagesource.CombatEntry;
1012
import io.papermc.paper.world.damagesource.FallLocationType;
1113
import io.papermc.paper.world.damagesource.PaperCombatEntryWrapper;
@@ -116,4 +118,9 @@ public Component defaultMannequinDescription() {
116118
public <MODERN, LEGACY> GameRule<LEGACY> legacyGameRuleBridge(GameRule<MODERN> rule, Function<LEGACY, MODERN> fromLegacyToModern, Function<MODERN, LEGACY> toLegacyFromModern, Class<LEGACY> legacyClass) {
117119
return CraftGameRule.wrap(rule, fromLegacyToModern, toLegacyFromModern, legacyClass);
118120
}
121+
122+
@Override
123+
public EnvironmentalAttributeContext.Builder environmentalAttributeContextBuilder() {
124+
return new PaperEnvironmentalAttributeContext.PaperBuilder();
125+
}
119126
}

paper-server/src/main/java/io/papermc/paper/datacomponent/PaperDataComponentType.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
import static io.papermc.paper.util.MCUtil.transformUnmodifiable;
9292

93-
public abstract class PaperDataComponentType<T, NMS> extends HolderableBase<net.minecraft.core.component.DataComponentType<NMS>> implements DataComponentType {
93+
public abstract class PaperDataComponentType<API, NMS> extends HolderableBase<net.minecraft.core.component.DataComponentType<NMS>> implements DataComponentType {
9494

9595
private static final PaperTypedDataAdapters ADAPTERS = PaperTypedDataAdapters.create(
9696
BuiltInRegistries.DATA_COMPONENT_TYPE,
@@ -231,9 +231,9 @@ public static Set<DataComponentType> minecraftToBukkit(final Set<net.minecraft.c
231231
return type.getAdapter().fromVanilla(nmsValue);
232232
}
233233

234-
private final PaperTypedDataAdapter<NMS, T> adapter;
234+
private final PaperTypedDataAdapter<API, NMS> adapter;
235235

236-
private PaperDataComponentType(final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder, final PaperTypedDataAdapter<NMS, T> adapter) {
236+
private PaperDataComponentType(final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder, final PaperTypedDataAdapter<API, NMS> adapter) {
237237
super(holder);
238238
this.adapter = adapter;
239239
}
@@ -243,13 +243,13 @@ public boolean isPersistent() {
243243
return !this.getHandle().isTransient();
244244
}
245245

246-
public PaperTypedDataAdapter<NMS, T> getAdapter() {
246+
public PaperTypedDataAdapter<API, NMS> getAdapter() {
247247
return this.adapter;
248248
}
249249

250250
@SuppressWarnings("unchecked")
251251
public static <NMS> DataComponentType of(final Holder<?> holder) {
252-
final PaperTypedDataAdapter< NMS, ?> adapter = PaperDataComponentType.ADAPTERS.getAdapter(holder.unwrapKey().orElseThrow());
252+
final PaperTypedDataAdapter<?, NMS> adapter = PaperDataComponentType.ADAPTERS.getAdapter(holder.unwrapKey().orElseThrow());
253253
if (adapter == null) {
254254
throw new IllegalArgumentException("No adapter found for " + holder);
255255
}
@@ -262,31 +262,31 @@ public static <NMS> DataComponentType of(final Holder<?> holder) {
262262
}
263263
}
264264

265-
public static final class NonValuedImpl<T, NMS> extends PaperDataComponentType<T, NMS> implements NonValued {
265+
public static final class NonValuedImpl<API, NMS> extends PaperDataComponentType<API, NMS> implements NonValued {
266266

267267
NonValuedImpl(
268268
final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder,
269-
final PaperTypedDataAdapter<NMS, T> adapter
269+
final PaperTypedDataAdapter<API, NMS> adapter
270270
) {
271271
super(holder, adapter);
272272
}
273273
}
274274

275-
public static final class ValuedImpl<T, NMS> extends PaperDataComponentType<T, NMS> implements Valued<T> {
275+
public static final class ValuedImpl<API, NMS> extends PaperDataComponentType<API, NMS> implements Valued<API> {
276276

277277
ValuedImpl(
278278
final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder,
279-
final PaperTypedDataAdapter<NMS, T> adapter
279+
final PaperTypedDataAdapter<API, NMS> adapter
280280
) {
281281
super(holder, adapter);
282282
}
283283
}
284284

285-
public static final class Unimplemented<T, NMS> extends PaperDataComponentType<T, NMS> {
285+
public static final class Unimplemented<API, NMS> extends PaperDataComponentType<API, NMS> {
286286

287287
public Unimplemented(
288288
final Holder<net.minecraft.core.component.DataComponentType<NMS>> holder,
289-
final PaperTypedDataAdapter<NMS, T> adapter
289+
final PaperTypedDataAdapter<API, NMS> adapter
290290
) {
291291
super(holder, adapter);
292292
}

0 commit comments

Comments
 (0)