Files
src
build.gradle
gradle.properties
settings.gradle
Code
27 378
src/main/java/dev/kodari/hardstart/HardStart.java
package dev.kodari.hardstart;
import dev.kodari.hardstart.mixin.AxeItemAccessor;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.PillarBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.Items;
import net.minecraft.item.ToolMaterial;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
public class HardStart implements ModInitializer {
private static final TagKey<Item> FLINT_REPAIR = TagKey.of(RegistryKeys.ITEM, Identifier.of("hard_start", "flint_repairs"));
private static final ToolMaterial FLINT_MATERIAL = new ToolMaterial(BlockTags.INCORRECT_FOR_WOODEN_TOOL, 48, 2.0F, 0.0F, 10, FLINT_REPAIR);
private static final RegistryKey<Item> HATCHET_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("hard_start", "flint_hatchet"));
public static final Item FLINT_HATCHET = Registry.register(Registries.ITEM, HATCHET_KEY,
new Item(new Item.Settings().registryKey(HATCHET_KEY).axe(FLINT_MATERIAL, 6.0F, -3.2F)));
private static final RegistryKey<Item> FLINT_SAW_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("hard_start", "flint_saw"));
private static final RegistryKey<Item> IRON_SAW_KEY = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("hard_start", "iron_saw"));
public static final Item FLINT_SAW = Registry.register(Registries.ITEM, FLINT_SAW_KEY,
new Item(new Item.Settings().registryKey(FLINT_SAW_KEY).maxDamage(48).repairable(Items.FLINT)));
public static final Item IRON_SAW = Registry.register(Registries.ITEM, IRON_SAW_KEY,
new Item(new Item.Settings().registryKey(IRON_SAW_KEY).maxDamage(250).repairable(Items.IRON_INGOT)));
private static Block planksFor(BlockState state) {
if (state.isIn(BlockTags.OAK_LOGS)) return Blocks.OAK_PLANKS;
if (state.isIn(BlockTags.SPRUCE_LOGS)) return Blocks.SPRUCE_PLANKS;
if (state.isIn(BlockTags.BIRCH_LOGS)) return Blocks.BIRCH_PLANKS;
if (state.isIn(BlockTags.JUNGLE_LOGS)) return Blocks.JUNGLE_PLANKS;
if (state.isIn(BlockTags.ACACIA_LOGS)) return Blocks.ACACIA_PLANKS;
if (state.isIn(BlockTags.DARK_OAK_LOGS)) return Blocks.DARK_OAK_PLANKS;
if (state.isIn(BlockTags.PALE_OAK_LOGS)) return Blocks.PALE_OAK_PLANKS;
if (state.isIn(BlockTags.MANGROVE_LOGS)) return Blocks.MANGROVE_PLANKS;
if (state.isIn(BlockTags.CHERRY_LOGS)) return Blocks.CHERRY_PLANKS;
if (state.isIn(BlockTags.CRIMSON_STEMS)) return Blocks.CRIMSON_PLANKS;
if (state.isIn(BlockTags.WARPED_STEMS)) return Blocks.WARPED_PLANKS;
if (state.isIn(BlockTags.BAMBOO_BLOCKS)) return Blocks.BAMBOO_PLANKS;
return null;
}
public static boolean canBreakByHand(BlockState state) {
return state.isIn(BlockTags.LEAVES)
|| state.isOf(Blocks.GRASS_BLOCK)
|| state.isOf(Blocks.DIRT)
|| state.isOf(Blocks.SHORT_GRASS)
|| state.isOf(Blocks.TALL_GRASS)
|| state.isOf(Blocks.SAND)
|| state.isOf(Blocks.GRAVEL);
}
@Override
public void onInitialize() {
ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS).register(entries -> entries.add(FLINT_HATCHET));
ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS).register(entries -> {
entries.add(FLINT_SAW);
entries.add(IRON_SAW);
});
PlayerBlockBreakEvents.AFTER.register((world, player, pos, state, blockEntity) -> {
if (player.isCreative()) return;
if ((state.isOf(Blocks.DIRT) || state.isOf(Blocks.GRASS_BLOCK)) && world.random.nextFloat() < 0.25F) {
Block.dropStack(world, pos, new ItemStack(Items.FLINT));
}
if (state.isIn(BlockTags.LEAVES) && world.random.nextFloat() < 0.36F) {
Block.dropStack(world, pos, new ItemStack(Items.STICK));
}
});
UseBlockCallback.EVENT.register((player, world, hand, hit) -> {
if (player.isSpectator()) return ActionResult.PASS;
BlockPos pos = hit.getBlockPos();
BlockState state = world.getBlockState(pos);
ItemStack stack = player.getStackInHand(hand);
if (stack.isOf(FLINT_SAW) || stack.isOf(IRON_SAW)) {
Block planks = planksFor(state);
if (planks != null) {
if (!world.isClient()) {
world.removeBlock(pos, false);
Block.dropStack(world, pos, new ItemStack(planks, 4));
world.playSound(null, pos, SoundEvents.BLOCK_BAMBOO_WOOD_BREAK, SoundCategory.BLOCKS, 1.0F, 1.0F);
if (!player.isCreative()) stack.damage(1, player, hand);
}
return ActionResult.SUCCESS;
}
}
Block stripped = stack.isOf(Items.FLINT) ? AxeItemAccessor.hardStart$getStrippedBlocks().get(state.getBlock()) : null;
if (stripped != null) {
if (!world.isClient()) {
world.setBlockState(pos, stripped.getDefaultState().with(PillarBlock.AXIS, state.get(PillarBlock.AXIS)));
world.playSound(null, pos, SoundEvents.ITEM_AXE_STRIP, SoundCategory.BLOCKS, 1.0F, 1.0F);
if (!player.isCreative()) stack.damage(1, player, hand);
}
return ActionResult.SUCCESS;
}
if (stack.isOf(Items.STICK) && state.isOf(Blocks.STRIPPED_OAK_LOG)) {
if (!world.isClient()) {
world.removeBlock(pos, false);
Block.dropStack(world, pos, new ItemStack(Items.OAK_PLANKS));
world.playSound(null, pos, SoundEvents.ITEM_AXE_STRIP, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
});
}
}