Skip to content

Commit db7b1ed

Browse files
完善对应物品形态渲染
1 parent 46681eb commit db7b1ed

15 files changed

Lines changed: 632 additions & 2550 deletions

src/main/java/top/ctnstudio/futurefood/client/model/BasicGeoModel.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,52 @@
66
import top.ctnstudio.futurefood.core.FutureFood;
77

88
public class BasicGeoModel<T extends GeoAnimatable> extends GeoModel<T> {
9-
public final String path;
9+
public final ResourceLocation modelPath;
10+
public final ResourceLocation texturePath;
11+
public final ResourceLocation animationsPath;
1012

11-
public BasicGeoModel(String path) {
12-
this.path = path;
13+
public BasicGeoModel(String pathName) {
14+
this(pathName, pathName, pathName);
15+
}
16+
17+
public BasicGeoModel(String modelPath, String texturePath, String animationsPath) {
18+
this.modelPath = FutureFood.modRL("geo/" + modelPath + ".geo.json");
19+
this.texturePath = FutureFood.modRL("textures/" + texturePath + ".png");
20+
this.animationsPath = FutureFood.modRL("textures/" + animationsPath + ".png");
1321
}
1422

1523
@Override
1624
public ResourceLocation getModelResource(T animatable) {
17-
return FutureFood.modRL("geo/" + path + ".geo.json");
25+
return modelPath;
1826
}
1927

2028
@Override
2129
public ResourceLocation getTextureResource(T animatable) {
22-
return FutureFood.modRL("textures/" + path + ".png");
30+
return texturePath;
2331
}
2432

2533
@Override
2634
public ResourceLocation getAnimationResource(T animatable) {
27-
return FutureFood.modRL("animations/" + path + ".json");
35+
return animationsPath;
2836
}
2937

3038
public static class BlockModel<T extends GeoAnimatable> extends BasicGeoModel<T> {
31-
public BlockModel(String path) {
32-
super("block/" + path);
39+
public BlockModel(String pathName) {
40+
super("block/" + pathName);
41+
}
42+
43+
public BlockModel(String modelPath, String texturePath, String animationsPath) {
44+
super(modelPath, texturePath, animationsPath);
3345
}
3446
}
3547

3648
public static class ItemModel<T extends GeoAnimatable> extends BasicGeoModel<T> {
37-
public ItemModel(String path) {
38-
super("item/" + path);
49+
public ItemModel(String pathName) {
50+
super("item/" + pathName);
51+
}
52+
53+
public ItemModel(String modelPath, String texturePath, String animationsPath) {
54+
super(modelPath, texturePath, animationsPath);
3955
}
4056
}
4157
}

src/main/java/top/ctnstudio/futurefood/client/model/ParticleColliderBlockEntityModel.java renamed to src/main/java/top/ctnstudio/futurefood/client/model/ParticleColliderModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import top.ctnstudio.futurefood.common.block.tile.ParticleColliderBlockEntity;
44

5-
public class ParticleColliderBlockEntityModel<T extends ParticleColliderBlockEntity>
5+
public class ParticleColliderModel<T extends ParticleColliderBlockEntity>
66
extends BasicGeoModel.BlockModel<T> {
7-
public ParticleColliderBlockEntityModel(String path) {
7+
public ParticleColliderModel(String path) {
88
super(path);
99
}
1010
}

src/main/java/top/ctnstudio/futurefood/client/renderer/BasicGeoBlockItemRenderer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package top.ctnstudio.futurefood.client.renderer;
22

3+
import net.minecraft.core.registries.BuiltInRegistries;
34
import net.minecraft.resources.ResourceLocation;
45
import net.minecraft.world.item.BlockItem;
56
import net.minecraft.world.level.block.Block;
@@ -12,7 +13,7 @@ public BasicGeoBlockItemRenderer(BasicGeoModel.BlockModel<T> model) {
1213
}
1314

1415
public BasicGeoBlockItemRenderer(Block block) {
15-
this(ResourceLocation.parse(block.getDescriptionId()).getPath());
16+
this(BuiltInRegistries.BLOCK.getKey(block).getPath());
1617
}
1718

1819
public BasicGeoBlockItemRenderer(String path) {
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package top.ctnstudio.futurefood.client.renderer;
22

33
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
4-
import top.ctnstudio.futurefood.client.model.ParticleColliderBlockEntityModel;
4+
import software.bernie.geckolib.cache.object.GeoBone;
5+
import software.bernie.geckolib.model.GeoModel;
6+
import top.ctnstudio.futurefood.client.model.ParticleColliderModel;
57
import top.ctnstudio.futurefood.common.block.tile.ParticleColliderBlockEntity;
68

9+
import java.util.Optional;
10+
711
public class ParticleColliderBlockEntityRenderer<T extends ParticleColliderBlockEntity>
812
extends BasicGeoBlockRenderer<T> {
913
public ParticleColliderBlockEntityRenderer(BlockEntityRendererProvider.Context context) {
10-
super(new ParticleColliderBlockEntityModel<>("particle_collider"));
14+
super(new ParticleColliderModel<>("particle_collider"));
1115
}
1216
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package top.ctnstudio.futurefood.client.renderer;
2+
3+
import net.minecraft.world.level.block.Block;
4+
import top.ctnstudio.futurefood.common.item.ModGeoBlockItem;
5+
6+
public class ParticleColliderBlockItemRenderer<T extends ModGeoBlockItem> extends BasicGeoBlockItemRenderer<T> {
7+
public ParticleColliderBlockItemRenderer(Block block) {
8+
super(block);
9+
}
10+
}

src/main/java/top/ctnstudio/futurefood/common/block/HorizontalDirectionalEntityBlock.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package top.ctnstudio.futurefood.common.block;
22

3-
import com.mojang.serialization.MapCodec;
43
import net.minecraft.core.Direction;
54
import net.minecraft.world.item.context.BlockPlaceContext;
65
import net.minecraft.world.level.block.BaseEntityBlock;
@@ -28,7 +27,7 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt
2827

2928
@Override
3029
public BlockState getStateForPlacement(BlockPlaceContext context) {
31-
return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection());
30+
return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
3231
}
3332

3433
@Override

src/main/java/top/ctnstudio/futurefood/common/block/ParticleColliderEntityBlock.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package top.ctnstudio.futurefood.common.block;
22

33
import com.mojang.serialization.MapCodec;
4+
import net.minecraft.client.particle.FireworkParticles;
45
import net.minecraft.core.BlockPos;
5-
import net.minecraft.world.entity.EntityType;
6+
import net.minecraft.server.level.ServerLevel;
7+
import net.minecraft.world.entity.player.Player;
8+
import net.minecraft.world.item.ItemStack;
9+
import net.minecraft.world.level.Level;
610
import net.minecraft.world.level.block.RenderShape;
711
import net.minecraft.world.level.block.entity.BlockEntity;
8-
import net.minecraft.world.level.block.state.BlockBehaviour;
912
import net.minecraft.world.level.block.state.BlockState;
10-
import org.jetbrains.annotations.NotNull;
1113
import org.jetbrains.annotations.Nullable;
12-
import software.bernie.geckolib.animatable.GeoBlockEntity;
1314
import top.ctnstudio.futurefood.common.block.tile.ParticleColliderBlockEntity;
1415
import top.ctnstudio.futurefood.core.init.ModBlock;
1516

16-
// TODO 这
1717
public class ParticleColliderEntityBlock extends HorizontalDirectionalEntityBlock {
1818
private static final MapCodec<ParticleColliderEntityBlock> CODEC =
1919
simpleCodec(ParticleColliderEntityBlock::new);

src/main/java/top/ctnstudio/futurefood/common/item/ModGeoBlockItem.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@
1111
import top.ctnstudio.futurefood.client.renderer.BasicGeoBlockItemRenderer;
1212

1313
import java.util.function.Consumer;
14+
import java.util.function.Supplier;
1415

1516
public class ModGeoBlockItem extends BlockItem implements GeoItem {
16-
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
17+
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
18+
private final Supplier<BlockEntityWithoutLevelRenderer> defaultRenderer;
1719

1820
public ModGeoBlockItem(Block block, Properties properties) {
21+
this(block, properties, () -> new BasicGeoBlockItemRenderer<ModGeoBlockItem>(block));
22+
}
23+
24+
public ModGeoBlockItem(Block block, Properties properties, Supplier<BlockEntityWithoutLevelRenderer> defaultRenderer) {
1925
super(block, properties);
26+
this.defaultRenderer = defaultRenderer;
2027
}
2128

2229
@Override
@@ -35,11 +42,25 @@ public void createGeoRenderer(Consumer<GeoRenderProvider> consumer) {
3542

3643
@Override
3744
public BlockEntityWithoutLevelRenderer getGeoItemRenderer() {
38-
if (this.renderer == null)
39-
this.renderer = new BasicGeoBlockItemRenderer<ModGeoBlockItem>(getBlock());
45+
if (this.renderer == null) {
46+
BlockEntityWithoutLevelRenderer defaultRenderer = getDefaultRenderer();
47+
if (defaultRenderer != null) {
48+
this.renderer = defaultRenderer;
49+
}
50+
}
4051

4152
return this.renderer;
4253
}
4354
});
4455
}
56+
57+
public BlockEntityWithoutLevelRenderer getDefaultRenderer() {
58+
if (defaultRenderer != null) {
59+
BlockEntityWithoutLevelRenderer renderer = defaultRenderer.get();
60+
if (renderer != null) {
61+
return renderer;
62+
}
63+
}
64+
return new BasicGeoBlockItemRenderer<ModGeoBlockItem>(getBlock());
65+
}
4566
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package top.ctnstudio.futurefood.core.init;
22

3+
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
34
import net.minecraft.world.item.BlockItem;
45
import net.minecraft.world.item.Item;
56
import net.minecraft.world.level.block.Block;
67
import net.neoforged.neoforge.registries.DeferredItem;
78
import net.neoforged.neoforge.registries.DeferredRegister;
9+
import top.ctnstudio.futurefood.client.renderer.ParticleColliderBlockItemRenderer;
810
import top.ctnstudio.futurefood.common.item.ModGeoBlockItem;
911
import top.ctnstudio.futurefood.core.FutureFood;
1012

13+
import java.util.function.BiFunction;
14+
import java.util.function.Function;
1115
import java.util.function.Supplier;
1216

1317
public final class ModItem {
@@ -17,9 +21,7 @@ public final class ModItem {
1721
public static final DeferredItem<Item> QER =
1822
ITEMS.register("quantum_energy_receiver", createBlockItem(ModBlock.QER));
1923
public static final DeferredItem<Item> PARTICLE_COLLIDER =
20-
ITEMS.register("particle_collider", createBlockItem(ModBlock.PARTICLE_COLLIDER));
21-
private ModItem() {
22-
}
24+
ITEMS.register("particle_collider", createGeoBlockItem(ModBlock.PARTICLE_COLLIDER, ParticleColliderBlockItemRenderer::new));
2325

2426
private static Supplier<BlockItem> createBlockItem(Supplier<Block> block) {
2527
return () -> new BlockItem(block.get(), new Item.Properties());
@@ -28,4 +30,11 @@ private static Supplier<BlockItem> createBlockItem(Supplier<Block> block) {
2830
private static Supplier<BlockItem> createGeoBlockItem(Supplier<Block> block) {
2931
return () -> new ModGeoBlockItem(block.get(), new Item.Properties());
3032
}
33+
34+
private static Supplier<BlockItem> createGeoBlockItem(Supplier<Block> block, Function<Block, BlockEntityWithoutLevelRenderer> renderer) {
35+
return () -> {
36+
Block b = block.get();
37+
return new ModGeoBlockItem(b, new Item.Properties(), () -> renderer.apply(b));
38+
};
39+
}
3140
}

src/main/java/top/ctnstudio/futurefood/datagen/DatagenI18ZhCn.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.minecraft.resources.ResourceKey;
77
import net.minecraft.world.damagesource.DamageType;
88
import net.minecraft.world.entity.ai.attributes.Attribute;
9+
import net.minecraft.world.item.CreativeModeTab;
910
import net.neoforged.neoforge.common.data.LanguageProvider;
1011
import net.neoforged.neoforge.registries.DeferredHolder;
1112
import top.ctnstudio.futurefood.core.FutureFood;
@@ -21,20 +22,13 @@ public DatagenI18ZhCn(PackOutput output) {
2122

2223
@Override
2324
protected void addTranslations() {
24-
add(ModCreativeModeTab.TAB, "未来食物");
25+
add("itemGroup.futurefood", "未来食物");
2526

2627
addBlock(ModBlock.QED, "量子能源扩散器");
2728
addBlock(ModBlock.QER, "量子能源接收器");
2829
addBlock(ModBlock.PARTICLE_COLLIDER, "粒子对撞器");
2930
}
3031

31-
/**
32-
* 创造模式物品栏名称翻译
33-
*/
34-
public <R, T extends R> void add(DeferredHolder<R, T> itemGroup, String name) {
35-
add("itemGroup." + itemGroup.getId().toString().replace(":", "."), name);
36-
}
37-
3832
public void addConfig(String configKey, String translationDescribe, String commentDescribe) {
3933
add(translationKey(configKey), translationDescribe);
4034
add(commentKey(configKey), commentDescribe);

0 commit comments

Comments
 (0)