Skip to content

Commit c39a37e

Browse files
committed
feat: added Block#getAttributeValue(..) and optimized positioned lookups
1 parent a40ace1 commit c39a37e

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ public interface EnvironmentalAttribute<T> {
99

1010
T getGlobal();
1111

12-
default T getPositioned(Position position) {
13-
return this.getValue(EnvironmentalAttributeContext.builder().position(position).build());
14-
}
12+
T getPositioned(Position position);
1513

1614
default T getTimed(long time) {
1715
return this.getValue(EnvironmentalAttributeContext.builder().time(time).build());

paper-api/src/main/java/org/bukkit/block/Block.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bukkit.block;
22

3+
import io.papermc.paper.world.attribute.EnvironmentalAttributeType;
34
import java.util.Collection;
45
import org.bukkit.Chunk;
56
import org.bukkit.FluidCollisionMode;
@@ -834,4 +835,6 @@ default float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnch
834835
* @return {@code true} if the block can suffocate
835836
*/
836837
boolean isSuffocating();
838+
839+
<T> @NotNull T getAttributeValue(@NotNull EnvironmentalAttributeType<T> type);
837840
}

paper-server/src/main/java/io/papermc/paper/world/attribute/PaperEnvironmentalAttribute.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.papermc.paper.world.attribute;
22

3+
import io.papermc.paper.math.Position;
34
import io.papermc.paper.registry.data.typed.PaperTypedDataAdapter;
45
import io.papermc.paper.util.MCUtil;
6+
import net.minecraft.core.BlockPos;
57
import net.minecraft.world.attribute.EnvironmentAttributeSystem;
68

79
public class PaperEnvironmentalAttribute<API, NMS> implements EnvironmentalAttribute<API> {
@@ -21,11 +23,31 @@ public API getGlobal() {
2123
return this.adapter.fromVanilla(this.attributeSystem.getDimensionValue(this.type.getHandle()));
2224
}
2325

26+
@Override
27+
public API getPositioned(final Position position) {
28+
return this.adapter.fromVanilla(this.attributeSystem.getValue(this.type.getHandle(), MCUtil.toVec3(position)));
29+
}
30+
31+
public API getPositioned(final BlockPos blockPos) {
32+
return this.adapter.fromVanilla(this.attributeSystem.getValue(this.type.getHandle(), blockPos));
33+
}
34+
2435
@Override
2536
public API getValue(final EnvironmentalAttributeContext context) {
37+
if (context.equals(PaperEnvironmentalAttributeContext.EMPTY)) {
38+
// No field is set, return the global value to prevent invalidating cache
39+
return this.getGlobal();
40+
}
41+
42+
Position position = context.position();
43+
if (position != null && context.time() == null && context.rainLevel() == null && context.thunderLevel() == null) {
44+
// Only position is set, return cached positioned value
45+
return this.getPositioned(position);
46+
}
47+
2648
PaperEnvironmentalAttributeContext.CURRENT_CONTEXT.set((PaperEnvironmentalAttributeContext) context);
2749
this.attributeSystem.invalidateTickCache(); // Invalidate cache, otherwise it would return the cached value if it was already requested in the same tick
28-
API value = context.position() == null ? this.getGlobal() : this.adapter.fromVanilla(this.attributeSystem.getValue(this.type.getHandle(), MCUtil.toVec3(context.position())));
50+
API value = position == null ? this.getGlobal() : this.adapter.fromVanilla(this.attributeSystem.getValue(this.type.getHandle(), MCUtil.toVec3(position)));
2951
PaperEnvironmentalAttributeContext.CURRENT_CONTEXT.set(PaperEnvironmentalAttributeContext.EMPTY);
3052
return value;
3153
}

paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ public net.kyori.adventure.pointer.Pointers pointers() {
20552055
// Paper end
20562056

20572057
@Override
2058-
public @NotNull <T> EnvironmentalAttribute<T> getEnvironmentalAttribute(@NotNull final EnvironmentalAttributeType<T> type) {
2058+
public @NotNull <T> PaperEnvironmentalAttribute<T, ?> getEnvironmentalAttribute(@NotNull final EnvironmentalAttributeType<T> type) {
20592059
return new PaperEnvironmentalAttribute<>(this.getHandle().environmentAttributes(), (PaperEnvironmentalAttributeType<T, ?>) type);
20602060
}
20612061
}

paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Collections;
77
import java.util.List;
88
import java.util.stream.Collectors;
9+
import io.papermc.paper.world.attribute.EnvironmentalAttributeType;
910
import net.minecraft.core.BlockPos;
1011
import net.minecraft.core.Direction;
1112
import net.minecraft.server.level.ServerLevel;
@@ -60,6 +61,7 @@
6061
import org.bukkit.util.BoundingBox;
6162
import org.bukkit.util.RayTraceResult;
6263
import org.bukkit.util.Vector;
64+
import org.jetbrains.annotations.NotNull;
6365

6466
public class CraftBlock implements Block {
6567
private final net.minecraft.world.level.LevelAccessor world;
@@ -746,4 +748,9 @@ public void randomTick() {
746748
this.getNMS().randomTick(level, this.position, level.random);
747749
}
748750
// Paper end
751+
752+
@Override
753+
public <T> @NotNull T getAttributeValue(@NotNull final EnvironmentalAttributeType<T> type) {
754+
return this.getCraftWorld().getEnvironmentalAttribute(type).getPositioned(this.getPosition());
755+
}
749756
}

0 commit comments

Comments
 (0)