Skip to content

Commit c7b06ac

Browse files
committed
Add BlockState.getMaterial()
1 parent 6118167 commit c7b06ac

14 files changed

Lines changed: 124 additions & 0 deletions

File tree

worldedit-bukkit/adapters/adapter-1.21.11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_11/PaperweightAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
import org.enginehub.linbus.tree.LinStringTag;
176176
import org.enginehub.linbus.tree.LinTag;
177177
import org.enginehub.linbus.tree.LinTagType;
178+
import org.jspecify.annotations.NonNull;
178179
import org.spigotmc.SpigotConfig;
179180
import org.spigotmc.WatchdogThread;
180181

@@ -578,6 +579,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
578579
return new PaperweightBlockMaterial(mcBlockState);
579580
}
580581

582+
@Override
583+
public @NonNull BlockMaterial getBlockMaterial(BlockState blockState) {
584+
return new PaperweightBlockMaterial(adapt(blockState));
585+
}
586+
581587
@SuppressWarnings({ "unchecked", "rawtypes" })
582588
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
583589
@Override

worldedit-bukkit/adapters/adapter-1.21.4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_4/PaperweightAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
import org.enginehub.linbus.tree.LinStringTag;
173173
import org.enginehub.linbus.tree.LinTag;
174174
import org.enginehub.linbus.tree.LinTagType;
175+
import org.jspecify.annotations.NonNull;
175176
import org.spigotmc.SpigotConfig;
176177
import org.spigotmc.WatchdogThread;
177178

@@ -567,6 +568,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
567568
return new PaperweightBlockMaterial(mcBlockState);
568569
}
569570

571+
@Override
572+
public @NonNull BlockMaterial getBlockMaterial(BlockState blockState) {
573+
return new PaperweightBlockMaterial(adapt(blockState));
574+
}
575+
570576
@SuppressWarnings({ "unchecked", "rawtypes" })
571577
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
572578
@Override

worldedit-bukkit/adapters/adapter-1.21.5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_5/PaperweightAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
import org.enginehub.linbus.tree.LinStringTag;
173173
import org.enginehub.linbus.tree.LinTag;
174174
import org.enginehub.linbus.tree.LinTagType;
175+
import org.jspecify.annotations.NonNull;
175176
import org.spigotmc.SpigotConfig;
176177
import org.spigotmc.WatchdogThread;
177178

@@ -565,6 +566,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
565566
return new PaperweightBlockMaterial(mcBlockState);
566567
}
567568

569+
@Override
570+
public @NonNull BlockMaterial getBlockMaterial(BlockState blockState) {
571+
return new PaperweightBlockMaterial(adapt(blockState));
572+
}
573+
568574
@SuppressWarnings({ "unchecked", "rawtypes" })
569575
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
570576
@Override

worldedit-bukkit/adapters/adapter-1.21.6/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_6/PaperweightAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
import org.enginehub.linbus.tree.LinStringTag;
177177
import org.enginehub.linbus.tree.LinTag;
178178
import org.enginehub.linbus.tree.LinTagType;
179+
import org.jspecify.annotations.NonNull;
179180
import org.spigotmc.SpigotConfig;
180181
import org.spigotmc.WatchdogThread;
181182

@@ -579,6 +580,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
579580
return new PaperweightBlockMaterial(mcBlockState);
580581
}
581582

583+
@Override
584+
public @NonNull BlockMaterial getBlockMaterial(BlockState blockState) {
585+
return new PaperweightBlockMaterial(adapt(blockState));
586+
}
587+
582588
@SuppressWarnings({ "unchecked", "rawtypes" })
583589
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
584590
@Override

worldedit-bukkit/adapters/adapter-1.21.9/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_9/PaperweightAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
import org.enginehub.linbus.tree.LinStringTag;
176176
import org.enginehub.linbus.tree.LinTag;
177177
import org.enginehub.linbus.tree.LinTagType;
178+
import org.jspecify.annotations.NonNull;
178179
import org.spigotmc.SpigotConfig;
179180
import org.spigotmc.WatchdogThread;
180181

@@ -578,6 +579,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
578579
return new PaperweightBlockMaterial(mcBlockState);
579580
}
580581

582+
@Override
583+
public @NonNull BlockMaterial getBlockMaterial(BlockState blockState) {
584+
return new PaperweightBlockMaterial(adapt(blockState));
585+
}
586+
581587
@SuppressWarnings({ "unchecked", "rawtypes" })
582588
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
583589
@Override

worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ public BlockMaterial getMaterial(BlockType blockType) {
6161
});
6262
}
6363

64+
@Override
65+
public @org.jspecify.annotations.Nullable BlockMaterial getMaterial(BlockState blockState) {
66+
Material mat = BukkitAdapter.adapt(blockState.getBlockType());
67+
if (mat == null) {
68+
return null;
69+
}
70+
return materialMap.computeIfAbsent(mat, material -> {
71+
BlockMaterial platformMaterial = null;
72+
if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) {
73+
platformMaterial = WorldEditPlugin.getInstance().getBukkitImplAdapter().getBlockMaterial(blockState);
74+
}
75+
return new BukkitBlockMaterial(platformMaterial, material);
76+
});
77+
}
78+
6479
@Nullable
6580
@Override
6681
public Map<String, ? extends Property<?>> getProperties(BlockType blockType) {

worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ default void tickWatchdog() {
161161
@Nullable
162162
BlockMaterial getBlockMaterial(BlockType blockType);
163163

164+
/**
165+
* Gets the block material for the given block state.
166+
*
167+
* @param blockState the block state
168+
* @return the material
169+
*/
170+
default @org.jspecify.annotations.Nullable BlockMaterial getBlockMaterial(BlockState blockState) {
171+
return getBlockMaterial(blockState.getBlockType());
172+
}
173+
164174
/**
165175
* Get a map of {@code string -> property}.
166176
*

worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.sk89q.worldedit.blocks.TileEntityBlock;
2525
import com.sk89q.worldedit.registry.state.Property;
2626
import com.sk89q.worldedit.util.concurrency.LazyReference;
27+
import com.sk89q.worldedit.world.registry.BlockMaterial;
2728
import org.enginehub.linbus.format.snbt.LinStringIO;
2829
import org.enginehub.linbus.tree.LinCompoundTag;
2930
import org.enginehub.linbus.tree.LinStringTag;
@@ -104,6 +105,11 @@ public BlockType getBlockType() {
104105
return this.blockState.getBlockType();
105106
}
106107

108+
@Override
109+
public BlockMaterial getMaterial() {
110+
return blockState.getMaterial();
111+
}
112+
107113
@Override
108114
public <V> BaseBlock with(Property<V> property, V value) {
109115
return this.blockState.with(property, value).toBaseBlock(getNbtReference());

worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
package com.sk89q.worldedit.world.block;
2121

22+
import com.sk89q.worldedit.WorldEdit;
23+
import com.sk89q.worldedit.extension.platform.Capability;
2224
import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
2325
import com.sk89q.worldedit.registry.state.Property;
2426
import com.sk89q.worldedit.util.concurrency.LazyReference;
27+
import com.sk89q.worldedit.world.registry.BlockMaterial;
2528
import org.enginehub.linbus.tree.LinCompoundTag;
2629

2730
import java.util.HashSet;
@@ -74,6 +77,12 @@ public BlockType getBlockType() {
7477
return this.blockType;
7578
}
7679

80+
@Override
81+
public BlockMaterial getMaterial() {
82+
return WorldEdit.getInstance().getPlatformManager()
83+
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getMaterial(this);
84+
}
85+
7786
@Override
7887
public <V> BlockState with(final Property<V> property, final V value) {
7988
if (this.stateListIndex == -1) {

worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.sk89q.worldedit.math.BlockVector3;
2727
import com.sk89q.worldedit.registry.state.Property;
2828
import com.sk89q.worldedit.util.concurrency.LazyReference;
29+
import com.sk89q.worldedit.world.registry.BlockMaterial;
2930
import org.enginehub.linbus.tree.LinCompoundTag;
3031

3132
import java.util.Locale;
@@ -41,6 +42,15 @@ public interface BlockStateHolder<B extends BlockStateHolder<B>> extends Pattern
4142
*/
4243
BlockType getBlockType();
4344

45+
/**
46+
* Get the block material.
47+
*
48+
* @return The material
49+
*/
50+
default BlockMaterial getMaterial() {
51+
return getBlockType().getMaterial();
52+
}
53+
4454
/**
4555
* Returns a BlockState with the given state and value applied.
4656
*

0 commit comments

Comments
 (0)