Skip to content

Commit 444bd8d

Browse files
committed
feat(register): 添加方块硬度、阻力和摩擦力属性支持
为ModBlocks.java中的方块注册添加了硬度、阻力和摩擦力参数, 通过VoxelBlockPropertiesFactory.create方法创建方块属性时包含这些新属性。 同时更新了BlockIndexData类以支持从JSON配置中解析和存储方块的强度属性 (strength: hardness, resistance)和摩擦力(friction)值,并将这些属性应用到 方块行为属性中。
1 parent 630767a commit 444bd8d

File tree

5 files changed

+2879
-779
lines changed

5 files changed

+2879
-779
lines changed

src/main/java/com/box3lab/register/ModBlocks.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ public static void initialize() {
4343

4444
boolean solid = BlockIndexUtil.isSolid(id);
4545
boolean transparent = !solid;
46-
var props = VoxelBlockPropertiesFactory.create(solid, soundType, lightLevel);
46+
47+
int index = data.indexById.get(id);
48+
float hardness = data.blockHardness[index];
49+
float resistance = data.blockResistance[index];
50+
float friction = data.blockFriction[index];
51+
52+
var props = VoxelBlockPropertiesFactory.create(solid, soundType, lightLevel, hardness, resistance,
53+
friction);
4754

4855
Block block = BlockRegistrar.register(
4956
Box3.MOD_ID,

src/main/java/com/box3lab/register/voxel/VoxelBlockPropertiesFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ public final class VoxelBlockPropertiesFactory {
88
private VoxelBlockPropertiesFactory() {
99
}
1010

11-
public static BlockBehaviour.Properties create(boolean solid, SoundType soundType, int lightLevel) {
11+
public static BlockBehaviour.Properties create(boolean solid, SoundType soundType, int lightLevel, float hardness,
12+
float resistance, float friction) {
1213
BlockBehaviour.Properties props = BlockBehaviour.Properties.of()
1314
.sound(soundType)
1415
.mapColor(MapColor.COLOR_CYAN)
15-
.lightLevel(state -> lightLevel);
16+
.lightLevel(state -> lightLevel)
17+
.strength(hardness, resistance)
18+
.friction(friction);
1619

1720
if (!solid) {
1821
props = props.noOcclusion();

src/main/java/com/box3lab/util/BlockIndexData.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public FluidInfo(int id, int mass, long info, double fluidExtinction) {
3333
public final int[] ids;
3434
public final String[] names;
3535
public final int[] emissive;
36+
public final float[] blockHardness;
37+
public final float[] blockResistance;
38+
public final float[] blockFriction;
3639
public final Map<String, String> categoryByName;
3740
public final Set<Integer> notSolidIds;
3841
public final Map<Integer, FluidInfo> fluidsById;
@@ -44,13 +47,18 @@ private BlockIndexData(
4447
int[] ids,
4548
String[] names,
4649
int[] emissive,
50+
float[] blockHardness,
51+
float[] blockResistance,
52+
float[] blockFriction,
4753
Map<String, String> categoryByName,
4854
Set<Integer> notSolidIds,
49-
Map<Integer, FluidInfo> fluidsById
50-
) {
55+
Map<Integer, FluidInfo> fluidsById) {
5156
this.ids = ids;
5257
this.names = names;
5358
this.emissive = emissive;
59+
this.blockHardness = blockHardness;
60+
this.blockResistance = blockResistance;
61+
this.blockFriction = blockFriction;
5462
this.categoryByName = categoryByName;
5563
this.notSolidIds = notSolidIds;
5664
this.fluidsById = fluidsById;
@@ -107,8 +115,13 @@ final class Entry {
107115
final int fluidG;
108116
final int fluidB;
109117
final double fluidExtinction;
118+
final float hardness;
119+
final float resistance;
120+
final float friction;
110121

111-
Entry(String name, int id, int emissive, String category, boolean transparent, boolean fluid, int mass, int fluidR, int fluidG, int fluidB, double fluidExtinction) {
122+
Entry(String name, int id, int emissive, String category, boolean transparent, boolean fluid, int mass,
123+
int fluidR, int fluidG, int fluidB, double fluidExtinction, float hardness, float resistance,
124+
float friction) {
112125
this.name = name;
113126
this.id = id;
114127
this.emissive = emissive;
@@ -120,6 +133,9 @@ final class Entry {
120133
this.fluidG = fluidG;
121134
this.fluidB = fluidB;
122135
this.fluidExtinction = fluidExtinction;
136+
this.hardness = hardness;
137+
this.resistance = resistance;
138+
this.friction = friction;
123139
}
124140
}
125141

@@ -135,7 +151,8 @@ final class Entry {
135151
int mass = obj.has("mass") ? obj.get("mass").getAsInt() : 0;
136152

137153
int emissivePacked = 0;
138-
if (obj.has("emissive") && obj.get("emissive").isJsonArray() && obj.getAsJsonArray("emissive").size() >= 3) {
154+
if (obj.has("emissive") && obj.get("emissive").isJsonArray()
155+
&& obj.getAsJsonArray("emissive").size() >= 3) {
139156
double er = obj.getAsJsonArray("emissive").get(0).getAsDouble();
140157
double eg = obj.getAsJsonArray("emissive").get(1).getAsDouble();
141158
double eb = obj.getAsJsonArray("emissive").get(2).getAsDouble();
@@ -144,7 +161,8 @@ final class Entry {
144161
}
145162

146163
int fr = 0, fg = 0, fb = 0;
147-
if (obj.has("fluidColor") && obj.get("fluidColor").isJsonArray() && obj.getAsJsonArray("fluidColor").size() >= 3) {
164+
if (obj.has("fluidColor") && obj.get("fluidColor").isJsonArray()
165+
&& obj.getAsJsonArray("fluidColor").size() >= 3) {
148166
double r = obj.getAsJsonArray("fluidColor").get(0).getAsDouble();
149167
double g = obj.getAsJsonArray("fluidColor").get(1).getAsDouble();
150168
double b = obj.getAsJsonArray("fluidColor").get(2).getAsDouble();
@@ -163,8 +181,25 @@ final class Entry {
163181
fluidExtinction = obj.get("fluidExtinction").getAsDouble();
164182
}
165183

184+
// Parse strength values
185+
float hardness = 1.0f; // default hardness
186+
float resistance = 1.0f; // default resistance
187+
188+
if (obj.has("strength") && obj.get("strength").isJsonObject()) {
189+
JsonObject strengthObj = obj.get("strength").getAsJsonObject();
190+
hardness = strengthObj.has("hardness") ? strengthObj.get("hardness").getAsFloat() : 1.0f;
191+
resistance = strengthObj.has("resistance") ? strengthObj.get("resistance").getAsFloat() : 1.0f;
192+
}
193+
194+
// Parse friction value (default 1.0 if missing)
195+
float friction = 1.0f;
196+
if (obj.has("friction")) {
197+
friction = obj.get("friction").getAsFloat();
198+
}
199+
166200
if (id >= 0) {
167-
entries.add(new Entry(name, id, emissivePacked, category, transparent, fluid, mass, fr, fg, fb, fluidExtinction));
201+
entries.add(new Entry(name, id, emissivePacked, category, transparent, fluid, mass, fr, fg, fb,
202+
fluidExtinction, hardness, resistance, friction));
168203
}
169204
}
170205

@@ -173,6 +208,9 @@ final class Entry {
173208
int[] ids = new int[entries.size()];
174209
String[] names = new String[entries.size()];
175210
int[] emissive = new int[entries.size()];
211+
float[] blockHardness = new float[entries.size()];
212+
float[] blockResistance = new float[entries.size()];
213+
float[] blockFriction = new float[entries.size()];
176214
Map<String, String> categoryByName = new HashMap<>(entries.size() * 2);
177215

178216
Set<Integer> notSolidSet = new HashSet<>();
@@ -183,18 +221,23 @@ final class Entry {
183221
ids[i] = en.id;
184222
names[i] = en.name;
185223
emissive[i] = en.emissive;
224+
blockHardness[i] = en.hardness;
225+
blockResistance[i] = en.resistance;
226+
blockFriction[i] = en.friction;
186227
categoryByName.put(en.name.toLowerCase(Locale.ROOT), en.category == null ? "" : en.category);
187228

188229
if (en.transparent || en.fluid) {
189230
notSolidSet.add(en.id);
190231
}
191232
if (en.fluid) {
192233
int a = 255;
193-
long info = (en.fluidR & 255L) | ((en.fluidG & 255L) << 8) | ((en.fluidB & 255L) << 16) | ((a & 255L) << 24);
234+
long info = (en.fluidR & 255L) | ((en.fluidG & 255L) << 8) | ((en.fluidB & 255L) << 16)
235+
| ((a & 255L) << 24);
194236
fluidsById.put(en.id, new FluidInfo(en.id, en.mass, info, en.fluidExtinction));
195237
}
196238
}
197239

198-
return new BlockIndexData(ids, names, emissive, categoryByName, notSolidSet, fluidsById);
240+
return new BlockIndexData(ids, names, emissive, blockHardness, blockResistance, blockFriction, categoryByName,
241+
notSolidSet, fluidsById);
199242
}
200243
}

0 commit comments

Comments
 (0)