Skip to content

Commit

Permalink
铁砧锤可以戴在头上,充当类似工程师护目镜的作用,查看电网范围
Browse files Browse the repository at this point in the history
resolved #293
  • Loading branch information
Gu-ZT committed Apr 16, 2024
1 parent 567991e commit 69bf4f1
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.block.entity.CreativeDynamoBlockEntity;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
import dev.dubhe.anvilcraft.network.SliderInitPack;
Expand All @@ -23,7 +24,7 @@

import javax.annotation.Nonnull;

public class CreativeDynamoBlock extends BaseEntityBlock {
public class CreativeDynamoBlock extends BaseEntityBlock implements IHammerRemovable {
public static final VoxelShape AABB = Block.box(0, 0, 0, 16, 4, 16);

public CreativeDynamoBlock(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.block.entity.HeaterBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
Expand All @@ -20,7 +21,7 @@

import javax.annotation.Nonnull;

public class HeaterBlock extends BaseEntityBlock {
public class HeaterBlock extends BaseEntityBlock implements IHammerRemovable {
public static final BooleanProperty LIT = RedstoneTorchBlock.LIT;

public HeaterBlock(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.block.entity.TransmissionPoleBlockEntity;
import dev.dubhe.anvilcraft.block.state.Half;
import net.minecraft.core.BlockPos;
Expand All @@ -23,7 +24,7 @@

import javax.annotation.Nonnull;

public class TransmissionPoleBlock extends BaseEntityBlock {
public class TransmissionPoleBlock extends BaseEntityBlock implements IHammerRemovable {
public static final EnumProperty<Half> HALF = EnumProperty.create("half", Half.class);

public TransmissionPoleBlock(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dev.dubhe.anvilcraft.client.renderer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import dev.dubhe.anvilcraft.api.power.PowerGrid;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class PowerGridRenderer {
/**
* 渲染
*/
public static void render(PoseStack poseStack, VertexConsumer consumer, double camX, double camY, double camZ) {
if (Minecraft.getInstance().level == null) return;
RandomSource random = Minecraft.getInstance().level.random;
for (PowerGrid grid : PowerGrid.GRID_SET) {
random.setSeed(grid.hashCode());
PowerGridRenderer.renderOutline(
poseStack, consumer, camX, camY, camZ,
grid.getPos(), grid.getRange(),
random.nextFloat(), random.nextFloat(), random.nextFloat(), 0.4f
);
}
}

@SuppressWarnings("SameParameterValue")
private static void renderOutline(
PoseStack poseStack, VertexConsumer consumer,
double camX, double camY, double camZ, @NotNull BlockPos pos, @NotNull VoxelShape shape,
float red, float green, float blue, float alpha
) {
PowerGridRenderer.renderShape(
poseStack, consumer, shape,
(double) pos.getX() - camX, (double) pos.getY() - camY, (double) pos.getZ() - camZ,
red, green, blue, alpha
);
}

private static void renderShape(
@NotNull PoseStack poseStack, VertexConsumer consumer, @NotNull VoxelShape shape,
double x, double y, double z, float red, float green, float blue, float alpha
) {
PoseStack.Pose pose = poseStack.last();
shape.forAllEdges((minX, minY, minZ, maxX, maxY, maxZ) -> {
float k = (float) (maxX - minX);
float l = (float) (maxY - minY);
float m = (float) (maxZ - minZ);
float n = Mth.sqrt(k * k + l * l + m * m);
consumer.vertex(pose.pose(), (float) (minX + x), (float) (minY + y), (float) (minZ + z))
.color(red, green, blue, alpha)
.normal(pose.normal(), k /= n, l /= n, m /= n)
.endVertex();
consumer.vertex(pose.pose(), (float) (maxX + x), (float) (maxY + y), (float) (maxZ + z))
.color(red, green, blue, alpha)
.normal(pose.normal(), k, l, m)
.endVertex();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Equipable;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Vanishable;
Expand All @@ -29,7 +30,7 @@
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public class AnvilHammerItem extends Item implements Vanishable {
public class AnvilHammerItem extends Item implements Vanishable, Equipable {
private long lastDropAnvilTime = 0;
private final Multimap<Attribute, AttributeModifier> defaultModifiers;

Expand Down Expand Up @@ -168,4 +169,9 @@ public boolean isCorrectToolForDrops(@NotNull BlockState block) {
}
return super.getDefaultAttributeModifiers(slot);
}

@Override
public @NotNull EquipmentSlot getEquipmentSlot() {
return EquipmentSlot.HEAD;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.dubhe.anvilcraft.mixin;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import dev.dubhe.anvilcraft.client.renderer.PowerGridRenderer;
import dev.dubhe.anvilcraft.init.ModItems;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderBuffers;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(LevelRenderer.class)
@Environment(EnvType.CLIENT)
public abstract class LevelRendererMixin {
@Shadow
@Final
private RenderBuffers renderBuffers;

@Inject(
method = "renderLevel",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/renderer/debug/DebugRenderer;render(Lcom/mojang/blaze3d/vertex/PoseStack;"
+ "Lnet/minecraft/client/renderer/MultiBufferSource$BufferSource;DDD)V"
),
locals = LocalCapture.CAPTURE_FAILEXCEPTION
)
private void renderLevel(
PoseStack poseStack, float partialTick, long finishNanoTime,
boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer,
LightTexture lightTexture, Matrix4f projectionMatrix, CallbackInfo ci,
@NotNull ProfilerFiller profilerFiller, @NotNull Vec3 vec3
) {
Entity entity = camera.getEntity();
boolean bl = true;
for (ItemStack slot : entity.getArmorSlots()) {
if (slot.is(ModItems.ANVIL_HAMMER.get())) {
bl = false;
break;
}
}
if (bl) return;
MultiBufferSource.BufferSource bufferSource = this.renderBuffers.bufferSource();
VertexConsumer vertexConsumer3 = bufferSource.getBuffer(RenderType.lines());
double camX = vec3.x();
double camY = vec3.y();
double camZ = vec3.z();
profilerFiller.popPush("grid");
PowerGridRenderer.render(poseStack, vertexConsumer3, camX, camY, camZ);
}
}
1 change: 1 addition & 0 deletions common/src/main/resources/anvilcraft-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"SolidBucketItemMixin"
],
"client": [
"LevelRendererMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 69bf4f1

Please sign in to comment.