Skip to content

Commit 5510343

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

14 files changed

Lines changed: 114 additions & 2 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public Component getRichName(BlockType blockType) {
4545
return TextComponent.of(blockType.id());
4646
}
4747

48+
@Deprecated
4849
@Nullable
4950
@Override
5051
public BlockMaterial getMaterial(BlockType blockType) {
@@ -61,6 +62,21 @@ public BlockMaterial getMaterial(BlockType blockType) {
6162
});
6263
}
6364

65+
@Override
66+
public @Nullable BlockMaterial getMaterial(BlockState blockState) {
67+
Material mat = BukkitAdapter.adapt(blockState.getBlockType());
68+
if (mat == null) {
69+
return null;
70+
}
71+
return materialMap.computeIfAbsent(mat, material -> {
72+
BlockMaterial platformMaterial = null;
73+
if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) {
74+
platformMaterial = WorldEditPlugin.getInstance().getBukkitImplAdapter().getBlockMaterial(blockState);
75+
}
76+
return new BukkitBlockMaterial(platformMaterial, material);
77+
});
78+
}
79+
6480
@Nullable
6581
@Override
6682
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public Component getRichName(BlockType blockType) {
5151
return TranslatableComponent.of(platform.getAdapter().toNativeBlock(blockType).getDescriptionId());
5252
}
5353

54+
@Deprecated
5455
@Override
5556
public BlockMaterial getMaterial(BlockType blockType) {
5657
Block block = platform.getAdapter().toNativeBlock(blockType);
@@ -60,6 +61,14 @@ public BlockMaterial getMaterial(BlockType blockType) {
6061
);
6162
}
6263

64+
@Override
65+
public BlockMaterial getMaterial(BlockState blockState) {
66+
return materialMap.computeIfAbsent(
67+
platform.getAdapter().toNativeBlockState(blockState),
68+
CoreMcBlockMaterial::new
69+
);
70+
}
71+
6372
@Override
6473
public Map<String, ? extends Property<?>> getProperties(BlockType blockType) {
6574
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)