Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Seismic Sense #3

Draft
wants to merge 5 commits into
base: 1.21
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"anvil_cost": 1,
"description": {
"translate": "enchantment.enchiridion.seismic_sense"
},
"effects": {
"enchiridion:block_broken": [
{
"effect": {
"type": "enchiridion:highlight_blocks",
"blacklist": "enchiridion:seismic_sense_blacklist",
"duration": {
"type": "minecraft:linear",
"base": 40.0,
"per_level_above_first": 20.0
},
"invert_list": false,
"range": {
"type": "minecraft:linear",
"base": 3.0,
"per_level_above_first": 2.0
}
}
}
]
},
"max_cost": {
"base": 35,
"per_level_above_first": 0
},
"max_level": 2,
"min_cost": {
"base": 12,
"per_level_above_first": 4
},
"slots": [
"mainhand"
],
"supported_items": "#minecraft:enchantable/mining",
"weight": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"values": [
"minecraft:stone",
"minecraft:deepslate",
"minecraft:netherrack",
"minecraft:end_stone"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"minecraft:swift_sneak",
"enchiridion:exhilarating",
"enchiridion:ice_strike",
"enchiridion:reach"
"enchiridion:reach",
"enchiridion:seismic_sense"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"values": [
"enchiridion:crumble"
"enchiridion:crumble",
"enchiridion:seismic_sense"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static EnchiridionPlatformHelper getHelper() {
public static class BlockTags {
public static final TagKey<Block> BASE_STONE = TagKey.create(Registries.BLOCK, asResource("base_stone"));
public static final TagKey<Block> HARDER_STONE = TagKey.create(Registries.BLOCK, asResource("harder_stone"));
public static final TagKey<Block> SEISMIC_SENSE_BLACKLIST = TagKey.create(Registries.BLOCK, asResource("seismic_sense_blacklist"));
}

public static class EnchantmentTags {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package dev.greenhouseteam.enchiridion.client;

import com.mojang.blaze3d.vertex.*;
import dev.greenhouseteam.enchiridion.Enchiridion;
import net.minecraft.client.Camera;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.*;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.FastColor;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;

import java.util.EnumSet;
import java.util.List;

public class GlowingBlocksClientHolder {

public static List<BlockPos> outlinedBlocks;
public static int color;
public static int duration;
public static int red;
public static int green;
public static int blue;

private static EntityRenderDispatcher entityRenderer;
private static final ModelPart.Cube BLOCK = new ModelPart.Cube(
0, 0,
0, 0, 0,
16, 16, 16,
0, 0, 0,
false,
0, 0, EnumSet.allOf(Direction.class));
private static final RenderType GLOWING = RenderType.outline(Enchiridion.asResource("textures/dummy.png"));

public static boolean isGlowing() {
return outlinedBlocks != null && !outlinedBlocks.isEmpty();
}


public static void clearGlowingBlocks() {
outlinedBlocks = List.of();
}

public static void setGlowingBlocks(List<BlockPos> outlinedBlocks, int color, int duration) {

for (BlockPos pos : outlinedBlocks) {
Enchiridion.LOGGER.info("Block pos: {}", pos);
}
GlowingBlocksClientHolder.outlinedBlocks = outlinedBlocks;
GlowingBlocksClientHolder.color = color;
GlowingBlocksClientHolder.red = FastColor.ARGB32.red(color);
GlowingBlocksClientHolder.green = FastColor.ARGB32.green(color);
GlowingBlocksClientHolder.blue = FastColor.ARGB32.blue(color);
GlowingBlocksClientHolder.duration = duration;
}

public static void tick(ClientLevel clientLevel) {
if (duration > 0) {
duration--;
}
if (duration == 0) {
outlinedBlocks = List.of();
}
}

public static void render(GameRenderer gameRenderer, Camera camera, @Nullable PoseStack poseStack, Matrix4f matrix4f, Matrix4f f, @Nullable MultiBufferSource consumers) {
if (entityRenderer == null) {
entityRenderer = gameRenderer.getMinecraft().getEntityRenderDispatcher();
}
if (poseStack == null && consumers == null) {
return;
}

if (outlinedBlocks != null && !outlinedBlocks.isEmpty()) {
OutlineBufferSource bufferSource = gameRenderer.getMinecraft().renderBuffers().outlineBufferSource();
poseStack.pushPose();
poseStack.translate(-camera.getPosition().x(), -camera.getPosition().y(), -camera.getPosition().z());
bufferSource.setColor(red, green, blue, 255);
var buffer = bufferSource.getBuffer(GLOWING);
for (BlockPos pos : outlinedBlocks) {
poseStack.pushPose();
poseStack.translate(pos.getX(), pos.getY(), pos.getZ());
BLOCK.compile(
poseStack.last(),
buffer,
0,
OverlayTexture.NO_OVERLAY,
0, 0, 0, 0
);
poseStack.popPose();
}
poseStack.popPose();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dev.greenhouseteam.enchiridion.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.greenhouseteam.enchiridion.Enchiridion;
import dev.greenhouseteam.enchiridion.network.clientbound.SetOutlinedBlocksClientboundPacket;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.blocks.BlockStateArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MapColor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BlockHighlightCommand {

private static int executeCommand(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSourceStack source = context.getSource();
if (!source.isPlayer()) {
return 0;
}
ServerPlayer player = source.getPlayerOrException();
Block block = BlockStateArgument.getBlock(context, "block").getState().getBlock();
var range = IntegerArgumentType.getInteger(context, "range");
var color = block.defaultMapColor().calculateRGBColor(MapColor.Brightness.HIGH);
int duration = IntegerArgumentType.getInteger(context, "duration");
Iterator<BlockPos> blocks = BlockPos.withinManhattan(
player.blockPosition(),
range,
range,
range
).iterator();
List<BlockPos> blocksList = new ArrayList<>();
while (blocks.hasNext()) {
BlockPos pos = blocks.next();
BlockState state = player.level().getBlockState(pos);
if (state.getBlock().equals(block)) {
blocksList.add(new BlockPos(pos)); // To deal with mutable BlockPos
}
}

Enchiridion.getHelper().sendClientbound(player, new SetOutlinedBlocksClientboundPacket(
blocksList,
color,
duration

));


return 1;
}

public static void register(CommandDispatcher<CommandSourceStack> commandDispatcher, CommandBuildContext commandBuildContext) {
commandDispatcher.register(
Commands.literal("blockhighlight")
.then(
Commands.argument("block", BlockStateArgument.block(commandBuildContext))
.then(
Commands.argument("range", IntegerArgumentType.integer(1))
.then(
Commands.argument("duration", IntegerArgumentType.integer(1))
.executes(BlockHighlightCommand::executeCommand)
)
)
)
);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.greenhouseteam.enchiridion.data;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.BlockPos;

import java.util.List;

public record GlowingBlocks(List<BlockPos> glowingBlocks, int highlightColor) {
public static final Codec<GlowingBlocks> CODEC = RecordCodecBuilder.create(instance -> instance.group(
BlockPos.CODEC.listOf().fieldOf("glowingBlocks").forGetter(GlowingBlocks::glowingBlocks),
Codec.INT.fieldOf("highlightColor").forGetter(GlowingBlocks::highlightColor)
).apply(instance, GlowingBlocks::new));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.greenhouseteam.enchiridion.enchantment.effects;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.enchantment.EnchantedItemInUse;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;


/**
* Represents an enchantment effect that is triggered when a block is mined by a player.
*/
public interface BlockMinedEffect {

void onBlockMined(ServerLevel serverLevel, int i, EnchantedItemInUse enchantedItemInUse, Entity entity, Vec3 vec3, BlockState block);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package dev.greenhouseteam.enchiridion.enchantment.effects;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.greenhouseteam.enchiridion.Enchiridion;
import dev.greenhouseteam.enchiridion.network.clientbound.SetOutlinedBlocksClientboundPacket;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.DiggerItem;
import net.minecraft.world.item.enchantment.EnchantedItemInUse;
import net.minecraft.world.item.enchantment.LevelBasedValue;
import net.minecraft.world.item.enchantment.effects.EnchantmentLocationBasedEffect;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.phys.Vec3;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public record HighlightMinedBlocksEffect(LevelBasedValue duration, LevelBasedValue range, TagKey<Block> blacklist, boolean invertList) implements BlockMinedEffect, EnchantmentLocationBasedEffect {
public static final MapCodec<HighlightMinedBlocksEffect> CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group(
LevelBasedValue.CODEC.fieldOf("duration").forGetter(HighlightMinedBlocksEffect::duration),
LevelBasedValue.CODEC.fieldOf("range").forGetter(HighlightMinedBlocksEffect::range),
TagKey.codec(Registries.BLOCK).fieldOf("blacklist").forGetter(HighlightMinedBlocksEffect::blacklist),
Codec.BOOL.fieldOf("invert_list").forGetter(HighlightMinedBlocksEffect::invertList)
).apply(inst, HighlightMinedBlocksEffect::new));


@Override
public void onChangedBlock(ServerLevel var1, int var2, EnchantedItemInUse var3, Entity var4, Vec3 var5, boolean var6) {
// No-op
}

@Override
public MapCodec<? extends EnchantmentLocationBasedEffect> codec() {
return CODEC;
}


@Override
public void onBlockMined(ServerLevel serverLevel, int i, EnchantedItemInUse enchantedItemInUse, Entity entity, Vec3 vec3, BlockState block) {
LivingEntity livingEntity = enchantedItemInUse.owner();

if (block.is(blacklist) != invertList) return;
if (enchantedItemInUse.itemStack().getItem() instanceof DiggerItem diggerItem && !diggerItem.isCorrectToolForDrops(enchantedItemInUse.itemStack(), block)) return;
if (livingEntity instanceof ServerPlayer player) {
int range = (int) this.range.calculate(i);
int duration = (int) this.duration.calculate(i);
int color = block.getBlock().defaultMapColor().calculateRGBColor(MapColor.Brightness.HIGH);
// Highlight the mined block
Iterator<BlockPos> blocks = BlockPos.withinManhattan(
player.blockPosition(),
range,
range,
range
).iterator();
List<BlockPos> blocksList = new ArrayList<>();
while (blocks.hasNext()) {
BlockPos pos = blocks.next();
BlockState state = player.level().getBlockState(pos);
if (state.getBlock().equals(block.getBlock())) {
blocksList.add(new BlockPos(pos)); // To deal with mutable BlockPos
}
}

Enchiridion.getHelper().sendClientbound(player, new SetOutlinedBlocksClientboundPacket(
blocksList,
color,
duration

));

}

}

}
Loading