Skip to content

Commit 87264b1

Browse files
committed
starting work on girder strut
1 parent 14e5006 commit 87264b1

8 files changed

Lines changed: 140 additions & 32 deletions

File tree

src/main/java/com/kipti/bnb/CreateBitsnBobs.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package com.kipti.bnb;
22

33
import com.kipti.bnb.registry.*;
4+
import com.mojang.logging.LogUtils;
45
import com.simibubi.create.foundation.data.CreateRegistrate;
56
import com.simibubi.create.foundation.item.ItemDescription;
67
import com.simibubi.create.foundation.item.KineticStats;
78
import com.simibubi.create.foundation.item.TooltipModifier;
89
import net.createmod.catnip.lang.FontHelper;
910
import net.minecraft.resources.ResourceKey;
1011
import net.minecraft.resources.ResourceLocation;
11-
import org.slf4j.Logger;
12-
13-
import com.mojang.logging.LogUtils;
14-
1512
import net.minecraft.world.item.CreativeModeTab;
1613
import net.neoforged.bus.api.IEventBus;
1714
import net.neoforged.fml.ModContainer;
1815
import net.neoforged.fml.common.Mod;
1916
import net.neoforged.fml.config.ModConfig;
17+
import org.slf4j.Logger;
2018

2119
// The value here should match an entry in the META-INF/neoforge.mods.toml file
2220
@Mod(CreateBitsnBobs.MOD_ID)
@@ -42,6 +40,7 @@ public CreateBitsnBobs(IEventBus modEventBus, ModContainer modContainer) {
4240
BnbPartialModels.register();
4341
BnbBlockEntities.register();
4442
BnbTags.register();
43+
BnbDataComponents.register(modEventBus);
4544

4645
modContainer.registerConfig(ModConfig.Type.CLIENT, Config.SPEC);
4746
}
@@ -50,4 +49,6 @@ public static ResourceLocation asResource(String s) {
5049
return ResourceLocation.fromNamespaceAndPath(MOD_ID, s);
5150
}
5251

53-
}
52+
}
53+
54+

src/main/java/com/kipti/bnb/content/girder_strut/GirderStrutBlock.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,7 @@ public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldS
5959
BlockEntity selfBe = level.getBlockEntity(pos);
6060
if (!(selfBe instanceof GirderStrutBlockEntity self)) return;
6161

62-
// Scan partner struts and link them, for now, in future the item directly presses it
6362

64-
for (int x = -MAX_SPAN; x <= MAX_SPAN; x++) {
65-
for (int y = -MAX_SPAN; y <= MAX_SPAN; y++) {
66-
for (int z = -MAX_SPAN; z <= MAX_SPAN; z++) {
67-
if (x == 0 && y == 0 && z == 0) continue;
68-
BlockPos scan = pos.offset(x, y, z);
69-
BlockState scanState = level.getBlockState(scan);
70-
if (scanState.getBlock() instanceof GirderStrutBlock) {
71-
BlockEntity otherBe = level.getBlockEntity(scan);
72-
if (otherBe instanceof GirderStrutBlockEntity other) {
73-
// avoid duplicate connection creation by only connecting when this pos is 'smaller' along direction
74-
if (!other.hasConnectionTo(pos)) {
75-
self.addConnection(scan);
76-
other.addConnection(pos);
77-
self.setChanged();
78-
other.setChanged();
79-
}
80-
}
81-
}
82-
}
83-
}
84-
}
8563
}
8664

8765
@Override

src/main/java/com/kipti/bnb/content/girder_strut/GirderStrutBlockEntityRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ protected void renderSafe(GirderStrutBlockEntity blockEntity, float partialTicks
5858
.rotateX(-(float) xRot)
5959
.uncenter();
6060

61-
ms.pushPose();
62-
renderSegments(state, BnbPartialModels.GIRDER_STRUT_JOINT_SEGMENT, ms, 1, buffer, light);
63-
ms.popPose();
61+
// ms.pushPose();
62+
// renderSegments(state, BnbPartialModels.GIRDER_STRUT_JOINT_SEGMENT, ms, 1, buffer, light);
63+
// ms.popPose();
6464

6565
ms.translate(0, 0, lengthOffset + 0.5); // Adjust the translation based on segment length
6666
if (getRenderPriority(relative) > getRenderPriority(relative.multiply(-1))) {
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
package com.kipti.bnb.content.girder_strut;
22

3-
public class GirderStrutBlockItem {
3+
import com.kipti.bnb.registry.BnbDataComponents;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.world.InteractionResult;
6+
import net.minecraft.world.item.BlockItem;
7+
import net.minecraft.world.item.ItemStack;
8+
import net.minecraft.world.item.context.UseOnContext;
9+
import net.minecraft.world.level.block.Block;
10+
import net.minecraft.world.level.block.state.BlockState;
11+
12+
public class GirderStrutBlockItem extends BlockItem {
13+
14+
public GirderStrutBlockItem(Block block, Properties properties) {
15+
super(block, properties);
16+
}
17+
18+
@Override
19+
public InteractionResult useOn(UseOnContext context) {
20+
//Check if the current stack has the place from data tag, if not, add it in
21+
22+
ItemStack stack = context.getItemInHand();
23+
24+
if (stack.has(BnbDataComponents.GIRDER_STRUT_FROM)) {
25+
return super.useOn(context);
26+
}
27+
28+
BlockPos clickedPos = context.getClickedPos();
29+
30+
//If clicked pos is not already a girder strut block, offset by the clicked face
31+
BlockState clickedState = context.getLevel().getBlockState(clickedPos);
32+
if (!(clickedState.getBlock() instanceof GirderStrutBlock)) {
33+
clickedPos = clickedPos.relative(context.getClickedFace());
34+
}
35+
36+
stack.set(BnbDataComponents.GIRDER_STRUT_FROM, clickedPos);
37+
38+
return InteractionResult.SUCCESS;
39+
}
440
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.kipti.bnb.content.girder_strut;
2+
3+
import com.kipti.bnb.registry.BnbDataComponents;
4+
import net.minecraft.client.Minecraft;
5+
import net.minecraft.client.multiplayer.ClientLevel;
6+
import net.minecraft.client.player.LocalPlayer;
7+
import net.minecraft.core.BlockPos;
8+
import net.minecraft.core.particles.DustParticleOptions;
9+
import net.minecraft.world.item.ItemStack;
10+
import net.minecraft.world.phys.Vec3;
11+
import org.joml.Vector3f;
12+
13+
public class GirderStrutPlacementEffects {
14+
15+
public static void tick(LocalPlayer player) {
16+
if (Minecraft.getInstance().isPaused()) return;
17+
18+
//Get held item
19+
ItemStack heldItem = player.getMainHandItem().getItem() instanceof GirderStrutBlockItem girderStrutMainHand ? player.getMainHandItem() :
20+
player.getOffhandItem().getItem() instanceof GirderStrutBlockItem girderStrutOffHand ? player.getOffhandItem() : null;
21+
if (heldItem != null) {
22+
display(player, heldItem);
23+
}
24+
}
25+
26+
private static void display(LocalPlayer player, ItemStack heldItem) {
27+
ClientLevel level = Minecraft.getInstance().level;
28+
boolean valid = true;
29+
BlockPos fromPos = heldItem.get(BnbDataComponents.GIRDER_STRUT_FROM);
30+
31+
if (fromPos == null) {
32+
return;
33+
}
34+
35+
BlockPos targetPos = BlockPos.containing(Minecraft.getInstance().hitResult.getLocation());
36+
37+
Vec3 renderFrom = Vec3.atCenterOf(fromPos);
38+
Vec3 renderTo = Vec3.atCenterOf(targetPos);
39+
40+
Vec3 dir = renderTo.subtract(renderFrom).normalize();
41+
42+
for (double t = 0; t <= renderFrom.length(); t += 0.1) {
43+
Vec3 lerped = renderFrom.add(dir.scale(t));
44+
45+
level.addParticle(
46+
new DustParticleOptions(new Vector3f(valid ? .3f : .9f, valid ? .9f : .3f, .5f), 1), true,
47+
lerped.x + .5f, lerped.y + .5f, lerped.z + .5f, 0, 0, 0);
48+
}
49+
50+
}
51+
52+
}

src/main/java/com/kipti/bnb/foundation/ClientEvents.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.kipti.bnb.foundation;
22

3+
import com.kipti.bnb.content.girder_strut.GirderStrutPlacementEffects;
34
import com.kipti.bnb.content.weathered_girder.WeatheredGirderWrenchBehaviour;
5+
import net.minecraft.client.Minecraft;
46
import net.neoforged.api.distmarker.Dist;
57
import net.neoforged.bus.api.SubscribeEvent;
68
import net.neoforged.fml.common.EventBusSubscriber;
@@ -14,4 +16,13 @@ public static void onTickPost(ClientTickEvent.Post event) {
1416
WeatheredGirderWrenchBehaviour.tick();
1517
}
1618

19+
@SubscribeEvent
20+
public static void onTickPre(ClientTickEvent.Pre event) {
21+
//If in a level, there is a player, and the player is holding a girder strut block item, update the preview
22+
Minecraft mc = Minecraft.getInstance();
23+
if (mc.level != null && mc.player != null) {
24+
GirderStrutPlacementEffects.tick(mc.player);
25+
}
26+
}
27+
1728
}

src/main/java/com/kipti/bnb/registry/BnbBlocks.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.kipti.bnb.CreateBitsnBobs;
44
import com.kipti.bnb.content.chair.ChairBlock;
55
import com.kipti.bnb.content.girder_strut.GirderStrutBlock;
6+
import com.kipti.bnb.content.girder_strut.GirderStrutBlockItem;
67
import com.kipti.bnb.content.light.founation.LightBlock;
78
import com.kipti.bnb.content.light.headlamp.HeadlampBlock;
89
import com.kipti.bnb.content.light.headlamp.HeadlampBlockItem;
@@ -202,7 +203,7 @@ public static <T extends LargeNixieTubeBlock, P> NonNullFunction<BlockBuilder<T,
202203
(state) -> p.models().getExistingFile(CreateBitsnBobs.asResource(
203204
"block/girder_strut/normal_girder_strut_attachment")
204205
)))
205-
.item()
206+
.item(GirderStrutBlockItem::new)
206207
.model((c, p) ->
207208
p.withExistingParent(c.getName(), CreateBitsnBobs.asResource("block/girder_strut/normal_girder_strut_attachment"))
208209
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
package com.kipti.bnb.registry;
22

3+
import com.kipti.bnb.CreateBitsnBobs;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.core.component.DataComponentType;
6+
import net.minecraft.core.registries.Registries;
7+
import net.neoforged.bus.api.IEventBus;
8+
import net.neoforged.neoforge.registries.DeferredRegister;
9+
import org.jetbrains.annotations.ApiStatus;
10+
11+
import java.util.function.UnaryOperator;
12+
313
public class BnbDataComponents {
14+
15+
private static final DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(Registries.DATA_COMPONENT_TYPE, CreateBitsnBobs.MOD_ID);
16+
17+
public static final DataComponentType<BlockPos> GIRDER_STRUT_FROM = register(
18+
"girder_strut_from",
19+
builder -> builder.persistent(BlockPos.CODEC).networkSynchronized(BlockPos.STREAM_CODEC)
20+
);
21+
22+
private static <T> DataComponentType<T> register(String name, UnaryOperator<DataComponentType.Builder<T>> builder) {
23+
DataComponentType<T> type = builder.apply(DataComponentType.builder()).build();
24+
DATA_COMPONENTS.register(name, () -> type);
25+
return type;
26+
}
27+
28+
@ApiStatus.Internal
29+
public static void register(IEventBus modEventBus) {
30+
DATA_COMPONENTS.register(modEventBus);
31+
}
32+
433
}

0 commit comments

Comments
 (0)