Skip to content

Commit

Permalink
hammer item
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaupenjoe committed Sep 15, 2024
1 parent afcd8d3 commit b848603
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// 1.21.1 2024-09-15T22:30:18.7957421 Tutorial Mod/Block Loot Tables
// 1.21.1 2024-09-15T23:45:22.4559283 Tutorial Mod/Block Loot Tables
1382e8045641fe6784d8535d113c80a6028a5ace data\tutorialmod\loot_table\blocks\pink_garnet_pressure_plate.json
cedbc7c7be44e1b0df48ece4790b5266a35f2b72 data\tutorialmod\loot_table\blocks\pink_garnet_slab.json
574a89ee1b10a8e85c7f90b61a61c3dd6edeb638 data\tutorialmod\loot_table\blocks\pink_garnet_trapdoor.json
f9fed56792aac989521a48d25cde5a55d5973db4 data\tutorialmod\loot_table\blocks\pink_garnet_fence_gate.json
c524d99e89d1a6b91131c503e69d1343642df802 data\tutorialmod\loot_table\blocks\raw_pink_garnet_block.json
fdb21182d3115edeb12f16aa5d40f8aca9e17f9a data\tutorialmod\loot_table\blocks\pink_garnet_button.json
3bfcba243820f2344471574d75b7b48ba680fd3f data\tutorialmod\loot_table\blocks\pink_garnet_door.json
e324243da1cda477b8f75c1cf06f408d17143f95 data\tutorialmod\loot_table\blocks\pink_garnet_wall.json
940d49e1ad8940b0f97eab05f81d6734b7dc942f data\tutorialmod\loot_table\blocks\pink_garnet_fence.json
81e1cfbc4d86d3fd0189a0c8ea6ecf867f65d766 data\tutorialmod\loot_table\blocks\pink_garnet_ore.json
cbb358d78cd57da58c907a35212d486fa6f68efa data\tutorialmod\loot_table\blocks\magic_block.json
20c1b94f16da89972e44952293dc87c35d46d12c data\tutorialmod\loot_table\blocks\pink_garnet_block.json
64c3df15b6e946537414a0cc30f320d8b8019264 data\tutorialmod\loot_table\blocks\pink_garnet_stairs.json
a8a175045f0d3d679e9afac5111d25b6d0b78ab7 data\tutorialmod\loot_table\blocks\pink_garnet_deepslate_ore.json
cedbc7c7be44e1b0df48ece4790b5266a35f2b72 data\tutorialmod\loot_table\blocks\pink_garnet_slab.json
574a89ee1b10a8e85c7f90b61a61c3dd6edeb638 data\tutorialmod\loot_table\blocks\pink_garnet_trapdoor.json
c524d99e89d1a6b91131c503e69d1343642df802 data\tutorialmod\loot_table\blocks\raw_pink_garnet_block.json
fdb21182d3115edeb12f16aa5d40f8aca9e17f9a data\tutorialmod\loot_table\blocks\pink_garnet_button.json
81e1cfbc4d86d3fd0189a0c8ea6ecf867f65d766 data\tutorialmod\loot_table\blocks\pink_garnet_ore.json
64c3df15b6e946537414a0cc30f320d8b8019264 data\tutorialmod\loot_table\blocks\pink_garnet_stairs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.21.1 2024-09-15T22:30:18.7967428 Tutorial Mod/Tags for minecraft:block
// 1.21.1 2024-09-15T23:45:22.4579781 Tutorial Mod/Tags for minecraft:block
8159fdbcb7619cbd57674525489b6dfed8a407fd data\minecraft\tags\block\fence_gates.json
c07c4c3be2f1e2228e70cca883d68ce9bd7ba47e data\tutorialmod\tags\block\needs_pink_garnet_tool.json
079e21ee82e19be3b06c0ad61d06e9bdce588108 data\minecraft\tags\block\needs_iron_tool.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.21.1 2024-09-15T22:30:18.7967428 Tutorial Mod/Model Definitions
// 1.21.1 2024-09-15T23:45:22.4569776 Tutorial Mod/Model Definitions
51e9c213683a5edc7250a3fc72a4dda3cd4a547e assets\tutorialmod\models\block\pink_garnet_stairs_inner.json
06cb8ccbdefea36e870bb0d601dec9e4baddc6b9 assets\tutorialmod\blockstates\pink_garnet_wall.json
44df06c1ed35df1eb8954755fdc61e81fb77e9da assets\tutorialmod\models\item\pink_garnet_deepslate_ore.json
Expand All @@ -22,14 +22,15 @@ b8a328f7a3c0600e44839a46d05666c47291ffb9 assets\tutorialmod\models\block\raw_pin
b7fa9063d9cfffb94e5b0e258de053c495a8ab3a assets\tutorialmod\models\block\pink_garnet_slab_top.json
dcb8c72e5f3b703f9f0ef355f6d347e50c94b4db assets\tutorialmod\models\block\pink_garnet_fence_gate_wall_open.json
fa9080666ba4e3910d0814ec35a3e300f861fb1c assets\tutorialmod\models\item\starlight_ashes.json
f8b6f1021ac80c221d8d83ede8e5816c45ef723f assets\tutorialmod\blockstates\pink_garnet_button.json
eefb2c80e1274d2af9997558850ef1f06bf4ae04 assets\tutorialmod\blockstates\pink_garnet_pressure_plate.json
91bcbb578a52ac33eb56704bbe5f62e310abe5ee assets\tutorialmod\models\block\pink_garnet_fence_inventory.json
f8b6f1021ac80c221d8d83ede8e5816c45ef723f assets\tutorialmod\blockstates\pink_garnet_button.json
7db57b6386f02d11557397a148c30dea085235e0 assets\tutorialmod\blockstates\pink_garnet_slab.json
e10ed0f44080df33ec18cbea1e4ca95ffa54b62f assets\tutorialmod\models\item\pink_garnet_button.json
2c337e9d04bbac33f9d13fdfaac49fb239b1b0d9 assets\tutorialmod\models\item\pink_garnet_lamp.json
ed27aaa00c275028e1495bb6d6bfdcea1851067b assets\tutorialmod\models\block\magic_block.json
a7779ae2aac4f4da3808d0d77fdeae99af2b43f8 assets\tutorialmod\models\item\pink_garnet_wall.json
00d484aeafa1b6b8a7a6dcc575cfd572b517bd23 assets\tutorialmod\models\item\pink_garnet_hammer.json
f3f72a1eb57f8b905366cac9bf1a67bca00e859b assets\tutorialmod\models\block\pink_garnet_slab.json
3188a352602d2d423ee9366748119ff0ba3905c9 assets\tutorialmod\models\block\pink_garnet_fence_gate_open.json
98a32ab0585217e2409a4eaa0d6a648966347c13 assets\tutorialmod\models\item\pink_garnet_trapdoor.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.21.1 2024-09-15T22:30:18.7957421 Tutorial Mod/Recipes
// 1.21.1 2024-09-15T23:45:22.4569776 Tutorial Mod/Recipes
612885645657e824e35aaf7ab21f9a0d6d427c19 data\tutorialmod\advancement\recipes\misc\raw_pink_garnet_from_magic_block.json
434e6d50936f8f4e61b7019a3b6f0717cf6020ac data\tutorialmod\recipe\pink_garnet_from_smelting_raw_pink_garnet.json
e4ae33d7b5461829f0d6342e361ffab69adf9fbf data\tutorialmod\advancement\recipes\misc\pink_garnet_from_smelting_raw_pink_garnet.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 1.21.1 2024-09-15T22:30:18.794746 Tutorial Mod/Tags for minecraft:item
// 1.21.1 2024-09-15T23:45:22.4549278 Tutorial Mod/Tags for minecraft:item
092261f42ebfb549fb26de57a2f4a5a8b86bc6b9 data\tutorialmod\tags\item\transformable_items.json
8f653a3350d8f1fbd15d245e7de66379a30930e2 data\minecraft\tags\item\swords.json
a83d7a926074b69c0b715a6d7e603b0fc4c78d11 data\minecraft\tags\item\hoes.json
8ced195f504be266fa1818b0cfbd2ee244987195 data\minecraft\tags\item\axes.json
351330e211b4558f91f0550566fbac71f5a3b2e5 data\minecraft\tags\item\shovels.json
8ced195f504be266fa1818b0cfbd2ee244987195 data\minecraft\tags\item\axes.json
840726859672b531edb33e064e2e0943de7fc82d data\minecraft\tags\item\pickaxes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "tutorialmod:item/pink_garnet_hammer"
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/kaupenjoe/tutorialmod/TutorialMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.registry.FuelRegistry;
import net.kaupenjoe.tutorialmod.block.ModBlocks;
import net.kaupenjoe.tutorialmod.component.ModDataComponentTypes;
import net.kaupenjoe.tutorialmod.item.ModItemGroups;
import net.kaupenjoe.tutorialmod.item.ModItems;
import net.kaupenjoe.tutorialmod.util.HammerUsageEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -25,5 +27,7 @@ public void onInitialize() {
ModDataComponentTypes.registerDataComponentTypes();

FuelRegistry.INSTANCE.add(ModItems.STARLIGHT_ASHES, 600);

PlayerBlockBreakEvents.BEFORE.register(new HammerUsageEvent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ public void generateItemModels(ItemModelGenerator itemModelGenerator) {
itemModelGenerator.register(ModItems.PINK_GARNET_SHOVEL, Models.HANDHELD);
itemModelGenerator.register(ModItems.PINK_GARNET_AXE, Models.HANDHELD);
itemModelGenerator.register(ModItems.PINK_GARNET_HOE, Models.HANDHELD);

itemModelGenerator.register(ModItems.PINK_GARNET_HAMMER, Models.HANDHELD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ModItemGroups {
entries.add(ModItems.PINK_GARNET_AXE);
entries.add(ModItems.PINK_GARNET_HOE);

entries.add(ModItems.PINK_GARNET_HAMMER);


}).build());

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/kaupenjoe/tutorialmod/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.kaupenjoe.tutorialmod.TutorialMod;
import net.kaupenjoe.tutorialmod.item.custom.ChiselItem;
import net.kaupenjoe.tutorialmod.item.custom.HammerItem;
import net.minecraft.item.*;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.Registries;
Expand Down Expand Up @@ -43,6 +44,11 @@ public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> to
new HoeItem(ModToolMaterials.PINK_GARNET, new Item.Settings()
.attributeModifiers(HoeItem.createAttributeModifiers(ModToolMaterials.PINK_GARNET, 0, -3f))));

public static final Item PINK_GARNET_HAMMER = registerItem("pink_garnet_hammer",
new HammerItem(ModToolMaterials.PINK_GARNET, new Item.Settings()
.attributeModifiers(PickaxeItem.createAttributeModifiers(ModToolMaterials.PINK_GARNET, 7, -3.4f))));


private static Item registerItem(String name, Item item) {
return Registry.register(Registries.ITEM, Identifier.of(TutorialMod.MOD_ID, name), item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.kaupenjoe.tutorialmod.item.custom;

import net.minecraft.item.MiningToolItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;

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

public class HammerItem extends MiningToolItem {
public HammerItem(ToolMaterial material, Settings settings) {
super(material, BlockTags.PICKAXE_MINEABLE, settings);
}

public static List<BlockPos> getBlocksToBeDestroyed(int range, BlockPos initalBlockPos, ServerPlayerEntity player) {
List<BlockPos> positions = new ArrayList<>();
HitResult hit = player.raycast(20, 0, false);
if (hit.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockHit = (BlockHitResult) hit;

if(blockHit.getSide() == Direction.DOWN || blockHit.getSide() == Direction.UP) {
for(int x = -range; x <= range; x++) {
for(int y = -range; y <= range; y++) {
positions.add(new BlockPos(initalBlockPos.getX() + x, initalBlockPos.getY(), initalBlockPos.getZ() + y));
}
}
}

if(blockHit.getSide() == Direction.NORTH || blockHit.getSide() == Direction.SOUTH) {
for(int x = -range; x <= range; x++) {
for(int y = -range; y <= range; y++) {
positions.add(new BlockPos(initalBlockPos.getX() + x, initalBlockPos.getY() + y, initalBlockPos.getZ()));
}
}
}

if(blockHit.getSide() == Direction.EAST || blockHit.getSide() == Direction.WEST) {
for(int x = -range; x <= range; x++) {
for(int y = -range; y <= range; y++) {
positions.add(new BlockPos(initalBlockPos.getX(), initalBlockPos.getY() + y, initalBlockPos.getZ() + x));
}
}
}
}

return positions;
}
}
44 changes: 44 additions & 0 deletions src/main/java/net/kaupenjoe/tutorialmod/util/HammerUsageEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.kaupenjoe.tutorialmod.util;

import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.kaupenjoe.tutorialmod.item.custom.HammerItem;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Set;

public class HammerUsageEvent implements PlayerBlockBreakEvents.Before{
// Done with the help of https://github.com/CoFH/CoFHCore/blob/c23d117dcd3b3b3408a138716b15507f709494cd/src/main/java/cofh/core/event/AreaEffectEvents.java
private static final Set<BlockPos> HARVESTED_BLOCKS = new HashSet<>();

@Override
public boolean beforeBlockBreak(World world, PlayerEntity player, BlockPos pos,
BlockState state, @Nullable BlockEntity blockEntity) {
ItemStack mainHandItem = player.getMainHandStack();

if(mainHandItem.getItem() instanceof HammerItem hammer && player instanceof ServerPlayerEntity serverPlayer) {
if(HARVESTED_BLOCKS.contains(pos)) {
return true;
}

for(BlockPos position : HammerItem.getBlocksToBeDestroyed(1, pos, serverPlayer)) {
if(pos == position || !hammer.isCorrectForDrops(mainHandItem, world.getBlockState(position))) {
continue;
}

HARVESTED_BLOCKS.add(position);
serverPlayer.interactionManager.tryBreakBlock(position);
HARVESTED_BLOCKS.remove(position);
}
}

return true;
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/tutorialmod/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"item.tutorialmod.pink_garnet_axe": "Pink Garnet Axe",
"item.tutorialmod.pink_garnet_hoe": "Pink Garnet Hoe",

"item.tutorialmod.pink_garnet_hammer": "Pink Garnet Hammer",


"block.tutorialmod.pink_garnet_block": "Block of Pink Garnet",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b848603

Please sign in to comment.