Skip to content

Commit 0cf9813

Browse files
authored
Reduce code duplication of PaperweightBlockMaterial (#3041)
1 parent 9f4be9e commit 0cf9813

File tree

13 files changed

+235
-482
lines changed

13 files changed

+235
-482
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,126 @@
11
package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2;
22

3+
import com.fastasyncworldedit.bukkit.adapter.BukkitBlockMaterial;
34
import com.fastasyncworldedit.core.nbt.FaweCompoundTag;
4-
import com.sk89q.util.ReflectionUtil;
5-
import com.sk89q.worldedit.bukkit.adapter.Refraction;
6-
import com.sk89q.worldedit.world.registry.BlockMaterial;
75
import net.minecraft.core.BlockPos;
86
import net.minecraft.world.level.EmptyBlockGetter;
97
import net.minecraft.world.level.block.Block;
108
import net.minecraft.world.level.block.EntityBlock;
11-
import net.minecraft.world.level.block.LiquidBlock;
129
import net.minecraft.world.level.block.entity.BlockEntity;
13-
import net.minecraft.world.level.block.state.BlockBehaviour;
1410
import net.minecraft.world.level.block.state.BlockState;
11+
import net.minecraft.world.level.material.Fluids;
1512
import net.minecraft.world.level.material.PushReaction;
1613
import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData;
1714

18-
import javax.annotation.Nullable;
15+
public class PaperweightBlockMaterial extends BukkitBlockMaterial<Block, BlockState> {
1916

20-
public class PaperweightBlockMaterial implements BlockMaterial {
21-
22-
private final Block block;
23-
private final BlockState blockState;
24-
private final boolean isTranslucent;
25-
private final CraftBlockData craftBlockData;
26-
private final org.bukkit.Material craftMaterial;
2717
private final int opacity;
28-
private final FaweCompoundTag tile;
2918

3019
public PaperweightBlockMaterial(Block block) {
3120
this(block, block.defaultBlockState());
3221
}
3322

3423
public PaperweightBlockMaterial(Block block, BlockState blockState) {
35-
this.block = block;
36-
this.blockState = blockState;
37-
this.craftBlockData = CraftBlockData.fromData(blockState);
38-
this.craftMaterial = craftBlockData.getMaterial();
39-
BlockBehaviour.Properties blockInfo = ReflectionUtil.getField(BlockBehaviour.class, block,
40-
Refraction.pickName("properties", "aN"));
41-
this.isTranslucent = !(boolean) ReflectionUtil.getField(BlockBehaviour.Properties.class, blockInfo,
42-
Refraction.pickName("canOcclude", "m")
43-
);
44-
opacity = blockState.getLightBlock(EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
45-
BlockEntity tileEntity = !(block instanceof EntityBlock) ? null : ((EntityBlock) block).newBlockEntity(
46-
BlockPos.ZERO,
47-
blockState
48-
);
49-
tile = tileEntity == null
50-
? null
51-
: PaperweightGetBlocks.NMS_TO_TILE.apply(tileEntity);
52-
}
53-
54-
public Block getBlock() {
55-
return block;
24+
super(block, blockState, CraftBlockData.fromData(blockState));
25+
this.opacity = blockState.getLightBlock(EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
5626
}
5727

58-
public BlockState getState() {
59-
return blockState;
60-
}
61-
62-
public CraftBlockData getCraftBlockData() {
63-
return craftBlockData;
28+
@Override
29+
protected FaweCompoundTag tileForBlock(final Block block) {
30+
BlockEntity tileEntity = !(block instanceof EntityBlock eb) ? null : eb.newBlockEntity(BlockPos.ZERO, this.blockState);
31+
return tileEntity == null ? null : PaperweightGetBlocks.NMS_TO_TILE.apply(tileEntity);
6432
}
6533

6634
@Override
6735
public boolean isAir() {
68-
return blockState.isAir();
36+
return this.blockState.isAir();
6937
}
7038

7139
@Override
7240
public boolean isFullCube() {
73-
return Block.isShapeFullBlock(blockState.getShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO));
41+
return Block.isShapeFullBlock(this.blockState.getShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO));
7442
}
7543

7644
@Override
7745
public boolean isOpaque() {
78-
return blockState.isOpaque();
46+
return this.blockState.canOcclude();
7947
}
8048

8149
@Override
8250
public boolean isPowerSource() {
83-
return blockState.isSignalSource();
51+
return this.blockState.isSignalSource();
8452
}
8553

8654
@Override
8755
public boolean isLiquid() {
88-
// TODO: Better check ?
89-
return block instanceof LiquidBlock;
56+
return !this.blockState.getFluidState().is(Fluids.EMPTY);
9057
}
9158

9259
@Override
9360
public boolean isSolid() {
94-
// TODO: Replace
95-
return blockState.isSolid();
61+
return this.blockState.isSolidRender(EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
9662
}
9763

9864
@Override
9965
public float getHardness() {
100-
return craftBlockData.getState().destroySpeed;
66+
return this.blockState.destroySpeed;
10167
}
10268

10369
@Override
10470
public float getResistance() {
105-
return block.getExplosionResistance();
71+
return this.block.getExplosionResistance();
10672
}
10773

10874
@Override
10975
public float getSlipperiness() {
110-
return block.getFriction();
76+
return this.block.getFriction();
11177
}
11278

11379
@Override
11480
public int getLightValue() {
115-
return blockState.getLightEmission();
81+
return this.blockState.getLightEmission();
11682
}
11783

11884
@Override
11985
public int getLightOpacity() {
120-
return opacity;
86+
return this.opacity;
12187
}
12288

12389
@Override
12490
public boolean isFragileWhenPushed() {
125-
return blockState.getPistonPushReaction() == PushReaction.DESTROY;
91+
return this.blockState.getPistonPushReaction() == PushReaction.DESTROY;
12692
}
12793

12894
@Override
12995
public boolean isUnpushable() {
130-
return blockState.getPistonPushReaction() == PushReaction.BLOCK;
96+
return this.blockState.getPistonPushReaction() == PushReaction.BLOCK;
13197
}
13298

13399
@Override
134100
public boolean isTicksRandomly() {
135-
return block.isRandomlyTicking(blockState);
101+
return this.blockState.isRandomlyTicking();
136102
}
137103

138104
@SuppressWarnings("deprecation")
139105
@Override
140106
public boolean isMovementBlocker() {
141-
return blockState.blocksMotion();
142-
}
143-
144-
@Override
145-
public boolean isBurnable() {
146-
return craftMaterial.isBurnable();
147-
}
148-
149-
@Override
150-
public boolean isToolRequired() {
151-
// Removed in 1.16.1, this is not present in higher versions
152-
return false;
107+
return this.blockState.blocksMotion();
153108
}
154109

155110
@Override
156111
public boolean isReplacedDuringPlacement() {
157-
return blockState.canBeReplaced();
112+
return this.blockState.canBeReplaced();
158113
}
159114

160115
@Override
161116
public boolean isTranslucent() {
162-
return isTranslucent;
163-
}
164-
165-
@Override
166-
public boolean hasContainer() {
167-
return block instanceof EntityBlock;
168-
}
169-
170-
@Override
171-
public boolean isTile() {
172-
return block instanceof EntityBlock;
173-
}
174-
175-
@Override
176-
public @Nullable FaweCompoundTag defaultTile() {
177-
return tile;
117+
return !this.blockState.canOcclude();
178118
}
179119

180120
@Override
181121
public int getMapColor() {
182122
// rgb field
183-
return block.defaultMapColor().col;
123+
return this.block.defaultMapColor().col;
184124
}
185125

186126
}

worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public Component getRichItemName(BaseItemStack itemStack) {
347347
@Override
348348
public OptionalInt getInternalBlockStateId(BlockState state) {
349349
PaperweightBlockMaterial material = (PaperweightBlockMaterial) state.getMaterial();
350-
net.minecraft.world.level.block.state.BlockState mcState = material.getCraftBlockData().getState();
350+
net.minecraft.world.level.block.state.BlockState mcState = material.getState();
351351
return OptionalInt.of(Block.BLOCK_STATE_REGISTRY.getId(mcState));
352352
}
353353

@@ -440,7 +440,7 @@ public int[] getOrdinalToIbdID() {
440440
@Override
441441
public <B extends BlockStateHolder<B>> BlockData adapt(B state) {
442442
PaperweightBlockMaterial material = (PaperweightBlockMaterial) state.getMaterial();
443-
return material.getCraftBlockData();
443+
return material.getBlockData();
444444
}
445445

446446
public net.minecraft.world.level.block.state.BlockState adapt(BlockState blockState) {

0 commit comments

Comments
 (0)