Skip to content

Commit 16a63b6

Browse files
committed
Add BlockState.getMaterial()
1 parent 59f7f46 commit 16a63b6

12 files changed

Lines changed: 104 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
586586
return new PaperweightBlockMaterial(mcBlockState);
587587
}
588588

589+
@Override
590+
public BlockMaterial getBlockMaterial(BlockState blockState) {
591+
return new PaperweightBlockMaterial(adapt(blockState));
592+
}
593+
589594
@SuppressWarnings({ "unchecked", "rawtypes" })
590595
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
591596
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
567567
return new PaperweightBlockMaterial(mcBlockState);
568568
}
569569

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
565565
return new PaperweightBlockMaterial(mcBlockState);
566566
}
567567

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
587587
return new PaperweightBlockMaterial(mcBlockState);
588588
}
589589

590+
@Override
591+
public BlockMaterial getBlockMaterial(BlockState blockState) {
592+
return new PaperweightBlockMaterial(adapt(blockState));
593+
}
594+
590595
@SuppressWarnings({ "unchecked", "rawtypes" })
591596
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
592597
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
586586
return new PaperweightBlockMaterial(mcBlockState);
587587
}
588588

589+
@Override
590+
public BlockMaterial getBlockMaterial(BlockState blockState) {
591+
return new PaperweightBlockMaterial(adapt(blockState));
592+
}
593+
589594
@SuppressWarnings({ "unchecked", "rawtypes" })
590595
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
591596
@Override

worldedit-bukkit/adapters/adapter-26.1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v26_1/PaperweightAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ public BlockMaterial getBlockMaterial(BlockType blockType) {
603603
return new PaperweightBlockMaterial(mcBlockState);
604604
}
605605

606+
@Override
607+
public BlockMaterial getBlockMaterial(BlockState blockState) {
608+
return new PaperweightBlockMaterial(adapt(blockState));
609+
}
610+
606611
@SuppressWarnings({ "unchecked", "rawtypes" })
607612
private static final LoadingCache<net.minecraft.world.level.block.state.properties.Property, Property<?>> PROPERTY_CACHE = CacheBuilder.newBuilder().build(new CacheLoader<>() {
608613
@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 @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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ 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+
@Nullable
171+
default BlockMaterial getBlockMaterial(BlockState blockState) {
172+
return getBlockMaterial(blockState.getBlockType());
173+
}
174+
164175
/**
165176
* Get a map of {@code string -> property}.
166177
*

worldedit-core-mc/src/main/java/com/sk89q/worldedit/coremc/internal/CoreMcBlockRegistry.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public BlockMaterial getMaterial(BlockType blockType) {
6060
);
6161
}
6262

63+
@Override
64+
public BlockMaterial getMaterial(BlockState blockState) {
65+
return materialMap.computeIfAbsent(
66+
platform.getAdapter().toNativeBlockState(blockState),
67+
CoreMcBlockMaterial::new
68+
);
69+
}
70+
6371
@Override
6472
public Map<String, ? extends Property<?>> getProperties(BlockType blockType) {
6573
Block block = platform.getAdapter().toNativeBlock(blockType);

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

Lines changed: 8 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,11 @@ public BlockType getBlockType() {
7477
return this.blockType;
7578
}
7679

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

0 commit comments

Comments
 (0)